p2p_sd.c 21 KB

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