p2p_parse.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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 || nlen > 32) {
  148. wpa_printf(MSG_DEBUG, "P2P: Invalid Device Name "
  149. "length %d (buf len %d)", (int) nlen,
  150. (int) (data + len - pos));
  151. return -1;
  152. }
  153. os_memcpy(msg->device_name, pos, nlen);
  154. msg->device_name[nlen] = '\0';
  155. for (i = 0; i < nlen; i++) {
  156. if (msg->device_name[i] == '\0')
  157. break;
  158. if (msg->device_name[i] > 0 &&
  159. msg->device_name[i] < 32)
  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 + 32) {
  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. default:
  267. wpa_printf(MSG_DEBUG, "P2P: Skipped unknown attribute %d "
  268. "(length %d)", id, len);
  269. break;
  270. }
  271. return 0;
  272. }
  273. /**
  274. * p2p_parse_p2p_ie - Parse P2P IE
  275. * @buf: Concatenated P2P IE(s) payload
  276. * @msg: Buffer for returning parsed attributes
  277. * Returns: 0 on success, -1 on failure
  278. *
  279. * Note: Caller is responsible for clearing the msg data structure before
  280. * calling this function.
  281. */
  282. int p2p_parse_p2p_ie(const struct wpabuf *buf, struct p2p_message *msg)
  283. {
  284. const u8 *pos = wpabuf_head_u8(buf);
  285. const u8 *end = pos + wpabuf_len(buf);
  286. wpa_printf(MSG_DEBUG, "P2P: Parsing P2P IE");
  287. while (pos < end) {
  288. u16 attr_len;
  289. if (pos + 2 >= end) {
  290. wpa_printf(MSG_DEBUG, "P2P: Invalid P2P attribute");
  291. return -1;
  292. }
  293. attr_len = WPA_GET_LE16(pos + 1);
  294. wpa_printf(MSG_DEBUG, "P2P: Attribute %d length %u",
  295. pos[0], attr_len);
  296. if (pos + 3 + attr_len > end) {
  297. wpa_printf(MSG_DEBUG, "P2P: Attribute underflow "
  298. "(len=%u left=%d)",
  299. attr_len, (int) (end - pos - 3));
  300. wpa_hexdump(MSG_MSGDUMP, "P2P: Data", pos, end - pos);
  301. return -1;
  302. }
  303. if (p2p_parse_attribute(pos[0], pos + 3, attr_len, msg))
  304. return -1;
  305. pos += 3 + attr_len;
  306. }
  307. return 0;
  308. }
  309. static int p2p_parse_wps_ie(const struct wpabuf *buf, struct p2p_message *msg)
  310. {
  311. struct wps_parse_attr attr;
  312. int i;
  313. wpa_printf(MSG_DEBUG, "P2P: Parsing WPS IE");
  314. if (wps_parse_msg(buf, &attr))
  315. return -1;
  316. if (attr.dev_name && attr.dev_name_len < sizeof(msg->device_name) &&
  317. !msg->device_name[0])
  318. os_memcpy(msg->device_name, attr.dev_name, attr.dev_name_len);
  319. if (attr.config_methods) {
  320. msg->wps_config_methods =
  321. WPA_GET_BE16(attr.config_methods);
  322. wpa_printf(MSG_DEBUG, "P2P: Config Methods (WPS): 0x%x",
  323. msg->wps_config_methods);
  324. }
  325. if (attr.dev_password_id) {
  326. msg->dev_password_id = WPA_GET_BE16(attr.dev_password_id);
  327. wpa_printf(MSG_DEBUG, "P2P: Device Password ID: %d",
  328. msg->dev_password_id);
  329. }
  330. if (attr.primary_dev_type) {
  331. char devtype[WPS_DEV_TYPE_BUFSIZE];
  332. msg->wps_pri_dev_type = attr.primary_dev_type;
  333. wpa_printf(MSG_DEBUG, "P2P: Primary Device Type (WPS): %s",
  334. wps_dev_type_bin2str(msg->wps_pri_dev_type, devtype,
  335. sizeof(devtype)));
  336. }
  337. if (attr.sec_dev_type_list) {
  338. msg->wps_sec_dev_type_list = attr.sec_dev_type_list;
  339. msg->wps_sec_dev_type_list_len = attr.sec_dev_type_list_len;
  340. }
  341. for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
  342. msg->wps_vendor_ext[i] = attr.vendor_ext[i];
  343. msg->wps_vendor_ext_len[i] = attr.vendor_ext_len[i];
  344. }
  345. msg->manufacturer = attr.manufacturer;
  346. msg->manufacturer_len = attr.manufacturer_len;
  347. msg->model_name = attr.model_name;
  348. msg->model_name_len = attr.model_name_len;
  349. msg->model_number = attr.model_number;
  350. msg->model_number_len = attr.model_number_len;
  351. msg->serial_number = attr.serial_number;
  352. msg->serial_number_len = attr.serial_number_len;
  353. return 0;
  354. }
  355. /**
  356. * p2p_parse_ies - Parse P2P message IEs (both WPS and P2P IE)
  357. * @data: IEs from the message
  358. * @len: Length of data buffer in octets
  359. * @msg: Buffer for returning parsed attributes
  360. * Returns: 0 on success, -1 on failure
  361. *
  362. * Note: Caller is responsible for clearing the msg data structure before
  363. * calling this function.
  364. *
  365. * Note: Caller must free temporary memory allocations by calling
  366. * p2p_parse_free() when the parsed data is not needed anymore.
  367. */
  368. int p2p_parse_ies(const u8 *data, size_t len, struct p2p_message *msg)
  369. {
  370. struct ieee802_11_elems elems;
  371. ieee802_11_parse_elems(data, len, &elems, 0);
  372. if (elems.ds_params && elems.ds_params_len >= 1)
  373. msg->ds_params = elems.ds_params;
  374. if (elems.ssid)
  375. msg->ssid = elems.ssid - 2;
  376. msg->wps_attributes = ieee802_11_vendor_ie_concat(data, len,
  377. WPS_DEV_OUI_WFA);
  378. if (msg->wps_attributes &&
  379. p2p_parse_wps_ie(msg->wps_attributes, msg)) {
  380. p2p_parse_free(msg);
  381. return -1;
  382. }
  383. msg->p2p_attributes = ieee802_11_vendor_ie_concat(data, len,
  384. P2P_IE_VENDOR_TYPE);
  385. if (msg->p2p_attributes &&
  386. p2p_parse_p2p_ie(msg->p2p_attributes, msg)) {
  387. wpa_printf(MSG_DEBUG, "P2P: Failed to parse P2P IE data");
  388. if (msg->p2p_attributes)
  389. wpa_hexdump_buf(MSG_MSGDUMP, "P2P: P2P IE data",
  390. msg->p2p_attributes);
  391. p2p_parse_free(msg);
  392. return -1;
  393. }
  394. return 0;
  395. }
  396. /**
  397. * p2p_parse - Parse a P2P Action frame contents
  398. * @data: Action frame payload after Category and Code fields
  399. * @len: Length of data buffer in octets
  400. * @msg: Buffer for returning parsed attributes
  401. * Returns: 0 on success, -1 on failure
  402. *
  403. * Note: Caller must free temporary memory allocations by calling
  404. * p2p_parse_free() when the parsed data is not needed anymore.
  405. */
  406. int p2p_parse(const u8 *data, size_t len, struct p2p_message *msg)
  407. {
  408. os_memset(msg, 0, sizeof(*msg));
  409. wpa_printf(MSG_DEBUG, "P2P: Parsing the received message");
  410. if (len < 1) {
  411. wpa_printf(MSG_DEBUG, "P2P: No Dialog Token in the message");
  412. return -1;
  413. }
  414. msg->dialog_token = data[0];
  415. wpa_printf(MSG_DEBUG, "P2P: * Dialog Token: %d", msg->dialog_token);
  416. return p2p_parse_ies(data + 1, len - 1, msg);
  417. }
  418. /**
  419. * p2p_parse_free - Free temporary data from P2P parsing
  420. * @msg: Parsed attributes
  421. */
  422. void p2p_parse_free(struct p2p_message *msg)
  423. {
  424. wpabuf_free(msg->p2p_attributes);
  425. msg->p2p_attributes = NULL;
  426. wpabuf_free(msg->wps_attributes);
  427. msg->wps_attributes = NULL;
  428. }
  429. int p2p_group_info_parse(const u8 *gi, size_t gi_len,
  430. struct p2p_group_info *info)
  431. {
  432. const u8 *g, *gend;
  433. os_memset(info, 0, sizeof(*info));
  434. if (gi == NULL)
  435. return 0;
  436. g = gi;
  437. gend = gi + gi_len;
  438. while (g < gend) {
  439. struct p2p_client_info *cli;
  440. const u8 *t, *cend;
  441. int count;
  442. cli = &info->client[info->num_clients];
  443. cend = g + 1 + g[0];
  444. if (cend > gend)
  445. return -1; /* invalid data */
  446. /* g at start of P2P Client Info Descriptor */
  447. /* t at Device Capability Bitmap */
  448. t = g + 1 + 2 * ETH_ALEN;
  449. if (t > cend)
  450. return -1; /* invalid data */
  451. cli->p2p_device_addr = g + 1;
  452. cli->p2p_interface_addr = g + 1 + ETH_ALEN;
  453. cli->dev_capab = t[0];
  454. if (t + 1 + 2 + 8 + 1 > cend)
  455. return -1; /* invalid data */
  456. cli->config_methods = WPA_GET_BE16(&t[1]);
  457. cli->pri_dev_type = &t[3];
  458. t += 1 + 2 + 8;
  459. /* t at Number of Secondary Device Types */
  460. cli->num_sec_dev_types = *t++;
  461. if (t + 8 * cli->num_sec_dev_types > cend)
  462. return -1; /* invalid data */
  463. cli->sec_dev_types = t;
  464. t += 8 * cli->num_sec_dev_types;
  465. /* t at Device Name in WPS TLV format */
  466. if (t + 2 + 2 > cend)
  467. return -1; /* invalid data */
  468. if (WPA_GET_BE16(t) != ATTR_DEV_NAME)
  469. return -1; /* invalid Device Name TLV */
  470. t += 2;
  471. count = WPA_GET_BE16(t);
  472. t += 2;
  473. if (count > cend - t)
  474. return -1; /* invalid Device Name TLV */
  475. if (count >= 32)
  476. count = 32;
  477. cli->dev_name = (const char *) t;
  478. cli->dev_name_len = count;
  479. g = cend;
  480. info->num_clients++;
  481. if (info->num_clients == P2P_MAX_GROUP_ENTRIES)
  482. return -1;
  483. }
  484. return 0;
  485. }
  486. static int p2p_group_info_text(const u8 *gi, size_t gi_len, char *buf,
  487. char *end)
  488. {
  489. char *pos = buf;
  490. int ret;
  491. struct p2p_group_info info;
  492. unsigned int i;
  493. if (p2p_group_info_parse(gi, gi_len, &info) < 0)
  494. return 0;
  495. for (i = 0; i < info.num_clients; i++) {
  496. struct p2p_client_info *cli;
  497. char name[33];
  498. char devtype[WPS_DEV_TYPE_BUFSIZE];
  499. u8 s;
  500. int count;
  501. cli = &info.client[i];
  502. ret = os_snprintf(pos, end - pos, "p2p_group_client: "
  503. "dev=" MACSTR " iface=" MACSTR,
  504. MAC2STR(cli->p2p_device_addr),
  505. MAC2STR(cli->p2p_interface_addr));
  506. if (ret < 0 || ret >= end - pos)
  507. return pos - buf;
  508. pos += ret;
  509. ret = os_snprintf(pos, end - pos,
  510. " dev_capab=0x%x config_methods=0x%x "
  511. "dev_type=%s",
  512. cli->dev_capab, cli->config_methods,
  513. wps_dev_type_bin2str(cli->pri_dev_type,
  514. devtype,
  515. sizeof(devtype)));
  516. if (ret < 0 || ret >= end - pos)
  517. return pos - buf;
  518. pos += ret;
  519. for (s = 0; s < cli->num_sec_dev_types; s++) {
  520. ret = os_snprintf(pos, end - pos, " dev_type=%s",
  521. wps_dev_type_bin2str(
  522. &cli->sec_dev_types[s * 8],
  523. devtype, sizeof(devtype)));
  524. if (ret < 0 || ret >= end - pos)
  525. return pos - buf;
  526. pos += ret;
  527. }
  528. os_memcpy(name, cli->dev_name, cli->dev_name_len);
  529. name[cli->dev_name_len] = '\0';
  530. count = (int) cli->dev_name_len - 1;
  531. while (count >= 0) {
  532. if (name[count] > 0 && name[count] < 32)
  533. name[count] = '_';
  534. count--;
  535. }
  536. ret = os_snprintf(pos, end - pos, " dev_name='%s'\n", name);
  537. if (ret < 0 || ret >= end - pos)
  538. return pos - buf;
  539. pos += ret;
  540. }
  541. return pos - buf;
  542. }
  543. /**
  544. * p2p_attr_text - Build text format description of P2P IE attributes
  545. * @data: P2P IE contents
  546. * @buf: Buffer for returning text
  547. * @end: Pointer to the end of the buf area
  548. * Returns: Number of octets written to the buffer or -1 on faikure
  549. *
  550. * This function can be used to parse P2P IE contents into text format
  551. * field=value lines.
  552. */
  553. int p2p_attr_text(struct wpabuf *data, char *buf, char *end)
  554. {
  555. struct p2p_message msg;
  556. char *pos = buf;
  557. int ret;
  558. os_memset(&msg, 0, sizeof(msg));
  559. if (p2p_parse_p2p_ie(data, &msg))
  560. return -1;
  561. if (msg.capability) {
  562. ret = os_snprintf(pos, end - pos,
  563. "p2p_dev_capab=0x%x\n"
  564. "p2p_group_capab=0x%x\n",
  565. msg.capability[0], msg.capability[1]);
  566. if (ret < 0 || ret >= end - pos)
  567. return pos - buf;
  568. pos += ret;
  569. }
  570. if (msg.pri_dev_type) {
  571. char devtype[WPS_DEV_TYPE_BUFSIZE];
  572. ret = os_snprintf(pos, end - pos,
  573. "p2p_primary_device_type=%s\n",
  574. wps_dev_type_bin2str(msg.pri_dev_type,
  575. devtype,
  576. sizeof(devtype)));
  577. if (ret < 0 || ret >= end - pos)
  578. return pos - buf;
  579. pos += ret;
  580. }
  581. ret = os_snprintf(pos, end - pos, "p2p_device_name=%s\n",
  582. msg.device_name);
  583. if (ret < 0 || ret >= end - pos)
  584. return pos - buf;
  585. pos += ret;
  586. if (msg.p2p_device_addr) {
  587. ret = os_snprintf(pos, end - pos, "p2p_device_addr=" MACSTR
  588. "\n",
  589. MAC2STR(msg.p2p_device_addr));
  590. if (ret < 0 || ret >= end - pos)
  591. return pos - buf;
  592. pos += ret;
  593. }
  594. ret = os_snprintf(pos, end - pos, "p2p_config_methods=0x%x\n",
  595. msg.config_methods);
  596. if (ret < 0 || ret >= end - pos)
  597. return pos - buf;
  598. pos += ret;
  599. ret = p2p_group_info_text(msg.group_info, msg.group_info_len,
  600. pos, end);
  601. if (ret < 0)
  602. return pos - buf;
  603. pos += ret;
  604. return pos - buf;
  605. }
  606. int p2p_get_cross_connect_disallowed(const struct wpabuf *p2p_ie)
  607. {
  608. struct p2p_message msg;
  609. os_memset(&msg, 0, sizeof(msg));
  610. if (p2p_parse_p2p_ie(p2p_ie, &msg))
  611. return 0;
  612. if (!msg.manageability)
  613. return 0;
  614. return !(msg.manageability[0] & P2P_MAN_CROSS_CONNECTION_PERMITTED);
  615. }
  616. u8 p2p_get_group_capab(const struct wpabuf *p2p_ie)
  617. {
  618. struct p2p_message msg;
  619. os_memset(&msg, 0, sizeof(msg));
  620. if (p2p_parse_p2p_ie(p2p_ie, &msg))
  621. return 0;
  622. if (!msg.capability)
  623. return 0;
  624. return msg.capability[1];
  625. }
  626. const u8 * p2p_get_go_dev_addr(const struct wpabuf *p2p_ie)
  627. {
  628. struct p2p_message msg;
  629. os_memset(&msg, 0, sizeof(msg));
  630. if (p2p_parse_p2p_ie(p2p_ie, &msg))
  631. return NULL;
  632. if (msg.p2p_device_addr)
  633. return msg.p2p_device_addr;
  634. if (msg.device_id)
  635. return msg.device_id;
  636. return NULL;
  637. }