p2p_build.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. /*
  2. * P2P - IE builder
  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 "wps/wps_i.h"
  12. #include "p2p_i.h"
  13. void p2p_buf_add_action_hdr(struct wpabuf *buf, u8 subtype, u8 dialog_token)
  14. {
  15. wpabuf_put_u8(buf, WLAN_ACTION_VENDOR_SPECIFIC);
  16. wpabuf_put_be32(buf, P2P_IE_VENDOR_TYPE);
  17. wpabuf_put_u8(buf, subtype); /* OUI Subtype */
  18. wpabuf_put_u8(buf, dialog_token);
  19. wpa_printf(MSG_DEBUG, "P2P: * Dialog Token: %d", dialog_token);
  20. }
  21. void p2p_buf_add_public_action_hdr(struct wpabuf *buf, u8 subtype,
  22. u8 dialog_token)
  23. {
  24. wpabuf_put_u8(buf, WLAN_ACTION_PUBLIC);
  25. wpabuf_put_u8(buf, WLAN_PA_VENDOR_SPECIFIC);
  26. wpabuf_put_be32(buf, P2P_IE_VENDOR_TYPE);
  27. wpabuf_put_u8(buf, subtype); /* OUI Subtype */
  28. wpabuf_put_u8(buf, dialog_token);
  29. wpa_printf(MSG_DEBUG, "P2P: * Dialog Token: %d", dialog_token);
  30. }
  31. u8 * p2p_buf_add_ie_hdr(struct wpabuf *buf)
  32. {
  33. u8 *len;
  34. /* P2P IE header */
  35. wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
  36. len = wpabuf_put(buf, 1); /* IE length to be filled */
  37. wpabuf_put_be32(buf, P2P_IE_VENDOR_TYPE);
  38. wpa_printf(MSG_DEBUG, "P2P: * P2P IE header");
  39. return len;
  40. }
  41. void p2p_buf_update_ie_hdr(struct wpabuf *buf, u8 *len)
  42. {
  43. /* Update P2P IE Length */
  44. *len = (u8 *) wpabuf_put(buf, 0) - len - 1;
  45. }
  46. void p2p_buf_add_capability(struct wpabuf *buf, u8 dev_capab, u8 group_capab)
  47. {
  48. /* P2P Capability */
  49. wpabuf_put_u8(buf, P2P_ATTR_CAPABILITY);
  50. wpabuf_put_le16(buf, 2);
  51. wpabuf_put_u8(buf, dev_capab); /* Device Capabilities */
  52. wpabuf_put_u8(buf, group_capab); /* Group Capabilities */
  53. wpa_printf(MSG_DEBUG, "P2P: * Capability dev=%02x group=%02x",
  54. dev_capab, group_capab);
  55. }
  56. void p2p_buf_add_go_intent(struct wpabuf *buf, u8 go_intent)
  57. {
  58. /* Group Owner Intent */
  59. wpabuf_put_u8(buf, P2P_ATTR_GROUP_OWNER_INTENT);
  60. wpabuf_put_le16(buf, 1);
  61. wpabuf_put_u8(buf, go_intent);
  62. wpa_printf(MSG_DEBUG, "P2P: * GO Intent: Intent %u Tie breaker %u",
  63. go_intent >> 1, go_intent & 0x01);
  64. }
  65. void p2p_buf_add_listen_channel(struct wpabuf *buf, const char *country,
  66. u8 reg_class, u8 channel)
  67. {
  68. /* Listen Channel */
  69. wpabuf_put_u8(buf, P2P_ATTR_LISTEN_CHANNEL);
  70. wpabuf_put_le16(buf, 5);
  71. wpabuf_put_data(buf, country, 3);
  72. wpabuf_put_u8(buf, reg_class); /* Regulatory Class */
  73. wpabuf_put_u8(buf, channel); /* Channel Number */
  74. wpa_printf(MSG_DEBUG, "P2P: * Listen Channel: Regulatory Class %u "
  75. "Channel %u", reg_class, channel);
  76. }
  77. void p2p_buf_add_operating_channel(struct wpabuf *buf, const char *country,
  78. u8 reg_class, u8 channel)
  79. {
  80. /* Operating Channel */
  81. wpabuf_put_u8(buf, P2P_ATTR_OPERATING_CHANNEL);
  82. wpabuf_put_le16(buf, 5);
  83. wpabuf_put_data(buf, country, 3);
  84. wpabuf_put_u8(buf, reg_class); /* Regulatory Class */
  85. wpabuf_put_u8(buf, channel); /* Channel Number */
  86. wpa_printf(MSG_DEBUG, "P2P: * Operating Channel: Regulatory Class %u "
  87. "Channel %u", reg_class, channel);
  88. }
  89. void p2p_buf_add_channel_list(struct wpabuf *buf, const char *country,
  90. struct p2p_channels *chan)
  91. {
  92. u8 *len;
  93. size_t i;
  94. /* Channel List */
  95. wpabuf_put_u8(buf, P2P_ATTR_CHANNEL_LIST);
  96. len = wpabuf_put(buf, 2); /* IE length to be filled */
  97. wpabuf_put_data(buf, country, 3); /* Country String */
  98. for (i = 0; i < chan->reg_classes; i++) {
  99. struct p2p_reg_class *c = &chan->reg_class[i];
  100. wpabuf_put_u8(buf, c->reg_class);
  101. wpabuf_put_u8(buf, c->channels);
  102. wpabuf_put_data(buf, c->channel, c->channels);
  103. }
  104. /* Update attribute length */
  105. WPA_PUT_LE16(len, (u8 *) wpabuf_put(buf, 0) - len - 2);
  106. wpa_hexdump(MSG_DEBUG, "P2P: * Channel List",
  107. len + 2, (u8 *) wpabuf_put(buf, 0) - len - 2);
  108. }
  109. void p2p_buf_add_status(struct wpabuf *buf, u8 status)
  110. {
  111. /* Status */
  112. wpabuf_put_u8(buf, P2P_ATTR_STATUS);
  113. wpabuf_put_le16(buf, 1);
  114. wpabuf_put_u8(buf, status);
  115. wpa_printf(MSG_DEBUG, "P2P: * Status: %d", status);
  116. }
  117. void p2p_buf_add_device_info(struct wpabuf *buf, struct p2p_data *p2p,
  118. struct p2p_device *peer)
  119. {
  120. u8 *len;
  121. u16 methods;
  122. size_t nlen, i;
  123. /* P2P Device Info */
  124. wpabuf_put_u8(buf, P2P_ATTR_DEVICE_INFO);
  125. len = wpabuf_put(buf, 2); /* IE length to be filled */
  126. /* P2P Device address */
  127. wpabuf_put_data(buf, p2p->cfg->dev_addr, ETH_ALEN);
  128. /* Config Methods */
  129. methods = 0;
  130. if (peer && peer->wps_method != WPS_NOT_READY) {
  131. if (peer->wps_method == WPS_PBC)
  132. methods |= WPS_CONFIG_PUSHBUTTON;
  133. else if (peer->wps_method == WPS_PIN_DISPLAY ||
  134. peer->wps_method == WPS_PIN_KEYPAD) {
  135. methods |= WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
  136. methods |= WPS_CONFIG_P2PS;
  137. }
  138. } else if (p2p->cfg->config_methods) {
  139. methods |= p2p->cfg->config_methods &
  140. (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_DISPLAY |
  141. WPS_CONFIG_KEYPAD | WPS_CONFIG_P2PS);
  142. } else {
  143. methods |= WPS_CONFIG_PUSHBUTTON;
  144. methods |= WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
  145. methods |= WPS_CONFIG_P2PS;
  146. }
  147. wpabuf_put_be16(buf, methods);
  148. /* Primary Device Type */
  149. wpabuf_put_data(buf, p2p->cfg->pri_dev_type,
  150. sizeof(p2p->cfg->pri_dev_type));
  151. /* Number of Secondary Device Types */
  152. wpabuf_put_u8(buf, p2p->cfg->num_sec_dev_types);
  153. /* Secondary Device Type List */
  154. for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
  155. wpabuf_put_data(buf, p2p->cfg->sec_dev_type[i],
  156. WPS_DEV_TYPE_LEN);
  157. /* Device Name */
  158. nlen = p2p->cfg->dev_name ? os_strlen(p2p->cfg->dev_name) : 0;
  159. wpabuf_put_be16(buf, ATTR_DEV_NAME);
  160. wpabuf_put_be16(buf, nlen);
  161. wpabuf_put_data(buf, p2p->cfg->dev_name, nlen);
  162. /* Update attribute length */
  163. WPA_PUT_LE16(len, (u8 *) wpabuf_put(buf, 0) - len - 2);
  164. wpa_printf(MSG_DEBUG, "P2P: * Device Info");
  165. }
  166. void p2p_buf_add_device_id(struct wpabuf *buf, const u8 *dev_addr)
  167. {
  168. /* P2P Device ID */
  169. wpabuf_put_u8(buf, P2P_ATTR_DEVICE_ID);
  170. wpabuf_put_le16(buf, ETH_ALEN);
  171. wpabuf_put_data(buf, dev_addr, ETH_ALEN);
  172. wpa_printf(MSG_DEBUG, "P2P: * Device ID: " MACSTR, MAC2STR(dev_addr));
  173. }
  174. void p2p_buf_add_config_timeout(struct wpabuf *buf, u8 go_timeout,
  175. u8 client_timeout)
  176. {
  177. /* Configuration Timeout */
  178. wpabuf_put_u8(buf, P2P_ATTR_CONFIGURATION_TIMEOUT);
  179. wpabuf_put_le16(buf, 2);
  180. wpabuf_put_u8(buf, go_timeout);
  181. wpabuf_put_u8(buf, client_timeout);
  182. wpa_printf(MSG_DEBUG, "P2P: * Configuration Timeout: GO %d (*10ms) "
  183. "client %d (*10ms)", go_timeout, client_timeout);
  184. }
  185. void p2p_buf_add_intended_addr(struct wpabuf *buf, const u8 *interface_addr)
  186. {
  187. /* Intended P2P Interface Address */
  188. wpabuf_put_u8(buf, P2P_ATTR_INTENDED_INTERFACE_ADDR);
  189. wpabuf_put_le16(buf, ETH_ALEN);
  190. wpabuf_put_data(buf, interface_addr, ETH_ALEN);
  191. wpa_printf(MSG_DEBUG, "P2P: * Intended P2P Interface Address " MACSTR,
  192. MAC2STR(interface_addr));
  193. }
  194. void p2p_buf_add_group_bssid(struct wpabuf *buf, const u8 *bssid)
  195. {
  196. /* P2P Group BSSID */
  197. wpabuf_put_u8(buf, P2P_ATTR_GROUP_BSSID);
  198. wpabuf_put_le16(buf, ETH_ALEN);
  199. wpabuf_put_data(buf, bssid, ETH_ALEN);
  200. wpa_printf(MSG_DEBUG, "P2P: * P2P Group BSSID " MACSTR,
  201. MAC2STR(bssid));
  202. }
  203. void p2p_buf_add_group_id(struct wpabuf *buf, const u8 *dev_addr,
  204. const u8 *ssid, size_t ssid_len)
  205. {
  206. /* P2P Group ID */
  207. wpabuf_put_u8(buf, P2P_ATTR_GROUP_ID);
  208. wpabuf_put_le16(buf, ETH_ALEN + ssid_len);
  209. wpabuf_put_data(buf, dev_addr, ETH_ALEN);
  210. wpabuf_put_data(buf, ssid, ssid_len);
  211. wpa_printf(MSG_DEBUG, "P2P: * P2P Group ID " MACSTR,
  212. MAC2STR(dev_addr));
  213. wpa_hexdump_ascii(MSG_DEBUG, "P2P: P2P Group ID SSID", ssid, ssid_len);
  214. }
  215. void p2p_buf_add_invitation_flags(struct wpabuf *buf, u8 flags)
  216. {
  217. /* Invitation Flags */
  218. wpabuf_put_u8(buf, P2P_ATTR_INVITATION_FLAGS);
  219. wpabuf_put_le16(buf, 1);
  220. wpabuf_put_u8(buf, flags);
  221. wpa_printf(MSG_DEBUG, "P2P: * Invitation Flags: bitmap 0x%x", flags);
  222. }
  223. static void p2p_buf_add_noa_desc(struct wpabuf *buf, struct p2p_noa_desc *desc)
  224. {
  225. if (desc == NULL)
  226. return;
  227. wpabuf_put_u8(buf, desc->count_type);
  228. wpabuf_put_le32(buf, desc->duration);
  229. wpabuf_put_le32(buf, desc->interval);
  230. wpabuf_put_le32(buf, desc->start_time);
  231. }
  232. void p2p_buf_add_noa(struct wpabuf *buf, u8 noa_index, u8 opp_ps, u8 ctwindow,
  233. struct p2p_noa_desc *desc1, struct p2p_noa_desc *desc2)
  234. {
  235. /* Notice of Absence */
  236. wpabuf_put_u8(buf, P2P_ATTR_NOTICE_OF_ABSENCE);
  237. wpabuf_put_le16(buf, 2 + (desc1 ? 13 : 0) + (desc2 ? 13 : 0));
  238. wpabuf_put_u8(buf, noa_index);
  239. wpabuf_put_u8(buf, (opp_ps ? 0x80 : 0) | (ctwindow & 0x7f));
  240. p2p_buf_add_noa_desc(buf, desc1);
  241. p2p_buf_add_noa_desc(buf, desc2);
  242. wpa_printf(MSG_DEBUG, "P2P: * Notice of Absence");
  243. }
  244. void p2p_buf_add_ext_listen_timing(struct wpabuf *buf, u16 period,
  245. u16 interval)
  246. {
  247. /* Extended Listen Timing */
  248. wpabuf_put_u8(buf, P2P_ATTR_EXT_LISTEN_TIMING);
  249. wpabuf_put_le16(buf, 4);
  250. wpabuf_put_le16(buf, period);
  251. wpabuf_put_le16(buf, interval);
  252. wpa_printf(MSG_DEBUG, "P2P: * Extended Listen Timing (period %u msec "
  253. "interval %u msec)", period, interval);
  254. }
  255. void p2p_buf_add_p2p_interface(struct wpabuf *buf, struct p2p_data *p2p)
  256. {
  257. /* P2P Interface */
  258. wpabuf_put_u8(buf, P2P_ATTR_INTERFACE);
  259. wpabuf_put_le16(buf, ETH_ALEN + 1 + ETH_ALEN);
  260. /* P2P Device address */
  261. wpabuf_put_data(buf, p2p->cfg->dev_addr, ETH_ALEN);
  262. /*
  263. * FIX: Fetch interface address list from driver. Do not include
  264. * the P2P Device address if it is never used as interface address.
  265. */
  266. /* P2P Interface Address Count */
  267. wpabuf_put_u8(buf, 1);
  268. wpabuf_put_data(buf, p2p->cfg->dev_addr, ETH_ALEN);
  269. }
  270. void p2p_buf_add_oob_go_neg_channel(struct wpabuf *buf, const char *country,
  271. u8 oper_class, u8 channel,
  272. enum p2p_role_indication role)
  273. {
  274. /* OOB Group Owner Negotiation Channel */
  275. wpabuf_put_u8(buf, P2P_ATTR_OOB_GO_NEG_CHANNEL);
  276. wpabuf_put_le16(buf, 6);
  277. wpabuf_put_data(buf, country, 3);
  278. wpabuf_put_u8(buf, oper_class); /* Operating Class */
  279. wpabuf_put_u8(buf, channel); /* Channel Number */
  280. wpabuf_put_u8(buf, (u8) role); /* Role indication */
  281. wpa_printf(MSG_DEBUG, "P2P: * OOB GO Negotiation Channel: Operating "
  282. "Class %u Channel %u Role %d",
  283. oper_class, channel, role);
  284. }
  285. void p2p_buf_add_service_hash(struct wpabuf *buf, struct p2p_data *p2p)
  286. {
  287. if (!p2p)
  288. return;
  289. /* Service Hash */
  290. wpabuf_put_u8(buf, P2P_ATTR_SERVICE_HASH);
  291. wpabuf_put_le16(buf, p2p->p2ps_seek_count * P2PS_HASH_LEN);
  292. wpabuf_put_data(buf, p2p->query_hash,
  293. p2p->p2ps_seek_count * P2PS_HASH_LEN);
  294. wpa_hexdump(MSG_DEBUG, "P2P: * Service Hash",
  295. p2p->query_hash, p2p->p2ps_seek_count * P2PS_HASH_LEN);
  296. }
  297. void p2p_buf_add_session_info(struct wpabuf *buf, const char *info)
  298. {
  299. size_t info_len = 0;
  300. if (info && info[0])
  301. info_len = os_strlen(info);
  302. /* Session Information Data Info */
  303. wpabuf_put_u8(buf, P2P_ATTR_SESSION_INFORMATION_DATA);
  304. wpabuf_put_le16(buf, (u16) info_len);
  305. if (info) {
  306. wpabuf_put_data(buf, info, info_len);
  307. wpa_printf(MSG_DEBUG, "P2P: * Session Info Data (%s)", info);
  308. }
  309. }
  310. void p2p_buf_add_connection_capability(struct wpabuf *buf, u8 connection_cap)
  311. {
  312. /* Connection Capability Info */
  313. wpabuf_put_u8(buf, P2P_ATTR_CONNECTION_CAPABILITY);
  314. wpabuf_put_le16(buf, 1);
  315. wpabuf_put_u8(buf, connection_cap);
  316. wpa_printf(MSG_DEBUG, "P2P: * Connection Capability: 0x%x",
  317. connection_cap);
  318. }
  319. void p2p_buf_add_advertisement_id(struct wpabuf *buf, u32 id, const u8 *mac)
  320. {
  321. if (!buf || !mac)
  322. return;
  323. /* Advertisement ID Info */
  324. wpabuf_put_u8(buf, P2P_ATTR_ADVERTISEMENT_ID);
  325. wpabuf_put_le16(buf, (u16) (sizeof(u32) + ETH_ALEN));
  326. wpabuf_put_le32(buf, id);
  327. wpabuf_put_data(buf, mac, ETH_ALEN);
  328. wpa_printf(MSG_DEBUG, "P2P: * Advertisement ID (%x) " MACSTR,
  329. id, MAC2STR(mac));
  330. }
  331. void p2p_buf_add_service_instance(struct wpabuf *buf, struct p2p_data *p2p,
  332. u8 hash_count, const u8 *hash,
  333. struct p2ps_advertisement *adv_list)
  334. {
  335. struct p2ps_advertisement *adv;
  336. struct wpabuf *tmp_buf;
  337. u8 *tag_len = NULL, *ie_len = NULL;
  338. size_t svc_len = 0, remaining = 0, total_len = 0;
  339. if (!adv_list || !hash)
  340. return;
  341. /* Allocate temp buffer, allowing for overflow of 1 instance */
  342. tmp_buf = wpabuf_alloc(MAX_SVC_ADV_IE_LEN + 256 + P2PS_HASH_LEN);
  343. if (!tmp_buf)
  344. return;
  345. for (adv = adv_list; adv && total_len <= MAX_SVC_ADV_LEN;
  346. adv = adv->next) {
  347. u8 count = hash_count;
  348. const u8 *test = hash;
  349. while (count--) {
  350. /* Check for wildcard */
  351. if (os_memcmp(test, p2p->wild_card_hash,
  352. P2PS_HASH_LEN) == 0) {
  353. total_len = MAX_SVC_ADV_LEN + 1;
  354. goto wild_hash;
  355. }
  356. if (os_memcmp(test, adv->hash, P2PS_HASH_LEN) == 0)
  357. goto hash_match;
  358. test += P2PS_HASH_LEN;
  359. }
  360. /* No matches found - Skip this Adv Instance */
  361. continue;
  362. hash_match:
  363. if (!tag_len) {
  364. tag_len = p2p_buf_add_ie_hdr(tmp_buf);
  365. remaining = 255 - 4;
  366. if (!ie_len) {
  367. wpabuf_put_u8(tmp_buf,
  368. P2P_ATTR_ADVERTISED_SERVICE);
  369. ie_len = wpabuf_put(tmp_buf, sizeof(u16));
  370. remaining -= (sizeof(u8) + sizeof(u16));
  371. }
  372. }
  373. svc_len = os_strlen(adv->svc_name);
  374. if (7 + svc_len + total_len > MAX_SVC_ADV_LEN) {
  375. /* Can't fit... return wildcard */
  376. total_len = MAX_SVC_ADV_LEN + 1;
  377. break;
  378. }
  379. if (remaining <= (sizeof(adv->id) +
  380. sizeof(adv->config_methods))) {
  381. size_t front = remaining;
  382. size_t back = (sizeof(adv->id) +
  383. sizeof(adv->config_methods)) - front;
  384. u8 holder[sizeof(adv->id) +
  385. sizeof(adv->config_methods)];
  386. /* This works even if front or back == 0 */
  387. WPA_PUT_LE32(holder, adv->id);
  388. WPA_PUT_BE16(&holder[sizeof(adv->id)],
  389. adv->config_methods);
  390. wpabuf_put_data(tmp_buf, holder, front);
  391. p2p_buf_update_ie_hdr(tmp_buf, tag_len);
  392. tag_len = p2p_buf_add_ie_hdr(tmp_buf);
  393. wpabuf_put_data(tmp_buf, &holder[front], back);
  394. remaining = 255 - (sizeof(adv->id) +
  395. sizeof(adv->config_methods)) - back;
  396. } else {
  397. wpabuf_put_le32(tmp_buf, adv->id);
  398. wpabuf_put_be16(tmp_buf, adv->config_methods);
  399. remaining -= (sizeof(adv->id) +
  400. sizeof(adv->config_methods));
  401. }
  402. /* We are guaranteed at least one byte for svc_len */
  403. wpabuf_put_u8(tmp_buf, svc_len);
  404. remaining -= sizeof(u8);
  405. if (remaining < svc_len) {
  406. size_t front = remaining;
  407. size_t back = svc_len - front;
  408. wpabuf_put_data(tmp_buf, adv->svc_name, front);
  409. p2p_buf_update_ie_hdr(tmp_buf, tag_len);
  410. tag_len = p2p_buf_add_ie_hdr(tmp_buf);
  411. /* In rare cases, we must split across 3 attributes */
  412. if (back > 255 - 4) {
  413. wpabuf_put_data(tmp_buf,
  414. &adv->svc_name[front], 255 - 4);
  415. back -= 255 - 4;
  416. front += 255 - 4;
  417. p2p_buf_update_ie_hdr(tmp_buf, tag_len);
  418. tag_len = p2p_buf_add_ie_hdr(tmp_buf);
  419. }
  420. wpabuf_put_data(tmp_buf, &adv->svc_name[front], back);
  421. remaining = 255 - 4 - back;
  422. } else {
  423. wpabuf_put_data(tmp_buf, adv->svc_name, svc_len);
  424. remaining -= svc_len;
  425. }
  426. /* adv_id config_methods svc_string */
  427. total_len += sizeof(u32) + sizeof(u16) + sizeof(u8) + svc_len;
  428. }
  429. if (tag_len)
  430. p2p_buf_update_ie_hdr(tmp_buf, tag_len);
  431. if (ie_len)
  432. WPA_PUT_LE16(ie_len, (u16) total_len);
  433. wild_hash:
  434. /* If all fit, return matching instances, otherwise the wildcard */
  435. if (total_len <= MAX_SVC_ADV_LEN) {
  436. wpabuf_put_buf(buf, tmp_buf);
  437. } else {
  438. char *wild_card = P2PS_WILD_HASH_STR;
  439. u8 wild_len;
  440. /* Insert wildcard instance */
  441. tag_len = p2p_buf_add_ie_hdr(buf);
  442. wpabuf_put_u8(buf, P2P_ATTR_ADVERTISED_SERVICE);
  443. ie_len = wpabuf_put(buf, sizeof(u16));
  444. wild_len = (u8) os_strlen(wild_card);
  445. wpabuf_put_le32(buf, 0);
  446. wpabuf_put_be16(buf, 0);
  447. wpabuf_put_u8(buf, wild_len);
  448. wpabuf_put_data(buf, wild_card, wild_len);
  449. WPA_PUT_LE16(ie_len, 4 + 2 + 1 + wild_len);
  450. p2p_buf_update_ie_hdr(buf, tag_len);
  451. }
  452. wpabuf_free(tmp_buf);
  453. }
  454. void p2p_buf_add_session_id(struct wpabuf *buf, u32 id, const u8 *mac)
  455. {
  456. if (!buf || !mac)
  457. return;
  458. /* Session ID Info */
  459. wpabuf_put_u8(buf, P2P_ATTR_SESSION_ID);
  460. wpabuf_put_le16(buf, (u16) (sizeof(u32) + ETH_ALEN));
  461. wpabuf_put_le32(buf, id);
  462. wpabuf_put_data(buf, mac, ETH_ALEN);
  463. wpa_printf(MSG_DEBUG, "P2P: * Session ID Info (%x) " MACSTR,
  464. id, MAC2STR(mac));
  465. }
  466. void p2p_buf_add_feature_capability(struct wpabuf *buf, u16 len, const u8 *mask)
  467. {
  468. if (!buf || !len || !mask)
  469. return;
  470. /* Feature Capability */
  471. wpabuf_put_u8(buf, P2P_ATTR_FEATURE_CAPABILITY);
  472. wpabuf_put_le16(buf, len);
  473. wpabuf_put_data(buf, mask, len);
  474. wpa_printf(MSG_DEBUG, "P2P: * Feature Capability (%d)", len);
  475. }
  476. void p2p_buf_add_persistent_group_info(struct wpabuf *buf, const u8 *dev_addr,
  477. const u8 *ssid, size_t ssid_len)
  478. {
  479. /* P2P Group ID */
  480. wpabuf_put_u8(buf, P2P_ATTR_PERSISTENT_GROUP);
  481. wpabuf_put_le16(buf, ETH_ALEN + ssid_len);
  482. wpabuf_put_data(buf, dev_addr, ETH_ALEN);
  483. wpabuf_put_data(buf, ssid, ssid_len);
  484. wpa_printf(MSG_DEBUG, "P2P: * P2P Group ID " MACSTR,
  485. MAC2STR(dev_addr));
  486. }
  487. static int p2p_add_wps_string(struct wpabuf *buf, enum wps_attribute attr,
  488. const char *val)
  489. {
  490. size_t len;
  491. len = val ? os_strlen(val) : 0;
  492. if (wpabuf_tailroom(buf) < 4 + len)
  493. return -1;
  494. wpabuf_put_be16(buf, attr);
  495. #ifndef CONFIG_WPS_STRICT
  496. if (len == 0) {
  497. /*
  498. * Some deployed WPS implementations fail to parse zeor-length
  499. * attributes. As a workaround, send a space character if the
  500. * device attribute string is empty.
  501. */
  502. if (wpabuf_tailroom(buf) < 3)
  503. return -1;
  504. wpabuf_put_be16(buf, 1);
  505. wpabuf_put_u8(buf, ' ');
  506. return 0;
  507. }
  508. #endif /* CONFIG_WPS_STRICT */
  509. wpabuf_put_be16(buf, len);
  510. if (val)
  511. wpabuf_put_data(buf, val, len);
  512. return 0;
  513. }
  514. int p2p_build_wps_ie(struct p2p_data *p2p, struct wpabuf *buf, int pw_id,
  515. int all_attr)
  516. {
  517. u8 *len;
  518. int i;
  519. if (wpabuf_tailroom(buf) < 6)
  520. return -1;
  521. wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
  522. len = wpabuf_put(buf, 1);
  523. wpabuf_put_be32(buf, WPS_DEV_OUI_WFA);
  524. if (wps_build_version(buf) < 0)
  525. return -1;
  526. if (all_attr) {
  527. if (wpabuf_tailroom(buf) < 5)
  528. return -1;
  529. wpabuf_put_be16(buf, ATTR_WPS_STATE);
  530. wpabuf_put_be16(buf, 1);
  531. wpabuf_put_u8(buf, WPS_STATE_NOT_CONFIGURED);
  532. }
  533. if (pw_id >= 0) {
  534. if (wpabuf_tailroom(buf) < 6)
  535. return -1;
  536. /* Device Password ID */
  537. wpabuf_put_be16(buf, ATTR_DEV_PASSWORD_ID);
  538. wpabuf_put_be16(buf, 2);
  539. wpa_printf(MSG_DEBUG, "P2P: WPS IE Device Password ID: %d",
  540. pw_id);
  541. wpabuf_put_be16(buf, pw_id);
  542. }
  543. if (all_attr) {
  544. if (wpabuf_tailroom(buf) < 5)
  545. return -1;
  546. wpabuf_put_be16(buf, ATTR_RESPONSE_TYPE);
  547. wpabuf_put_be16(buf, 1);
  548. wpabuf_put_u8(buf, WPS_RESP_ENROLLEE_INFO);
  549. if (wps_build_uuid_e(buf, p2p->cfg->uuid) < 0 ||
  550. p2p_add_wps_string(buf, ATTR_MANUFACTURER,
  551. p2p->cfg->manufacturer) < 0 ||
  552. p2p_add_wps_string(buf, ATTR_MODEL_NAME,
  553. p2p->cfg->model_name) < 0 ||
  554. p2p_add_wps_string(buf, ATTR_MODEL_NUMBER,
  555. p2p->cfg->model_number) < 0 ||
  556. p2p_add_wps_string(buf, ATTR_SERIAL_NUMBER,
  557. p2p->cfg->serial_number) < 0)
  558. return -1;
  559. if (wpabuf_tailroom(buf) < 4 + WPS_DEV_TYPE_LEN)
  560. return -1;
  561. wpabuf_put_be16(buf, ATTR_PRIMARY_DEV_TYPE);
  562. wpabuf_put_be16(buf, WPS_DEV_TYPE_LEN);
  563. wpabuf_put_data(buf, p2p->cfg->pri_dev_type, WPS_DEV_TYPE_LEN);
  564. if (p2p_add_wps_string(buf, ATTR_DEV_NAME, p2p->cfg->dev_name)
  565. < 0)
  566. return -1;
  567. if (wpabuf_tailroom(buf) < 6)
  568. return -1;
  569. wpabuf_put_be16(buf, ATTR_CONFIG_METHODS);
  570. wpabuf_put_be16(buf, 2);
  571. wpabuf_put_be16(buf, p2p->cfg->config_methods);
  572. }
  573. if (wps_build_wfa_ext(buf, 0, NULL, 0) < 0)
  574. return -1;
  575. if (all_attr && p2p->cfg->num_sec_dev_types) {
  576. if (wpabuf_tailroom(buf) <
  577. 4 + WPS_DEV_TYPE_LEN * p2p->cfg->num_sec_dev_types)
  578. return -1;
  579. wpabuf_put_be16(buf, ATTR_SECONDARY_DEV_TYPE_LIST);
  580. wpabuf_put_be16(buf, WPS_DEV_TYPE_LEN *
  581. p2p->cfg->num_sec_dev_types);
  582. wpabuf_put_data(buf, p2p->cfg->sec_dev_type,
  583. WPS_DEV_TYPE_LEN *
  584. p2p->cfg->num_sec_dev_types);
  585. }
  586. /* Add the WPS vendor extensions */
  587. for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
  588. if (p2p->wps_vendor_ext[i] == NULL)
  589. break;
  590. if (wpabuf_tailroom(buf) <
  591. 4 + wpabuf_len(p2p->wps_vendor_ext[i]))
  592. continue;
  593. wpabuf_put_be16(buf, ATTR_VENDOR_EXT);
  594. wpabuf_put_be16(buf, wpabuf_len(p2p->wps_vendor_ext[i]));
  595. wpabuf_put_buf(buf, p2p->wps_vendor_ext[i]);
  596. }
  597. p2p_buf_update_ie_hdr(buf, len);
  598. return 0;
  599. }