p2p_go_neg.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  1. /*
  2. * Wi-Fi Direct - P2P Group Owner Negotiation
  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 "utils/eloop.h"
  11. #include "common/ieee802_11_defs.h"
  12. #include "common/wpa_ctrl.h"
  13. #include "wps/wps_defs.h"
  14. #include "p2p_i.h"
  15. #include "p2p.h"
  16. static int p2p_go_det(u8 own_intent, u8 peer_value)
  17. {
  18. u8 peer_intent = peer_value >> 1;
  19. if (own_intent == peer_intent) {
  20. if (own_intent == P2P_MAX_GO_INTENT)
  21. return -1; /* both devices want to become GO */
  22. /* Use tie breaker bit to determine GO */
  23. return (peer_value & 0x01) ? 0 : 1;
  24. }
  25. return own_intent > peer_intent;
  26. }
  27. int p2p_peer_channels_check(struct p2p_data *p2p, struct p2p_channels *own,
  28. struct p2p_device *dev,
  29. const u8 *channel_list, size_t channel_list_len)
  30. {
  31. const u8 *pos, *end;
  32. struct p2p_channels *ch;
  33. size_t channels;
  34. struct p2p_channels intersection;
  35. ch = &dev->channels;
  36. os_memset(ch, 0, sizeof(*ch));
  37. pos = channel_list;
  38. end = channel_list + channel_list_len;
  39. if (end - pos < 3)
  40. return -1;
  41. os_memcpy(dev->country, pos, 3);
  42. wpa_hexdump_ascii(MSG_DEBUG, "P2P: Peer country", pos, 3);
  43. if (pos[2] != 0x04 && os_memcmp(pos, p2p->cfg->country, 2) != 0) {
  44. p2p_info(p2p, "Mismatching country (ours=%c%c peer's=%c%c)",
  45. p2p->cfg->country[0], p2p->cfg->country[1],
  46. pos[0], pos[1]);
  47. return -1;
  48. }
  49. pos += 3;
  50. while (pos + 2 < end) {
  51. struct p2p_reg_class *cl = &ch->reg_class[ch->reg_classes];
  52. cl->reg_class = *pos++;
  53. if (pos + 1 + pos[0] > end) {
  54. p2p_info(p2p, "Invalid peer Channel List");
  55. return -1;
  56. }
  57. channels = *pos++;
  58. cl->channels = channels > P2P_MAX_REG_CLASS_CHANNELS ?
  59. P2P_MAX_REG_CLASS_CHANNELS : channels;
  60. os_memcpy(cl->channel, pos, cl->channels);
  61. pos += channels;
  62. ch->reg_classes++;
  63. if (ch->reg_classes == P2P_MAX_REG_CLASSES)
  64. break;
  65. }
  66. p2p_channels_intersect(own, &dev->channels, &intersection);
  67. p2p_dbg(p2p, "Own reg_classes %d peer reg_classes %d intersection reg_classes %d",
  68. (int) own->reg_classes,
  69. (int) dev->channels.reg_classes,
  70. (int) intersection.reg_classes);
  71. if (intersection.reg_classes == 0) {
  72. p2p_info(p2p, "No common channels found");
  73. return -1;
  74. }
  75. return 0;
  76. }
  77. static int p2p_peer_channels(struct p2p_data *p2p, struct p2p_device *dev,
  78. const u8 *channel_list, size_t channel_list_len)
  79. {
  80. return p2p_peer_channels_check(p2p, &p2p->channels, dev,
  81. channel_list, channel_list_len);
  82. }
  83. u16 p2p_wps_method_pw_id(enum p2p_wps_method wps_method)
  84. {
  85. switch (wps_method) {
  86. case WPS_PIN_DISPLAY:
  87. return DEV_PW_REGISTRAR_SPECIFIED;
  88. case WPS_PIN_KEYPAD:
  89. return DEV_PW_USER_SPECIFIED;
  90. case WPS_PBC:
  91. return DEV_PW_PUSHBUTTON;
  92. case WPS_NFC:
  93. return DEV_PW_NFC_CONNECTION_HANDOVER;
  94. case WPS_P2PS:
  95. return DEV_PW_P2PS_DEFAULT;
  96. default:
  97. return DEV_PW_DEFAULT;
  98. }
  99. }
  100. static const char * p2p_wps_method_str(enum p2p_wps_method wps_method)
  101. {
  102. switch (wps_method) {
  103. case WPS_PIN_DISPLAY:
  104. return "Display";
  105. case WPS_PIN_KEYPAD:
  106. return "Keypad";
  107. case WPS_PBC:
  108. return "PBC";
  109. case WPS_NFC:
  110. return "NFC";
  111. case WPS_P2PS:
  112. return "P2PS";
  113. default:
  114. return "??";
  115. }
  116. }
  117. static struct wpabuf * p2p_build_go_neg_req(struct p2p_data *p2p,
  118. struct p2p_device *peer)
  119. {
  120. struct wpabuf *buf;
  121. u8 *len;
  122. u8 group_capab;
  123. size_t extra = 0;
  124. u16 pw_id;
  125. #ifdef CONFIG_WIFI_DISPLAY
  126. if (p2p->wfd_ie_go_neg)
  127. extra = wpabuf_len(p2p->wfd_ie_go_neg);
  128. #endif /* CONFIG_WIFI_DISPLAY */
  129. if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ])
  130. extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ]);
  131. buf = wpabuf_alloc(1000 + extra);
  132. if (buf == NULL)
  133. return NULL;
  134. p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_REQ, peer->dialog_token);
  135. len = p2p_buf_add_ie_hdr(buf);
  136. group_capab = 0;
  137. if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
  138. group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
  139. if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
  140. group_capab |= P2P_GROUP_CAPAB_PERSISTENT_RECONN;
  141. }
  142. if (p2p->cross_connect)
  143. group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
  144. if (p2p->cfg->p2p_intra_bss)
  145. group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
  146. p2p_buf_add_capability(buf, p2p->dev_capab &
  147. ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
  148. group_capab);
  149. p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | peer->tie_breaker);
  150. p2p_buf_add_config_timeout(buf, p2p->go_timeout, p2p->client_timeout);
  151. p2p_buf_add_listen_channel(buf, p2p->cfg->country, p2p->cfg->reg_class,
  152. p2p->cfg->channel);
  153. if (p2p->ext_listen_interval)
  154. p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
  155. p2p->ext_listen_interval);
  156. p2p_buf_add_intended_addr(buf, p2p->intended_addr);
  157. p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
  158. p2p_buf_add_device_info(buf, p2p, peer);
  159. p2p_buf_add_operating_channel(buf, p2p->cfg->country,
  160. p2p->op_reg_class, p2p->op_channel);
  161. p2p_buf_update_ie_hdr(buf, len);
  162. p2p_buf_add_pref_channel_list(buf, p2p->pref_freq_list,
  163. p2p->num_pref_freq);
  164. /* WPS IE with Device Password ID attribute */
  165. pw_id = p2p_wps_method_pw_id(peer->wps_method);
  166. if (peer->oob_pw_id)
  167. pw_id = peer->oob_pw_id;
  168. if (p2p_build_wps_ie(p2p, buf, pw_id, 0) < 0) {
  169. p2p_dbg(p2p, "Failed to build WPS IE for GO Negotiation Request");
  170. wpabuf_free(buf);
  171. return NULL;
  172. }
  173. #ifdef CONFIG_WIFI_DISPLAY
  174. if (p2p->wfd_ie_go_neg)
  175. wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
  176. #endif /* CONFIG_WIFI_DISPLAY */
  177. if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ])
  178. wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_REQ]);
  179. return buf;
  180. }
  181. int p2p_connect_send(struct p2p_data *p2p, struct p2p_device *dev)
  182. {
  183. struct wpabuf *req;
  184. int freq;
  185. if (dev->flags & P2P_DEV_PD_BEFORE_GO_NEG) {
  186. u16 config_method;
  187. p2p_dbg(p2p, "Use PD-before-GO-Neg workaround for " MACSTR,
  188. MAC2STR(dev->info.p2p_device_addr));
  189. if (dev->wps_method == WPS_PIN_DISPLAY)
  190. config_method = WPS_CONFIG_KEYPAD;
  191. else if (dev->wps_method == WPS_PIN_KEYPAD)
  192. config_method = WPS_CONFIG_DISPLAY;
  193. else if (dev->wps_method == WPS_PBC)
  194. config_method = WPS_CONFIG_PUSHBUTTON;
  195. else if (dev->wps_method == WPS_P2PS)
  196. config_method = WPS_CONFIG_P2PS;
  197. else
  198. return -1;
  199. return p2p_prov_disc_req(p2p, dev->info.p2p_device_addr,
  200. NULL, config_method, 0, 0, 1);
  201. }
  202. freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
  203. if (dev->oob_go_neg_freq > 0)
  204. freq = dev->oob_go_neg_freq;
  205. if (freq <= 0) {
  206. p2p_dbg(p2p, "No Listen/Operating frequency known for the peer "
  207. MACSTR " to send GO Negotiation Request",
  208. MAC2STR(dev->info.p2p_device_addr));
  209. return -1;
  210. }
  211. req = p2p_build_go_neg_req(p2p, dev);
  212. if (req == NULL)
  213. return -1;
  214. p2p_dbg(p2p, "Sending GO Negotiation Request");
  215. p2p_set_state(p2p, P2P_CONNECT);
  216. p2p->pending_action_state = P2P_PENDING_GO_NEG_REQUEST;
  217. p2p->go_neg_peer = dev;
  218. eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
  219. dev->flags |= P2P_DEV_WAIT_GO_NEG_RESPONSE;
  220. dev->connect_reqs++;
  221. if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
  222. p2p->cfg->dev_addr, dev->info.p2p_device_addr,
  223. wpabuf_head(req), wpabuf_len(req), 500) < 0) {
  224. p2p_dbg(p2p, "Failed to send Action frame");
  225. /* Use P2P find to recover and retry */
  226. p2p_set_timeout(p2p, 0, 0);
  227. } else
  228. dev->go_neg_req_sent++;
  229. wpabuf_free(req);
  230. return 0;
  231. }
  232. static struct wpabuf * p2p_build_go_neg_resp(struct p2p_data *p2p,
  233. struct p2p_device *peer,
  234. u8 dialog_token, u8 status,
  235. u8 tie_breaker)
  236. {
  237. struct wpabuf *buf;
  238. u8 *len;
  239. u8 group_capab;
  240. size_t extra = 0;
  241. u16 pw_id;
  242. p2p_dbg(p2p, "Building GO Negotiation Response");
  243. #ifdef CONFIG_WIFI_DISPLAY
  244. if (p2p->wfd_ie_go_neg)
  245. extra = wpabuf_len(p2p->wfd_ie_go_neg);
  246. #endif /* CONFIG_WIFI_DISPLAY */
  247. if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP])
  248. extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP]);
  249. buf = wpabuf_alloc(1000 + extra);
  250. if (buf == NULL)
  251. return NULL;
  252. p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_RESP, dialog_token);
  253. len = p2p_buf_add_ie_hdr(buf);
  254. p2p_buf_add_status(buf, status);
  255. group_capab = 0;
  256. if (peer && peer->go_state == LOCAL_GO) {
  257. if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
  258. group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
  259. if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
  260. group_capab |=
  261. P2P_GROUP_CAPAB_PERSISTENT_RECONN;
  262. }
  263. if (p2p->cross_connect)
  264. group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
  265. if (p2p->cfg->p2p_intra_bss)
  266. group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
  267. }
  268. p2p_buf_add_capability(buf, p2p->dev_capab &
  269. ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
  270. group_capab);
  271. p2p_buf_add_go_intent(buf, (p2p->go_intent << 1) | tie_breaker);
  272. p2p_buf_add_config_timeout(buf, p2p->go_timeout, p2p->client_timeout);
  273. if (peer && peer->go_state == REMOTE_GO) {
  274. p2p_dbg(p2p, "Omit Operating Channel attribute");
  275. } else {
  276. p2p_buf_add_operating_channel(buf, p2p->cfg->country,
  277. p2p->op_reg_class,
  278. p2p->op_channel);
  279. }
  280. p2p_buf_add_intended_addr(buf, p2p->intended_addr);
  281. if (status || peer == NULL) {
  282. p2p_buf_add_channel_list(buf, p2p->cfg->country,
  283. &p2p->channels);
  284. } else if (peer->go_state == REMOTE_GO) {
  285. p2p_buf_add_channel_list(buf, p2p->cfg->country,
  286. &p2p->channels);
  287. } else {
  288. struct p2p_channels res;
  289. p2p_channels_intersect(&p2p->channels, &peer->channels,
  290. &res);
  291. p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
  292. }
  293. p2p_buf_add_device_info(buf, p2p, peer);
  294. if (peer && peer->go_state == LOCAL_GO) {
  295. p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
  296. p2p->ssid_len);
  297. }
  298. p2p_buf_update_ie_hdr(buf, len);
  299. /* WPS IE with Device Password ID attribute */
  300. pw_id = p2p_wps_method_pw_id(peer ? peer->wps_method : WPS_NOT_READY);
  301. if (peer && peer->oob_pw_id)
  302. pw_id = peer->oob_pw_id;
  303. if (p2p_build_wps_ie(p2p, buf, pw_id, 0) < 0) {
  304. p2p_dbg(p2p, "Failed to build WPS IE for GO Negotiation Response");
  305. wpabuf_free(buf);
  306. return NULL;
  307. }
  308. #ifdef CONFIG_WIFI_DISPLAY
  309. if (p2p->wfd_ie_go_neg)
  310. wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
  311. #endif /* CONFIG_WIFI_DISPLAY */
  312. if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP])
  313. wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_RESP]);
  314. return buf;
  315. }
  316. /**
  317. * p2p_reselect_channel - Re-select operating channel based on peer information
  318. * @p2p: P2P module context from p2p_init()
  319. * @intersection: Support channel list intersection from local and peer
  320. *
  321. * This function is used to re-select the best channel after having received
  322. * information from the peer to allow supported channel lists to be intersected.
  323. * This can be used to improve initial channel selection done in
  324. * p2p_prepare_channel() prior to the start of GO Negotiation. In addition, this
  325. * can be used for Invitation case.
  326. */
  327. void p2p_reselect_channel(struct p2p_data *p2p,
  328. struct p2p_channels *intersection)
  329. {
  330. struct p2p_reg_class *cl;
  331. int freq;
  332. u8 op_reg_class, op_channel;
  333. unsigned int i;
  334. const int op_classes_5ghz[] = { 124, 125, 115, 0 };
  335. const int op_classes_ht40[] = { 126, 127, 116, 117, 0 };
  336. const int op_classes_vht[] = { 128, 0 };
  337. if (p2p->own_freq_preference > 0 &&
  338. p2p_freq_to_channel(p2p->own_freq_preference,
  339. &op_reg_class, &op_channel) == 0 &&
  340. p2p_channels_includes(intersection, op_reg_class, op_channel)) {
  341. p2p_dbg(p2p, "Pick own channel preference (reg_class %u channel %u) from intersection",
  342. op_reg_class, op_channel);
  343. p2p->op_reg_class = op_reg_class;
  344. p2p->op_channel = op_channel;
  345. return;
  346. }
  347. if (p2p->best_freq_overall > 0 &&
  348. p2p_freq_to_channel(p2p->best_freq_overall,
  349. &op_reg_class, &op_channel) == 0 &&
  350. p2p_channels_includes(intersection, op_reg_class, op_channel)) {
  351. p2p_dbg(p2p, "Pick best overall channel (reg_class %u channel %u) from intersection",
  352. op_reg_class, op_channel);
  353. p2p->op_reg_class = op_reg_class;
  354. p2p->op_channel = op_channel;
  355. return;
  356. }
  357. /* First, try to pick the best channel from another band */
  358. freq = p2p_channel_to_freq(p2p->op_reg_class, p2p->op_channel);
  359. if (freq >= 2400 && freq < 2500 && p2p->best_freq_5 > 0 &&
  360. !p2p_channels_includes(intersection, p2p->op_reg_class,
  361. p2p->op_channel) &&
  362. p2p_freq_to_channel(p2p->best_freq_5,
  363. &op_reg_class, &op_channel) == 0 &&
  364. p2p_channels_includes(intersection, op_reg_class, op_channel)) {
  365. p2p_dbg(p2p, "Pick best 5 GHz channel (reg_class %u channel %u) from intersection",
  366. op_reg_class, op_channel);
  367. p2p->op_reg_class = op_reg_class;
  368. p2p->op_channel = op_channel;
  369. return;
  370. }
  371. if (freq >= 4900 && freq < 6000 && p2p->best_freq_24 > 0 &&
  372. !p2p_channels_includes(intersection, p2p->op_reg_class,
  373. p2p->op_channel) &&
  374. p2p_freq_to_channel(p2p->best_freq_24,
  375. &op_reg_class, &op_channel) == 0 &&
  376. p2p_channels_includes(intersection, op_reg_class, op_channel)) {
  377. p2p_dbg(p2p, "Pick best 2.4 GHz channel (reg_class %u channel %u) from intersection",
  378. op_reg_class, op_channel);
  379. p2p->op_reg_class = op_reg_class;
  380. p2p->op_channel = op_channel;
  381. return;
  382. }
  383. /* Select channel with highest preference if the peer supports it */
  384. for (i = 0; p2p->cfg->pref_chan && i < p2p->cfg->num_pref_chan; i++) {
  385. if (p2p_channels_includes(intersection,
  386. p2p->cfg->pref_chan[i].op_class,
  387. p2p->cfg->pref_chan[i].chan)) {
  388. p2p->op_reg_class = p2p->cfg->pref_chan[i].op_class;
  389. p2p->op_channel = p2p->cfg->pref_chan[i].chan;
  390. p2p_dbg(p2p, "Pick highest preferred channel (op_class %u channel %u) from intersection",
  391. p2p->op_reg_class, p2p->op_channel);
  392. return;
  393. }
  394. }
  395. /* Try a channel where we might be able to use VHT */
  396. if (p2p_channel_select(intersection, op_classes_vht,
  397. &p2p->op_reg_class, &p2p->op_channel) == 0) {
  398. p2p_dbg(p2p, "Pick possible VHT channel (op_class %u channel %u) from intersection",
  399. p2p->op_reg_class, p2p->op_channel);
  400. return;
  401. }
  402. /* Try a channel where we might be able to use HT40 */
  403. if (p2p_channel_select(intersection, op_classes_ht40,
  404. &p2p->op_reg_class, &p2p->op_channel) == 0) {
  405. p2p_dbg(p2p, "Pick possible HT40 channel (op_class %u channel %u) from intersection",
  406. p2p->op_reg_class, p2p->op_channel);
  407. return;
  408. }
  409. /* Prefer a 5 GHz channel */
  410. if (p2p_channel_select(intersection, op_classes_5ghz,
  411. &p2p->op_reg_class, &p2p->op_channel) == 0) {
  412. p2p_dbg(p2p, "Pick possible 5 GHz channel (op_class %u channel %u) from intersection",
  413. p2p->op_reg_class, p2p->op_channel);
  414. return;
  415. }
  416. /*
  417. * Try to see if the original channel is in the intersection. If
  418. * so, no need to change anything, as it already contains some
  419. * randomness.
  420. */
  421. if (p2p_channels_includes(intersection, p2p->op_reg_class,
  422. p2p->op_channel)) {
  423. p2p_dbg(p2p, "Using original operating class and channel (op_class %u channel %u) from intersection",
  424. p2p->op_reg_class, p2p->op_channel);
  425. return;
  426. }
  427. /*
  428. * Fall back to whatever is included in the channel intersection since
  429. * no better options seems to be available.
  430. */
  431. cl = &intersection->reg_class[0];
  432. p2p_dbg(p2p, "Pick another channel (reg_class %u channel %u) from intersection",
  433. cl->reg_class, cl->channel[0]);
  434. p2p->op_reg_class = cl->reg_class;
  435. p2p->op_channel = cl->channel[0];
  436. }
  437. int p2p_go_select_channel(struct p2p_data *p2p, struct p2p_device *dev,
  438. u8 *status)
  439. {
  440. struct p2p_channels tmp, intersection;
  441. p2p_channels_dump(p2p, "own channels", &p2p->channels);
  442. p2p_channels_dump(p2p, "peer channels", &dev->channels);
  443. p2p_channels_intersect(&p2p->channels, &dev->channels, &tmp);
  444. p2p_channels_dump(p2p, "intersection", &tmp);
  445. p2p_channels_remove_freqs(&tmp, &p2p->no_go_freq);
  446. p2p_channels_dump(p2p, "intersection after no-GO removal", &tmp);
  447. p2p_channels_intersect(&tmp, &p2p->cfg->channels, &intersection);
  448. p2p_channels_dump(p2p, "intersection with local channel list",
  449. &intersection);
  450. if (intersection.reg_classes == 0 ||
  451. intersection.reg_class[0].channels == 0) {
  452. *status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
  453. p2p_dbg(p2p, "No common channels found");
  454. return -1;
  455. }
  456. if (!p2p_channels_includes(&intersection, p2p->op_reg_class,
  457. p2p->op_channel)) {
  458. if (dev->flags & P2P_DEV_FORCE_FREQ) {
  459. *status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
  460. p2p_dbg(p2p, "Peer does not support the forced channel");
  461. return -1;
  462. }
  463. p2p_dbg(p2p, "Selected operating channel (op_class %u channel %u) not acceptable to the peer",
  464. p2p->op_reg_class, p2p->op_channel);
  465. p2p_reselect_channel(p2p, &intersection);
  466. } else if (!(dev->flags & P2P_DEV_FORCE_FREQ) &&
  467. !p2p->cfg->cfg_op_channel) {
  468. p2p_dbg(p2p, "Try to optimize channel selection with peer information received; previously selected op_class %u channel %u",
  469. p2p->op_reg_class, p2p->op_channel);
  470. p2p_reselect_channel(p2p, &intersection);
  471. }
  472. if (!p2p->ssid_set) {
  473. p2p_build_ssid(p2p, p2p->ssid, &p2p->ssid_len);
  474. p2p->ssid_set = 1;
  475. }
  476. return 0;
  477. }
  478. void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa,
  479. const u8 *data, size_t len, int rx_freq)
  480. {
  481. struct p2p_device *dev = NULL;
  482. struct wpabuf *resp;
  483. struct p2p_message msg;
  484. u8 status = P2P_SC_FAIL_INVALID_PARAMS;
  485. int tie_breaker = 0;
  486. int freq;
  487. p2p_dbg(p2p, "Received GO Negotiation Request from " MACSTR "(freq=%d)",
  488. MAC2STR(sa), rx_freq);
  489. if (p2p_parse(data, len, &msg))
  490. return;
  491. if (!msg.capability) {
  492. p2p_dbg(p2p, "Mandatory Capability attribute missing from GO Negotiation Request");
  493. #ifdef CONFIG_P2P_STRICT
  494. goto fail;
  495. #endif /* CONFIG_P2P_STRICT */
  496. }
  497. if (msg.go_intent)
  498. tie_breaker = *msg.go_intent & 0x01;
  499. else {
  500. p2p_dbg(p2p, "Mandatory GO Intent attribute missing from GO Negotiation Request");
  501. #ifdef CONFIG_P2P_STRICT
  502. goto fail;
  503. #endif /* CONFIG_P2P_STRICT */
  504. }
  505. if (!msg.config_timeout) {
  506. p2p_dbg(p2p, "Mandatory Configuration Timeout attribute missing from GO Negotiation Request");
  507. #ifdef CONFIG_P2P_STRICT
  508. goto fail;
  509. #endif /* CONFIG_P2P_STRICT */
  510. }
  511. if (!msg.listen_channel) {
  512. p2p_dbg(p2p, "No Listen Channel attribute received");
  513. goto fail;
  514. }
  515. if (!msg.operating_channel) {
  516. p2p_dbg(p2p, "No Operating Channel attribute received");
  517. goto fail;
  518. }
  519. if (!msg.channel_list) {
  520. p2p_dbg(p2p, "No Channel List attribute received");
  521. goto fail;
  522. }
  523. if (!msg.intended_addr) {
  524. p2p_dbg(p2p, "No Intended P2P Interface Address attribute received");
  525. goto fail;
  526. }
  527. if (!msg.p2p_device_info) {
  528. p2p_dbg(p2p, "No P2P Device Info attribute received");
  529. goto fail;
  530. }
  531. if (os_memcmp(msg.p2p_device_addr, sa, ETH_ALEN) != 0) {
  532. p2p_dbg(p2p, "Unexpected GO Negotiation Request SA=" MACSTR
  533. " != dev_addr=" MACSTR,
  534. MAC2STR(sa), MAC2STR(msg.p2p_device_addr));
  535. goto fail;
  536. }
  537. dev = p2p_get_device(p2p, sa);
  538. if (msg.status && *msg.status) {
  539. p2p_dbg(p2p, "Unexpected Status attribute (%d) in GO Negotiation Request",
  540. *msg.status);
  541. if (dev && p2p->go_neg_peer == dev &&
  542. *msg.status == P2P_SC_FAIL_REJECTED_BY_USER) {
  543. /*
  544. * This mechanism for using Status attribute in GO
  545. * Negotiation Request is not compliant with the P2P
  546. * specification, but some deployed devices use it to
  547. * indicate rejection of GO Negotiation in a case where
  548. * they have sent out GO Negotiation Response with
  549. * status 1. The P2P specification explicitly disallows
  550. * this. To avoid unnecessary interoperability issues
  551. * and extra frames, mark the pending negotiation as
  552. * failed and do not reply to this GO Negotiation
  553. * Request frame.
  554. */
  555. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  556. p2p_go_neg_failed(p2p, *msg.status);
  557. p2p_parse_free(&msg);
  558. return;
  559. }
  560. goto fail;
  561. }
  562. if (dev == NULL)
  563. dev = p2p_add_dev_from_go_neg_req(p2p, sa, &msg);
  564. else if ((dev->flags & P2P_DEV_PROBE_REQ_ONLY) ||
  565. !(dev->flags & P2P_DEV_REPORTED))
  566. p2p_add_dev_info(p2p, sa, dev, &msg);
  567. else if (!dev->listen_freq && !dev->oper_freq) {
  568. /*
  569. * This may happen if the peer entry was added based on PD
  570. * Request and no Probe Request/Response frame has been received
  571. * from this peer (or that information has timed out).
  572. */
  573. p2p_dbg(p2p, "Update peer " MACSTR
  574. " based on GO Neg Req since listen/oper freq not known",
  575. MAC2STR(dev->info.p2p_device_addr));
  576. p2p_add_dev_info(p2p, sa, dev, &msg);
  577. }
  578. if (p2p->go_neg_peer && p2p->go_neg_peer == dev)
  579. eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
  580. if (dev && dev->flags & P2P_DEV_USER_REJECTED) {
  581. p2p_dbg(p2p, "User has rejected this peer");
  582. status = P2P_SC_FAIL_REJECTED_BY_USER;
  583. } else if (dev == NULL ||
  584. (dev->wps_method == WPS_NOT_READY &&
  585. (p2p->authorized_oob_dev_pw_id == 0 ||
  586. p2p->authorized_oob_dev_pw_id !=
  587. msg.dev_password_id))) {
  588. p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR,
  589. MAC2STR(sa));
  590. status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  591. p2p->cfg->go_neg_req_rx(p2p->cfg->cb_ctx, sa,
  592. msg.dev_password_id,
  593. msg.go_intent ? (*msg.go_intent >> 1) :
  594. 0);
  595. } else if (p2p->go_neg_peer && p2p->go_neg_peer != dev) {
  596. p2p_dbg(p2p, "Already in Group Formation with another peer");
  597. status = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  598. } else {
  599. int go;
  600. if (!p2p->go_neg_peer) {
  601. p2p_dbg(p2p, "Starting GO Negotiation with previously authorized peer");
  602. if (!(dev->flags & P2P_DEV_FORCE_FREQ)) {
  603. p2p_dbg(p2p, "Use default channel settings");
  604. p2p->op_reg_class = p2p->cfg->op_reg_class;
  605. p2p->op_channel = p2p->cfg->op_channel;
  606. os_memcpy(&p2p->channels, &p2p->cfg->channels,
  607. sizeof(struct p2p_channels));
  608. } else {
  609. p2p_dbg(p2p, "Use previously configured forced channel settings");
  610. }
  611. }
  612. dev->flags &= ~P2P_DEV_NOT_YET_READY;
  613. if (!msg.go_intent) {
  614. p2p_dbg(p2p, "No GO Intent attribute received");
  615. goto fail;
  616. }
  617. if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
  618. p2p_dbg(p2p, "Invalid GO Intent value (%u) received",
  619. *msg.go_intent >> 1);
  620. goto fail;
  621. }
  622. if (dev->go_neg_req_sent &&
  623. os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) > 0) {
  624. p2p_dbg(p2p, "Do not reply since peer has higher address and GO Neg Request already sent");
  625. p2p_parse_free(&msg);
  626. return;
  627. }
  628. go = p2p_go_det(p2p->go_intent, *msg.go_intent);
  629. if (go < 0) {
  630. p2p_dbg(p2p, "Incompatible GO Intent");
  631. status = P2P_SC_FAIL_BOTH_GO_INTENT_15;
  632. goto fail;
  633. }
  634. if (p2p_peer_channels(p2p, dev, msg.channel_list,
  635. msg.channel_list_len) < 0) {
  636. p2p_dbg(p2p, "No common channels found");
  637. status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
  638. goto fail;
  639. }
  640. switch (msg.dev_password_id) {
  641. case DEV_PW_REGISTRAR_SPECIFIED:
  642. p2p_dbg(p2p, "PIN from peer Display");
  643. if (dev->wps_method != WPS_PIN_KEYPAD) {
  644. p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
  645. p2p_wps_method_str(dev->wps_method));
  646. status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
  647. goto fail;
  648. }
  649. break;
  650. case DEV_PW_USER_SPECIFIED:
  651. p2p_dbg(p2p, "Peer entered PIN on Keypad");
  652. if (dev->wps_method != WPS_PIN_DISPLAY) {
  653. p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
  654. p2p_wps_method_str(dev->wps_method));
  655. status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
  656. goto fail;
  657. }
  658. break;
  659. case DEV_PW_PUSHBUTTON:
  660. p2p_dbg(p2p, "Peer using pushbutton");
  661. if (dev->wps_method != WPS_PBC) {
  662. p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
  663. p2p_wps_method_str(dev->wps_method));
  664. status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
  665. goto fail;
  666. }
  667. break;
  668. case DEV_PW_P2PS_DEFAULT:
  669. p2p_dbg(p2p, "Peer using P2PS pin");
  670. if (dev->wps_method != WPS_P2PS) {
  671. p2p_dbg(p2p,
  672. "We have wps_method=%s -> incompatible",
  673. p2p_wps_method_str(dev->wps_method));
  674. status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
  675. goto fail;
  676. }
  677. break;
  678. default:
  679. if (msg.dev_password_id &&
  680. msg.dev_password_id == dev->oob_pw_id) {
  681. p2p_dbg(p2p, "Peer using NFC");
  682. if (dev->wps_method != WPS_NFC) {
  683. p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
  684. p2p_wps_method_str(
  685. dev->wps_method));
  686. status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
  687. goto fail;
  688. }
  689. break;
  690. }
  691. #ifdef CONFIG_WPS_NFC
  692. if (p2p->authorized_oob_dev_pw_id &&
  693. msg.dev_password_id ==
  694. p2p->authorized_oob_dev_pw_id) {
  695. p2p_dbg(p2p, "Using static handover with our device password from NFC Tag");
  696. dev->wps_method = WPS_NFC;
  697. dev->oob_pw_id = p2p->authorized_oob_dev_pw_id;
  698. break;
  699. }
  700. #endif /* CONFIG_WPS_NFC */
  701. p2p_dbg(p2p, "Unsupported Device Password ID %d",
  702. msg.dev_password_id);
  703. status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
  704. goto fail;
  705. }
  706. if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
  707. goto fail;
  708. dev->go_state = go ? LOCAL_GO : REMOTE_GO;
  709. dev->oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
  710. msg.operating_channel[4]);
  711. p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
  712. dev->oper_freq);
  713. if (msg.config_timeout) {
  714. dev->go_timeout = msg.config_timeout[0];
  715. dev->client_timeout = msg.config_timeout[1];
  716. }
  717. p2p_dbg(p2p, "GO Negotiation with " MACSTR, MAC2STR(sa));
  718. if (p2p->state != P2P_IDLE)
  719. p2p_stop_find_for_freq(p2p, rx_freq);
  720. p2p_set_state(p2p, P2P_GO_NEG);
  721. p2p_clear_timeout(p2p);
  722. dev->dialog_token = msg.dialog_token;
  723. os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
  724. p2p->go_neg_peer = dev;
  725. eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
  726. status = P2P_SC_SUCCESS;
  727. }
  728. fail:
  729. if (dev)
  730. dev->status = status;
  731. resp = p2p_build_go_neg_resp(p2p, dev, msg.dialog_token, status,
  732. !tie_breaker);
  733. p2p_parse_free(&msg);
  734. if (resp == NULL)
  735. return;
  736. p2p_dbg(p2p, "Sending GO Negotiation Response");
  737. if (rx_freq > 0)
  738. freq = rx_freq;
  739. else
  740. freq = p2p_channel_to_freq(p2p->cfg->reg_class,
  741. p2p->cfg->channel);
  742. if (freq < 0) {
  743. p2p_dbg(p2p, "Unknown regulatory class/channel");
  744. wpabuf_free(resp);
  745. return;
  746. }
  747. if (status == P2P_SC_SUCCESS) {
  748. p2p->pending_action_state = P2P_PENDING_GO_NEG_RESPONSE;
  749. dev->flags |= P2P_DEV_WAIT_GO_NEG_CONFIRM;
  750. if (os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) < 0) {
  751. /*
  752. * Peer has smaller address, so the GO Negotiation
  753. * Response from us is expected to complete
  754. * negotiation. Ignore a GO Negotiation Response from
  755. * the peer if it happens to be received after this
  756. * point due to a race condition in GO Negotiation
  757. * Request transmission and processing.
  758. */
  759. dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
  760. }
  761. } else
  762. p2p->pending_action_state =
  763. P2P_PENDING_GO_NEG_RESPONSE_FAILURE;
  764. if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
  765. p2p->cfg->dev_addr,
  766. wpabuf_head(resp), wpabuf_len(resp), 500) < 0) {
  767. p2p_dbg(p2p, "Failed to send Action frame");
  768. }
  769. wpabuf_free(resp);
  770. }
  771. static struct wpabuf * p2p_build_go_neg_conf(struct p2p_data *p2p,
  772. struct p2p_device *peer,
  773. u8 dialog_token, u8 status,
  774. const u8 *resp_chan, int go)
  775. {
  776. struct wpabuf *buf;
  777. u8 *len;
  778. struct p2p_channels res;
  779. u8 group_capab;
  780. size_t extra = 0;
  781. p2p_dbg(p2p, "Building GO Negotiation Confirm");
  782. #ifdef CONFIG_WIFI_DISPLAY
  783. if (p2p->wfd_ie_go_neg)
  784. extra = wpabuf_len(p2p->wfd_ie_go_neg);
  785. #endif /* CONFIG_WIFI_DISPLAY */
  786. if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF])
  787. extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF]);
  788. buf = wpabuf_alloc(1000 + extra);
  789. if (buf == NULL)
  790. return NULL;
  791. p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_CONF, dialog_token);
  792. len = p2p_buf_add_ie_hdr(buf);
  793. p2p_buf_add_status(buf, status);
  794. group_capab = 0;
  795. if (peer->go_state == LOCAL_GO) {
  796. if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
  797. group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
  798. if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
  799. group_capab |=
  800. P2P_GROUP_CAPAB_PERSISTENT_RECONN;
  801. }
  802. if (p2p->cross_connect)
  803. group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
  804. if (p2p->cfg->p2p_intra_bss)
  805. group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
  806. }
  807. p2p_buf_add_capability(buf, p2p->dev_capab &
  808. ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
  809. group_capab);
  810. if (go || resp_chan == NULL)
  811. p2p_buf_add_operating_channel(buf, p2p->cfg->country,
  812. p2p->op_reg_class,
  813. p2p->op_channel);
  814. else
  815. p2p_buf_add_operating_channel(buf, (const char *) resp_chan,
  816. resp_chan[3], resp_chan[4]);
  817. p2p_channels_intersect(&p2p->channels, &peer->channels, &res);
  818. p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
  819. if (go) {
  820. p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
  821. p2p->ssid_len);
  822. }
  823. p2p_buf_update_ie_hdr(buf, len);
  824. #ifdef CONFIG_WIFI_DISPLAY
  825. if (p2p->wfd_ie_go_neg)
  826. wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
  827. #endif /* CONFIG_WIFI_DISPLAY */
  828. if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF])
  829. wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF]);
  830. return buf;
  831. }
  832. void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa,
  833. const u8 *data, size_t len, int rx_freq)
  834. {
  835. struct p2p_device *dev;
  836. int go = -1;
  837. struct p2p_message msg;
  838. u8 status = P2P_SC_SUCCESS;
  839. int freq;
  840. p2p_dbg(p2p, "Received GO Negotiation Response from " MACSTR
  841. " (freq=%d)", MAC2STR(sa), rx_freq);
  842. dev = p2p_get_device(p2p, sa);
  843. if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
  844. dev != p2p->go_neg_peer) {
  845. p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR,
  846. MAC2STR(sa));
  847. return;
  848. }
  849. if (p2p_parse(data, len, &msg))
  850. return;
  851. if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE)) {
  852. p2p_dbg(p2p, "Was not expecting GO Negotiation Response - ignore");
  853. p2p_parse_free(&msg);
  854. return;
  855. }
  856. dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
  857. if (msg.dialog_token != dev->dialog_token) {
  858. p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)",
  859. msg.dialog_token, dev->dialog_token);
  860. p2p_parse_free(&msg);
  861. return;
  862. }
  863. if (!msg.status) {
  864. p2p_dbg(p2p, "No Status attribute received");
  865. status = P2P_SC_FAIL_INVALID_PARAMS;
  866. goto fail;
  867. }
  868. if (*msg.status) {
  869. p2p_dbg(p2p, "GO Negotiation rejected: status %d", *msg.status);
  870. dev->go_neg_req_sent = 0;
  871. if (*msg.status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  872. p2p_dbg(p2p, "Wait for the peer to become ready for GO Negotiation");
  873. dev->flags |= P2P_DEV_NOT_YET_READY;
  874. eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p,
  875. NULL);
  876. eloop_register_timeout(120, 0, p2p_go_neg_wait_timeout,
  877. p2p, NULL);
  878. if (p2p->state == P2P_CONNECT_LISTEN)
  879. p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
  880. else
  881. p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
  882. p2p_set_timeout(p2p, 0, 0);
  883. } else {
  884. p2p_dbg(p2p, "Stop GO Negotiation attempt");
  885. p2p_go_neg_failed(p2p, *msg.status);
  886. }
  887. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  888. p2p_parse_free(&msg);
  889. return;
  890. }
  891. if (!msg.capability) {
  892. p2p_dbg(p2p, "Mandatory Capability attribute missing from GO Negotiation Response");
  893. #ifdef CONFIG_P2P_STRICT
  894. status = P2P_SC_FAIL_INVALID_PARAMS;
  895. goto fail;
  896. #endif /* CONFIG_P2P_STRICT */
  897. }
  898. if (!msg.p2p_device_info) {
  899. p2p_dbg(p2p, "Mandatory P2P Device Info attribute missing from GO Negotiation Response");
  900. #ifdef CONFIG_P2P_STRICT
  901. status = P2P_SC_FAIL_INVALID_PARAMS;
  902. goto fail;
  903. #endif /* CONFIG_P2P_STRICT */
  904. }
  905. if (!msg.intended_addr) {
  906. p2p_dbg(p2p, "No Intended P2P Interface Address attribute received");
  907. status = P2P_SC_FAIL_INVALID_PARAMS;
  908. goto fail;
  909. }
  910. if (!msg.go_intent) {
  911. p2p_dbg(p2p, "No GO Intent attribute received");
  912. status = P2P_SC_FAIL_INVALID_PARAMS;
  913. goto fail;
  914. }
  915. if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
  916. p2p_dbg(p2p, "Invalid GO Intent value (%u) received",
  917. *msg.go_intent >> 1);
  918. status = P2P_SC_FAIL_INVALID_PARAMS;
  919. goto fail;
  920. }
  921. go = p2p_go_det(p2p->go_intent, *msg.go_intent);
  922. if (go < 0) {
  923. p2p_dbg(p2p, "Incompatible GO Intent");
  924. status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
  925. goto fail;
  926. }
  927. if (!go && msg.group_id) {
  928. /* Store SSID for Provisioning step */
  929. p2p->ssid_len = msg.group_id_len - ETH_ALEN;
  930. os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
  931. } else if (!go) {
  932. p2p_dbg(p2p, "Mandatory P2P Group ID attribute missing from GO Negotiation Response");
  933. p2p->ssid_len = 0;
  934. status = P2P_SC_FAIL_INVALID_PARAMS;
  935. goto fail;
  936. }
  937. if (!msg.config_timeout) {
  938. p2p_dbg(p2p, "Mandatory Configuration Timeout attribute missing from GO Negotiation Response");
  939. #ifdef CONFIG_P2P_STRICT
  940. status = P2P_SC_FAIL_INVALID_PARAMS;
  941. goto fail;
  942. #endif /* CONFIG_P2P_STRICT */
  943. } else {
  944. dev->go_timeout = msg.config_timeout[0];
  945. dev->client_timeout = msg.config_timeout[1];
  946. }
  947. if (!msg.operating_channel && !go) {
  948. /*
  949. * Note: P2P Client may omit Operating Channel attribute to
  950. * indicate it does not have a preference.
  951. */
  952. p2p_dbg(p2p, "No Operating Channel attribute received");
  953. status = P2P_SC_FAIL_INVALID_PARAMS;
  954. goto fail;
  955. }
  956. if (!msg.channel_list) {
  957. p2p_dbg(p2p, "No Channel List attribute received");
  958. status = P2P_SC_FAIL_INVALID_PARAMS;
  959. goto fail;
  960. }
  961. if (p2p_peer_channels(p2p, dev, msg.channel_list,
  962. msg.channel_list_len) < 0) {
  963. p2p_dbg(p2p, "No common channels found");
  964. status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
  965. goto fail;
  966. }
  967. if (msg.operating_channel) {
  968. dev->oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
  969. msg.operating_channel[4]);
  970. p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
  971. dev->oper_freq);
  972. } else
  973. dev->oper_freq = 0;
  974. switch (msg.dev_password_id) {
  975. case DEV_PW_REGISTRAR_SPECIFIED:
  976. p2p_dbg(p2p, "PIN from peer Display");
  977. if (dev->wps_method != WPS_PIN_KEYPAD) {
  978. p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
  979. p2p_wps_method_str(dev->wps_method));
  980. status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
  981. goto fail;
  982. }
  983. break;
  984. case DEV_PW_USER_SPECIFIED:
  985. p2p_dbg(p2p, "Peer entered PIN on Keypad");
  986. if (dev->wps_method != WPS_PIN_DISPLAY) {
  987. p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
  988. p2p_wps_method_str(dev->wps_method));
  989. status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
  990. goto fail;
  991. }
  992. break;
  993. case DEV_PW_PUSHBUTTON:
  994. p2p_dbg(p2p, "Peer using pushbutton");
  995. if (dev->wps_method != WPS_PBC) {
  996. p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
  997. p2p_wps_method_str(dev->wps_method));
  998. status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
  999. goto fail;
  1000. }
  1001. break;
  1002. case DEV_PW_P2PS_DEFAULT:
  1003. p2p_dbg(p2p, "P2P: Peer using P2PS default pin");
  1004. if (dev->wps_method != WPS_P2PS) {
  1005. p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
  1006. p2p_wps_method_str(dev->wps_method));
  1007. status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
  1008. goto fail;
  1009. }
  1010. break;
  1011. default:
  1012. if (msg.dev_password_id &&
  1013. msg.dev_password_id == dev->oob_pw_id) {
  1014. p2p_dbg(p2p, "Peer using NFC");
  1015. if (dev->wps_method != WPS_NFC) {
  1016. p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
  1017. p2p_wps_method_str(dev->wps_method));
  1018. status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
  1019. goto fail;
  1020. }
  1021. break;
  1022. }
  1023. p2p_dbg(p2p, "Unsupported Device Password ID %d",
  1024. msg.dev_password_id);
  1025. status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
  1026. goto fail;
  1027. }
  1028. if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
  1029. goto fail;
  1030. p2p_set_state(p2p, P2P_GO_NEG);
  1031. p2p_clear_timeout(p2p);
  1032. p2p_dbg(p2p, "GO Negotiation with " MACSTR, MAC2STR(sa));
  1033. os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
  1034. fail:
  1035. /* Store GO Negotiation Confirmation to allow retransmission */
  1036. wpabuf_free(dev->go_neg_conf);
  1037. dev->go_neg_conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token,
  1038. status, msg.operating_channel,
  1039. go);
  1040. p2p_parse_free(&msg);
  1041. if (dev->go_neg_conf == NULL)
  1042. return;
  1043. p2p_dbg(p2p, "Sending GO Negotiation Confirm");
  1044. if (status == P2P_SC_SUCCESS) {
  1045. p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
  1046. dev->go_state = go ? LOCAL_GO : REMOTE_GO;
  1047. } else
  1048. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  1049. if (rx_freq > 0)
  1050. freq = rx_freq;
  1051. else
  1052. freq = dev->listen_freq;
  1053. dev->go_neg_conf_freq = freq;
  1054. dev->go_neg_conf_sent = 0;
  1055. if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, sa,
  1056. wpabuf_head(dev->go_neg_conf),
  1057. wpabuf_len(dev->go_neg_conf), 200) < 0) {
  1058. p2p_dbg(p2p, "Failed to send Action frame");
  1059. p2p_go_neg_failed(p2p, -1);
  1060. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  1061. } else
  1062. dev->go_neg_conf_sent++;
  1063. if (status != P2P_SC_SUCCESS) {
  1064. p2p_dbg(p2p, "GO Negotiation failed");
  1065. p2p_go_neg_failed(p2p, status);
  1066. }
  1067. }
  1068. void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa,
  1069. const u8 *data, size_t len)
  1070. {
  1071. struct p2p_device *dev;
  1072. struct p2p_message msg;
  1073. p2p_dbg(p2p, "Received GO Negotiation Confirm from " MACSTR,
  1074. MAC2STR(sa));
  1075. dev = p2p_get_device(p2p, sa);
  1076. if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
  1077. dev != p2p->go_neg_peer) {
  1078. p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR,
  1079. MAC2STR(sa));
  1080. return;
  1081. }
  1082. if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) {
  1083. p2p_dbg(p2p, "Stopped waiting for TX status on GO Negotiation Response since we already received Confirmation");
  1084. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  1085. }
  1086. if (p2p_parse(data, len, &msg))
  1087. return;
  1088. if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
  1089. p2p_dbg(p2p, "Was not expecting GO Negotiation Confirm - ignore");
  1090. p2p_parse_free(&msg);
  1091. return;
  1092. }
  1093. dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
  1094. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  1095. if (msg.dialog_token != dev->dialog_token) {
  1096. p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)",
  1097. msg.dialog_token, dev->dialog_token);
  1098. p2p_parse_free(&msg);
  1099. return;
  1100. }
  1101. if (!msg.status) {
  1102. p2p_dbg(p2p, "No Status attribute received");
  1103. p2p_parse_free(&msg);
  1104. return;
  1105. }
  1106. if (*msg.status) {
  1107. p2p_dbg(p2p, "GO Negotiation rejected: status %d", *msg.status);
  1108. p2p_go_neg_failed(p2p, *msg.status);
  1109. p2p_parse_free(&msg);
  1110. return;
  1111. }
  1112. if (dev->go_state == REMOTE_GO && msg.group_id) {
  1113. /* Store SSID for Provisioning step */
  1114. p2p->ssid_len = msg.group_id_len - ETH_ALEN;
  1115. os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
  1116. } else if (dev->go_state == REMOTE_GO) {
  1117. p2p_dbg(p2p, "Mandatory P2P Group ID attribute missing from GO Negotiation Confirmation");
  1118. p2p->ssid_len = 0;
  1119. p2p_go_neg_failed(p2p, P2P_SC_FAIL_INVALID_PARAMS);
  1120. p2p_parse_free(&msg);
  1121. return;
  1122. }
  1123. if (!msg.operating_channel) {
  1124. p2p_dbg(p2p, "Mandatory Operating Channel attribute missing from GO Negotiation Confirmation");
  1125. #ifdef CONFIG_P2P_STRICT
  1126. p2p_parse_free(&msg);
  1127. return;
  1128. #endif /* CONFIG_P2P_STRICT */
  1129. } else if (dev->go_state == REMOTE_GO) {
  1130. int oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
  1131. msg.operating_channel[4]);
  1132. if (oper_freq != dev->oper_freq) {
  1133. p2p_dbg(p2p, "Updated peer (GO) operating channel preference from %d MHz to %d MHz",
  1134. dev->oper_freq, oper_freq);
  1135. dev->oper_freq = oper_freq;
  1136. }
  1137. }
  1138. if (!msg.channel_list) {
  1139. p2p_dbg(p2p, "Mandatory Operating Channel attribute missing from GO Negotiation Confirmation");
  1140. #ifdef CONFIG_P2P_STRICT
  1141. p2p_parse_free(&msg);
  1142. return;
  1143. #endif /* CONFIG_P2P_STRICT */
  1144. }
  1145. p2p_parse_free(&msg);
  1146. if (dev->go_state == UNKNOWN_GO) {
  1147. /*
  1148. * This should not happen since GO negotiation has already
  1149. * been completed.
  1150. */
  1151. p2p_dbg(p2p, "Unexpected GO Neg state - do not know which end becomes GO");
  1152. return;
  1153. }
  1154. /*
  1155. * The peer could have missed our ctrl::ack frame for GO Negotiation
  1156. * Confirm and continue retransmitting the frame. To reduce the
  1157. * likelihood of the peer not getting successful TX status for the
  1158. * GO Negotiation Confirm frame, wait a short time here before starting
  1159. * the group so that we will remain on the current channel to
  1160. * acknowledge any possible retransmission from the peer.
  1161. */
  1162. p2p_dbg(p2p, "20 ms wait on current channel before starting group");
  1163. os_sleep(0, 20000);
  1164. p2p_go_complete(p2p, dev);
  1165. }