p2p_go_neg.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  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. u8 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 (end - pos > 2) {
  51. struct p2p_reg_class *cl = &ch->reg_class[ch->reg_classes];
  52. cl->reg_class = *pos++;
  53. channels = *pos++;
  54. if (channels > end - pos) {
  55. p2p_info(p2p, "Invalid peer Channel List");
  56. return -1;
  57. }
  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 && !p2p->num_pref_freq) {
  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, 129, 130, 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. static void p2p_check_pref_chan_no_recv(struct p2p_data *p2p, int go,
  479. struct p2p_device *dev,
  480. struct p2p_message *msg,
  481. unsigned freq_list[], unsigned int size)
  482. {
  483. u8 op_class, op_channel;
  484. unsigned int oper_freq = 0, i, j;
  485. int found = 0;
  486. p2p_dbg(p2p,
  487. "Peer didn't provide a preferred frequency list, see if any of our preferred channels are supported by peer device");
  488. /*
  489. * Search for a common channel in our preferred frequency list which is
  490. * also supported by the peer device.
  491. */
  492. for (i = 0; i < size && !found; i++) {
  493. /*
  494. * Make sure that the common frequency is:
  495. * 1. Supported by peer
  496. * 2. Allowed for P2P use.
  497. */
  498. oper_freq = freq_list[i];
  499. if (p2p_freq_to_channel(oper_freq, &op_class,
  500. &op_channel) < 0) {
  501. p2p_dbg(p2p, "Unsupported frequency %u MHz", oper_freq);
  502. continue;
  503. }
  504. if (!p2p_channels_includes(&p2p->cfg->channels,
  505. op_class, op_channel) &&
  506. (go || !p2p_channels_includes(&p2p->cfg->cli_channels,
  507. op_class, op_channel))) {
  508. p2p_dbg(p2p,
  509. "Freq %u MHz (oper_class %u channel %u) not allowed for P2P",
  510. oper_freq, op_class, op_channel);
  511. break;
  512. }
  513. for (j = 0; j < msg->channel_list_len; j++) {
  514. if (op_channel != msg->channel_list[j])
  515. continue;
  516. p2p->op_reg_class = op_class;
  517. p2p->op_channel = op_channel;
  518. os_memcpy(&p2p->channels, &p2p->cfg->channels,
  519. sizeof(struct p2p_channels));
  520. found = 1;
  521. break;
  522. }
  523. }
  524. if (found) {
  525. p2p_dbg(p2p,
  526. "Freq %d MHz is a preferred channel and is also supported by peer, use it as the operating channel",
  527. oper_freq);
  528. } else {
  529. p2p_dbg(p2p,
  530. "None of our preferred channels are supported by peer!. Use: %d MHz for oper_channel",
  531. dev->oper_freq);
  532. }
  533. }
  534. static void p2p_check_pref_chan_recv(struct p2p_data *p2p, int go,
  535. struct p2p_device *dev,
  536. struct p2p_message *msg,
  537. unsigned freq_list[], unsigned int size)
  538. {
  539. u8 op_class, op_channel;
  540. unsigned int oper_freq = 0, i, j;
  541. int found = 0;
  542. /*
  543. * Peer device supports a Preferred Frequency List.
  544. * Search for a common channel in the preferred frequency lists
  545. * of both peer and local devices.
  546. */
  547. for (i = 0; i < size && !found; i++) {
  548. for (j = 2; j < (msg->pref_freq_list_len / 2); j++) {
  549. oper_freq = p2p_channel_to_freq(
  550. msg->pref_freq_list[2 * j],
  551. msg->pref_freq_list[2 * j + 1]);
  552. if (freq_list[i] != oper_freq)
  553. continue;
  554. /*
  555. * Make sure that the found frequency is:
  556. * 1. Supported
  557. * 2. Allowed for P2P use.
  558. */
  559. if (p2p_freq_to_channel(oper_freq, &op_class,
  560. &op_channel) < 0) {
  561. p2p_dbg(p2p, "Unsupported frequency %u MHz",
  562. oper_freq);
  563. continue;
  564. }
  565. if (!p2p_channels_includes(&p2p->cfg->channels,
  566. op_class, op_channel) &&
  567. (go ||
  568. !p2p_channels_includes(&p2p->cfg->cli_channels,
  569. op_class, op_channel))) {
  570. p2p_dbg(p2p,
  571. "Freq %u MHz (oper_class %u channel %u) not allowed for P2P",
  572. oper_freq, op_class, op_channel);
  573. break;
  574. }
  575. p2p->op_reg_class = op_class;
  576. p2p->op_channel = op_channel;
  577. os_memcpy(&p2p->channels, &p2p->cfg->channels,
  578. sizeof(struct p2p_channels));
  579. found = 1;
  580. break;
  581. }
  582. }
  583. if (found) {
  584. p2p_dbg(p2p,
  585. "Freq %d MHz is a common preferred channel for both peer and local, use it as operating channel",
  586. oper_freq);
  587. } else {
  588. p2p_dbg(p2p,
  589. "No common preferred channels found! Use: %d MHz for oper_channel",
  590. dev->oper_freq);
  591. }
  592. }
  593. void p2p_check_pref_chan(struct p2p_data *p2p, int go,
  594. struct p2p_device *dev, struct p2p_message *msg)
  595. {
  596. unsigned int freq_list[P2P_MAX_PREF_CHANNELS], size;
  597. unsigned int i;
  598. u8 op_class, op_channel;
  599. /*
  600. * Use the preferred channel list from the driver only if there is no
  601. * forced_freq, e.g., P2P_CONNECT freq=..., and no preferred operating
  602. * channel hardcoded in the configuration file.
  603. */
  604. if (!p2p->cfg->get_pref_freq_list || p2p->cfg->num_pref_chan ||
  605. (dev->flags & P2P_DEV_FORCE_FREQ) || p2p->cfg->cfg_op_channel)
  606. return;
  607. /* Obtain our preferred frequency list from driver based on P2P role. */
  608. size = P2P_MAX_PREF_CHANNELS;
  609. if (p2p->cfg->get_pref_freq_list(p2p->cfg->cb_ctx, go, &size,
  610. freq_list))
  611. return;
  612. /*
  613. * Check if peer's preference of operating channel is in
  614. * our preferred channel list.
  615. */
  616. for (i = 0; i < size; i++) {
  617. if (freq_list[i] == (unsigned int) dev->oper_freq)
  618. break;
  619. }
  620. if (i != size) {
  621. /* Peer operating channel preference matches our preference */
  622. if (p2p_freq_to_channel(freq_list[i], &op_class, &op_channel) <
  623. 0) {
  624. p2p_dbg(p2p,
  625. "Peer operating channel preference is unsupported frequency %u MHz",
  626. freq_list[i]);
  627. } else {
  628. p2p->op_reg_class = op_class;
  629. p2p->op_channel = op_channel;
  630. os_memcpy(&p2p->channels, &p2p->cfg->channels,
  631. sizeof(struct p2p_channels));
  632. return;
  633. }
  634. }
  635. p2p_dbg(p2p,
  636. "Peer operating channel preference: %d MHz is not in our preferred channel list",
  637. dev->oper_freq);
  638. /*
  639. Check if peer's preferred channel list is
  640. * _not_ included in the GO Negotiation Request or Invitation Request.
  641. */
  642. if (msg->pref_freq_list_len == 0)
  643. p2p_check_pref_chan_no_recv(p2p, go, dev, msg, freq_list, size);
  644. else
  645. p2p_check_pref_chan_recv(p2p, go, dev, msg, freq_list, size);
  646. }
  647. void p2p_process_go_neg_req(struct p2p_data *p2p, const u8 *sa,
  648. const u8 *data, size_t len, int rx_freq)
  649. {
  650. struct p2p_device *dev = NULL;
  651. struct wpabuf *resp;
  652. struct p2p_message msg;
  653. u8 status = P2P_SC_FAIL_INVALID_PARAMS;
  654. int tie_breaker = 0;
  655. int freq;
  656. p2p_dbg(p2p, "Received GO Negotiation Request from " MACSTR "(freq=%d)",
  657. MAC2STR(sa), rx_freq);
  658. if (p2p_parse(data, len, &msg))
  659. return;
  660. if (!msg.capability) {
  661. p2p_dbg(p2p, "Mandatory Capability attribute missing from GO Negotiation Request");
  662. #ifdef CONFIG_P2P_STRICT
  663. goto fail;
  664. #endif /* CONFIG_P2P_STRICT */
  665. }
  666. if (msg.go_intent)
  667. tie_breaker = *msg.go_intent & 0x01;
  668. else {
  669. p2p_dbg(p2p, "Mandatory GO Intent attribute missing from GO Negotiation Request");
  670. #ifdef CONFIG_P2P_STRICT
  671. goto fail;
  672. #endif /* CONFIG_P2P_STRICT */
  673. }
  674. if (!msg.config_timeout) {
  675. p2p_dbg(p2p, "Mandatory Configuration Timeout attribute missing from GO Negotiation Request");
  676. #ifdef CONFIG_P2P_STRICT
  677. goto fail;
  678. #endif /* CONFIG_P2P_STRICT */
  679. }
  680. if (!msg.listen_channel) {
  681. p2p_dbg(p2p, "No Listen Channel attribute received");
  682. goto fail;
  683. }
  684. if (!msg.operating_channel) {
  685. p2p_dbg(p2p, "No Operating Channel attribute received");
  686. goto fail;
  687. }
  688. if (!msg.channel_list) {
  689. p2p_dbg(p2p, "No Channel List attribute received");
  690. goto fail;
  691. }
  692. if (!msg.intended_addr) {
  693. p2p_dbg(p2p, "No Intended P2P Interface Address attribute received");
  694. goto fail;
  695. }
  696. if (!msg.p2p_device_info) {
  697. p2p_dbg(p2p, "No P2P Device Info attribute received");
  698. goto fail;
  699. }
  700. if (os_memcmp(msg.p2p_device_addr, sa, ETH_ALEN) != 0) {
  701. p2p_dbg(p2p, "Unexpected GO Negotiation Request SA=" MACSTR
  702. " != dev_addr=" MACSTR,
  703. MAC2STR(sa), MAC2STR(msg.p2p_device_addr));
  704. goto fail;
  705. }
  706. dev = p2p_get_device(p2p, sa);
  707. if (msg.status && *msg.status) {
  708. p2p_dbg(p2p, "Unexpected Status attribute (%d) in GO Negotiation Request",
  709. *msg.status);
  710. if (dev && p2p->go_neg_peer == dev &&
  711. *msg.status == P2P_SC_FAIL_REJECTED_BY_USER) {
  712. /*
  713. * This mechanism for using Status attribute in GO
  714. * Negotiation Request is not compliant with the P2P
  715. * specification, but some deployed devices use it to
  716. * indicate rejection of GO Negotiation in a case where
  717. * they have sent out GO Negotiation Response with
  718. * status 1. The P2P specification explicitly disallows
  719. * this. To avoid unnecessary interoperability issues
  720. * and extra frames, mark the pending negotiation as
  721. * failed and do not reply to this GO Negotiation
  722. * Request frame.
  723. */
  724. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  725. p2p_go_neg_failed(p2p, *msg.status);
  726. p2p_parse_free(&msg);
  727. return;
  728. }
  729. goto fail;
  730. }
  731. if (dev == NULL)
  732. dev = p2p_add_dev_from_go_neg_req(p2p, sa, &msg);
  733. else if ((dev->flags & P2P_DEV_PROBE_REQ_ONLY) ||
  734. !(dev->flags & P2P_DEV_REPORTED))
  735. p2p_add_dev_info(p2p, sa, dev, &msg);
  736. else if (!dev->listen_freq && !dev->oper_freq) {
  737. /*
  738. * This may happen if the peer entry was added based on PD
  739. * Request and no Probe Request/Response frame has been received
  740. * from this peer (or that information has timed out).
  741. */
  742. p2p_dbg(p2p, "Update peer " MACSTR
  743. " based on GO Neg Req since listen/oper freq not known",
  744. MAC2STR(dev->info.p2p_device_addr));
  745. p2p_add_dev_info(p2p, sa, dev, &msg);
  746. }
  747. if (p2p->go_neg_peer && p2p->go_neg_peer == dev)
  748. eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
  749. if (dev && dev->flags & P2P_DEV_USER_REJECTED) {
  750. p2p_dbg(p2p, "User has rejected this peer");
  751. status = P2P_SC_FAIL_REJECTED_BY_USER;
  752. } else if (dev == NULL ||
  753. (dev->wps_method == WPS_NOT_READY &&
  754. (p2p->authorized_oob_dev_pw_id == 0 ||
  755. p2p->authorized_oob_dev_pw_id !=
  756. msg.dev_password_id))) {
  757. p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR,
  758. MAC2STR(sa));
  759. status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
  760. p2p->cfg->go_neg_req_rx(p2p->cfg->cb_ctx, sa,
  761. msg.dev_password_id,
  762. msg.go_intent ? (*msg.go_intent >> 1) :
  763. 0);
  764. } else if (p2p->go_neg_peer && p2p->go_neg_peer != dev) {
  765. p2p_dbg(p2p, "Already in Group Formation with another peer");
  766. status = P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
  767. } else {
  768. int go;
  769. if (!p2p->go_neg_peer) {
  770. p2p_dbg(p2p, "Starting GO Negotiation with previously authorized peer");
  771. if (!(dev->flags & P2P_DEV_FORCE_FREQ)) {
  772. p2p_dbg(p2p, "Use default channel settings");
  773. p2p->op_reg_class = p2p->cfg->op_reg_class;
  774. p2p->op_channel = p2p->cfg->op_channel;
  775. os_memcpy(&p2p->channels, &p2p->cfg->channels,
  776. sizeof(struct p2p_channels));
  777. } else {
  778. p2p_dbg(p2p, "Use previously configured forced channel settings");
  779. }
  780. }
  781. dev->flags &= ~P2P_DEV_NOT_YET_READY;
  782. if (!msg.go_intent) {
  783. p2p_dbg(p2p, "No GO Intent attribute received");
  784. goto fail;
  785. }
  786. if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
  787. p2p_dbg(p2p, "Invalid GO Intent value (%u) received",
  788. *msg.go_intent >> 1);
  789. goto fail;
  790. }
  791. if (dev->go_neg_req_sent &&
  792. os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) > 0) {
  793. p2p_dbg(p2p, "Do not reply since peer has higher address and GO Neg Request already sent");
  794. p2p_parse_free(&msg);
  795. return;
  796. }
  797. if (dev->go_neg_req_sent &&
  798. (dev->flags & P2P_DEV_PEER_WAITING_RESPONSE)) {
  799. p2p_dbg(p2p,
  800. "Do not reply since peer is waiting for us to start a new GO Negotiation and GO Neg Request already sent");
  801. p2p_parse_free(&msg);
  802. return;
  803. }
  804. go = p2p_go_det(p2p->go_intent, *msg.go_intent);
  805. if (go < 0) {
  806. p2p_dbg(p2p, "Incompatible GO Intent");
  807. status = P2P_SC_FAIL_BOTH_GO_INTENT_15;
  808. goto fail;
  809. }
  810. if (p2p_peer_channels(p2p, dev, msg.channel_list,
  811. msg.channel_list_len) < 0) {
  812. p2p_dbg(p2p, "No common channels found");
  813. status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
  814. goto fail;
  815. }
  816. switch (msg.dev_password_id) {
  817. case DEV_PW_REGISTRAR_SPECIFIED:
  818. p2p_dbg(p2p, "PIN from peer Display");
  819. if (dev->wps_method != WPS_PIN_KEYPAD) {
  820. p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
  821. p2p_wps_method_str(dev->wps_method));
  822. status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
  823. goto fail;
  824. }
  825. break;
  826. case DEV_PW_USER_SPECIFIED:
  827. p2p_dbg(p2p, "Peer entered PIN on Keypad");
  828. if (dev->wps_method != WPS_PIN_DISPLAY) {
  829. p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
  830. p2p_wps_method_str(dev->wps_method));
  831. status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
  832. goto fail;
  833. }
  834. break;
  835. case DEV_PW_PUSHBUTTON:
  836. p2p_dbg(p2p, "Peer using pushbutton");
  837. if (dev->wps_method != WPS_PBC) {
  838. p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
  839. p2p_wps_method_str(dev->wps_method));
  840. status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
  841. goto fail;
  842. }
  843. break;
  844. case DEV_PW_P2PS_DEFAULT:
  845. p2p_dbg(p2p, "Peer using P2PS pin");
  846. if (dev->wps_method != WPS_P2PS) {
  847. p2p_dbg(p2p,
  848. "We have wps_method=%s -> incompatible",
  849. p2p_wps_method_str(dev->wps_method));
  850. status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
  851. goto fail;
  852. }
  853. break;
  854. default:
  855. if (msg.dev_password_id &&
  856. msg.dev_password_id == dev->oob_pw_id) {
  857. p2p_dbg(p2p, "Peer using NFC");
  858. if (dev->wps_method != WPS_NFC) {
  859. p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
  860. p2p_wps_method_str(
  861. dev->wps_method));
  862. status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
  863. goto fail;
  864. }
  865. break;
  866. }
  867. #ifdef CONFIG_WPS_NFC
  868. if (p2p->authorized_oob_dev_pw_id &&
  869. msg.dev_password_id ==
  870. p2p->authorized_oob_dev_pw_id) {
  871. p2p_dbg(p2p, "Using static handover with our device password from NFC Tag");
  872. dev->wps_method = WPS_NFC;
  873. dev->oob_pw_id = p2p->authorized_oob_dev_pw_id;
  874. break;
  875. }
  876. #endif /* CONFIG_WPS_NFC */
  877. p2p_dbg(p2p, "Unsupported Device Password ID %d",
  878. msg.dev_password_id);
  879. status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
  880. goto fail;
  881. }
  882. if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
  883. goto fail;
  884. dev->go_state = go ? LOCAL_GO : REMOTE_GO;
  885. dev->oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
  886. msg.operating_channel[4]);
  887. p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
  888. dev->oper_freq);
  889. /*
  890. * Use the driver preferred frequency list extension if
  891. * supported.
  892. */
  893. p2p_check_pref_chan(p2p, go, dev, &msg);
  894. if (msg.config_timeout) {
  895. dev->go_timeout = msg.config_timeout[0];
  896. dev->client_timeout = msg.config_timeout[1];
  897. }
  898. p2p_dbg(p2p, "GO Negotiation with " MACSTR, MAC2STR(sa));
  899. if (p2p->state != P2P_IDLE)
  900. p2p_stop_find_for_freq(p2p, rx_freq);
  901. p2p_set_state(p2p, P2P_GO_NEG);
  902. p2p_clear_timeout(p2p);
  903. dev->dialog_token = msg.dialog_token;
  904. os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
  905. p2p->go_neg_peer = dev;
  906. eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
  907. status = P2P_SC_SUCCESS;
  908. }
  909. fail:
  910. if (dev)
  911. dev->status = status;
  912. resp = p2p_build_go_neg_resp(p2p, dev, msg.dialog_token, status,
  913. !tie_breaker);
  914. p2p_parse_free(&msg);
  915. if (resp == NULL)
  916. return;
  917. p2p_dbg(p2p, "Sending GO Negotiation Response");
  918. if (rx_freq > 0)
  919. freq = rx_freq;
  920. else
  921. freq = p2p_channel_to_freq(p2p->cfg->reg_class,
  922. p2p->cfg->channel);
  923. if (freq < 0) {
  924. p2p_dbg(p2p, "Unknown regulatory class/channel");
  925. wpabuf_free(resp);
  926. return;
  927. }
  928. if (status == P2P_SC_SUCCESS) {
  929. p2p->pending_action_state = P2P_PENDING_GO_NEG_RESPONSE;
  930. dev->flags |= P2P_DEV_WAIT_GO_NEG_CONFIRM;
  931. if (os_memcmp(sa, p2p->cfg->dev_addr, ETH_ALEN) < 0) {
  932. /*
  933. * Peer has smaller address, so the GO Negotiation
  934. * Response from us is expected to complete
  935. * negotiation. Ignore a GO Negotiation Response from
  936. * the peer if it happens to be received after this
  937. * point due to a race condition in GO Negotiation
  938. * Request transmission and processing.
  939. */
  940. dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
  941. }
  942. } else
  943. p2p->pending_action_state =
  944. P2P_PENDING_GO_NEG_RESPONSE_FAILURE;
  945. if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
  946. p2p->cfg->dev_addr,
  947. wpabuf_head(resp), wpabuf_len(resp), 100) < 0) {
  948. p2p_dbg(p2p, "Failed to send Action frame");
  949. }
  950. wpabuf_free(resp);
  951. }
  952. static struct wpabuf * p2p_build_go_neg_conf(struct p2p_data *p2p,
  953. struct p2p_device *peer,
  954. u8 dialog_token, u8 status,
  955. const u8 *resp_chan, int go)
  956. {
  957. struct wpabuf *buf;
  958. u8 *len;
  959. struct p2p_channels res;
  960. u8 group_capab;
  961. size_t extra = 0;
  962. p2p_dbg(p2p, "Building GO Negotiation Confirm");
  963. #ifdef CONFIG_WIFI_DISPLAY
  964. if (p2p->wfd_ie_go_neg)
  965. extra = wpabuf_len(p2p->wfd_ie_go_neg);
  966. #endif /* CONFIG_WIFI_DISPLAY */
  967. if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF])
  968. extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF]);
  969. buf = wpabuf_alloc(1000 + extra);
  970. if (buf == NULL)
  971. return NULL;
  972. p2p_buf_add_public_action_hdr(buf, P2P_GO_NEG_CONF, dialog_token);
  973. len = p2p_buf_add_ie_hdr(buf);
  974. p2p_buf_add_status(buf, status);
  975. group_capab = 0;
  976. if (peer->go_state == LOCAL_GO) {
  977. if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
  978. group_capab |= P2P_GROUP_CAPAB_PERSISTENT_GROUP;
  979. if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
  980. group_capab |=
  981. P2P_GROUP_CAPAB_PERSISTENT_RECONN;
  982. }
  983. if (p2p->cross_connect)
  984. group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
  985. if (p2p->cfg->p2p_intra_bss)
  986. group_capab |= P2P_GROUP_CAPAB_INTRA_BSS_DIST;
  987. }
  988. p2p_buf_add_capability(buf, p2p->dev_capab &
  989. ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY,
  990. group_capab);
  991. if (go || resp_chan == NULL)
  992. p2p_buf_add_operating_channel(buf, p2p->cfg->country,
  993. p2p->op_reg_class,
  994. p2p->op_channel);
  995. else
  996. p2p_buf_add_operating_channel(buf, (const char *) resp_chan,
  997. resp_chan[3], resp_chan[4]);
  998. p2p_channels_intersect(&p2p->channels, &peer->channels, &res);
  999. p2p_buf_add_channel_list(buf, p2p->cfg->country, &res);
  1000. if (go) {
  1001. p2p_buf_add_group_id(buf, p2p->cfg->dev_addr, p2p->ssid,
  1002. p2p->ssid_len);
  1003. }
  1004. p2p_buf_update_ie_hdr(buf, len);
  1005. #ifdef CONFIG_WIFI_DISPLAY
  1006. if (p2p->wfd_ie_go_neg)
  1007. wpabuf_put_buf(buf, p2p->wfd_ie_go_neg);
  1008. #endif /* CONFIG_WIFI_DISPLAY */
  1009. if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF])
  1010. wpabuf_put_buf(buf, p2p->vendor_elem[VENDOR_ELEM_P2P_GO_NEG_CONF]);
  1011. return buf;
  1012. }
  1013. void p2p_process_go_neg_resp(struct p2p_data *p2p, const u8 *sa,
  1014. const u8 *data, size_t len, int rx_freq)
  1015. {
  1016. struct p2p_device *dev;
  1017. int go = -1;
  1018. struct p2p_message msg;
  1019. u8 status = P2P_SC_SUCCESS;
  1020. int freq;
  1021. p2p_dbg(p2p, "Received GO Negotiation Response from " MACSTR
  1022. " (freq=%d)", MAC2STR(sa), rx_freq);
  1023. dev = p2p_get_device(p2p, sa);
  1024. if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
  1025. dev != p2p->go_neg_peer) {
  1026. p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR,
  1027. MAC2STR(sa));
  1028. return;
  1029. }
  1030. if (p2p_parse(data, len, &msg))
  1031. return;
  1032. if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE)) {
  1033. p2p_dbg(p2p, "Was not expecting GO Negotiation Response - ignore");
  1034. p2p_parse_free(&msg);
  1035. return;
  1036. }
  1037. dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
  1038. if (msg.dialog_token != dev->dialog_token) {
  1039. p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)",
  1040. msg.dialog_token, dev->dialog_token);
  1041. p2p_parse_free(&msg);
  1042. return;
  1043. }
  1044. if (!msg.status) {
  1045. p2p_dbg(p2p, "No Status attribute received");
  1046. status = P2P_SC_FAIL_INVALID_PARAMS;
  1047. goto fail;
  1048. }
  1049. if (*msg.status) {
  1050. p2p_dbg(p2p, "GO Negotiation rejected: status %d", *msg.status);
  1051. dev->go_neg_req_sent = 0;
  1052. if (*msg.status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
  1053. p2p_dbg(p2p, "Wait for the peer to become ready for GO Negotiation");
  1054. dev->flags |= P2P_DEV_NOT_YET_READY;
  1055. eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p,
  1056. NULL);
  1057. eloop_register_timeout(120, 0, p2p_go_neg_wait_timeout,
  1058. p2p, NULL);
  1059. if (p2p->state == P2P_CONNECT_LISTEN)
  1060. p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
  1061. else
  1062. p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
  1063. p2p_set_timeout(p2p, 0, 0);
  1064. } else {
  1065. p2p_dbg(p2p, "Stop GO Negotiation attempt");
  1066. p2p_go_neg_failed(p2p, *msg.status);
  1067. }
  1068. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  1069. p2p_parse_free(&msg);
  1070. return;
  1071. }
  1072. if (!msg.capability) {
  1073. p2p_dbg(p2p, "Mandatory Capability attribute missing from GO Negotiation Response");
  1074. #ifdef CONFIG_P2P_STRICT
  1075. status = P2P_SC_FAIL_INVALID_PARAMS;
  1076. goto fail;
  1077. #endif /* CONFIG_P2P_STRICT */
  1078. }
  1079. if (!msg.p2p_device_info) {
  1080. p2p_dbg(p2p, "Mandatory P2P Device Info attribute missing from GO Negotiation Response");
  1081. #ifdef CONFIG_P2P_STRICT
  1082. status = P2P_SC_FAIL_INVALID_PARAMS;
  1083. goto fail;
  1084. #endif /* CONFIG_P2P_STRICT */
  1085. }
  1086. if (!msg.intended_addr) {
  1087. p2p_dbg(p2p, "No Intended P2P Interface Address attribute received");
  1088. status = P2P_SC_FAIL_INVALID_PARAMS;
  1089. goto fail;
  1090. }
  1091. if (!msg.go_intent) {
  1092. p2p_dbg(p2p, "No GO Intent attribute received");
  1093. status = P2P_SC_FAIL_INVALID_PARAMS;
  1094. goto fail;
  1095. }
  1096. if ((*msg.go_intent >> 1) > P2P_MAX_GO_INTENT) {
  1097. p2p_dbg(p2p, "Invalid GO Intent value (%u) received",
  1098. *msg.go_intent >> 1);
  1099. status = P2P_SC_FAIL_INVALID_PARAMS;
  1100. goto fail;
  1101. }
  1102. go = p2p_go_det(p2p->go_intent, *msg.go_intent);
  1103. if (go < 0) {
  1104. p2p_dbg(p2p, "Incompatible GO Intent");
  1105. status = P2P_SC_FAIL_INCOMPATIBLE_PARAMS;
  1106. goto fail;
  1107. }
  1108. if (!go && msg.group_id) {
  1109. /* Store SSID for Provisioning step */
  1110. p2p->ssid_len = msg.group_id_len - ETH_ALEN;
  1111. os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
  1112. } else if (!go) {
  1113. p2p_dbg(p2p, "Mandatory P2P Group ID attribute missing from GO Negotiation Response");
  1114. p2p->ssid_len = 0;
  1115. status = P2P_SC_FAIL_INVALID_PARAMS;
  1116. goto fail;
  1117. }
  1118. if (!msg.config_timeout) {
  1119. p2p_dbg(p2p, "Mandatory Configuration Timeout attribute missing from GO Negotiation Response");
  1120. #ifdef CONFIG_P2P_STRICT
  1121. status = P2P_SC_FAIL_INVALID_PARAMS;
  1122. goto fail;
  1123. #endif /* CONFIG_P2P_STRICT */
  1124. } else {
  1125. dev->go_timeout = msg.config_timeout[0];
  1126. dev->client_timeout = msg.config_timeout[1];
  1127. }
  1128. if (msg.wfd_subelems) {
  1129. wpabuf_free(dev->info.wfd_subelems);
  1130. dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
  1131. }
  1132. if (!msg.operating_channel && !go) {
  1133. /*
  1134. * Note: P2P Client may omit Operating Channel attribute to
  1135. * indicate it does not have a preference.
  1136. */
  1137. p2p_dbg(p2p, "No Operating Channel attribute received");
  1138. status = P2P_SC_FAIL_INVALID_PARAMS;
  1139. goto fail;
  1140. }
  1141. if (!msg.channel_list) {
  1142. p2p_dbg(p2p, "No Channel List attribute received");
  1143. status = P2P_SC_FAIL_INVALID_PARAMS;
  1144. goto fail;
  1145. }
  1146. if (p2p_peer_channels(p2p, dev, msg.channel_list,
  1147. msg.channel_list_len) < 0) {
  1148. p2p_dbg(p2p, "No common channels found");
  1149. status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
  1150. goto fail;
  1151. }
  1152. if (msg.operating_channel) {
  1153. dev->oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
  1154. msg.operating_channel[4]);
  1155. p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
  1156. dev->oper_freq);
  1157. } else
  1158. dev->oper_freq = 0;
  1159. switch (msg.dev_password_id) {
  1160. case DEV_PW_REGISTRAR_SPECIFIED:
  1161. p2p_dbg(p2p, "PIN from peer Display");
  1162. if (dev->wps_method != WPS_PIN_KEYPAD) {
  1163. p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
  1164. p2p_wps_method_str(dev->wps_method));
  1165. status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
  1166. goto fail;
  1167. }
  1168. break;
  1169. case DEV_PW_USER_SPECIFIED:
  1170. p2p_dbg(p2p, "Peer entered PIN on Keypad");
  1171. if (dev->wps_method != WPS_PIN_DISPLAY) {
  1172. p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
  1173. p2p_wps_method_str(dev->wps_method));
  1174. status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
  1175. goto fail;
  1176. }
  1177. break;
  1178. case DEV_PW_PUSHBUTTON:
  1179. p2p_dbg(p2p, "Peer using pushbutton");
  1180. if (dev->wps_method != WPS_PBC) {
  1181. p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
  1182. p2p_wps_method_str(dev->wps_method));
  1183. status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
  1184. goto fail;
  1185. }
  1186. break;
  1187. case DEV_PW_P2PS_DEFAULT:
  1188. p2p_dbg(p2p, "P2P: Peer using P2PS default pin");
  1189. if (dev->wps_method != WPS_P2PS) {
  1190. p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
  1191. p2p_wps_method_str(dev->wps_method));
  1192. status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
  1193. goto fail;
  1194. }
  1195. break;
  1196. default:
  1197. if (msg.dev_password_id &&
  1198. msg.dev_password_id == dev->oob_pw_id) {
  1199. p2p_dbg(p2p, "Peer using NFC");
  1200. if (dev->wps_method != WPS_NFC) {
  1201. p2p_dbg(p2p, "We have wps_method=%s -> incompatible",
  1202. p2p_wps_method_str(dev->wps_method));
  1203. status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
  1204. goto fail;
  1205. }
  1206. break;
  1207. }
  1208. p2p_dbg(p2p, "Unsupported Device Password ID %d",
  1209. msg.dev_password_id);
  1210. status = P2P_SC_FAIL_INCOMPATIBLE_PROV_METHOD;
  1211. goto fail;
  1212. }
  1213. if (go && p2p_go_select_channel(p2p, dev, &status) < 0)
  1214. goto fail;
  1215. /*
  1216. * Use the driver preferred frequency list extension if local device is
  1217. * GO.
  1218. */
  1219. if (go)
  1220. p2p_check_pref_chan(p2p, go, dev, &msg);
  1221. p2p_set_state(p2p, P2P_GO_NEG);
  1222. p2p_clear_timeout(p2p);
  1223. p2p_dbg(p2p, "GO Negotiation with " MACSTR, MAC2STR(sa));
  1224. os_memcpy(dev->intended_addr, msg.intended_addr, ETH_ALEN);
  1225. fail:
  1226. /* Store GO Negotiation Confirmation to allow retransmission */
  1227. wpabuf_free(dev->go_neg_conf);
  1228. dev->go_neg_conf = p2p_build_go_neg_conf(p2p, dev, msg.dialog_token,
  1229. status, msg.operating_channel,
  1230. go);
  1231. p2p_parse_free(&msg);
  1232. if (dev->go_neg_conf == NULL)
  1233. return;
  1234. p2p_dbg(p2p, "Sending GO Negotiation Confirm");
  1235. if (status == P2P_SC_SUCCESS) {
  1236. p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
  1237. dev->go_state = go ? LOCAL_GO : REMOTE_GO;
  1238. } else
  1239. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  1240. if (rx_freq > 0)
  1241. freq = rx_freq;
  1242. else
  1243. freq = dev->listen_freq;
  1244. dev->go_neg_conf_freq = freq;
  1245. dev->go_neg_conf_sent = 0;
  1246. if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr, sa,
  1247. wpabuf_head(dev->go_neg_conf),
  1248. wpabuf_len(dev->go_neg_conf), 50) < 0) {
  1249. p2p_dbg(p2p, "Failed to send Action frame");
  1250. p2p_go_neg_failed(p2p, -1);
  1251. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  1252. } else
  1253. dev->go_neg_conf_sent++;
  1254. if (status != P2P_SC_SUCCESS) {
  1255. p2p_dbg(p2p, "GO Negotiation failed");
  1256. p2p_go_neg_failed(p2p, status);
  1257. }
  1258. }
  1259. void p2p_process_go_neg_conf(struct p2p_data *p2p, const u8 *sa,
  1260. const u8 *data, size_t len)
  1261. {
  1262. struct p2p_device *dev;
  1263. struct p2p_message msg;
  1264. p2p_dbg(p2p, "Received GO Negotiation Confirm from " MACSTR,
  1265. MAC2STR(sa));
  1266. dev = p2p_get_device(p2p, sa);
  1267. if (dev == NULL || dev->wps_method == WPS_NOT_READY ||
  1268. dev != p2p->go_neg_peer) {
  1269. p2p_dbg(p2p, "Not ready for GO negotiation with " MACSTR,
  1270. MAC2STR(sa));
  1271. return;
  1272. }
  1273. if (p2p->pending_action_state == P2P_PENDING_GO_NEG_RESPONSE) {
  1274. p2p_dbg(p2p, "Stopped waiting for TX status on GO Negotiation Response since we already received Confirmation");
  1275. p2p->pending_action_state = P2P_NO_PENDING_ACTION;
  1276. }
  1277. if (p2p_parse(data, len, &msg))
  1278. return;
  1279. if (!(dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
  1280. p2p_dbg(p2p, "Was not expecting GO Negotiation Confirm - ignore");
  1281. p2p_parse_free(&msg);
  1282. return;
  1283. }
  1284. dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
  1285. p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
  1286. if (msg.dialog_token != dev->dialog_token) {
  1287. p2p_dbg(p2p, "Unexpected Dialog Token %u (expected %u)",
  1288. msg.dialog_token, dev->dialog_token);
  1289. p2p_parse_free(&msg);
  1290. return;
  1291. }
  1292. if (!msg.status) {
  1293. p2p_dbg(p2p, "No Status attribute received");
  1294. p2p_parse_free(&msg);
  1295. return;
  1296. }
  1297. if (*msg.status) {
  1298. p2p_dbg(p2p, "GO Negotiation rejected: status %d", *msg.status);
  1299. p2p_go_neg_failed(p2p, *msg.status);
  1300. p2p_parse_free(&msg);
  1301. return;
  1302. }
  1303. if (dev->go_state == REMOTE_GO && msg.group_id) {
  1304. /* Store SSID for Provisioning step */
  1305. p2p->ssid_len = msg.group_id_len - ETH_ALEN;
  1306. os_memcpy(p2p->ssid, msg.group_id + ETH_ALEN, p2p->ssid_len);
  1307. } else if (dev->go_state == REMOTE_GO) {
  1308. p2p_dbg(p2p, "Mandatory P2P Group ID attribute missing from GO Negotiation Confirmation");
  1309. p2p->ssid_len = 0;
  1310. p2p_go_neg_failed(p2p, P2P_SC_FAIL_INVALID_PARAMS);
  1311. p2p_parse_free(&msg);
  1312. return;
  1313. }
  1314. if (!msg.operating_channel) {
  1315. p2p_dbg(p2p, "Mandatory Operating Channel attribute missing from GO Negotiation Confirmation");
  1316. #ifdef CONFIG_P2P_STRICT
  1317. p2p_parse_free(&msg);
  1318. return;
  1319. #endif /* CONFIG_P2P_STRICT */
  1320. } else if (dev->go_state == REMOTE_GO) {
  1321. int oper_freq = p2p_channel_to_freq(msg.operating_channel[3],
  1322. msg.operating_channel[4]);
  1323. if (oper_freq != dev->oper_freq) {
  1324. p2p_dbg(p2p, "Updated peer (GO) operating channel preference from %d MHz to %d MHz",
  1325. dev->oper_freq, oper_freq);
  1326. dev->oper_freq = oper_freq;
  1327. }
  1328. }
  1329. if (!msg.channel_list) {
  1330. p2p_dbg(p2p, "Mandatory Operating Channel attribute missing from GO Negotiation Confirmation");
  1331. #ifdef CONFIG_P2P_STRICT
  1332. p2p_parse_free(&msg);
  1333. return;
  1334. #endif /* CONFIG_P2P_STRICT */
  1335. }
  1336. p2p_parse_free(&msg);
  1337. if (dev->go_state == UNKNOWN_GO) {
  1338. /*
  1339. * This should not happen since GO negotiation has already
  1340. * been completed.
  1341. */
  1342. p2p_dbg(p2p, "Unexpected GO Neg state - do not know which end becomes GO");
  1343. return;
  1344. }
  1345. /*
  1346. * The peer could have missed our ctrl::ack frame for GO Negotiation
  1347. * Confirm and continue retransmitting the frame. To reduce the
  1348. * likelihood of the peer not getting successful TX status for the
  1349. * GO Negotiation Confirm frame, wait a short time here before starting
  1350. * the group so that we will remain on the current channel to
  1351. * acknowledge any possible retransmission from the peer.
  1352. */
  1353. p2p_dbg(p2p, "20 ms wait on current channel before starting group");
  1354. os_sleep(0, 20000);
  1355. p2p_go_complete(p2p, dev);
  1356. }