p2p_build.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. } else if (p2p->cfg->config_methods) {
  137. methods |= p2p->cfg->config_methods &
  138. (WPS_CONFIG_PUSHBUTTON | WPS_CONFIG_DISPLAY |
  139. WPS_CONFIG_KEYPAD);
  140. } else {
  141. methods |= WPS_CONFIG_PUSHBUTTON;
  142. methods |= WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
  143. }
  144. wpabuf_put_be16(buf, methods);
  145. /* Primary Device Type */
  146. wpabuf_put_data(buf, p2p->cfg->pri_dev_type,
  147. sizeof(p2p->cfg->pri_dev_type));
  148. /* Number of Secondary Device Types */
  149. wpabuf_put_u8(buf, p2p->cfg->num_sec_dev_types);
  150. /* Secondary Device Type List */
  151. for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
  152. wpabuf_put_data(buf, p2p->cfg->sec_dev_type[i],
  153. WPS_DEV_TYPE_LEN);
  154. /* Device Name */
  155. nlen = p2p->cfg->dev_name ? os_strlen(p2p->cfg->dev_name) : 0;
  156. wpabuf_put_be16(buf, ATTR_DEV_NAME);
  157. wpabuf_put_be16(buf, nlen);
  158. wpabuf_put_data(buf, p2p->cfg->dev_name, nlen);
  159. /* Update attribute length */
  160. WPA_PUT_LE16(len, (u8 *) wpabuf_put(buf, 0) - len - 2);
  161. wpa_printf(MSG_DEBUG, "P2P: * Device Info");
  162. }
  163. void p2p_buf_add_device_id(struct wpabuf *buf, const u8 *dev_addr)
  164. {
  165. /* P2P Device ID */
  166. wpabuf_put_u8(buf, P2P_ATTR_DEVICE_ID);
  167. wpabuf_put_le16(buf, ETH_ALEN);
  168. wpabuf_put_data(buf, dev_addr, ETH_ALEN);
  169. wpa_printf(MSG_DEBUG, "P2P: * Device ID: " MACSTR, MAC2STR(dev_addr));
  170. }
  171. void p2p_buf_add_config_timeout(struct wpabuf *buf, u8 go_timeout,
  172. u8 client_timeout)
  173. {
  174. /* Configuration Timeout */
  175. wpabuf_put_u8(buf, P2P_ATTR_CONFIGURATION_TIMEOUT);
  176. wpabuf_put_le16(buf, 2);
  177. wpabuf_put_u8(buf, go_timeout);
  178. wpabuf_put_u8(buf, client_timeout);
  179. wpa_printf(MSG_DEBUG, "P2P: * Configuration Timeout: GO %d (*10ms) "
  180. "client %d (*10ms)", go_timeout, client_timeout);
  181. }
  182. void p2p_buf_add_intended_addr(struct wpabuf *buf, const u8 *interface_addr)
  183. {
  184. /* Intended P2P Interface Address */
  185. wpabuf_put_u8(buf, P2P_ATTR_INTENDED_INTERFACE_ADDR);
  186. wpabuf_put_le16(buf, ETH_ALEN);
  187. wpabuf_put_data(buf, interface_addr, ETH_ALEN);
  188. wpa_printf(MSG_DEBUG, "P2P: * Intended P2P Interface Address " MACSTR,
  189. MAC2STR(interface_addr));
  190. }
  191. void p2p_buf_add_group_bssid(struct wpabuf *buf, const u8 *bssid)
  192. {
  193. /* P2P Group BSSID */
  194. wpabuf_put_u8(buf, P2P_ATTR_GROUP_BSSID);
  195. wpabuf_put_le16(buf, ETH_ALEN);
  196. wpabuf_put_data(buf, bssid, ETH_ALEN);
  197. wpa_printf(MSG_DEBUG, "P2P: * P2P Group BSSID " MACSTR,
  198. MAC2STR(bssid));
  199. }
  200. void p2p_buf_add_group_id(struct wpabuf *buf, const u8 *dev_addr,
  201. const u8 *ssid, size_t ssid_len)
  202. {
  203. /* P2P Group ID */
  204. wpabuf_put_u8(buf, P2P_ATTR_GROUP_ID);
  205. wpabuf_put_le16(buf, ETH_ALEN + ssid_len);
  206. wpabuf_put_data(buf, dev_addr, ETH_ALEN);
  207. wpabuf_put_data(buf, ssid, ssid_len);
  208. wpa_printf(MSG_DEBUG, "P2P: * P2P Group ID " MACSTR,
  209. MAC2STR(dev_addr));
  210. wpa_hexdump_ascii(MSG_DEBUG, "P2P: P2P Group ID SSID", ssid, ssid_len);
  211. }
  212. void p2p_buf_add_invitation_flags(struct wpabuf *buf, u8 flags)
  213. {
  214. /* Invitation Flags */
  215. wpabuf_put_u8(buf, P2P_ATTR_INVITATION_FLAGS);
  216. wpabuf_put_le16(buf, 1);
  217. wpabuf_put_u8(buf, flags);
  218. wpa_printf(MSG_DEBUG, "P2P: * Invitation Flags: bitmap 0x%x", flags);
  219. }
  220. static void p2p_buf_add_noa_desc(struct wpabuf *buf, struct p2p_noa_desc *desc)
  221. {
  222. if (desc == NULL)
  223. return;
  224. wpabuf_put_u8(buf, desc->count_type);
  225. wpabuf_put_le32(buf, desc->duration);
  226. wpabuf_put_le32(buf, desc->interval);
  227. wpabuf_put_le32(buf, desc->start_time);
  228. }
  229. void p2p_buf_add_noa(struct wpabuf *buf, u8 noa_index, u8 opp_ps, u8 ctwindow,
  230. struct p2p_noa_desc *desc1, struct p2p_noa_desc *desc2)
  231. {
  232. /* Notice of Absence */
  233. wpabuf_put_u8(buf, P2P_ATTR_NOTICE_OF_ABSENCE);
  234. wpabuf_put_le16(buf, 2 + (desc1 ? 13 : 0) + (desc2 ? 13 : 0));
  235. wpabuf_put_u8(buf, noa_index);
  236. wpabuf_put_u8(buf, (opp_ps ? 0x80 : 0) | (ctwindow & 0x7f));
  237. p2p_buf_add_noa_desc(buf, desc1);
  238. p2p_buf_add_noa_desc(buf, desc2);
  239. wpa_printf(MSG_DEBUG, "P2P: * Notice of Absence");
  240. }
  241. void p2p_buf_add_ext_listen_timing(struct wpabuf *buf, u16 period,
  242. u16 interval)
  243. {
  244. /* Extended Listen Timing */
  245. wpabuf_put_u8(buf, P2P_ATTR_EXT_LISTEN_TIMING);
  246. wpabuf_put_le16(buf, 4);
  247. wpabuf_put_le16(buf, period);
  248. wpabuf_put_le16(buf, interval);
  249. wpa_printf(MSG_DEBUG, "P2P: * Extended Listen Timing (period %u msec "
  250. "interval %u msec)", period, interval);
  251. }
  252. void p2p_buf_add_p2p_interface(struct wpabuf *buf, struct p2p_data *p2p)
  253. {
  254. /* P2P Interface */
  255. wpabuf_put_u8(buf, P2P_ATTR_INTERFACE);
  256. wpabuf_put_le16(buf, ETH_ALEN + 1 + ETH_ALEN);
  257. /* P2P Device address */
  258. wpabuf_put_data(buf, p2p->cfg->dev_addr, ETH_ALEN);
  259. /*
  260. * FIX: Fetch interface address list from driver. Do not include
  261. * the P2P Device address if it is never used as interface address.
  262. */
  263. /* P2P Interface Address Count */
  264. wpabuf_put_u8(buf, 1);
  265. wpabuf_put_data(buf, p2p->cfg->dev_addr, ETH_ALEN);
  266. }
  267. void p2p_buf_add_oob_go_neg_channel(struct wpabuf *buf, const char *country,
  268. u8 oper_class, u8 channel,
  269. enum p2p_role_indication role)
  270. {
  271. /* OOB Group Owner Negotiation Channel */
  272. wpabuf_put_u8(buf, P2P_ATTR_OOB_GO_NEG_CHANNEL);
  273. wpabuf_put_le16(buf, 6);
  274. wpabuf_put_data(buf, country, 3);
  275. wpabuf_put_u8(buf, oper_class); /* Operating Class */
  276. wpabuf_put_u8(buf, channel); /* Channel Number */
  277. wpabuf_put_u8(buf, (u8) role); /* Role indication */
  278. wpa_printf(MSG_DEBUG, "P2P: * OOB GO Negotiation Channel: Operating "
  279. "Class %u Channel %u Role %d",
  280. oper_class, channel, role);
  281. }
  282. static int p2p_add_wps_string(struct wpabuf *buf, enum wps_attribute attr,
  283. const char *val)
  284. {
  285. size_t len;
  286. len = val ? os_strlen(val) : 0;
  287. if (wpabuf_tailroom(buf) < 4 + len)
  288. return -1;
  289. wpabuf_put_be16(buf, attr);
  290. #ifndef CONFIG_WPS_STRICT
  291. if (len == 0) {
  292. /*
  293. * Some deployed WPS implementations fail to parse zeor-length
  294. * attributes. As a workaround, send a space character if the
  295. * device attribute string is empty.
  296. */
  297. if (wpabuf_tailroom(buf) < 3)
  298. return -1;
  299. wpabuf_put_be16(buf, 1);
  300. wpabuf_put_u8(buf, ' ');
  301. return 0;
  302. }
  303. #endif /* CONFIG_WPS_STRICT */
  304. wpabuf_put_be16(buf, len);
  305. if (val)
  306. wpabuf_put_data(buf, val, len);
  307. return 0;
  308. }
  309. int p2p_build_wps_ie(struct p2p_data *p2p, struct wpabuf *buf, int pw_id,
  310. int all_attr)
  311. {
  312. u8 *len;
  313. int i;
  314. if (wpabuf_tailroom(buf) < 6)
  315. return -1;
  316. wpabuf_put_u8(buf, WLAN_EID_VENDOR_SPECIFIC);
  317. len = wpabuf_put(buf, 1);
  318. wpabuf_put_be32(buf, WPS_DEV_OUI_WFA);
  319. if (wps_build_version(buf) < 0)
  320. return -1;
  321. if (all_attr) {
  322. if (wpabuf_tailroom(buf) < 5)
  323. return -1;
  324. wpabuf_put_be16(buf, ATTR_WPS_STATE);
  325. wpabuf_put_be16(buf, 1);
  326. wpabuf_put_u8(buf, WPS_STATE_NOT_CONFIGURED);
  327. }
  328. if (pw_id >= 0) {
  329. if (wpabuf_tailroom(buf) < 6)
  330. return -1;
  331. /* Device Password ID */
  332. wpabuf_put_be16(buf, ATTR_DEV_PASSWORD_ID);
  333. wpabuf_put_be16(buf, 2);
  334. wpa_printf(MSG_DEBUG, "P2P: WPS IE Device Password ID: %d",
  335. pw_id);
  336. wpabuf_put_be16(buf, pw_id);
  337. }
  338. if (all_attr) {
  339. if (wpabuf_tailroom(buf) < 5)
  340. return -1;
  341. wpabuf_put_be16(buf, ATTR_RESPONSE_TYPE);
  342. wpabuf_put_be16(buf, 1);
  343. wpabuf_put_u8(buf, WPS_RESP_ENROLLEE_INFO);
  344. if (wps_build_uuid_e(buf, p2p->cfg->uuid) < 0 ||
  345. p2p_add_wps_string(buf, ATTR_MANUFACTURER,
  346. p2p->cfg->manufacturer) < 0 ||
  347. p2p_add_wps_string(buf, ATTR_MODEL_NAME,
  348. p2p->cfg->model_name) < 0 ||
  349. p2p_add_wps_string(buf, ATTR_MODEL_NUMBER,
  350. p2p->cfg->model_number) < 0 ||
  351. p2p_add_wps_string(buf, ATTR_SERIAL_NUMBER,
  352. p2p->cfg->serial_number) < 0)
  353. return -1;
  354. if (wpabuf_tailroom(buf) < 4 + WPS_DEV_TYPE_LEN)
  355. return -1;
  356. wpabuf_put_be16(buf, ATTR_PRIMARY_DEV_TYPE);
  357. wpabuf_put_be16(buf, WPS_DEV_TYPE_LEN);
  358. wpabuf_put_data(buf, p2p->cfg->pri_dev_type, WPS_DEV_TYPE_LEN);
  359. if (p2p_add_wps_string(buf, ATTR_DEV_NAME, p2p->cfg->dev_name)
  360. < 0)
  361. return -1;
  362. if (wpabuf_tailroom(buf) < 6)
  363. return -1;
  364. wpabuf_put_be16(buf, ATTR_CONFIG_METHODS);
  365. wpabuf_put_be16(buf, 2);
  366. wpabuf_put_be16(buf, p2p->cfg->config_methods);
  367. }
  368. if (wps_build_wfa_ext(buf, 0, NULL, 0) < 0)
  369. return -1;
  370. if (all_attr && p2p->cfg->num_sec_dev_types) {
  371. if (wpabuf_tailroom(buf) <
  372. 4 + WPS_DEV_TYPE_LEN * p2p->cfg->num_sec_dev_types)
  373. return -1;
  374. wpabuf_put_be16(buf, ATTR_SECONDARY_DEV_TYPE_LIST);
  375. wpabuf_put_be16(buf, WPS_DEV_TYPE_LEN *
  376. p2p->cfg->num_sec_dev_types);
  377. wpabuf_put_data(buf, p2p->cfg->sec_dev_type,
  378. WPS_DEV_TYPE_LEN *
  379. p2p->cfg->num_sec_dev_types);
  380. }
  381. /* Add the WPS vendor extensions */
  382. for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
  383. if (p2p->wps_vendor_ext[i] == NULL)
  384. break;
  385. if (wpabuf_tailroom(buf) <
  386. 4 + wpabuf_len(p2p->wps_vendor_ext[i]))
  387. continue;
  388. wpabuf_put_be16(buf, ATTR_VENDOR_EXT);
  389. wpabuf_put_be16(buf, wpabuf_len(p2p->wps_vendor_ext[i]));
  390. wpabuf_put_buf(buf, p2p->wps_vendor_ext[i]);
  391. }
  392. p2p_buf_update_ie_hdr(buf, len);
  393. return 0;
  394. }