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. 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. p2p_dbg(p2p, "Start Service Discovery with " MACSTR,
  240. MAC2STR(dev->info.p2p_device_addr));
  241. req = p2p_build_sd_query(p2p->srv_update_indic, query->tlvs);
  242. if (req == NULL)
  243. return -1;
  244. dev->sd_reqs++;
  245. p2p->sd_peer = dev;
  246. p2p->sd_query = query;
  247. p2p->pending_action_state = P2P_PENDING_SD;
  248. wait_time = 5000;
  249. if (p2p->cfg->max_listen && wait_time > p2p->cfg->max_listen)
  250. wait_time = p2p->cfg->max_listen;
  251. if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
  252. p2p->cfg->dev_addr, dev->info.p2p_device_addr,
  253. wpabuf_head(req), wpabuf_len(req), wait_time) < 0) {
  254. p2p_dbg(p2p, "Failed to send Action frame");
  255. ret = -1;
  256. }
  257. wpabuf_free(req);
  258. return ret;
  259. }
  260. void p2p_rx_gas_initial_req(struct p2p_data *p2p, const u8 *sa,
  261. const u8 *data, size_t len, int rx_freq)
  262. {
  263. const u8 *pos = data;
  264. const u8 *end = data + len;
  265. const u8 *next;
  266. u8 dialog_token;
  267. u16 slen;
  268. int freq;
  269. u16 update_indic;
  270. if (p2p->cfg->sd_request == NULL)
  271. return;
  272. if (rx_freq > 0)
  273. freq = rx_freq;
  274. else
  275. freq = p2p_channel_to_freq(p2p->cfg->reg_class,
  276. p2p->cfg->channel);
  277. if (freq < 0)
  278. return;
  279. if (len < 1 + 2)
  280. return;
  281. dialog_token = *pos++;
  282. p2p_dbg(p2p, "GAS Initial Request from " MACSTR
  283. " (dialog token %u, freq %d)",
  284. MAC2STR(sa), dialog_token, rx_freq);
  285. if (*pos != WLAN_EID_ADV_PROTO) {
  286. p2p_dbg(p2p, "Unexpected IE in GAS Initial Request: %u", *pos);
  287. return;
  288. }
  289. pos++;
  290. slen = *pos++;
  291. next = pos + slen;
  292. if (next > end || slen < 2) {
  293. p2p_dbg(p2p, "Invalid IE in GAS Initial Request");
  294. return;
  295. }
  296. pos++; /* skip QueryRespLenLimit and PAME-BI */
  297. if (*pos != ACCESS_NETWORK_QUERY_PROTOCOL) {
  298. p2p_dbg(p2p, "Unsupported GAS advertisement protocol id %u",
  299. *pos);
  300. return;
  301. }
  302. pos = next;
  303. /* Query Request */
  304. if (pos + 2 > end)
  305. return;
  306. slen = WPA_GET_LE16(pos);
  307. pos += 2;
  308. if (pos + slen > end)
  309. return;
  310. end = pos + slen;
  311. /* ANQP Query Request */
  312. if (pos + 4 > end)
  313. return;
  314. if (WPA_GET_LE16(pos) != ANQP_VENDOR_SPECIFIC) {
  315. p2p_dbg(p2p, "Unsupported ANQP Info ID %u", WPA_GET_LE16(pos));
  316. return;
  317. }
  318. pos += 2;
  319. slen = WPA_GET_LE16(pos);
  320. pos += 2;
  321. if (pos + slen > end || slen < 3 + 1) {
  322. p2p_dbg(p2p, "Invalid ANQP Query Request length");
  323. return;
  324. }
  325. if (WPA_GET_BE32(pos) != P2P_IE_VENDOR_TYPE) {
  326. p2p_dbg(p2p, "Unsupported ANQP vendor OUI-type %08x",
  327. WPA_GET_BE32(pos));
  328. return;
  329. }
  330. pos += 4;
  331. if (pos + 2 > end)
  332. return;
  333. update_indic = WPA_GET_LE16(pos);
  334. p2p_dbg(p2p, "Service Update Indicator: %u", update_indic);
  335. pos += 2;
  336. p2p->cfg->sd_request(p2p->cfg->cb_ctx, freq, sa, dialog_token,
  337. update_indic, pos, end - pos);
  338. /* the response will be indicated with a call to p2p_sd_response() */
  339. }
  340. void p2p_sd_response(struct p2p_data *p2p, int freq, const u8 *dst,
  341. u8 dialog_token, const struct wpabuf *resp_tlvs)
  342. {
  343. struct wpabuf *resp;
  344. /* TODO: fix the length limit to match with the maximum frame length */
  345. if (wpabuf_len(resp_tlvs) > 1400) {
  346. p2p_dbg(p2p, "SD response long enough to require fragmentation");
  347. if (p2p->sd_resp) {
  348. /*
  349. * TODO: Could consider storing the fragmented response
  350. * separately for each peer to avoid having to drop old
  351. * one if there is more than one pending SD query.
  352. * Though, that would eat more memory, so there are
  353. * also benefits to just using a single buffer.
  354. */
  355. p2p_dbg(p2p, "Drop previous SD response");
  356. wpabuf_free(p2p->sd_resp);
  357. }
  358. p2p->sd_resp = wpabuf_dup(resp_tlvs);
  359. if (p2p->sd_resp == NULL) {
  360. p2p_err(p2p, "Failed to allocate SD response fragmentation area");
  361. return;
  362. }
  363. os_memcpy(p2p->sd_resp_addr, dst, ETH_ALEN);
  364. p2p->sd_resp_dialog_token = dialog_token;
  365. p2p->sd_resp_pos = 0;
  366. p2p->sd_frag_id = 0;
  367. resp = p2p_build_sd_response(dialog_token, WLAN_STATUS_SUCCESS,
  368. 1, p2p->srv_update_indic, NULL);
  369. } else {
  370. p2p_dbg(p2p, "SD response fits in initial response");
  371. resp = p2p_build_sd_response(dialog_token,
  372. WLAN_STATUS_SUCCESS, 0,
  373. p2p->srv_update_indic, resp_tlvs);
  374. }
  375. if (resp == NULL)
  376. return;
  377. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  378. if (p2p_send_action(p2p, freq, dst, p2p->cfg->dev_addr,
  379. p2p->cfg->dev_addr,
  380. wpabuf_head(resp), wpabuf_len(resp), 200) < 0)
  381. p2p_dbg(p2p, "Failed to send Action frame");
  382. wpabuf_free(resp);
  383. }
  384. void p2p_rx_gas_initial_resp(struct p2p_data *p2p, const u8 *sa,
  385. const u8 *data, size_t len, int rx_freq)
  386. {
  387. const u8 *pos = data;
  388. const u8 *end = data + len;
  389. const u8 *next;
  390. u8 dialog_token;
  391. u16 status_code;
  392. u16 comeback_delay;
  393. u16 slen;
  394. u16 update_indic;
  395. if (p2p->state != P2P_SD_DURING_FIND || p2p->sd_peer == NULL ||
  396. os_memcmp(sa, p2p->sd_peer->info.p2p_device_addr, ETH_ALEN) != 0) {
  397. p2p_dbg(p2p, "Ignore unexpected GAS Initial Response from "
  398. MACSTR, MAC2STR(sa));
  399. return;
  400. }
  401. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  402. p2p_clear_timeout(p2p);
  403. p2p_dbg(p2p, "Received GAS Initial Response from " MACSTR " (len=%d)",
  404. MAC2STR(sa), (int) len);
  405. if (len < 5 + 2) {
  406. p2p_dbg(p2p, "Too short GAS Initial Response frame");
  407. return;
  408. }
  409. dialog_token = *pos++;
  410. /* TODO: check dialog_token match */
  411. status_code = WPA_GET_LE16(pos);
  412. pos += 2;
  413. comeback_delay = WPA_GET_LE16(pos);
  414. pos += 2;
  415. p2p_dbg(p2p, "dialog_token=%u status_code=%u comeback_delay=%u",
  416. dialog_token, status_code, comeback_delay);
  417. if (status_code) {
  418. p2p_dbg(p2p, "Service Discovery failed: status code %u",
  419. status_code);
  420. return;
  421. }
  422. if (*pos != WLAN_EID_ADV_PROTO) {
  423. p2p_dbg(p2p, "Unexpected IE in GAS Initial Response: %u", *pos);
  424. return;
  425. }
  426. pos++;
  427. slen = *pos++;
  428. next = pos + slen;
  429. if (next > end || slen < 2) {
  430. p2p_dbg(p2p, "Invalid IE in GAS Initial Response");
  431. return;
  432. }
  433. pos++; /* skip QueryRespLenLimit and PAME-BI */
  434. if (*pos != ACCESS_NETWORK_QUERY_PROTOCOL) {
  435. p2p_dbg(p2p, "Unsupported GAS advertisement protocol id %u",
  436. *pos);
  437. return;
  438. }
  439. pos = next;
  440. /* Query Response */
  441. if (pos + 2 > end) {
  442. p2p_dbg(p2p, "Too short Query Response");
  443. return;
  444. }
  445. slen = WPA_GET_LE16(pos);
  446. pos += 2;
  447. p2p_dbg(p2p, "Query Response Length: %d", slen);
  448. if (pos + slen > end) {
  449. p2p_dbg(p2p, "Not enough Query Response data");
  450. return;
  451. }
  452. end = pos + slen;
  453. if (comeback_delay) {
  454. p2p_dbg(p2p, "Fragmented response - request fragments");
  455. if (p2p->sd_rx_resp) {
  456. p2p_dbg(p2p, "Drop old SD reassembly buffer");
  457. wpabuf_free(p2p->sd_rx_resp);
  458. p2p->sd_rx_resp = NULL;
  459. }
  460. p2p_send_gas_comeback_req(p2p, sa, dialog_token, rx_freq);
  461. return;
  462. }
  463. /* ANQP Query Response */
  464. if (pos + 4 > end)
  465. return;
  466. if (WPA_GET_LE16(pos) != ANQP_VENDOR_SPECIFIC) {
  467. p2p_dbg(p2p, "Unsupported ANQP Info ID %u", WPA_GET_LE16(pos));
  468. return;
  469. }
  470. pos += 2;
  471. slen = WPA_GET_LE16(pos);
  472. pos += 2;
  473. if (pos + slen > end || slen < 3 + 1) {
  474. p2p_dbg(p2p, "Invalid ANQP Query Response length");
  475. return;
  476. }
  477. if (WPA_GET_BE32(pos) != P2P_IE_VENDOR_TYPE) {
  478. p2p_dbg(p2p, "Unsupported ANQP vendor OUI-type %08x",
  479. WPA_GET_BE32(pos));
  480. return;
  481. }
  482. pos += 4;
  483. if (pos + 2 > end)
  484. return;
  485. update_indic = WPA_GET_LE16(pos);
  486. p2p_dbg(p2p, "Service Update Indicator: %u", update_indic);
  487. pos += 2;
  488. p2p->sd_peer = NULL;
  489. if (p2p->sd_query) {
  490. if (!p2p->sd_query->for_all_peers) {
  491. struct p2p_sd_query *q;
  492. p2p_dbg(p2p, "Remove completed SD query %p",
  493. p2p->sd_query);
  494. q = p2p->sd_query;
  495. p2p_unlink_sd_query(p2p, p2p->sd_query);
  496. p2p_free_sd_query(q);
  497. }
  498. p2p->sd_query = NULL;
  499. }
  500. if (p2p->cfg->sd_response)
  501. p2p->cfg->sd_response(p2p->cfg->cb_ctx, sa, update_indic,
  502. pos, end - pos);
  503. p2p_continue_find(p2p);
  504. }
  505. void p2p_rx_gas_comeback_req(struct p2p_data *p2p, const u8 *sa,
  506. const u8 *data, size_t len, int rx_freq)
  507. {
  508. struct wpabuf *resp;
  509. u8 dialog_token;
  510. size_t frag_len;
  511. int more = 0;
  512. wpa_hexdump(MSG_DEBUG, "P2P: RX GAS Comeback Request", data, len);
  513. if (len < 1)
  514. return;
  515. dialog_token = *data;
  516. p2p_dbg(p2p, "Dialog Token: %u", dialog_token);
  517. if (dialog_token != p2p->sd_resp_dialog_token) {
  518. p2p_dbg(p2p, "No pending SD response fragment for dialog token %u",
  519. dialog_token);
  520. return;
  521. }
  522. if (p2p->sd_resp == NULL) {
  523. p2p_dbg(p2p, "No pending SD response fragment available");
  524. return;
  525. }
  526. if (os_memcmp(sa, p2p->sd_resp_addr, ETH_ALEN) != 0) {
  527. p2p_dbg(p2p, "No pending SD response fragment for " MACSTR,
  528. MAC2STR(sa));
  529. return;
  530. }
  531. frag_len = wpabuf_len(p2p->sd_resp) - p2p->sd_resp_pos;
  532. if (frag_len > 1400) {
  533. frag_len = 1400;
  534. more = 1;
  535. }
  536. resp = p2p_build_gas_comeback_resp(dialog_token, WLAN_STATUS_SUCCESS,
  537. p2p->srv_update_indic,
  538. wpabuf_head_u8(p2p->sd_resp) +
  539. p2p->sd_resp_pos, frag_len,
  540. p2p->sd_frag_id, more,
  541. wpabuf_len(p2p->sd_resp));
  542. if (resp == NULL)
  543. return;
  544. p2p_dbg(p2p, "Send GAS Comeback Response (frag_id %d more=%d frag_len=%d)",
  545. p2p->sd_frag_id, more, (int) frag_len);
  546. p2p->sd_frag_id++;
  547. p2p->sd_resp_pos += frag_len;
  548. if (more) {
  549. p2p_dbg(p2p, "%d more bytes remain to be sent",
  550. (int) (wpabuf_len(p2p->sd_resp) - p2p->sd_resp_pos));
  551. } else {
  552. p2p_dbg(p2p, "All fragments of SD response sent");
  553. wpabuf_free(p2p->sd_resp);
  554. p2p->sd_resp = NULL;
  555. }
  556. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  557. if (p2p_send_action(p2p, rx_freq, sa, p2p->cfg->dev_addr,
  558. p2p->cfg->dev_addr,
  559. wpabuf_head(resp), wpabuf_len(resp), 200) < 0)
  560. p2p_dbg(p2p, "Failed to send Action frame");
  561. wpabuf_free(resp);
  562. }
  563. void p2p_rx_gas_comeback_resp(struct p2p_data *p2p, const u8 *sa,
  564. const u8 *data, size_t len, int rx_freq)
  565. {
  566. const u8 *pos = data;
  567. const u8 *end = data + len;
  568. const u8 *next;
  569. u8 dialog_token;
  570. u16 status_code;
  571. u8 frag_id;
  572. u8 more_frags;
  573. u16 comeback_delay;
  574. u16 slen;
  575. wpa_hexdump(MSG_DEBUG, "P2P: RX GAS Comeback Response", data, len);
  576. if (p2p->state != P2P_SD_DURING_FIND || p2p->sd_peer == NULL ||
  577. os_memcmp(sa, p2p->sd_peer->info.p2p_device_addr, ETH_ALEN) != 0) {
  578. p2p_dbg(p2p, "Ignore unexpected GAS Comeback Response from "
  579. MACSTR, MAC2STR(sa));
  580. return;
  581. }
  582. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  583. p2p_clear_timeout(p2p);
  584. p2p_dbg(p2p, "Received GAS Comeback Response from " MACSTR " (len=%d)",
  585. MAC2STR(sa), (int) len);
  586. if (len < 6 + 2) {
  587. p2p_dbg(p2p, "Too short GAS Comeback Response frame");
  588. return;
  589. }
  590. dialog_token = *pos++;
  591. /* TODO: check dialog_token match */
  592. status_code = WPA_GET_LE16(pos);
  593. pos += 2;
  594. frag_id = *pos & 0x7f;
  595. more_frags = (*pos & 0x80) >> 7;
  596. pos++;
  597. comeback_delay = WPA_GET_LE16(pos);
  598. pos += 2;
  599. p2p_dbg(p2p, "dialog_token=%u status_code=%u frag_id=%d more_frags=%d "
  600. "comeback_delay=%u",
  601. dialog_token, status_code, frag_id, more_frags,
  602. comeback_delay);
  603. /* TODO: check frag_id match */
  604. if (status_code) {
  605. p2p_dbg(p2p, "Service Discovery failed: status code %u",
  606. status_code);
  607. return;
  608. }
  609. if (*pos != WLAN_EID_ADV_PROTO) {
  610. p2p_dbg(p2p, "Unexpected IE in GAS Comeback Response: %u",
  611. *pos);
  612. return;
  613. }
  614. pos++;
  615. slen = *pos++;
  616. next = pos + slen;
  617. if (next > end || slen < 2) {
  618. p2p_dbg(p2p, "Invalid IE in GAS Comeback Response");
  619. return;
  620. }
  621. pos++; /* skip QueryRespLenLimit and PAME-BI */
  622. if (*pos != ACCESS_NETWORK_QUERY_PROTOCOL) {
  623. p2p_dbg(p2p, "Unsupported GAS advertisement protocol id %u",
  624. *pos);
  625. return;
  626. }
  627. pos = next;
  628. /* Query Response */
  629. if (pos + 2 > end) {
  630. p2p_dbg(p2p, "Too short Query Response");
  631. return;
  632. }
  633. slen = WPA_GET_LE16(pos);
  634. pos += 2;
  635. p2p_dbg(p2p, "Query Response Length: %d", slen);
  636. if (pos + slen > end) {
  637. p2p_dbg(p2p, "Not enough Query Response data");
  638. return;
  639. }
  640. if (slen == 0) {
  641. p2p_dbg(p2p, "No Query Response data");
  642. return;
  643. }
  644. end = pos + slen;
  645. if (p2p->sd_rx_resp) {
  646. /*
  647. * ANQP header is only included in the first fragment; rest of
  648. * the fragments start with continue TLVs.
  649. */
  650. goto skip_nqp_header;
  651. }
  652. /* ANQP Query Response */
  653. if (pos + 4 > end)
  654. return;
  655. if (WPA_GET_LE16(pos) != ANQP_VENDOR_SPECIFIC) {
  656. p2p_dbg(p2p, "Unsupported ANQP Info ID %u", WPA_GET_LE16(pos));
  657. return;
  658. }
  659. pos += 2;
  660. slen = WPA_GET_LE16(pos);
  661. pos += 2;
  662. p2p_dbg(p2p, "ANQP Query Response length: %u", slen);
  663. if (slen < 3 + 1) {
  664. p2p_dbg(p2p, "Invalid ANQP Query Response length");
  665. return;
  666. }
  667. if (pos + 4 > end)
  668. return;
  669. if (WPA_GET_BE32(pos) != P2P_IE_VENDOR_TYPE) {
  670. p2p_dbg(p2p, "Unsupported ANQP vendor OUI-type %08x",
  671. WPA_GET_BE32(pos));
  672. return;
  673. }
  674. pos += 4;
  675. if (pos + 2 > end)
  676. return;
  677. p2p->sd_rx_update_indic = WPA_GET_LE16(pos);
  678. p2p_dbg(p2p, "Service Update Indicator: %u", p2p->sd_rx_update_indic);
  679. pos += 2;
  680. skip_nqp_header:
  681. if (wpabuf_resize(&p2p->sd_rx_resp, end - pos) < 0)
  682. return;
  683. wpabuf_put_data(p2p->sd_rx_resp, pos, end - pos);
  684. p2p_dbg(p2p, "Current SD reassembly buffer length: %u",
  685. (unsigned int) wpabuf_len(p2p->sd_rx_resp));
  686. if (more_frags) {
  687. p2p_dbg(p2p, "More fragments remains");
  688. /* TODO: what would be a good size limit? */
  689. if (wpabuf_len(p2p->sd_rx_resp) > 64000) {
  690. wpabuf_free(p2p->sd_rx_resp);
  691. p2p->sd_rx_resp = NULL;
  692. p2p_dbg(p2p, "Too long SD response - drop it");
  693. return;
  694. }
  695. p2p_send_gas_comeback_req(p2p, sa, dialog_token, rx_freq);
  696. return;
  697. }
  698. p2p->sd_peer = NULL;
  699. if (p2p->sd_query) {
  700. if (!p2p->sd_query->for_all_peers) {
  701. struct p2p_sd_query *q;
  702. p2p_dbg(p2p, "Remove completed SD query %p",
  703. p2p->sd_query);
  704. q = p2p->sd_query;
  705. p2p_unlink_sd_query(p2p, p2p->sd_query);
  706. p2p_free_sd_query(q);
  707. }
  708. p2p->sd_query = NULL;
  709. }
  710. if (p2p->cfg->sd_response)
  711. p2p->cfg->sd_response(p2p->cfg->cb_ctx, sa,
  712. p2p->sd_rx_update_indic,
  713. wpabuf_head(p2p->sd_rx_resp),
  714. wpabuf_len(p2p->sd_rx_resp));
  715. wpabuf_free(p2p->sd_rx_resp);
  716. p2p->sd_rx_resp = NULL;
  717. p2p_continue_find(p2p);
  718. }
  719. void * p2p_sd_request(struct p2p_data *p2p, const u8 *dst,
  720. const struct wpabuf *tlvs)
  721. {
  722. struct p2p_sd_query *q;
  723. q = os_zalloc(sizeof(*q));
  724. if (q == NULL)
  725. return NULL;
  726. if (dst)
  727. os_memcpy(q->peer, dst, ETH_ALEN);
  728. else
  729. q->for_all_peers = 1;
  730. q->tlvs = wpabuf_dup(tlvs);
  731. if (q->tlvs == NULL) {
  732. p2p_free_sd_query(q);
  733. return NULL;
  734. }
  735. q->next = p2p->sd_queries;
  736. p2p->sd_queries = q;
  737. p2p_dbg(p2p, "Added SD Query %p", q);
  738. if (dst == NULL) {
  739. struct p2p_device *dev;
  740. p2p->num_p2p_sd_queries++;
  741. /* Update all the devices for the newly added broadcast query */
  742. dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
  743. if (dev->sd_pending_bcast_queries <= 0)
  744. dev->sd_pending_bcast_queries = 1;
  745. else
  746. dev->sd_pending_bcast_queries++;
  747. }
  748. }
  749. return q;
  750. }
  751. #ifdef CONFIG_WIFI_DISPLAY
  752. void * p2p_sd_request_wfd(struct p2p_data *p2p, const u8 *dst,
  753. const struct wpabuf *tlvs)
  754. {
  755. struct p2p_sd_query *q;
  756. q = p2p_sd_request(p2p, dst, tlvs);
  757. if (q)
  758. q->wsd = 1;
  759. return q;
  760. }
  761. #endif /* CONFIG_WIFI_DISPLAY */
  762. void p2p_sd_service_update(struct p2p_data *p2p)
  763. {
  764. p2p->srv_update_indic++;
  765. }
  766. int p2p_sd_cancel_request(struct p2p_data *p2p, void *req)
  767. {
  768. if (p2p_unlink_sd_query(p2p, req)) {
  769. p2p_dbg(p2p, "Cancel pending SD query %p", req);
  770. p2p_free_sd_query(req);
  771. return 0;
  772. }
  773. return -1;
  774. }