p2p_parse.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. /*
  2. * P2P - IE parser
  3. * Copyright (c) 2009-2010, 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/ieee802_11_common.h"
  12. #include "wps/wps_i.h"
  13. #include "p2p_i.h"
  14. static int p2p_parse_attribute(u8 id, const u8 *data, u16 len,
  15. struct p2p_message *msg)
  16. {
  17. const u8 *pos;
  18. size_t i, nlen;
  19. char devtype[WPS_DEV_TYPE_BUFSIZE];
  20. switch (id) {
  21. case P2P_ATTR_CAPABILITY:
  22. if (len < 2) {
  23. wpa_printf(MSG_DEBUG, "P2P: Too short Capability "
  24. "attribute (length %d)", len);
  25. return -1;
  26. }
  27. msg->capability = data;
  28. wpa_printf(MSG_DEBUG, "P2P: * Device Capability %02x "
  29. "Group Capability %02x",
  30. data[0], data[1]);
  31. break;
  32. case P2P_ATTR_DEVICE_ID:
  33. if (len < ETH_ALEN) {
  34. wpa_printf(MSG_DEBUG, "P2P: Too short Device ID "
  35. "attribute (length %d)", len);
  36. return -1;
  37. }
  38. msg->device_id = data;
  39. wpa_printf(MSG_DEBUG, "P2P: * Device ID " MACSTR,
  40. MAC2STR(msg->device_id));
  41. break;
  42. case P2P_ATTR_GROUP_OWNER_INTENT:
  43. if (len < 1) {
  44. wpa_printf(MSG_DEBUG, "P2P: Too short GO Intent "
  45. "attribute (length %d)", len);
  46. return -1;
  47. }
  48. msg->go_intent = data;
  49. wpa_printf(MSG_DEBUG, "P2P: * GO Intent: Intent %u "
  50. "Tie breaker %u", data[0] >> 1, data[0] & 0x01);
  51. break;
  52. case P2P_ATTR_STATUS:
  53. if (len < 1) {
  54. wpa_printf(MSG_DEBUG, "P2P: Too short Status "
  55. "attribute (length %d)", len);
  56. return -1;
  57. }
  58. msg->status = data;
  59. wpa_printf(MSG_DEBUG, "P2P: * Status: %d", data[0]);
  60. break;
  61. case P2P_ATTR_LISTEN_CHANNEL:
  62. if (len == 0) {
  63. wpa_printf(MSG_DEBUG, "P2P: * Listen Channel: Ignore "
  64. "null channel");
  65. break;
  66. }
  67. if (len < 5) {
  68. wpa_printf(MSG_DEBUG, "P2P: Too short Listen Channel "
  69. "attribute (length %d)", len);
  70. return -1;
  71. }
  72. msg->listen_channel = data;
  73. wpa_printf(MSG_DEBUG, "P2P: * Listen Channel: "
  74. "Country %c%c(0x%02x) Regulatory "
  75. "Class %d Channel Number %d", data[0], data[1],
  76. data[2], data[3], data[4]);
  77. break;
  78. case P2P_ATTR_OPERATING_CHANNEL:
  79. if (len == 0) {
  80. wpa_printf(MSG_DEBUG, "P2P: * Operating Channel: "
  81. "Ignore null channel");
  82. break;
  83. }
  84. if (len < 5) {
  85. wpa_printf(MSG_DEBUG, "P2P: Too short Operating "
  86. "Channel attribute (length %d)", len);
  87. return -1;
  88. }
  89. msg->operating_channel = data;
  90. wpa_printf(MSG_DEBUG, "P2P: * Operating Channel: "
  91. "Country %c%c(0x%02x) Regulatory "
  92. "Class %d Channel Number %d", data[0], data[1],
  93. data[2], data[3], data[4]);
  94. break;
  95. case P2P_ATTR_CHANNEL_LIST:
  96. if (len < 3) {
  97. wpa_printf(MSG_DEBUG, "P2P: Too short Channel List "
  98. "attribute (length %d)", len);
  99. return -1;
  100. }
  101. msg->channel_list = data;
  102. msg->channel_list_len = len;
  103. wpa_printf(MSG_DEBUG, "P2P: * Channel List: Country String "
  104. "'%c%c(0x%02x)'", data[0], data[1], data[2]);
  105. wpa_hexdump(MSG_MSGDUMP, "P2P: Channel List",
  106. msg->channel_list, msg->channel_list_len);
  107. break;
  108. case P2P_ATTR_GROUP_INFO:
  109. msg->group_info = data;
  110. msg->group_info_len = len;
  111. wpa_printf(MSG_DEBUG, "P2P: * Group Info");
  112. break;
  113. case P2P_ATTR_DEVICE_INFO:
  114. if (len < ETH_ALEN + 2 + 8 + 1) {
  115. wpa_printf(MSG_DEBUG, "P2P: Too short Device Info "
  116. "attribute (length %d)", len);
  117. return -1;
  118. }
  119. msg->p2p_device_info = data;
  120. msg->p2p_device_info_len = len;
  121. pos = data;
  122. msg->p2p_device_addr = pos;
  123. pos += ETH_ALEN;
  124. msg->config_methods = WPA_GET_BE16(pos);
  125. pos += 2;
  126. msg->pri_dev_type = pos;
  127. pos += 8;
  128. msg->num_sec_dev_types = *pos++;
  129. if (msg->num_sec_dev_types * 8 > data + len - pos) {
  130. wpa_printf(MSG_DEBUG, "P2P: Device Info underflow");
  131. return -1;
  132. }
  133. pos += msg->num_sec_dev_types * 8;
  134. if (data + len - pos < 4) {
  135. wpa_printf(MSG_DEBUG, "P2P: Invalid Device Name "
  136. "length %d", (int) (data + len - pos));
  137. return -1;
  138. }
  139. if (WPA_GET_BE16(pos) != ATTR_DEV_NAME) {
  140. wpa_hexdump(MSG_DEBUG, "P2P: Unexpected Device Name "
  141. "header", pos, 4);
  142. return -1;
  143. }
  144. pos += 2;
  145. nlen = WPA_GET_BE16(pos);
  146. pos += 2;
  147. if (data + len - pos < (int) nlen ||
  148. nlen > WPS_DEV_NAME_MAX_LEN) {
  149. wpa_printf(MSG_DEBUG, "P2P: Invalid Device Name "
  150. "length %d (buf len %d)", (int) nlen,
  151. (int) (data + len - pos));
  152. return -1;
  153. }
  154. os_memcpy(msg->device_name, pos, nlen);
  155. msg->device_name[nlen] = '\0';
  156. for (i = 0; i < nlen; i++) {
  157. if (msg->device_name[i] == '\0')
  158. break;
  159. if (is_ctrl_char(msg->device_name[i]))
  160. msg->device_name[i] = '_';
  161. }
  162. wpa_printf(MSG_DEBUG, "P2P: * Device Info: addr " MACSTR
  163. " primary device type %s device name '%s' "
  164. "config methods 0x%x",
  165. MAC2STR(msg->p2p_device_addr),
  166. wps_dev_type_bin2str(msg->pri_dev_type, devtype,
  167. sizeof(devtype)),
  168. msg->device_name, msg->config_methods);
  169. break;
  170. case P2P_ATTR_CONFIGURATION_TIMEOUT:
  171. if (len < 2) {
  172. wpa_printf(MSG_DEBUG, "P2P: Too short Configuration "
  173. "Timeout attribute (length %d)", len);
  174. return -1;
  175. }
  176. msg->config_timeout = data;
  177. wpa_printf(MSG_DEBUG, "P2P: * Configuration Timeout");
  178. break;
  179. case P2P_ATTR_INTENDED_INTERFACE_ADDR:
  180. if (len < ETH_ALEN) {
  181. wpa_printf(MSG_DEBUG, "P2P: Too short Intended P2P "
  182. "Interface Address attribute (length %d)",
  183. len);
  184. return -1;
  185. }
  186. msg->intended_addr = data;
  187. wpa_printf(MSG_DEBUG, "P2P: * Intended P2P Interface Address: "
  188. MACSTR, MAC2STR(msg->intended_addr));
  189. break;
  190. case P2P_ATTR_GROUP_BSSID:
  191. if (len < ETH_ALEN) {
  192. wpa_printf(MSG_DEBUG, "P2P: Too short P2P Group BSSID "
  193. "attribute (length %d)", len);
  194. return -1;
  195. }
  196. msg->group_bssid = data;
  197. wpa_printf(MSG_DEBUG, "P2P: * P2P Group BSSID: " MACSTR,
  198. MAC2STR(msg->group_bssid));
  199. break;
  200. case P2P_ATTR_GROUP_ID:
  201. if (len < ETH_ALEN || len > ETH_ALEN + SSID_MAX_LEN) {
  202. wpa_printf(MSG_DEBUG, "P2P: Invalid P2P Group ID "
  203. "attribute length %d", len);
  204. return -1;
  205. }
  206. msg->group_id = data;
  207. msg->group_id_len = len;
  208. wpa_printf(MSG_DEBUG, "P2P: * P2P Group ID: Device Address "
  209. MACSTR, MAC2STR(msg->group_id));
  210. wpa_hexdump_ascii(MSG_DEBUG, "P2P: * P2P Group ID: SSID",
  211. msg->group_id + ETH_ALEN,
  212. msg->group_id_len - ETH_ALEN);
  213. break;
  214. case P2P_ATTR_INVITATION_FLAGS:
  215. if (len < 1) {
  216. wpa_printf(MSG_DEBUG, "P2P: Too short Invitation "
  217. "Flag attribute (length %d)", len);
  218. return -1;
  219. }
  220. msg->invitation_flags = data;
  221. wpa_printf(MSG_DEBUG, "P2P: * Invitation Flags: bitmap 0x%x",
  222. data[0]);
  223. break;
  224. case P2P_ATTR_MANAGEABILITY:
  225. if (len < 1) {
  226. wpa_printf(MSG_DEBUG, "P2P: Too short Manageability "
  227. "attribute (length %d)", len);
  228. return -1;
  229. }
  230. msg->manageability = data;
  231. wpa_printf(MSG_DEBUG, "P2P: * Manageability: bitmap 0x%x",
  232. data[0]);
  233. break;
  234. case P2P_ATTR_NOTICE_OF_ABSENCE:
  235. if (len < 2) {
  236. wpa_printf(MSG_DEBUG, "P2P: Too short Notice of "
  237. "Absence attribute (length %d)", len);
  238. return -1;
  239. }
  240. msg->noa = data;
  241. msg->noa_len = len;
  242. wpa_printf(MSG_DEBUG, "P2P: * Notice of Absence");
  243. break;
  244. case P2P_ATTR_EXT_LISTEN_TIMING:
  245. if (len < 4) {
  246. wpa_printf(MSG_DEBUG, "P2P: Too short Extended Listen "
  247. "Timing attribute (length %d)", len);
  248. return -1;
  249. }
  250. msg->ext_listen_timing = data;
  251. wpa_printf(MSG_DEBUG, "P2P: * Extended Listen Timing "
  252. "(period %u msec interval %u msec)",
  253. WPA_GET_LE16(msg->ext_listen_timing),
  254. WPA_GET_LE16(msg->ext_listen_timing + 2));
  255. break;
  256. case P2P_ATTR_MINOR_REASON_CODE:
  257. if (len < 1) {
  258. wpa_printf(MSG_DEBUG, "P2P: Too short Minor Reason "
  259. "Code attribute (length %d)", len);
  260. return -1;
  261. }
  262. msg->minor_reason_code = data;
  263. wpa_printf(MSG_DEBUG, "P2P: * Minor Reason Code: %u",
  264. *msg->minor_reason_code);
  265. break;
  266. case P2P_ATTR_OOB_GO_NEG_CHANNEL:
  267. if (len < 6) {
  268. wpa_printf(MSG_DEBUG, "P2P: Too short OOB GO Neg "
  269. "Channel attribute (length %d)", len);
  270. return -1;
  271. }
  272. msg->oob_go_neg_channel = data;
  273. wpa_printf(MSG_DEBUG, "P2P: * OOB GO Neg Channel: "
  274. "Country %c%c(0x%02x) Operating Class %d "
  275. "Channel Number %d Role %d",
  276. data[0], data[1], data[2], data[3], data[4],
  277. data[5]);
  278. break;
  279. case P2P_ATTR_SERVICE_HASH:
  280. if (len < P2PS_HASH_LEN) {
  281. wpa_printf(MSG_DEBUG,
  282. "P2P: Too short Service Hash (length %u)",
  283. len);
  284. return -1;
  285. }
  286. msg->service_hash_count = len / P2PS_HASH_LEN;
  287. msg->service_hash = data;
  288. wpa_hexdump(MSG_DEBUG, "P2P: * Service Hash(s)", data, len);
  289. break;
  290. case P2P_ATTR_SESSION_INFORMATION_DATA:
  291. msg->session_info = data;
  292. msg->session_info_len = len;
  293. wpa_printf(MSG_DEBUG, "P2P: * Service Instance: %u bytes - %p",
  294. len, data);
  295. break;
  296. case P2P_ATTR_CONNECTION_CAPABILITY:
  297. if (len < 1) {
  298. wpa_printf(MSG_DEBUG,
  299. "P2P: Too short Connection Capability (length %u)",
  300. len);
  301. return -1;
  302. }
  303. msg->conn_cap = data;
  304. wpa_printf(MSG_DEBUG, "P2P: * Connection Capability: 0x%x",
  305. *msg->conn_cap);
  306. break;
  307. case P2P_ATTR_ADVERTISEMENT_ID:
  308. if (len < 10) {
  309. wpa_printf(MSG_DEBUG,
  310. "P2P: Too short Advertisement ID (length %u)",
  311. len);
  312. return -1;
  313. }
  314. msg->adv_id = data;
  315. msg->adv_mac = &data[sizeof(u32)];
  316. wpa_printf(MSG_DEBUG, "P2P: * Advertisement ID %x",
  317. WPA_GET_LE32(data));
  318. break;
  319. case P2P_ATTR_ADVERTISED_SERVICE:
  320. if (len < 8) {
  321. wpa_printf(MSG_DEBUG,
  322. "P2P: Too short Service Instance (length %u)",
  323. len);
  324. return -1;
  325. }
  326. msg->adv_service_instance = data;
  327. msg->adv_service_instance_len = len;
  328. if (len <= 255 + 8) {
  329. char str[256];
  330. u8 namelen;
  331. namelen = data[6];
  332. if (namelen > len - 7)
  333. break;
  334. os_memcpy(str, &data[7], namelen);
  335. str[namelen] = '\0';
  336. wpa_printf(MSG_DEBUG, "P2P: * Service Instance: %x-%s",
  337. WPA_GET_LE32(data), str);
  338. } else {
  339. wpa_printf(MSG_DEBUG, "P2P: * Service Instance: %p",
  340. data);
  341. }
  342. break;
  343. case P2P_ATTR_SESSION_ID:
  344. if (len < sizeof(u32) + ETH_ALEN) {
  345. wpa_printf(MSG_DEBUG,
  346. "P2P: Too short Session ID Info (length %u)",
  347. len);
  348. return -1;
  349. }
  350. msg->session_id = data;
  351. msg->session_mac = &data[sizeof(u32)];
  352. wpa_printf(MSG_DEBUG, "P2P: * Session ID: %x " MACSTR,
  353. WPA_GET_LE32(data), MAC2STR(msg->session_mac));
  354. break;
  355. case P2P_ATTR_FEATURE_CAPABILITY:
  356. if (!len) {
  357. wpa_printf(MSG_DEBUG,
  358. "P2P: Too short Feature Capability (length %u)",
  359. len);
  360. return -1;
  361. }
  362. msg->feature_cap = data;
  363. msg->feature_cap_len = len;
  364. wpa_printf(MSG_DEBUG, "P2P: * Feature Cap (length=%u)", len);
  365. break;
  366. case P2P_ATTR_PERSISTENT_GROUP:
  367. {
  368. if (len < ETH_ALEN || len > ETH_ALEN + SSID_MAX_LEN) {
  369. wpa_printf(MSG_DEBUG,
  370. "P2P: Invalid Persistent Group Info (length %u)",
  371. len);
  372. return -1;
  373. }
  374. msg->persistent_dev = data;
  375. msg->persistent_ssid_len = len - ETH_ALEN;
  376. msg->persistent_ssid = &data[ETH_ALEN];
  377. wpa_printf(MSG_DEBUG, "P2P: * Persistent Group: " MACSTR " %s",
  378. MAC2STR(msg->persistent_dev),
  379. wpa_ssid_txt(msg->persistent_ssid,
  380. msg->persistent_ssid_len));
  381. break;
  382. }
  383. default:
  384. wpa_printf(MSG_DEBUG, "P2P: Skipped unknown attribute %d "
  385. "(length %d)", id, len);
  386. break;
  387. }
  388. return 0;
  389. }
  390. /**
  391. * p2p_parse_p2p_ie - Parse P2P IE
  392. * @buf: Concatenated P2P IE(s) payload
  393. * @msg: Buffer for returning parsed attributes
  394. * Returns: 0 on success, -1 on failure
  395. *
  396. * Note: Caller is responsible for clearing the msg data structure before
  397. * calling this function.
  398. */
  399. int p2p_parse_p2p_ie(const struct wpabuf *buf, struct p2p_message *msg)
  400. {
  401. const u8 *pos = wpabuf_head_u8(buf);
  402. const u8 *end = pos + wpabuf_len(buf);
  403. wpa_printf(MSG_DEBUG, "P2P: Parsing P2P IE");
  404. while (pos < end) {
  405. u16 attr_len;
  406. u8 id;
  407. if (end - pos < 3) {
  408. wpa_printf(MSG_DEBUG, "P2P: Invalid P2P attribute");
  409. return -1;
  410. }
  411. id = *pos++;
  412. attr_len = WPA_GET_LE16(pos);
  413. pos += 2;
  414. wpa_printf(MSG_DEBUG, "P2P: Attribute %d length %u",
  415. id, attr_len);
  416. if (attr_len > end - pos) {
  417. wpa_printf(MSG_DEBUG, "P2P: Attribute underflow "
  418. "(len=%u left=%d)",
  419. attr_len, (int) (end - pos));
  420. wpa_hexdump(MSG_MSGDUMP, "P2P: Data", pos, end - pos);
  421. return -1;
  422. }
  423. if (p2p_parse_attribute(id, pos, attr_len, msg))
  424. return -1;
  425. pos += attr_len;
  426. }
  427. return 0;
  428. }
  429. static int p2p_parse_wps_ie(const struct wpabuf *buf, struct p2p_message *msg)
  430. {
  431. struct wps_parse_attr attr;
  432. int i;
  433. wpa_printf(MSG_DEBUG, "P2P: Parsing WPS IE");
  434. if (wps_parse_msg(buf, &attr))
  435. return -1;
  436. if (attr.dev_name && attr.dev_name_len < sizeof(msg->device_name) &&
  437. !msg->device_name[0])
  438. os_memcpy(msg->device_name, attr.dev_name, attr.dev_name_len);
  439. if (attr.config_methods) {
  440. msg->wps_config_methods =
  441. WPA_GET_BE16(attr.config_methods);
  442. wpa_printf(MSG_DEBUG, "P2P: Config Methods (WPS): 0x%x",
  443. msg->wps_config_methods);
  444. }
  445. if (attr.dev_password_id) {
  446. msg->dev_password_id = WPA_GET_BE16(attr.dev_password_id);
  447. wpa_printf(MSG_DEBUG, "P2P: Device Password ID: %d",
  448. msg->dev_password_id);
  449. msg->dev_password_id_present = 1;
  450. }
  451. if (attr.primary_dev_type) {
  452. char devtype[WPS_DEV_TYPE_BUFSIZE];
  453. msg->wps_pri_dev_type = attr.primary_dev_type;
  454. wpa_printf(MSG_DEBUG, "P2P: Primary Device Type (WPS): %s",
  455. wps_dev_type_bin2str(msg->wps_pri_dev_type, devtype,
  456. sizeof(devtype)));
  457. }
  458. if (attr.sec_dev_type_list) {
  459. msg->wps_sec_dev_type_list = attr.sec_dev_type_list;
  460. msg->wps_sec_dev_type_list_len = attr.sec_dev_type_list_len;
  461. }
  462. for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
  463. msg->wps_vendor_ext[i] = attr.vendor_ext[i];
  464. msg->wps_vendor_ext_len[i] = attr.vendor_ext_len[i];
  465. }
  466. msg->manufacturer = attr.manufacturer;
  467. msg->manufacturer_len = attr.manufacturer_len;
  468. msg->model_name = attr.model_name;
  469. msg->model_name_len = attr.model_name_len;
  470. msg->model_number = attr.model_number;
  471. msg->model_number_len = attr.model_number_len;
  472. msg->serial_number = attr.serial_number;
  473. msg->serial_number_len = attr.serial_number_len;
  474. msg->oob_dev_password = attr.oob_dev_password;
  475. msg->oob_dev_password_len = attr.oob_dev_password_len;
  476. return 0;
  477. }
  478. /**
  479. * p2p_parse_ies - Parse P2P message IEs (both WPS and P2P IE)
  480. * @data: IEs from the message
  481. * @len: Length of data buffer in octets
  482. * @msg: Buffer for returning parsed attributes
  483. * Returns: 0 on success, -1 on failure
  484. *
  485. * Note: Caller is responsible for clearing the msg data structure before
  486. * calling this function.
  487. *
  488. * Note: Caller must free temporary memory allocations by calling
  489. * p2p_parse_free() when the parsed data is not needed anymore.
  490. */
  491. int p2p_parse_ies(const u8 *data, size_t len, struct p2p_message *msg)
  492. {
  493. struct ieee802_11_elems elems;
  494. ieee802_11_parse_elems(data, len, &elems, 0);
  495. if (elems.ds_params)
  496. msg->ds_params = elems.ds_params;
  497. if (elems.ssid)
  498. msg->ssid = elems.ssid - 2;
  499. msg->wps_attributes = ieee802_11_vendor_ie_concat(data, len,
  500. WPS_DEV_OUI_WFA);
  501. if (msg->wps_attributes &&
  502. p2p_parse_wps_ie(msg->wps_attributes, msg)) {
  503. p2p_parse_free(msg);
  504. return -1;
  505. }
  506. msg->p2p_attributes = ieee802_11_vendor_ie_concat(data, len,
  507. P2P_IE_VENDOR_TYPE);
  508. if (msg->p2p_attributes &&
  509. p2p_parse_p2p_ie(msg->p2p_attributes, msg)) {
  510. wpa_printf(MSG_DEBUG, "P2P: Failed to parse P2P IE data");
  511. if (msg->p2p_attributes)
  512. wpa_hexdump_buf(MSG_MSGDUMP, "P2P: P2P IE data",
  513. msg->p2p_attributes);
  514. p2p_parse_free(msg);
  515. return -1;
  516. }
  517. #ifdef CONFIG_WIFI_DISPLAY
  518. if (elems.wfd) {
  519. msg->wfd_subelems = ieee802_11_vendor_ie_concat(
  520. data, len, WFD_IE_VENDOR_TYPE);
  521. }
  522. #endif /* CONFIG_WIFI_DISPLAY */
  523. return 0;
  524. }
  525. /**
  526. * p2p_parse - Parse a P2P Action frame contents
  527. * @data: Action frame payload after Category and Code fields
  528. * @len: Length of data buffer in octets
  529. * @msg: Buffer for returning parsed attributes
  530. * Returns: 0 on success, -1 on failure
  531. *
  532. * Note: Caller must free temporary memory allocations by calling
  533. * p2p_parse_free() when the parsed data is not needed anymore.
  534. */
  535. int p2p_parse(const u8 *data, size_t len, struct p2p_message *msg)
  536. {
  537. os_memset(msg, 0, sizeof(*msg));
  538. wpa_printf(MSG_DEBUG, "P2P: Parsing the received message");
  539. if (len < 1) {
  540. wpa_printf(MSG_DEBUG, "P2P: No Dialog Token in the message");
  541. return -1;
  542. }
  543. msg->dialog_token = data[0];
  544. wpa_printf(MSG_DEBUG, "P2P: * Dialog Token: %d", msg->dialog_token);
  545. return p2p_parse_ies(data + 1, len - 1, msg);
  546. }
  547. int p2p_parse_ies_separate(const u8 *wsc, size_t wsc_len, const u8 *p2p,
  548. size_t p2p_len, struct p2p_message *msg)
  549. {
  550. os_memset(msg, 0, sizeof(*msg));
  551. msg->wps_attributes = wpabuf_alloc_copy(wsc, wsc_len);
  552. if (msg->wps_attributes &&
  553. p2p_parse_wps_ie(msg->wps_attributes, msg)) {
  554. p2p_parse_free(msg);
  555. return -1;
  556. }
  557. msg->p2p_attributes = wpabuf_alloc_copy(p2p, p2p_len);
  558. if (msg->p2p_attributes &&
  559. p2p_parse_p2p_ie(msg->p2p_attributes, msg)) {
  560. wpa_printf(MSG_DEBUG, "P2P: Failed to parse P2P IE data");
  561. if (msg->p2p_attributes)
  562. wpa_hexdump_buf(MSG_MSGDUMP, "P2P: P2P IE data",
  563. msg->p2p_attributes);
  564. p2p_parse_free(msg);
  565. return -1;
  566. }
  567. return 0;
  568. }
  569. /**
  570. * p2p_parse_free - Free temporary data from P2P parsing
  571. * @msg: Parsed attributes
  572. */
  573. void p2p_parse_free(struct p2p_message *msg)
  574. {
  575. wpabuf_free(msg->p2p_attributes);
  576. msg->p2p_attributes = NULL;
  577. wpabuf_free(msg->wps_attributes);
  578. msg->wps_attributes = NULL;
  579. #ifdef CONFIG_WIFI_DISPLAY
  580. wpabuf_free(msg->wfd_subelems);
  581. msg->wfd_subelems = NULL;
  582. #endif /* CONFIG_WIFI_DISPLAY */
  583. }
  584. int p2p_group_info_parse(const u8 *gi, size_t gi_len,
  585. struct p2p_group_info *info)
  586. {
  587. const u8 *g, *gend;
  588. os_memset(info, 0, sizeof(*info));
  589. if (gi == NULL)
  590. return 0;
  591. g = gi;
  592. gend = gi + gi_len;
  593. while (g < gend) {
  594. struct p2p_client_info *cli;
  595. const u8 *t, *cend;
  596. int count;
  597. cli = &info->client[info->num_clients];
  598. cend = g + 1 + g[0];
  599. if (cend > gend)
  600. return -1; /* invalid data */
  601. /* g at start of P2P Client Info Descriptor */
  602. /* t at Device Capability Bitmap */
  603. t = g + 1 + 2 * ETH_ALEN;
  604. if (t > cend)
  605. return -1; /* invalid data */
  606. cli->p2p_device_addr = g + 1;
  607. cli->p2p_interface_addr = g + 1 + ETH_ALEN;
  608. cli->dev_capab = t[0];
  609. if (t + 1 + 2 + 8 + 1 > cend)
  610. return -1; /* invalid data */
  611. cli->config_methods = WPA_GET_BE16(&t[1]);
  612. cli->pri_dev_type = &t[3];
  613. t += 1 + 2 + 8;
  614. /* t at Number of Secondary Device Types */
  615. cli->num_sec_dev_types = *t++;
  616. if (t + 8 * cli->num_sec_dev_types > cend)
  617. return -1; /* invalid data */
  618. cli->sec_dev_types = t;
  619. t += 8 * cli->num_sec_dev_types;
  620. /* t at Device Name in WPS TLV format */
  621. if (t + 2 + 2 > cend)
  622. return -1; /* invalid data */
  623. if (WPA_GET_BE16(t) != ATTR_DEV_NAME)
  624. return -1; /* invalid Device Name TLV */
  625. t += 2;
  626. count = WPA_GET_BE16(t);
  627. t += 2;
  628. if (count > cend - t)
  629. return -1; /* invalid Device Name TLV */
  630. if (count >= WPS_DEV_NAME_MAX_LEN)
  631. count = WPS_DEV_NAME_MAX_LEN;
  632. cli->dev_name = (const char *) t;
  633. cli->dev_name_len = count;
  634. g = cend;
  635. info->num_clients++;
  636. if (info->num_clients == P2P_MAX_GROUP_ENTRIES)
  637. return -1;
  638. }
  639. return 0;
  640. }
  641. static int p2p_group_info_text(const u8 *gi, size_t gi_len, char *buf,
  642. char *end)
  643. {
  644. char *pos = buf;
  645. int ret;
  646. struct p2p_group_info info;
  647. unsigned int i;
  648. if (p2p_group_info_parse(gi, gi_len, &info) < 0)
  649. return 0;
  650. for (i = 0; i < info.num_clients; i++) {
  651. struct p2p_client_info *cli;
  652. char name[WPS_DEV_NAME_MAX_LEN + 1];
  653. char devtype[WPS_DEV_TYPE_BUFSIZE];
  654. u8 s;
  655. int count;
  656. cli = &info.client[i];
  657. ret = os_snprintf(pos, end - pos, "p2p_group_client: "
  658. "dev=" MACSTR " iface=" MACSTR,
  659. MAC2STR(cli->p2p_device_addr),
  660. MAC2STR(cli->p2p_interface_addr));
  661. if (os_snprintf_error(end - pos, ret))
  662. return pos - buf;
  663. pos += ret;
  664. ret = os_snprintf(pos, end - pos,
  665. " dev_capab=0x%x config_methods=0x%x "
  666. "dev_type=%s",
  667. cli->dev_capab, cli->config_methods,
  668. wps_dev_type_bin2str(cli->pri_dev_type,
  669. devtype,
  670. sizeof(devtype)));
  671. if (os_snprintf_error(end - pos, ret))
  672. return pos - buf;
  673. pos += ret;
  674. for (s = 0; s < cli->num_sec_dev_types; s++) {
  675. ret = os_snprintf(pos, end - pos, " dev_type=%s",
  676. wps_dev_type_bin2str(
  677. &cli->sec_dev_types[s * 8],
  678. devtype, sizeof(devtype)));
  679. if (os_snprintf_error(end - pos, ret))
  680. return pos - buf;
  681. pos += ret;
  682. }
  683. os_memcpy(name, cli->dev_name, cli->dev_name_len);
  684. name[cli->dev_name_len] = '\0';
  685. count = (int) cli->dev_name_len - 1;
  686. while (count >= 0) {
  687. if (is_ctrl_char(name[count]))
  688. name[count] = '_';
  689. count--;
  690. }
  691. ret = os_snprintf(pos, end - pos, " dev_name='%s'\n", name);
  692. if (os_snprintf_error(end - pos, ret))
  693. return pos - buf;
  694. pos += ret;
  695. }
  696. return pos - buf;
  697. }
  698. /**
  699. * p2p_attr_text - Build text format description of P2P IE attributes
  700. * @data: P2P IE contents
  701. * @buf: Buffer for returning text
  702. * @end: Pointer to the end of the buf area
  703. * Returns: Number of octets written to the buffer or -1 on faikure
  704. *
  705. * This function can be used to parse P2P IE contents into text format
  706. * field=value lines.
  707. */
  708. int p2p_attr_text(struct wpabuf *data, char *buf, char *end)
  709. {
  710. struct p2p_message msg;
  711. char *pos = buf;
  712. int ret;
  713. os_memset(&msg, 0, sizeof(msg));
  714. if (p2p_parse_p2p_ie(data, &msg))
  715. return -1;
  716. if (msg.capability) {
  717. ret = os_snprintf(pos, end - pos,
  718. "p2p_dev_capab=0x%x\n"
  719. "p2p_group_capab=0x%x\n",
  720. msg.capability[0], msg.capability[1]);
  721. if (os_snprintf_error(end - pos, ret))
  722. return pos - buf;
  723. pos += ret;
  724. }
  725. if (msg.pri_dev_type) {
  726. char devtype[WPS_DEV_TYPE_BUFSIZE];
  727. ret = os_snprintf(pos, end - pos,
  728. "p2p_primary_device_type=%s\n",
  729. wps_dev_type_bin2str(msg.pri_dev_type,
  730. devtype,
  731. sizeof(devtype)));
  732. if (os_snprintf_error(end - pos, ret))
  733. return pos - buf;
  734. pos += ret;
  735. }
  736. ret = os_snprintf(pos, end - pos, "p2p_device_name=%s\n",
  737. msg.device_name);
  738. if (os_snprintf_error(end - pos, ret))
  739. return pos - buf;
  740. pos += ret;
  741. if (msg.p2p_device_addr) {
  742. ret = os_snprintf(pos, end - pos, "p2p_device_addr=" MACSTR
  743. "\n",
  744. MAC2STR(msg.p2p_device_addr));
  745. if (os_snprintf_error(end - pos, ret))
  746. return pos - buf;
  747. pos += ret;
  748. }
  749. ret = os_snprintf(pos, end - pos, "p2p_config_methods=0x%x\n",
  750. msg.config_methods);
  751. if (os_snprintf_error(end - pos, ret))
  752. return pos - buf;
  753. pos += ret;
  754. ret = p2p_group_info_text(msg.group_info, msg.group_info_len,
  755. pos, end);
  756. if (ret < 0)
  757. return pos - buf;
  758. pos += ret;
  759. return pos - buf;
  760. }
  761. int p2p_get_cross_connect_disallowed(const struct wpabuf *p2p_ie)
  762. {
  763. struct p2p_message msg;
  764. os_memset(&msg, 0, sizeof(msg));
  765. if (p2p_parse_p2p_ie(p2p_ie, &msg))
  766. return 0;
  767. if (!msg.manageability)
  768. return 0;
  769. return !(msg.manageability[0] & P2P_MAN_CROSS_CONNECTION_PERMITTED);
  770. }
  771. u8 p2p_get_group_capab(const struct wpabuf *p2p_ie)
  772. {
  773. struct p2p_message msg;
  774. os_memset(&msg, 0, sizeof(msg));
  775. if (p2p_parse_p2p_ie(p2p_ie, &msg))
  776. return 0;
  777. if (!msg.capability)
  778. return 0;
  779. return msg.capability[1];
  780. }
  781. const u8 * p2p_get_go_dev_addr(const struct wpabuf *p2p_ie)
  782. {
  783. struct p2p_message msg;
  784. os_memset(&msg, 0, sizeof(msg));
  785. if (p2p_parse_p2p_ie(p2p_ie, &msg))
  786. return NULL;
  787. if (msg.p2p_device_addr)
  788. return msg.p2p_device_addr;
  789. if (msg.device_id)
  790. return msg.device_id;
  791. return NULL;
  792. }