p2p_sd.c 22 KB

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