ieee80211_proto.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. /* $OpenBSD: ieee80211_proto.c,v 1.52 2015/07/15 22:16:42 deraadt Exp $ */
  2. /* $NetBSD: ieee80211_proto.c,v 1.8 2004/04/30 23:58:20 dyoung Exp $ */
  3. /*-
  4. * Copyright (c) 2001 Atsushi Onoe
  5. * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
  6. * Copyright (c) 2008, 2009 Damien Bergamini
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  21. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  22. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  23. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  29. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. /*
  32. * IEEE 802.11 protocol support.
  33. */
  34. #include <sys/param.h>
  35. #include <sys/systm.h>
  36. #include <sys/mbuf.h>
  37. #include <sys/kernel.h>
  38. #include <sys/socket.h>
  39. #include <sys/sockio.h>
  40. #include <sys/endian.h>
  41. #include <sys/errno.h>
  42. #include <sys/sysctl.h>
  43. #include <net/if.h>
  44. #include <net/if_dl.h>
  45. #include <net/if_media.h>
  46. #include <net/if_arp.h>
  47. #include <net/if_llc.h>
  48. #include <netinet/in.h>
  49. #include <netinet/if_ether.h>
  50. #include <net80211/ieee80211_var.h>
  51. #include <net80211/ieee80211_priv.h>
  52. const char * const ieee80211_mgt_subtype_name[] = {
  53. "assoc_req", "assoc_resp", "reassoc_req", "reassoc_resp",
  54. "probe_req", "probe_resp", "reserved#6", "reserved#7",
  55. "beacon", "atim", "disassoc", "auth",
  56. "deauth", "action", "action_noack", "reserved#15"
  57. };
  58. const char * const ieee80211_state_name[IEEE80211_S_MAX] = {
  59. "INIT", /* IEEE80211_S_INIT */
  60. "SCAN", /* IEEE80211_S_SCAN */
  61. "AUTH", /* IEEE80211_S_AUTH */
  62. "ASSOC", /* IEEE80211_S_ASSOC */
  63. "RUN" /* IEEE80211_S_RUN */
  64. };
  65. const char * const ieee80211_phymode_name[] = {
  66. "auto", /* IEEE80211_MODE_AUTO */
  67. "11a", /* IEEE80211_MODE_11A */
  68. "11b", /* IEEE80211_MODE_11B */
  69. "11g", /* IEEE80211_MODE_11G */
  70. "turbo", /* IEEE80211_MODE_TURBO */
  71. };
  72. int ieee80211_newstate(struct ieee80211com *, enum ieee80211_state, int);
  73. void
  74. ieee80211_proto_attach(struct ifnet *ifp)
  75. {
  76. struct ieee80211com *ic = (void *)ifp;
  77. ifp->if_hdrlen = sizeof(struct ieee80211_frame);
  78. #ifdef notdef
  79. ic->ic_rtsthreshold = IEEE80211_RTS_DEFAULT;
  80. #else
  81. ic->ic_rtsthreshold = IEEE80211_RTS_MAX;
  82. #endif
  83. ic->ic_fragthreshold = 2346; /* XXX not used yet */
  84. ic->ic_fixed_rate = -1; /* no fixed rate */
  85. ic->ic_protmode = IEEE80211_PROT_CTSONLY;
  86. /* protocol state change handler */
  87. ic->ic_newstate = ieee80211_newstate;
  88. /* initialize management frame handlers */
  89. ic->ic_recv_mgmt = ieee80211_recv_mgmt;
  90. ic->ic_send_mgmt = ieee80211_send_mgmt;
  91. }
  92. void
  93. ieee80211_proto_detach(struct ifnet *ifp)
  94. {
  95. struct ieee80211com *ic = (void *)ifp;
  96. IF_PURGE(&ic->ic_mgtq);
  97. IF_PURGE(&ic->ic_pwrsaveq);
  98. }
  99. void
  100. ieee80211_print_essid(const u_int8_t *essid, int len)
  101. {
  102. int i;
  103. const u_int8_t *p;
  104. if (len > IEEE80211_NWID_LEN)
  105. len = IEEE80211_NWID_LEN;
  106. /* determine printable or not */
  107. for (i = 0, p = essid; i < len; i++, p++) {
  108. if (*p < ' ' || *p > 0x7e)
  109. break;
  110. }
  111. if (i == len) {
  112. printf("\"");
  113. for (i = 0, p = essid; i < len; i++, p++)
  114. printf("%c", *p);
  115. printf("\"");
  116. } else {
  117. printf("0x");
  118. for (i = 0, p = essid; i < len; i++, p++)
  119. printf("%02x", *p);
  120. }
  121. }
  122. #ifdef IEEE80211_DEBUG
  123. void
  124. ieee80211_dump_pkt(const u_int8_t *buf, int len, int rate, int rssi)
  125. {
  126. struct ieee80211_frame *wh;
  127. int i;
  128. wh = (struct ieee80211_frame *)buf;
  129. switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
  130. case IEEE80211_FC1_DIR_NODS:
  131. printf("NODS %s", ether_sprintf(wh->i_addr2));
  132. printf("->%s", ether_sprintf(wh->i_addr1));
  133. printf("(%s)", ether_sprintf(wh->i_addr3));
  134. break;
  135. case IEEE80211_FC1_DIR_TODS:
  136. printf("TODS %s", ether_sprintf(wh->i_addr2));
  137. printf("->%s", ether_sprintf(wh->i_addr3));
  138. printf("(%s)", ether_sprintf(wh->i_addr1));
  139. break;
  140. case IEEE80211_FC1_DIR_FROMDS:
  141. printf("FRDS %s", ether_sprintf(wh->i_addr3));
  142. printf("->%s", ether_sprintf(wh->i_addr1));
  143. printf("(%s)", ether_sprintf(wh->i_addr2));
  144. break;
  145. case IEEE80211_FC1_DIR_DSTODS:
  146. printf("DSDS %s", ether_sprintf((u_int8_t *)&wh[1]));
  147. printf("->%s", ether_sprintf(wh->i_addr3));
  148. printf("(%s", ether_sprintf(wh->i_addr2));
  149. printf("->%s)", ether_sprintf(wh->i_addr1));
  150. break;
  151. }
  152. switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
  153. case IEEE80211_FC0_TYPE_DATA:
  154. printf(" data");
  155. break;
  156. case IEEE80211_FC0_TYPE_MGT:
  157. printf(" %s", ieee80211_mgt_subtype_name[
  158. (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK)
  159. >> IEEE80211_FC0_SUBTYPE_SHIFT]);
  160. break;
  161. default:
  162. printf(" type#%d", wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK);
  163. break;
  164. }
  165. if (wh->i_fc[1] & IEEE80211_FC1_WEP)
  166. printf(" WEP");
  167. if (rate >= 0)
  168. printf(" %d%sM", rate / 2, (rate & 1) ? ".5" : "");
  169. if (rssi >= 0)
  170. printf(" +%d", rssi);
  171. printf("\n");
  172. if (len > 0) {
  173. for (i = 0; i < len; i++) {
  174. if ((i & 1) == 0)
  175. printf(" ");
  176. printf("%02x", buf[i]);
  177. }
  178. printf("\n");
  179. }
  180. }
  181. #endif
  182. int
  183. ieee80211_fix_rate(struct ieee80211com *ic, struct ieee80211_node *ni,
  184. int flags)
  185. {
  186. #define RV(v) ((v) & IEEE80211_RATE_VAL)
  187. int i, j, ignore, error;
  188. int okrate, badrate, fixedrate;
  189. const struct ieee80211_rateset *srs;
  190. struct ieee80211_rateset *nrs;
  191. u_int8_t r;
  192. /*
  193. * If the fixed rate check was requested but no fixed rate has been
  194. * defined then just remove the check.
  195. */
  196. if ((flags & IEEE80211_F_DOFRATE) && ic->ic_fixed_rate == -1)
  197. flags &= ~IEEE80211_F_DOFRATE;
  198. error = 0;
  199. okrate = badrate = fixedrate = 0;
  200. srs = &ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
  201. nrs = &ni->ni_rates;
  202. for (i = 0; i < nrs->rs_nrates; ) {
  203. ignore = 0;
  204. if (flags & IEEE80211_F_DOSORT) {
  205. /*
  206. * Sort rates.
  207. */
  208. for (j = i + 1; j < nrs->rs_nrates; j++) {
  209. if (RV(nrs->rs_rates[i]) >
  210. RV(nrs->rs_rates[j])) {
  211. r = nrs->rs_rates[i];
  212. nrs->rs_rates[i] = nrs->rs_rates[j];
  213. nrs->rs_rates[j] = r;
  214. }
  215. }
  216. }
  217. r = nrs->rs_rates[i] & IEEE80211_RATE_VAL;
  218. badrate = r;
  219. if (flags & IEEE80211_F_DOFRATE) {
  220. /*
  221. * Check fixed rate is included.
  222. */
  223. if (r == RV(srs->rs_rates[ic->ic_fixed_rate]))
  224. fixedrate = r;
  225. }
  226. if (flags & IEEE80211_F_DONEGO) {
  227. /*
  228. * Check against supported rates.
  229. */
  230. for (j = 0; j < srs->rs_nrates; j++) {
  231. if (r == RV(srs->rs_rates[j])) {
  232. /*
  233. * Overwrite with the supported rate
  234. * value so any basic rate bit is set.
  235. * This insures that response we send
  236. * to stations have the necessary basic
  237. * rate bit set.
  238. */
  239. nrs->rs_rates[i] = srs->rs_rates[j];
  240. break;
  241. }
  242. }
  243. if (j == srs->rs_nrates) {
  244. /*
  245. * A rate in the node's rate set is not
  246. * supported. If this is a basic rate and we
  247. * are operating as an AP then this is an error.
  248. * Otherwise we just discard/ignore the rate.
  249. * Note that this is important for 11b stations
  250. * when they want to associate with an 11g AP.
  251. */
  252. #ifndef IEEE80211_STA_ONLY
  253. if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
  254. (nrs->rs_rates[i] & IEEE80211_RATE_BASIC))
  255. error++;
  256. #endif
  257. ignore++;
  258. }
  259. }
  260. if (flags & IEEE80211_F_DODEL) {
  261. /*
  262. * Delete unacceptable rates.
  263. */
  264. if (ignore) {
  265. nrs->rs_nrates--;
  266. for (j = i; j < nrs->rs_nrates; j++)
  267. nrs->rs_rates[j] = nrs->rs_rates[j + 1];
  268. nrs->rs_rates[j] = 0;
  269. continue;
  270. }
  271. }
  272. if (!ignore)
  273. okrate = nrs->rs_rates[i];
  274. i++;
  275. }
  276. if (okrate == 0 || error != 0 ||
  277. ((flags & IEEE80211_F_DOFRATE) && fixedrate == 0))
  278. return badrate | IEEE80211_RATE_BASIC;
  279. else
  280. return RV(okrate);
  281. #undef RV
  282. }
  283. /*
  284. * Reset 11g-related state.
  285. */
  286. void
  287. ieee80211_reset_erp(struct ieee80211com *ic)
  288. {
  289. ic->ic_flags &= ~IEEE80211_F_USEPROT;
  290. ic->ic_nonerpsta = 0;
  291. ic->ic_longslotsta = 0;
  292. /*
  293. * Enable short slot time iff:
  294. * - we're operating in 802.11a or
  295. * - we're operating in 802.11g and we're not in IBSS mode and
  296. * the device supports short slot time
  297. */
  298. ieee80211_set_shortslottime(ic,
  299. ic->ic_curmode == IEEE80211_MODE_11A
  300. #ifndef IEEE80211_STA_ONLY
  301. ||
  302. (ic->ic_curmode == IEEE80211_MODE_11G &&
  303. ic->ic_opmode == IEEE80211_M_HOSTAP &&
  304. (ic->ic_caps & IEEE80211_C_SHSLOT))
  305. #endif
  306. );
  307. if (ic->ic_curmode == IEEE80211_MODE_11A ||
  308. (ic->ic_caps & IEEE80211_C_SHPREAMBLE))
  309. ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
  310. else
  311. ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
  312. }
  313. /*
  314. * Set the short slot time state and notify the driver.
  315. */
  316. void
  317. ieee80211_set_shortslottime(struct ieee80211com *ic, int on)
  318. {
  319. if (on)
  320. ic->ic_flags |= IEEE80211_F_SHSLOT;
  321. else
  322. ic->ic_flags &= ~IEEE80211_F_SHSLOT;
  323. /* notify the driver */
  324. if (ic->ic_updateslot != NULL)
  325. ic->ic_updateslot(ic);
  326. }
  327. /*
  328. * This function is called by the 802.1X PACP machine (via an ioctl) when
  329. * the transmit key machine (4-Way Handshake for 802.11) should run.
  330. */
  331. int
  332. ieee80211_keyrun(struct ieee80211com *ic, u_int8_t *macaddr)
  333. {
  334. #ifndef IEEE80211_STA_ONLY
  335. struct ieee80211_node *ni;
  336. struct ieee80211_pmk *pmk;
  337. #endif
  338. /* STA must be associated or AP must be ready */
  339. if (ic->ic_state != IEEE80211_S_RUN ||
  340. !(ic->ic_flags & IEEE80211_F_RSNON))
  341. return ENETDOWN;
  342. #ifndef IEEE80211_STA_ONLY
  343. if (ic->ic_opmode == IEEE80211_M_STA)
  344. #endif
  345. return 0; /* supplicant only, do nothing */
  346. #ifndef IEEE80211_STA_ONLY
  347. /* find the STA with which we must start the key exchange */
  348. if ((ni = ieee80211_find_node(ic, macaddr)) == NULL) {
  349. DPRINTF(("no node found for %s\n", ether_sprintf(macaddr)));
  350. return EINVAL;
  351. }
  352. /* check that the STA is in the correct state */
  353. if (ni->ni_state != IEEE80211_STA_ASSOC ||
  354. ni->ni_rsn_state != RSNA_AUTHENTICATION_2) {
  355. DPRINTF(("unexpected in state %d\n", ni->ni_rsn_state));
  356. return EINVAL;
  357. }
  358. ni->ni_rsn_state = RSNA_INITPMK;
  359. /* make sure a PMK is available for this STA, otherwise deauth it */
  360. if ((pmk = ieee80211_pmksa_find(ic, ni, NULL)) == NULL) {
  361. DPRINTF(("no PMK available for %s\n", ether_sprintf(macaddr)));
  362. IEEE80211_SEND_MGMT(ic, ni, IEEE80211_FC0_SUBTYPE_DEAUTH,
  363. IEEE80211_REASON_AUTH_LEAVE);
  364. ieee80211_node_leave(ic, ni);
  365. return EINVAL;
  366. }
  367. memcpy(ni->ni_pmk, pmk->pmk_key, IEEE80211_PMK_LEN);
  368. memcpy(ni->ni_pmkid, pmk->pmk_pmkid, IEEE80211_PMKID_LEN);
  369. ni->ni_flags |= IEEE80211_NODE_PMK;
  370. /* initiate key exchange (4-Way Handshake) with STA */
  371. return ieee80211_send_4way_msg1(ic, ni);
  372. #endif /* IEEE80211_STA_ONLY */
  373. }
  374. #ifndef IEEE80211_STA_ONLY
  375. /*
  376. * Initiate a group key handshake with a node.
  377. */
  378. static void
  379. ieee80211_node_gtk_rekey(void *arg, struct ieee80211_node *ni)
  380. {
  381. struct ieee80211com *ic = arg;
  382. if (ni->ni_state != IEEE80211_STA_ASSOC ||
  383. ni->ni_rsn_gstate != RSNA_IDLE)
  384. return;
  385. /* initiate a group key handshake with STA */
  386. ni->ni_flags |= IEEE80211_NODE_REKEY;
  387. if (ieee80211_send_group_msg1(ic, ni) != 0)
  388. ni->ni_flags &= ~IEEE80211_NODE_REKEY;
  389. else
  390. ic->ic_rsn_keydonesta++;
  391. }
  392. /*
  393. * This function is called in HostAP mode when the group key needs to be
  394. * changed.
  395. */
  396. void
  397. ieee80211_setkeys(struct ieee80211com *ic)
  398. {
  399. struct ieee80211_key *k;
  400. u_int8_t kid;
  401. /* Swap(GM, GN) */
  402. kid = (ic->ic_def_txkey == 1) ? 2 : 1;
  403. k = &ic->ic_nw_keys[kid];
  404. memset(k, 0, sizeof(*k));
  405. k->k_id = kid;
  406. k->k_cipher = ic->ic_bss->ni_rsngroupcipher;
  407. k->k_flags = IEEE80211_KEY_GROUP | IEEE80211_KEY_TX;
  408. k->k_len = ieee80211_cipher_keylen(k->k_cipher);
  409. arc4random_buf(k->k_key, k->k_len);
  410. if (ic->ic_caps & IEEE80211_C_MFP) {
  411. /* Swap(GM_igtk, GN_igtk) */
  412. kid = (ic->ic_igtk_kid == 4) ? 5 : 4;
  413. k = &ic->ic_nw_keys[kid];
  414. memset(k, 0, sizeof(*k));
  415. k->k_id = kid;
  416. k->k_cipher = ic->ic_bss->ni_rsngroupmgmtcipher;
  417. k->k_flags = IEEE80211_KEY_IGTK | IEEE80211_KEY_TX;
  418. k->k_len = 16;
  419. arc4random_buf(k->k_key, k->k_len);
  420. }
  421. ic->ic_rsn_keydonesta = 0;
  422. ieee80211_iterate_nodes(ic, ieee80211_node_gtk_rekey, ic);
  423. }
  424. /*
  425. * The group key handshake has been completed with all associated stations.
  426. */
  427. void
  428. ieee80211_setkeysdone(struct ieee80211com *ic)
  429. {
  430. u_int8_t kid;
  431. /* install GTK */
  432. kid = (ic->ic_def_txkey == 1) ? 2 : 1;
  433. if ((*ic->ic_set_key)(ic, ic->ic_bss, &ic->ic_nw_keys[kid]) == 0)
  434. ic->ic_def_txkey = kid;
  435. if (ic->ic_caps & IEEE80211_C_MFP) {
  436. /* install IGTK */
  437. kid = (ic->ic_igtk_kid == 4) ? 5 : 4;
  438. if ((*ic->ic_set_key)(ic, ic->ic_bss,
  439. &ic->ic_nw_keys[kid]) == 0)
  440. ic->ic_igtk_kid = kid;
  441. }
  442. }
  443. /*
  444. * Group key lifetime has expired, update it.
  445. */
  446. void
  447. ieee80211_gtk_rekey_timeout(void *arg)
  448. {
  449. struct ieee80211com *ic = arg;
  450. int s;
  451. s = splnet();
  452. ieee80211_setkeys(ic);
  453. splx(s);
  454. /* re-schedule a GTK rekeying after 3600s */
  455. timeout_add_sec(&ic->ic_rsn_timeout, 3600);
  456. }
  457. void
  458. ieee80211_sa_query_timeout(void *arg)
  459. {
  460. struct ieee80211_node *ni = arg;
  461. struct ieee80211com *ic = ni->ni_ic;
  462. int s;
  463. s = splnet();
  464. if (++ni->ni_sa_query_count >= 3) {
  465. ni->ni_flags &= ~IEEE80211_NODE_SA_QUERY;
  466. ni->ni_flags |= IEEE80211_NODE_SA_QUERY_FAILED;
  467. } else /* retry SA Query Request */
  468. ieee80211_sa_query_request(ic, ni);
  469. splx(s);
  470. }
  471. /*
  472. * Request that a SA Query Request frame be sent to a specified peer STA
  473. * to which the STA is associated.
  474. */
  475. void
  476. ieee80211_sa_query_request(struct ieee80211com *ic, struct ieee80211_node *ni)
  477. {
  478. /* MLME-SAQuery.request */
  479. if (!(ni->ni_flags & IEEE80211_NODE_SA_QUERY)) {
  480. ni->ni_flags |= IEEE80211_NODE_SA_QUERY;
  481. ni->ni_flags &= ~IEEE80211_NODE_SA_QUERY_FAILED;
  482. ni->ni_sa_query_count = 0;
  483. }
  484. /* generate new Transaction Identifier */
  485. ni->ni_sa_query_trid++;
  486. /* send SA Query Request */
  487. IEEE80211_SEND_ACTION(ic, ni, IEEE80211_CATEG_SA_QUERY,
  488. IEEE80211_ACTION_SA_QUERY_REQ, 0);
  489. timeout_add_msec(&ni->ni_sa_query_to, 10);
  490. }
  491. #endif /* IEEE80211_STA_ONLY */
  492. #ifndef IEEE80211_NO_HT
  493. void
  494. ieee80211_tx_ba_timeout(void *arg)
  495. {
  496. struct ieee80211_tx_ba *ba = arg;
  497. struct ieee80211_node *ni = ba->ba_ni;
  498. struct ieee80211com *ic = ni->ni_ic;
  499. u_int8_t tid;
  500. int s;
  501. s = splnet();
  502. if (ba->ba_state == IEEE80211_BA_REQUESTED) {
  503. /* MLME-ADDBA.confirm(TIMEOUT) */
  504. ba->ba_state = IEEE80211_BA_INIT;
  505. } else if (ba->ba_state == IEEE80211_BA_AGREED) {
  506. /* Block Ack inactivity timeout */
  507. tid = ((caddr_t)ba - (caddr_t)ni->ni_tx_ba) / sizeof(*ba);
  508. ieee80211_delba_request(ic, ni, IEEE80211_REASON_TIMEOUT,
  509. 1, tid);
  510. }
  511. splx(s);
  512. }
  513. void
  514. ieee80211_rx_ba_timeout(void *arg)
  515. {
  516. struct ieee80211_rx_ba *ba = arg;
  517. struct ieee80211_node *ni = ba->ba_ni;
  518. struct ieee80211com *ic = ni->ni_ic;
  519. u_int8_t tid;
  520. int s;
  521. s = splnet();
  522. /* Block Ack inactivity timeout */
  523. tid = ((caddr_t)ba - (caddr_t)ni->ni_rx_ba) / sizeof(*ba);
  524. ieee80211_delba_request(ic, ni, IEEE80211_REASON_TIMEOUT, 0, tid);
  525. splx(s);
  526. }
  527. /*
  528. * Request initiation of Block Ack with the specified peer.
  529. */
  530. int
  531. ieee80211_addba_request(struct ieee80211com *ic, struct ieee80211_node *ni,
  532. u_int16_t ssn, u_int8_t tid)
  533. {
  534. struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid];
  535. /* MLME-ADDBA.request */
  536. /* setup Block Ack */
  537. ba->ba_state = IEEE80211_BA_REQUESTED;
  538. ba->ba_token = ic->ic_dialog_token++;
  539. ba->ba_timeout_val = IEEE80211_BA_MAX_TIMEOUT;
  540. timeout_set(&ba->ba_to, ieee80211_tx_ba_timeout, ba);
  541. ba->ba_winsize = IEEE80211_BA_MAX_WINSZ;
  542. ba->ba_winstart = ssn;
  543. ba->ba_winend = (ba->ba_winstart + ba->ba_winsize - 1) & 0xfff;
  544. timeout_add_sec(&ba->ba_to, 1); /* dot11ADDBAResponseTimeout */
  545. IEEE80211_SEND_ACTION(ic, ni, IEEE80211_CATEG_BA,
  546. IEEE80211_ACTION_ADDBA_REQ, tid);
  547. return 0;
  548. }
  549. /*
  550. * Request the deletion of Block Ack with a peer.
  551. */
  552. void
  553. ieee80211_delba_request(struct ieee80211com *ic, struct ieee80211_node *ni,
  554. u_int16_t reason, u_int8_t dir, u_int8_t tid)
  555. {
  556. /* MLME-DELBA.request */
  557. /* transmit a DELBA frame */
  558. IEEE80211_SEND_ACTION(ic, ni, IEEE80211_CATEG_BA,
  559. IEEE80211_ACTION_DELBA, reason << 16 | dir << 8 | tid);
  560. if (dir) {
  561. /* MLME-DELBA.confirm(Originator) */
  562. struct ieee80211_tx_ba *ba = &ni->ni_tx_ba[tid];
  563. if (ic->ic_ampdu_tx_stop != NULL)
  564. ic->ic_ampdu_tx_stop(ic, ni, tid);
  565. ba->ba_state = IEEE80211_BA_INIT;
  566. /* stop Block Ack inactivity timer */
  567. timeout_del(&ba->ba_to);
  568. } else {
  569. /* MLME-DELBA.confirm(Recipient) */
  570. struct ieee80211_rx_ba *ba = &ni->ni_rx_ba[tid];
  571. int i;
  572. if (ic->ic_ampdu_rx_stop != NULL)
  573. ic->ic_ampdu_rx_stop(ic, ni, tid);
  574. ba->ba_state = IEEE80211_BA_INIT;
  575. /* stop Block Ack inactivity timer */
  576. timeout_del(&ba->ba_to);
  577. if (ba->ba_buf != NULL) {
  578. /* free all MSDUs stored in reordering buffer */
  579. for (i = 0; i < IEEE80211_BA_MAX_WINSZ; i++)
  580. m_freem(ba->ba_buf[i].m);
  581. /* free reordering buffer */
  582. free(ba->ba_buf, M_DEVBUF, 0);
  583. ba->ba_buf = NULL;
  584. }
  585. }
  586. }
  587. #endif /* !IEEE80211_NO_HT */
  588. void
  589. ieee80211_auth_open(struct ieee80211com *ic, const struct ieee80211_frame *wh,
  590. struct ieee80211_node *ni, struct ieee80211_rxinfo *rxi, u_int16_t seq,
  591. u_int16_t status)
  592. {
  593. struct ifnet *ifp = &ic->ic_if;
  594. switch (ic->ic_opmode) {
  595. #ifndef IEEE80211_STA_ONLY
  596. case IEEE80211_M_IBSS:
  597. if (ic->ic_state != IEEE80211_S_RUN ||
  598. seq != IEEE80211_AUTH_OPEN_REQUEST) {
  599. DPRINTF(("discard auth from %s; state %u, seq %u\n",
  600. ether_sprintf((u_int8_t *)wh->i_addr2),
  601. ic->ic_state, seq));
  602. ic->ic_stats.is_rx_bad_auth++;
  603. return;
  604. }
  605. ieee80211_new_state(ic, IEEE80211_S_AUTH,
  606. wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
  607. break;
  608. case IEEE80211_M_AHDEMO:
  609. /* should not come here */
  610. break;
  611. case IEEE80211_M_HOSTAP:
  612. if (ic->ic_state != IEEE80211_S_RUN ||
  613. seq != IEEE80211_AUTH_OPEN_REQUEST) {
  614. DPRINTF(("discard auth from %s; state %u, seq %u\n",
  615. ether_sprintf((u_int8_t *)wh->i_addr2),
  616. ic->ic_state, seq));
  617. ic->ic_stats.is_rx_bad_auth++;
  618. return;
  619. }
  620. if (ni == ic->ic_bss) {
  621. ni = ieee80211_find_node(ic, wh->i_addr2);
  622. if (ni == NULL)
  623. ni = ieee80211_alloc_node(ic, wh->i_addr2);
  624. if (ni == NULL) {
  625. return;
  626. }
  627. IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
  628. ni->ni_rssi = rxi->rxi_rssi;
  629. ni->ni_rstamp = rxi->rxi_tstamp;
  630. ni->ni_chan = ic->ic_bss->ni_chan;
  631. }
  632. IEEE80211_SEND_MGMT(ic, ni,
  633. IEEE80211_FC0_SUBTYPE_AUTH, seq + 1);
  634. if (ifp->if_flags & IFF_DEBUG)
  635. printf("%s: station %s %s authenticated (open)\n",
  636. ifp->if_xname,
  637. ether_sprintf((u_int8_t *)ni->ni_macaddr),
  638. ni->ni_state != IEEE80211_STA_CACHE ?
  639. "newly" : "already");
  640. ieee80211_node_newstate(ni, IEEE80211_STA_AUTH);
  641. break;
  642. #endif /* IEEE80211_STA_ONLY */
  643. case IEEE80211_M_STA:
  644. if (ic->ic_state != IEEE80211_S_AUTH ||
  645. seq != IEEE80211_AUTH_OPEN_RESPONSE) {
  646. ic->ic_stats.is_rx_bad_auth++;
  647. DPRINTF(("discard auth from %s; state %u, seq %u\n",
  648. ether_sprintf((u_int8_t *)wh->i_addr2),
  649. ic->ic_state, seq));
  650. return;
  651. }
  652. if (ic->ic_flags & IEEE80211_F_RSNON) {
  653. /* XXX not here! */
  654. ic->ic_bss->ni_flags &= ~IEEE80211_NODE_TXRXPROT;
  655. ic->ic_bss->ni_port_valid = 0;
  656. ic->ic_bss->ni_replaycnt_ok = 0;
  657. (*ic->ic_delete_key)(ic, ic->ic_bss,
  658. &ic->ic_bss->ni_pairwise_key);
  659. }
  660. if (status != 0) {
  661. if (ifp->if_flags & IFF_DEBUG)
  662. printf("%s: open authentication failed "
  663. "(reason %d) for %s\n", ifp->if_xname,
  664. status,
  665. ether_sprintf((u_int8_t *)wh->i_addr3));
  666. if (ni != ic->ic_bss)
  667. ni->ni_fails++;
  668. ic->ic_stats.is_rx_auth_fail++;
  669. return;
  670. }
  671. ieee80211_new_state(ic, IEEE80211_S_ASSOC,
  672. wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK);
  673. break;
  674. default:
  675. break;
  676. }
  677. }
  678. int
  679. ieee80211_newstate(struct ieee80211com *ic, enum ieee80211_state nstate,
  680. int mgt)
  681. {
  682. struct ifnet *ifp = &ic->ic_if;
  683. struct ieee80211_node *ni;
  684. enum ieee80211_state ostate;
  685. u_int rate;
  686. #ifndef IEEE80211_STA_ONLY
  687. int s;
  688. #endif
  689. ostate = ic->ic_state;
  690. DPRINTF(("%s -> %s\n", ieee80211_state_name[ostate],
  691. ieee80211_state_name[nstate]));
  692. ic->ic_state = nstate; /* state transition */
  693. ni = ic->ic_bss; /* NB: no reference held */
  694. if (ostate == IEEE80211_S_RUN)
  695. ieee80211_set_link_state(ic, LINK_STATE_DOWN);
  696. switch (nstate) {
  697. case IEEE80211_S_INIT:
  698. /*
  699. * If mgt = -1, driver is already partway down, so do
  700. * not send management frames.
  701. */
  702. switch (ostate) {
  703. case IEEE80211_S_INIT:
  704. break;
  705. case IEEE80211_S_RUN:
  706. if (mgt == -1)
  707. goto justcleanup;
  708. switch (ic->ic_opmode) {
  709. case IEEE80211_M_STA:
  710. IEEE80211_SEND_MGMT(ic, ni,
  711. IEEE80211_FC0_SUBTYPE_DISASSOC,
  712. IEEE80211_REASON_ASSOC_LEAVE);
  713. break;
  714. #ifndef IEEE80211_STA_ONLY
  715. case IEEE80211_M_HOSTAP:
  716. s = splnet();
  717. RB_FOREACH(ni, ieee80211_tree, &ic->ic_tree) {
  718. if (ni->ni_associd == 0)
  719. continue;
  720. IEEE80211_SEND_MGMT(ic, ni,
  721. IEEE80211_FC0_SUBTYPE_DISASSOC,
  722. IEEE80211_REASON_ASSOC_LEAVE);
  723. }
  724. splx(s);
  725. break;
  726. #endif
  727. default:
  728. break;
  729. }
  730. /* FALLTHROUGH */
  731. case IEEE80211_S_ASSOC:
  732. if (mgt == -1)
  733. goto justcleanup;
  734. switch (ic->ic_opmode) {
  735. case IEEE80211_M_STA:
  736. IEEE80211_SEND_MGMT(ic, ni,
  737. IEEE80211_FC0_SUBTYPE_DEAUTH,
  738. IEEE80211_REASON_AUTH_LEAVE);
  739. break;
  740. #ifndef IEEE80211_STA_ONLY
  741. case IEEE80211_M_HOSTAP:
  742. s = splnet();
  743. RB_FOREACH(ni, ieee80211_tree, &ic->ic_tree) {
  744. IEEE80211_SEND_MGMT(ic, ni,
  745. IEEE80211_FC0_SUBTYPE_DEAUTH,
  746. IEEE80211_REASON_AUTH_LEAVE);
  747. }
  748. splx(s);
  749. break;
  750. #endif
  751. default:
  752. break;
  753. }
  754. /* FALLTHROUGH */
  755. case IEEE80211_S_AUTH:
  756. case IEEE80211_S_SCAN:
  757. justcleanup:
  758. #ifndef IEEE80211_STA_ONLY
  759. if (ic->ic_opmode == IEEE80211_M_HOSTAP)
  760. timeout_del(&ic->ic_rsn_timeout);
  761. #endif
  762. ic->ic_mgt_timer = 0;
  763. IF_PURGE(&ic->ic_mgtq);
  764. IF_PURGE(&ic->ic_pwrsaveq);
  765. ieee80211_free_allnodes(ic);
  766. break;
  767. }
  768. break;
  769. case IEEE80211_S_SCAN:
  770. ic->ic_flags &= ~IEEE80211_F_SIBSS;
  771. /* initialize bss for probe request */
  772. IEEE80211_ADDR_COPY(ni->ni_macaddr, etherbroadcastaddr);
  773. IEEE80211_ADDR_COPY(ni->ni_bssid, etherbroadcastaddr);
  774. ni->ni_rates = ic->ic_sup_rates[
  775. ieee80211_chan2mode(ic, ni->ni_chan)];
  776. ni->ni_associd = 0;
  777. ni->ni_rstamp = 0;
  778. switch (ostate) {
  779. case IEEE80211_S_INIT:
  780. #ifndef IEEE80211_STA_ONLY
  781. if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
  782. ic->ic_des_chan != IEEE80211_CHAN_ANYC) {
  783. /*
  784. * AP operation and we already have a channel;
  785. * bypass the scan and startup immediately.
  786. */
  787. ieee80211_create_ibss(ic, ic->ic_des_chan);
  788. } else
  789. #endif
  790. ieee80211_begin_scan(ifp);
  791. break;
  792. case IEEE80211_S_SCAN:
  793. /* scan next */
  794. if (ic->ic_flags & IEEE80211_F_ASCAN) {
  795. IEEE80211_SEND_MGMT(ic, ni,
  796. IEEE80211_FC0_SUBTYPE_PROBE_REQ, 0);
  797. }
  798. break;
  799. case IEEE80211_S_RUN:
  800. /* beacon miss */
  801. if (ifp->if_flags & IFF_DEBUG) {
  802. /* XXX bssid clobbered above */
  803. printf("%s: no recent beacons from %s;"
  804. " rescanning\n", ifp->if_xname,
  805. ether_sprintf(ic->ic_bss->ni_bssid));
  806. }
  807. ieee80211_free_allnodes(ic);
  808. /* FALLTHROUGH */
  809. case IEEE80211_S_AUTH:
  810. case IEEE80211_S_ASSOC:
  811. /* timeout restart scan */
  812. ni = ieee80211_find_node(ic, ic->ic_bss->ni_macaddr);
  813. if (ni != NULL)
  814. ni->ni_fails++;
  815. ieee80211_begin_scan(ifp);
  816. break;
  817. }
  818. break;
  819. case IEEE80211_S_AUTH:
  820. switch (ostate) {
  821. case IEEE80211_S_INIT:
  822. DPRINTF(("invalid transition\n"));
  823. break;
  824. case IEEE80211_S_SCAN:
  825. IEEE80211_SEND_MGMT(ic, ni,
  826. IEEE80211_FC0_SUBTYPE_AUTH, 1);
  827. break;
  828. case IEEE80211_S_AUTH:
  829. case IEEE80211_S_ASSOC:
  830. switch (mgt) {
  831. case IEEE80211_FC0_SUBTYPE_AUTH:
  832. /* ??? */
  833. IEEE80211_SEND_MGMT(ic, ni,
  834. IEEE80211_FC0_SUBTYPE_AUTH, 2);
  835. break;
  836. case IEEE80211_FC0_SUBTYPE_DEAUTH:
  837. /* ignore and retry scan on timeout */
  838. break;
  839. }
  840. break;
  841. case IEEE80211_S_RUN:
  842. switch (mgt) {
  843. case IEEE80211_FC0_SUBTYPE_AUTH:
  844. IEEE80211_SEND_MGMT(ic, ni,
  845. IEEE80211_FC0_SUBTYPE_AUTH, 2);
  846. ic->ic_state = ostate; /* stay RUN */
  847. break;
  848. case IEEE80211_FC0_SUBTYPE_DEAUTH:
  849. /* try to reauth */
  850. IEEE80211_SEND_MGMT(ic, ni,
  851. IEEE80211_FC0_SUBTYPE_AUTH, 1);
  852. break;
  853. }
  854. break;
  855. }
  856. break;
  857. case IEEE80211_S_ASSOC:
  858. switch (ostate) {
  859. case IEEE80211_S_INIT:
  860. case IEEE80211_S_SCAN:
  861. case IEEE80211_S_ASSOC:
  862. DPRINTF(("invalid transition\n"));
  863. break;
  864. case IEEE80211_S_AUTH:
  865. IEEE80211_SEND_MGMT(ic, ni,
  866. IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 0);
  867. break;
  868. case IEEE80211_S_RUN:
  869. IEEE80211_SEND_MGMT(ic, ni,
  870. IEEE80211_FC0_SUBTYPE_ASSOC_REQ, 1);
  871. break;
  872. }
  873. break;
  874. case IEEE80211_S_RUN:
  875. switch (ostate) {
  876. case IEEE80211_S_INIT:
  877. case IEEE80211_S_AUTH:
  878. case IEEE80211_S_RUN:
  879. DPRINTF(("invalid transition\n"));
  880. break;
  881. case IEEE80211_S_SCAN: /* adhoc/hostap mode */
  882. case IEEE80211_S_ASSOC: /* infra mode */
  883. if (ni->ni_txrate >= ni->ni_rates.rs_nrates)
  884. panic("%s: bogus xmit rate %u setup",
  885. __func__, ni->ni_txrate);
  886. if (ifp->if_flags & IFF_DEBUG) {
  887. printf("%s: %s with %s ssid ",
  888. ifp->if_xname,
  889. ic->ic_opmode == IEEE80211_M_STA ?
  890. "associated" : "synchronized",
  891. ether_sprintf(ni->ni_bssid));
  892. ieee80211_print_essid(ic->ic_bss->ni_essid,
  893. ni->ni_esslen);
  894. rate = ni->ni_rates.rs_rates[ni->ni_txrate] &
  895. IEEE80211_RATE_VAL;
  896. printf(" channel %d start %u%sMb",
  897. ieee80211_chan2ieee(ic, ni->ni_chan),
  898. rate / 2, (rate & 1) ? ".5" : "");
  899. printf(" %s preamble %s slot time%s\n",
  900. (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ?
  901. "short" : "long",
  902. (ic->ic_flags & IEEE80211_F_SHSLOT) ?
  903. "short" : "long",
  904. (ic->ic_flags & IEEE80211_F_USEPROT) ?
  905. " protection enabled" : "");
  906. }
  907. if (!(ic->ic_flags & IEEE80211_F_RSNON)) {
  908. /*
  909. * NB: When RSN is enabled, we defer setting
  910. * the link up until the port is valid.
  911. */
  912. ieee80211_set_link_state(ic, LINK_STATE_UP);
  913. }
  914. ic->ic_mgt_timer = 0;
  915. (*ifp->if_start)(ifp);
  916. break;
  917. }
  918. break;
  919. }
  920. return 0;
  921. }
  922. void
  923. ieee80211_set_link_state(struct ieee80211com *ic, int nstate)
  924. {
  925. struct ifnet *ifp = &ic->ic_if;
  926. switch (ic->ic_opmode) {
  927. #ifndef IEEE80211_STA_ONLY
  928. case IEEE80211_M_IBSS:
  929. case IEEE80211_M_HOSTAP:
  930. nstate = LINK_STATE_UNKNOWN;
  931. break;
  932. #endif
  933. case IEEE80211_M_MONITOR:
  934. nstate = LINK_STATE_DOWN;
  935. break;
  936. default:
  937. break;
  938. }
  939. if (nstate != ifp->if_link_state) {
  940. ifp->if_link_state = nstate;
  941. if_link_state_change(ifp);
  942. }
  943. }