ieee80211_node.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  1. /* $OpenBSD: ieee80211_node.c,v 1.88 2015/07/15 22:16:42 deraadt Exp $ */
  2. /* $NetBSD: ieee80211_node.c,v 1.14 2004/05/09 09:18:47 dyoung Exp $ */
  3. /*-
  4. * Copyright (c) 2001 Atsushi Onoe
  5. * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
  6. * Copyright (c) 2008 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. #include "bridge.h"
  32. #include <sys/param.h>
  33. #include <sys/systm.h>
  34. #include <sys/mbuf.h>
  35. #include <sys/malloc.h>
  36. #include <sys/kernel.h>
  37. #include <sys/socket.h>
  38. #include <sys/sockio.h>
  39. #include <sys/endian.h>
  40. #include <sys/errno.h>
  41. #include <sys/sysctl.h>
  42. #include <sys/tree.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 <netinet/in.h>
  48. #include <netinet/if_ether.h>
  49. #if NBRIDGE > 0
  50. #include <net/if_bridge.h>
  51. #endif
  52. #include <net80211/ieee80211_var.h>
  53. #include <net80211/ieee80211_priv.h>
  54. struct ieee80211_node *ieee80211_node_alloc(struct ieee80211com *);
  55. void ieee80211_node_free(struct ieee80211com *, struct ieee80211_node *);
  56. void ieee80211_node_copy(struct ieee80211com *, struct ieee80211_node *,
  57. const struct ieee80211_node *);
  58. void ieee80211_choose_rsnparams(struct ieee80211com *);
  59. u_int8_t ieee80211_node_getrssi(struct ieee80211com *,
  60. const struct ieee80211_node *);
  61. void ieee80211_setup_node(struct ieee80211com *, struct ieee80211_node *,
  62. const u_int8_t *);
  63. void ieee80211_free_node(struct ieee80211com *, struct ieee80211_node *);
  64. struct ieee80211_node *ieee80211_alloc_node_helper(struct ieee80211com *);
  65. void ieee80211_node_cleanup(struct ieee80211com *, struct ieee80211_node *);
  66. void ieee80211_needs_auth(struct ieee80211com *, struct ieee80211_node *);
  67. #ifndef IEEE80211_STA_ONLY
  68. #ifndef IEEE80211_NO_HT
  69. void ieee80211_node_join_ht(struct ieee80211com *, struct ieee80211_node *);
  70. #endif
  71. void ieee80211_node_join_rsn(struct ieee80211com *, struct ieee80211_node *);
  72. void ieee80211_node_join_11g(struct ieee80211com *, struct ieee80211_node *);
  73. #ifndef IEEE80211_NO_HT
  74. void ieee80211_node_leave_ht(struct ieee80211com *, struct ieee80211_node *);
  75. #endif
  76. void ieee80211_node_leave_rsn(struct ieee80211com *, struct ieee80211_node *);
  77. void ieee80211_node_leave_11g(struct ieee80211com *, struct ieee80211_node *);
  78. void ieee80211_inact_timeout(void *);
  79. void ieee80211_node_cache_timeout(void *);
  80. #endif
  81. #ifndef IEEE80211_STA_ONLY
  82. void
  83. ieee80211_inact_timeout(void *arg)
  84. {
  85. struct ieee80211com *ic = arg;
  86. struct ieee80211_node *ni, *next_ni;
  87. int s;
  88. s = splnet();
  89. for (ni = RB_MIN(ieee80211_tree, &ic->ic_tree);
  90. ni != NULL; ni = next_ni) {
  91. next_ni = RB_NEXT(ieee80211_tree, &ic->ic_tree, ni);
  92. if (ni->ni_refcnt > 0)
  93. continue;
  94. if (ni->ni_inact < IEEE80211_INACT_MAX)
  95. ni->ni_inact++;
  96. }
  97. splx(s);
  98. timeout_add_sec(&ic->ic_inact_timeout, IEEE80211_INACT_WAIT);
  99. }
  100. void
  101. ieee80211_node_cache_timeout(void *arg)
  102. {
  103. struct ieee80211com *ic = arg;
  104. ieee80211_clean_nodes(ic, 1);
  105. timeout_add_sec(&ic->ic_node_cache_timeout, IEEE80211_CACHE_WAIT);
  106. }
  107. #endif
  108. void
  109. ieee80211_node_attach(struct ifnet *ifp)
  110. {
  111. struct ieee80211com *ic = (void *)ifp;
  112. #ifndef IEEE80211_STA_ONLY
  113. int size;
  114. #endif
  115. RB_INIT(&ic->ic_tree);
  116. ic->ic_node_alloc = ieee80211_node_alloc;
  117. ic->ic_node_free = ieee80211_node_free;
  118. ic->ic_node_copy = ieee80211_node_copy;
  119. ic->ic_node_getrssi = ieee80211_node_getrssi;
  120. ic->ic_scangen = 1;
  121. ic->ic_max_nnodes = ieee80211_cache_size;
  122. if (ic->ic_max_aid == 0)
  123. ic->ic_max_aid = IEEE80211_AID_DEF;
  124. else if (ic->ic_max_aid > IEEE80211_AID_MAX)
  125. ic->ic_max_aid = IEEE80211_AID_MAX;
  126. #ifndef IEEE80211_STA_ONLY
  127. size = howmany(ic->ic_max_aid, 32) * sizeof(u_int32_t);
  128. ic->ic_aid_bitmap = malloc(size, M_DEVBUF, M_NOWAIT | M_ZERO);
  129. if (ic->ic_aid_bitmap == NULL) {
  130. /* XXX no way to recover */
  131. printf("%s: no memory for AID bitmap!\n", __func__);
  132. ic->ic_max_aid = 0;
  133. }
  134. if (ic->ic_caps & (IEEE80211_C_HOSTAP | IEEE80211_C_IBSS)) {
  135. ic->ic_tim_len = howmany(ic->ic_max_aid, 8);
  136. ic->ic_tim_bitmap = malloc(ic->ic_tim_len, M_DEVBUF,
  137. M_NOWAIT | M_ZERO);
  138. if (ic->ic_tim_bitmap == NULL) {
  139. printf("%s: no memory for TIM bitmap!\n", __func__);
  140. ic->ic_tim_len = 0;
  141. } else
  142. ic->ic_set_tim = ieee80211_set_tim;
  143. timeout_set(&ic->ic_rsn_timeout,
  144. ieee80211_gtk_rekey_timeout, ic);
  145. timeout_set(&ic->ic_inact_timeout,
  146. ieee80211_inact_timeout, ic);
  147. timeout_set(&ic->ic_node_cache_timeout,
  148. ieee80211_node_cache_timeout, ic);
  149. }
  150. #endif
  151. }
  152. struct ieee80211_node *
  153. ieee80211_alloc_node_helper(struct ieee80211com *ic)
  154. {
  155. struct ieee80211_node *ni;
  156. if (ic->ic_nnodes >= ic->ic_max_nnodes)
  157. ieee80211_clean_nodes(ic, 0);
  158. if (ic->ic_nnodes >= ic->ic_max_nnodes)
  159. return NULL;
  160. ni = (*ic->ic_node_alloc)(ic);
  161. return ni;
  162. }
  163. void
  164. ieee80211_node_lateattach(struct ifnet *ifp)
  165. {
  166. struct ieee80211com *ic = (void *)ifp;
  167. struct ieee80211_node *ni;
  168. ni = ieee80211_alloc_node_helper(ic);
  169. if (ni == NULL)
  170. panic("unable to setup inital BSS node");
  171. ni->ni_chan = IEEE80211_CHAN_ANYC;
  172. ic->ic_bss = ieee80211_ref_node(ni);
  173. ic->ic_txpower = IEEE80211_TXPOWER_MAX;
  174. #ifndef IEEE80211_STA_ONLY
  175. IFQ_SET_MAXLEN(&ni->ni_savedq, IEEE80211_PS_MAX_QUEUE);
  176. #endif
  177. }
  178. void
  179. ieee80211_node_detach(struct ifnet *ifp)
  180. {
  181. struct ieee80211com *ic = (void *)ifp;
  182. if (ic->ic_bss != NULL) {
  183. (*ic->ic_node_free)(ic, ic->ic_bss);
  184. ic->ic_bss = NULL;
  185. }
  186. ieee80211_free_allnodes(ic);
  187. #ifndef IEEE80211_STA_ONLY
  188. if (ic->ic_aid_bitmap != NULL)
  189. free(ic->ic_aid_bitmap, M_DEVBUF, 0);
  190. if (ic->ic_tim_bitmap != NULL)
  191. free(ic->ic_tim_bitmap, M_DEVBUF, 0);
  192. timeout_del(&ic->ic_inact_timeout);
  193. timeout_del(&ic->ic_node_cache_timeout);
  194. #endif
  195. timeout_del(&ic->ic_rsn_timeout);
  196. }
  197. /*
  198. * AP scanning support.
  199. */
  200. /*
  201. * Initialize the active channel set based on the set
  202. * of available channels and the current PHY mode.
  203. */
  204. void
  205. ieee80211_reset_scan(struct ifnet *ifp)
  206. {
  207. struct ieee80211com *ic = (void *)ifp;
  208. memcpy(ic->ic_chan_scan, ic->ic_chan_active,
  209. sizeof(ic->ic_chan_active));
  210. /* NB: hack, setup so next_scan starts with the first channel */
  211. if (ic->ic_bss != NULL && ic->ic_bss->ni_chan == IEEE80211_CHAN_ANYC)
  212. ic->ic_bss->ni_chan = &ic->ic_channels[IEEE80211_CHAN_MAX];
  213. }
  214. /*
  215. * Begin an active scan.
  216. */
  217. void
  218. ieee80211_begin_scan(struct ifnet *ifp)
  219. {
  220. struct ieee80211com *ic = (void *)ifp;
  221. if (ic->ic_scan_lock & IEEE80211_SCAN_LOCKED)
  222. return;
  223. ic->ic_scan_lock |= IEEE80211_SCAN_LOCKED;
  224. /*
  225. * In all but hostap mode scanning starts off in
  226. * an active mode before switching to passive.
  227. */
  228. #ifndef IEEE80211_STA_ONLY
  229. if (ic->ic_opmode != IEEE80211_M_HOSTAP)
  230. #endif
  231. {
  232. ic->ic_flags |= IEEE80211_F_ASCAN;
  233. ic->ic_stats.is_scan_active++;
  234. }
  235. #ifndef IEEE80211_STA_ONLY
  236. else
  237. ic->ic_stats.is_scan_passive++;
  238. #endif
  239. if (ifp->if_flags & IFF_DEBUG)
  240. printf("%s: begin %s scan\n", ifp->if_xname,
  241. (ic->ic_flags & IEEE80211_F_ASCAN) ?
  242. "active" : "passive");
  243. /*
  244. * Flush any previously seen AP's. Note that the latter
  245. * assumes we don't act as both an AP and a station,
  246. * otherwise we'll potentially flush state of stations
  247. * associated with us.
  248. */
  249. ieee80211_free_allnodes(ic);
  250. /*
  251. * Reset the current mode. Setting the current mode will also
  252. * reset scan state.
  253. */
  254. if (IFM_MODE(ic->ic_media.ifm_cur->ifm_media) == IFM_AUTO)
  255. ic->ic_curmode = IEEE80211_MODE_AUTO;
  256. ieee80211_setmode(ic, ic->ic_curmode);
  257. ic->ic_scan_count = 0;
  258. /* Scan the next channel. */
  259. ieee80211_next_scan(ifp);
  260. }
  261. /*
  262. * Switch to the next channel marked for scanning.
  263. */
  264. void
  265. ieee80211_next_scan(struct ifnet *ifp)
  266. {
  267. struct ieee80211com *ic = (void *)ifp;
  268. struct ieee80211_channel *chan;
  269. chan = ic->ic_bss->ni_chan;
  270. for (;;) {
  271. if (++chan > &ic->ic_channels[IEEE80211_CHAN_MAX])
  272. chan = &ic->ic_channels[0];
  273. if (isset(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan))) {
  274. /*
  275. * Ignore channels marked passive-only
  276. * during an active scan.
  277. */
  278. if ((ic->ic_flags & IEEE80211_F_ASCAN) == 0 ||
  279. (chan->ic_flags & IEEE80211_CHAN_PASSIVE) == 0)
  280. break;
  281. }
  282. if (chan == ic->ic_bss->ni_chan) {
  283. ieee80211_end_scan(ifp);
  284. return;
  285. }
  286. }
  287. clrbit(ic->ic_chan_scan, ieee80211_chan2ieee(ic, chan));
  288. DPRINTF(("chan %d->%d\n",
  289. ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan),
  290. ieee80211_chan2ieee(ic, chan)));
  291. ic->ic_bss->ni_chan = chan;
  292. ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
  293. }
  294. #ifndef IEEE80211_STA_ONLY
  295. void
  296. ieee80211_create_ibss(struct ieee80211com* ic, struct ieee80211_channel *chan)
  297. {
  298. struct ieee80211_node *ni;
  299. struct ifnet *ifp = &ic->ic_if;
  300. ni = ic->ic_bss;
  301. if (ifp->if_flags & IFF_DEBUG)
  302. printf("%s: creating ibss\n", ifp->if_xname);
  303. ic->ic_flags |= IEEE80211_F_SIBSS;
  304. ni->ni_chan = chan;
  305. ni->ni_rates = ic->ic_sup_rates[ieee80211_chan2mode(ic, ni->ni_chan)];
  306. ni->ni_txrate = 0;
  307. IEEE80211_ADDR_COPY(ni->ni_macaddr, ic->ic_myaddr);
  308. IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_myaddr);
  309. if (ic->ic_opmode == IEEE80211_M_IBSS) {
  310. if ((ic->ic_flags & IEEE80211_F_DESBSSID) != 0)
  311. IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_des_bssid);
  312. else
  313. ni->ni_bssid[0] |= 0x02; /* local bit for IBSS */
  314. }
  315. ni->ni_esslen = ic->ic_des_esslen;
  316. memcpy(ni->ni_essid, ic->ic_des_essid, ni->ni_esslen);
  317. ni->ni_rssi = 0;
  318. ni->ni_rstamp = 0;
  319. memset(ni->ni_tstamp, 0, sizeof(ni->ni_tstamp));
  320. ni->ni_intval = ic->ic_lintval;
  321. ni->ni_capinfo = IEEE80211_CAPINFO_IBSS;
  322. if (ic->ic_flags & IEEE80211_F_WEPON)
  323. ni->ni_capinfo |= IEEE80211_CAPINFO_PRIVACY;
  324. if (ic->ic_flags & IEEE80211_F_RSNON) {
  325. struct ieee80211_key *k;
  326. /* initialize 256-bit global key counter to a random value */
  327. arc4random_buf(ic->ic_globalcnt, EAPOL_KEY_NONCE_LEN);
  328. ni->ni_rsnprotos = ic->ic_rsnprotos;
  329. ni->ni_rsnakms = ic->ic_rsnakms;
  330. ni->ni_rsnciphers = ic->ic_rsnciphers;
  331. ni->ni_rsngroupcipher = ic->ic_rsngroupcipher;
  332. ni->ni_rsngroupmgmtcipher = ic->ic_rsngroupmgmtcipher;
  333. ni->ni_rsncaps = 0;
  334. if (ic->ic_caps & IEEE80211_C_MFP) {
  335. ni->ni_rsncaps |= IEEE80211_RSNCAP_MFPC;
  336. if (ic->ic_flags & IEEE80211_F_MFPR)
  337. ni->ni_rsncaps |= IEEE80211_RSNCAP_MFPR;
  338. }
  339. ic->ic_def_txkey = 1;
  340. k = &ic->ic_nw_keys[ic->ic_def_txkey];
  341. memset(k, 0, sizeof(*k));
  342. k->k_id = ic->ic_def_txkey;
  343. k->k_cipher = ni->ni_rsngroupcipher;
  344. k->k_flags = IEEE80211_KEY_GROUP | IEEE80211_KEY_TX;
  345. k->k_len = ieee80211_cipher_keylen(k->k_cipher);
  346. arc4random_buf(k->k_key, k->k_len);
  347. (*ic->ic_set_key)(ic, ni, k); /* XXX */
  348. if (ic->ic_caps & IEEE80211_C_MFP) {
  349. ic->ic_igtk_kid = 4;
  350. k = &ic->ic_nw_keys[ic->ic_igtk_kid];
  351. memset(k, 0, sizeof(*k));
  352. k->k_id = ic->ic_igtk_kid;
  353. k->k_cipher = ni->ni_rsngroupmgmtcipher;
  354. k->k_flags = IEEE80211_KEY_IGTK | IEEE80211_KEY_TX;
  355. k->k_len = 16;
  356. arc4random_buf(k->k_key, k->k_len);
  357. (*ic->ic_set_key)(ic, ni, k); /* XXX */
  358. }
  359. /*
  360. * In HostAP mode, multicast traffic is sent using ic_bss
  361. * as the Tx node, so mark our node as valid so we can send
  362. * multicast frames using the group key we've just configured.
  363. */
  364. ni->ni_port_valid = 1;
  365. ni->ni_flags |= IEEE80211_NODE_TXPROT;
  366. /* schedule a GTK/IGTK rekeying after 3600s */
  367. timeout_add_sec(&ic->ic_rsn_timeout, 3600);
  368. }
  369. timeout_add_sec(&ic->ic_inact_timeout, IEEE80211_INACT_WAIT);
  370. timeout_add_sec(&ic->ic_node_cache_timeout, IEEE80211_CACHE_WAIT);
  371. ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
  372. }
  373. #endif /* IEEE80211_STA_ONLY */
  374. int
  375. ieee80211_match_bss(struct ieee80211com *ic, struct ieee80211_node *ni)
  376. {
  377. u_int8_t rate;
  378. int fail;
  379. fail = 0;
  380. if (isclr(ic->ic_chan_active, ieee80211_chan2ieee(ic, ni->ni_chan)))
  381. fail |= 0x01;
  382. if (ic->ic_des_chan != IEEE80211_CHAN_ANYC &&
  383. ni->ni_chan != ic->ic_des_chan)
  384. fail |= 0x01;
  385. #ifndef IEEE80211_STA_ONLY
  386. if (ic->ic_opmode == IEEE80211_M_IBSS) {
  387. if ((ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) == 0)
  388. fail |= 0x02;
  389. } else
  390. #endif
  391. {
  392. if ((ni->ni_capinfo & IEEE80211_CAPINFO_ESS) == 0)
  393. fail |= 0x02;
  394. }
  395. if (ic->ic_flags & (IEEE80211_F_WEPON | IEEE80211_F_RSNON)) {
  396. if ((ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) == 0)
  397. fail |= 0x04;
  398. } else {
  399. if (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY)
  400. fail |= 0x04;
  401. }
  402. rate = ieee80211_fix_rate(ic, ni, IEEE80211_F_DONEGO);
  403. if (rate & IEEE80211_RATE_BASIC)
  404. fail |= 0x08;
  405. if (ic->ic_des_esslen != 0 &&
  406. (ni->ni_esslen != ic->ic_des_esslen ||
  407. memcmp(ni->ni_essid, ic->ic_des_essid, ic->ic_des_esslen) != 0))
  408. fail |= 0x10;
  409. if ((ic->ic_flags & IEEE80211_F_DESBSSID) &&
  410. !IEEE80211_ADDR_EQ(ic->ic_des_bssid, ni->ni_bssid))
  411. fail |= 0x20;
  412. if (ic->ic_flags & IEEE80211_F_RSNON) {
  413. /*
  414. * If at least one RSN IE field from the AP's RSN IE fails
  415. * to overlap with any value the STA supports, the STA shall
  416. * decline to associate with that AP.
  417. */
  418. if ((ni->ni_rsnprotos & ic->ic_rsnprotos) == 0)
  419. fail |= 0x40;
  420. if ((ni->ni_rsnakms & ic->ic_rsnakms) == 0)
  421. fail |= 0x40;
  422. if ((ni->ni_rsnakms & ic->ic_rsnakms &
  423. ~(IEEE80211_AKM_PSK | IEEE80211_AKM_SHA256_PSK)) == 0) {
  424. /* AP only supports PSK AKMPs */
  425. if (!(ic->ic_flags & IEEE80211_F_PSK))
  426. fail |= 0x40;
  427. }
  428. if (ni->ni_rsngroupcipher != IEEE80211_CIPHER_WEP40 &&
  429. ni->ni_rsngroupcipher != IEEE80211_CIPHER_TKIP &&
  430. ni->ni_rsngroupcipher != IEEE80211_CIPHER_CCMP &&
  431. ni->ni_rsngroupcipher != IEEE80211_CIPHER_WEP104)
  432. fail |= 0x40;
  433. if ((ni->ni_rsnciphers & ic->ic_rsnciphers) == 0)
  434. fail |= 0x40;
  435. /* we only support BIP as the IGTK cipher */
  436. if ((ni->ni_rsncaps & IEEE80211_RSNCAP_MFPC) &&
  437. ni->ni_rsngroupmgmtcipher != IEEE80211_CIPHER_BIP)
  438. fail |= 0x40;
  439. /* we do not support MFP but AP requires it */
  440. if (!(ic->ic_caps & IEEE80211_C_MFP) &&
  441. (ni->ni_rsncaps & IEEE80211_RSNCAP_MFPR))
  442. fail |= 0x40;
  443. /* we require MFP but AP does not support it */
  444. if ((ic->ic_caps & IEEE80211_C_MFP) &&
  445. (ic->ic_flags & IEEE80211_F_MFPR) &&
  446. !(ni->ni_rsncaps & IEEE80211_RSNCAP_MFPC))
  447. fail |= 0x40;
  448. }
  449. #ifdef IEEE80211_DEBUG
  450. if (ic->ic_if.if_flags & IFF_DEBUG) {
  451. printf(" %c %s", fail ? '-' : '+',
  452. ether_sprintf(ni->ni_macaddr));
  453. printf(" %s%c", ether_sprintf(ni->ni_bssid),
  454. fail & 0x20 ? '!' : ' ');
  455. printf(" %3d%c", ieee80211_chan2ieee(ic, ni->ni_chan),
  456. fail & 0x01 ? '!' : ' ');
  457. printf(" %+4d", ni->ni_rssi);
  458. printf(" %2dM%c", (rate & IEEE80211_RATE_VAL) / 2,
  459. fail & 0x08 ? '!' : ' ');
  460. printf(" %4s%c",
  461. (ni->ni_capinfo & IEEE80211_CAPINFO_ESS) ? "ess" :
  462. (ni->ni_capinfo & IEEE80211_CAPINFO_IBSS) ? "ibss" :
  463. "????",
  464. fail & 0x02 ? '!' : ' ');
  465. printf(" %7s%c ",
  466. (ni->ni_capinfo & IEEE80211_CAPINFO_PRIVACY) ?
  467. "privacy" : "no",
  468. fail & 0x04 ? '!' : ' ');
  469. printf(" %3s%c ",
  470. (ic->ic_flags & IEEE80211_F_RSNON) ?
  471. "rsn" : "no",
  472. fail & 0x40 ? '!' : ' ');
  473. ieee80211_print_essid(ni->ni_essid, ni->ni_esslen);
  474. printf("%s\n", fail & 0x10 ? "!" : "");
  475. }
  476. #endif
  477. return fail;
  478. }
  479. /*
  480. * Complete a scan of potential channels.
  481. */
  482. void
  483. ieee80211_end_scan(struct ifnet *ifp)
  484. {
  485. struct ieee80211com *ic = (void *)ifp;
  486. struct ieee80211_node *ni, *nextbs, *selbs;
  487. if (ifp->if_flags & IFF_DEBUG)
  488. printf("%s: end %s scan\n", ifp->if_xname,
  489. (ic->ic_flags & IEEE80211_F_ASCAN) ?
  490. "active" : "passive");
  491. if (ic->ic_scan_count)
  492. ic->ic_flags &= ~IEEE80211_F_ASCAN;
  493. ni = RB_MIN(ieee80211_tree, &ic->ic_tree);
  494. #ifndef IEEE80211_STA_ONLY
  495. if (ic->ic_opmode == IEEE80211_M_HOSTAP) {
  496. /* XXX off stack? */
  497. u_char occupied[howmany(IEEE80211_CHAN_MAX, NBBY)];
  498. int i, fail;
  499. /*
  500. * The passive scan to look for existing AP's completed,
  501. * select a channel to camp on. Identify the channels
  502. * that already have one or more AP's and try to locate
  503. * an unnoccupied one. If that fails, pick a random
  504. * channel from the active set.
  505. */
  506. memset(occupied, 0, sizeof(occupied));
  507. RB_FOREACH(ni, ieee80211_tree, &ic->ic_tree)
  508. setbit(occupied, ieee80211_chan2ieee(ic, ni->ni_chan));
  509. for (i = 0; i < IEEE80211_CHAN_MAX; i++)
  510. if (isset(ic->ic_chan_active, i) && isclr(occupied, i))
  511. break;
  512. if (i == IEEE80211_CHAN_MAX) {
  513. fail = arc4random() & 3; /* random 0-3 */
  514. for (i = 0; i < IEEE80211_CHAN_MAX; i++)
  515. if (isset(ic->ic_chan_active, i) && fail-- == 0)
  516. break;
  517. }
  518. ieee80211_create_ibss(ic, &ic->ic_channels[i]);
  519. goto wakeup;
  520. }
  521. #endif
  522. if (ni == NULL) {
  523. DPRINTF(("no scan candidate\n"));
  524. notfound:
  525. #ifndef IEEE80211_STA_ONLY
  526. if (ic->ic_opmode == IEEE80211_M_IBSS &&
  527. (ic->ic_flags & IEEE80211_F_IBSSON) &&
  528. ic->ic_des_esslen != 0) {
  529. ieee80211_create_ibss(ic, ic->ic_ibss_chan);
  530. goto wakeup;
  531. }
  532. #endif
  533. /*
  534. * Scan the next mode if nothing has been found. This
  535. * is necessary if the device supports different
  536. * incompatible modes in the same channel range, like
  537. * like 11b and "pure" 11G mode. This will loop
  538. * forever except for user-initiated scans.
  539. */
  540. if (ieee80211_next_mode(ifp) == IEEE80211_MODE_AUTO) {
  541. if (ic->ic_scan_lock & IEEE80211_SCAN_REQUEST &&
  542. ic->ic_scan_lock & IEEE80211_SCAN_RESUME) {
  543. ic->ic_scan_lock = IEEE80211_SCAN_LOCKED;
  544. /* Return from an user-initiated scan */
  545. wakeup(&ic->ic_scan_lock);
  546. } else if (ic->ic_scan_lock & IEEE80211_SCAN_REQUEST)
  547. goto wakeup;
  548. ic->ic_scan_count++;
  549. }
  550. /*
  551. * Reset the list of channels to scan and start again.
  552. */
  553. ieee80211_next_scan(ifp);
  554. return;
  555. }
  556. selbs = NULL;
  557. for (; ni != NULL; ni = nextbs) {
  558. nextbs = RB_NEXT(ieee80211_tree, &ic->ic_tree, ni);
  559. if (ni->ni_fails) {
  560. /*
  561. * The configuration of the access points may change
  562. * during my scan. So delete the entry for the AP
  563. * and retry to associate if there is another beacon.
  564. */
  565. if (ni->ni_fails++ > 2)
  566. ieee80211_free_node(ic, ni);
  567. continue;
  568. }
  569. if (ieee80211_match_bss(ic, ni) == 0) {
  570. if (selbs == NULL)
  571. selbs = ni;
  572. else if (ni->ni_rssi > selbs->ni_rssi)
  573. selbs = ni;
  574. }
  575. }
  576. if (selbs == NULL)
  577. goto notfound;
  578. (*ic->ic_node_copy)(ic, ic->ic_bss, selbs);
  579. ni = ic->ic_bss;
  580. /*
  581. * Set the erp state (mostly the slot time) to deal with
  582. * the auto-select case; this should be redundant if the
  583. * mode is locked.
  584. */
  585. ic->ic_curmode = ieee80211_chan2mode(ic, ni->ni_chan);
  586. ieee80211_reset_erp(ic);
  587. if (ic->ic_flags & IEEE80211_F_RSNON)
  588. ieee80211_choose_rsnparams(ic);
  589. else if (ic->ic_flags & IEEE80211_F_WEPON)
  590. ni->ni_rsncipher = IEEE80211_CIPHER_USEGROUP;
  591. ieee80211_node_newstate(selbs, IEEE80211_STA_BSS);
  592. #ifndef IEEE80211_STA_ONLY
  593. if (ic->ic_opmode == IEEE80211_M_IBSS) {
  594. ieee80211_fix_rate(ic, ni, IEEE80211_F_DOFRATE |
  595. IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
  596. if (ni->ni_rates.rs_nrates == 0)
  597. goto notfound;
  598. ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
  599. } else
  600. #endif
  601. ieee80211_new_state(ic, IEEE80211_S_AUTH, -1);
  602. wakeup:
  603. if (ic->ic_scan_lock & IEEE80211_SCAN_REQUEST) {
  604. /* Return from an user-initiated scan */
  605. wakeup(&ic->ic_scan_lock);
  606. }
  607. ic->ic_scan_lock = IEEE80211_SCAN_UNLOCKED;
  608. }
  609. /*
  610. * Autoselect the best RSN parameters (protocol, AKMP, pairwise cipher...)
  611. * that are supported by both peers (STA mode only).
  612. */
  613. void
  614. ieee80211_choose_rsnparams(struct ieee80211com *ic)
  615. {
  616. struct ieee80211_node *ni = ic->ic_bss;
  617. struct ieee80211_pmk *pmk;
  618. /* filter out unsupported protocol versions */
  619. ni->ni_rsnprotos &= ic->ic_rsnprotos;
  620. /* prefer RSN (aka WPA2) over WPA */
  621. if (ni->ni_rsnprotos & IEEE80211_PROTO_RSN)
  622. ni->ni_rsnprotos = IEEE80211_PROTO_RSN;
  623. else
  624. ni->ni_rsnprotos = IEEE80211_PROTO_WPA;
  625. /* filter out unsupported AKMPs */
  626. ni->ni_rsnakms &= ic->ic_rsnakms;
  627. /* prefer SHA-256 based AKMPs */
  628. if ((ic->ic_flags & IEEE80211_F_PSK) && (ni->ni_rsnakms &
  629. (IEEE80211_AKM_PSK | IEEE80211_AKM_SHA256_PSK))) {
  630. /* AP supports PSK AKMP and a PSK is configured */
  631. if (ni->ni_rsnakms & IEEE80211_AKM_SHA256_PSK)
  632. ni->ni_rsnakms = IEEE80211_AKM_SHA256_PSK;
  633. else
  634. ni->ni_rsnakms = IEEE80211_AKM_PSK;
  635. } else {
  636. if (ni->ni_rsnakms & IEEE80211_AKM_SHA256_8021X)
  637. ni->ni_rsnakms = IEEE80211_AKM_SHA256_8021X;
  638. else
  639. ni->ni_rsnakms = IEEE80211_AKM_8021X;
  640. /* check if we have a cached PMK for this AP */
  641. if (ni->ni_rsnprotos == IEEE80211_PROTO_RSN &&
  642. (pmk = ieee80211_pmksa_find(ic, ni, NULL)) != NULL) {
  643. memcpy(ni->ni_pmkid, pmk->pmk_pmkid,
  644. IEEE80211_PMKID_LEN);
  645. ni->ni_flags |= IEEE80211_NODE_PMKID;
  646. }
  647. }
  648. /* filter out unsupported pairwise ciphers */
  649. ni->ni_rsnciphers &= ic->ic_rsnciphers;
  650. /* prefer CCMP over TKIP */
  651. if (ni->ni_rsnciphers & IEEE80211_CIPHER_CCMP)
  652. ni->ni_rsnciphers = IEEE80211_CIPHER_CCMP;
  653. else
  654. ni->ni_rsnciphers = IEEE80211_CIPHER_TKIP;
  655. ni->ni_rsncipher = ni->ni_rsnciphers;
  656. /* use MFP if we both support it */
  657. if ((ic->ic_caps & IEEE80211_C_MFP) &&
  658. (ni->ni_rsncaps & IEEE80211_RSNCAP_MFPC))
  659. ni->ni_flags |= IEEE80211_NODE_MFP;
  660. }
  661. int
  662. ieee80211_get_rate(struct ieee80211com *ic)
  663. {
  664. u_int8_t (*rates)[IEEE80211_RATE_MAXSIZE];
  665. int rate;
  666. rates = &ic->ic_bss->ni_rates.rs_rates;
  667. if (ic->ic_fixed_rate != -1)
  668. rate = (*rates)[ic->ic_fixed_rate];
  669. else if (ic->ic_state == IEEE80211_S_RUN)
  670. rate = (*rates)[ic->ic_bss->ni_txrate];
  671. else
  672. rate = 0;
  673. return rate & IEEE80211_RATE_VAL;
  674. }
  675. struct ieee80211_node *
  676. ieee80211_node_alloc(struct ieee80211com *ic)
  677. {
  678. return malloc(sizeof(struct ieee80211_node), M_DEVBUF,
  679. M_NOWAIT | M_ZERO);
  680. }
  681. void
  682. ieee80211_node_cleanup(struct ieee80211com *ic, struct ieee80211_node *ni)
  683. {
  684. if (ni->ni_rsnie != NULL) {
  685. free(ni->ni_rsnie, M_DEVBUF, 0);
  686. ni->ni_rsnie = NULL;
  687. }
  688. }
  689. void
  690. ieee80211_node_free(struct ieee80211com *ic, struct ieee80211_node *ni)
  691. {
  692. ieee80211_node_cleanup(ic, ni);
  693. free(ni, M_DEVBUF, 0);
  694. }
  695. void
  696. ieee80211_node_copy(struct ieee80211com *ic,
  697. struct ieee80211_node *dst, const struct ieee80211_node *src)
  698. {
  699. ieee80211_node_cleanup(ic, dst);
  700. *dst = *src;
  701. dst->ni_rsnie = NULL;
  702. if (src->ni_rsnie != NULL)
  703. ieee80211_save_ie(src->ni_rsnie, &dst->ni_rsnie);
  704. }
  705. u_int8_t
  706. ieee80211_node_getrssi(struct ieee80211com *ic,
  707. const struct ieee80211_node *ni)
  708. {
  709. return ni->ni_rssi;
  710. }
  711. void
  712. ieee80211_setup_node(struct ieee80211com *ic,
  713. struct ieee80211_node *ni, const u_int8_t *macaddr)
  714. {
  715. int s;
  716. DPRINTF(("%s\n", ether_sprintf((u_int8_t *)macaddr)));
  717. IEEE80211_ADDR_COPY(ni->ni_macaddr, macaddr);
  718. ieee80211_node_newstate(ni, IEEE80211_STA_CACHE);
  719. ni->ni_ic = ic; /* back-pointer */
  720. #ifndef IEEE80211_STA_ONLY
  721. IFQ_SET_MAXLEN(&ni->ni_savedq, IEEE80211_PS_MAX_QUEUE);
  722. timeout_set(&ni->ni_eapol_to, ieee80211_eapol_timeout, ni);
  723. timeout_set(&ni->ni_sa_query_to, ieee80211_sa_query_timeout, ni);
  724. #endif
  725. s = splnet();
  726. RB_INSERT(ieee80211_tree, &ic->ic_tree, ni);
  727. ic->ic_nnodes++;
  728. splx(s);
  729. }
  730. struct ieee80211_node *
  731. ieee80211_alloc_node(struct ieee80211com *ic, const u_int8_t *macaddr)
  732. {
  733. struct ieee80211_node *ni = ieee80211_alloc_node_helper(ic);
  734. if (ni != NULL)
  735. ieee80211_setup_node(ic, ni, macaddr);
  736. else
  737. ic->ic_stats.is_rx_nodealloc++;
  738. return ni;
  739. }
  740. struct ieee80211_node *
  741. ieee80211_dup_bss(struct ieee80211com *ic, const u_int8_t *macaddr)
  742. {
  743. struct ieee80211_node *ni = ieee80211_alloc_node_helper(ic);
  744. if (ni != NULL) {
  745. ieee80211_setup_node(ic, ni, macaddr);
  746. /*
  747. * Inherit from ic_bss.
  748. */
  749. IEEE80211_ADDR_COPY(ni->ni_bssid, ic->ic_bss->ni_bssid);
  750. ni->ni_chan = ic->ic_bss->ni_chan;
  751. } else
  752. ic->ic_stats.is_rx_nodealloc++;
  753. return ni;
  754. }
  755. struct ieee80211_node *
  756. ieee80211_find_node(struct ieee80211com *ic, const u_int8_t *macaddr)
  757. {
  758. struct ieee80211_node *ni;
  759. int cmp;
  760. /* similar to RB_FIND except we compare keys, not nodes */
  761. ni = RB_ROOT(&ic->ic_tree);
  762. while (ni != NULL) {
  763. cmp = memcmp(macaddr, ni->ni_macaddr, IEEE80211_ADDR_LEN);
  764. if (cmp < 0)
  765. ni = RB_LEFT(ni, ni_node);
  766. else if (cmp > 0)
  767. ni = RB_RIGHT(ni, ni_node);
  768. else
  769. break;
  770. }
  771. return ni;
  772. }
  773. /*
  774. * Return a reference to the appropriate node for sending
  775. * a data frame. This handles node discovery in adhoc networks.
  776. *
  777. * Drivers will call this, so increase the reference count before
  778. * returning the node.
  779. */
  780. struct ieee80211_node *
  781. ieee80211_find_txnode(struct ieee80211com *ic, const u_int8_t *macaddr)
  782. {
  783. #ifndef IEEE80211_STA_ONLY
  784. struct ieee80211_node *ni;
  785. int s;
  786. #endif
  787. /*
  788. * The destination address should be in the node table
  789. * unless we are operating in station mode or this is a
  790. * multicast/broadcast frame.
  791. */
  792. if (ic->ic_opmode == IEEE80211_M_STA || IEEE80211_IS_MULTICAST(macaddr))
  793. return ieee80211_ref_node(ic->ic_bss);
  794. #ifndef IEEE80211_STA_ONLY
  795. s = splnet();
  796. ni = ieee80211_find_node(ic, macaddr);
  797. splx(s);
  798. if (ni == NULL) {
  799. if (ic->ic_opmode != IEEE80211_M_IBSS &&
  800. ic->ic_opmode != IEEE80211_M_AHDEMO)
  801. return NULL;
  802. /*
  803. * Fake up a node; this handles node discovery in
  804. * adhoc mode. Note that for the driver's benefit
  805. * we we treat this like an association so the driver
  806. * has an opportunity to setup its private state.
  807. *
  808. * XXX need better way to handle this; issue probe
  809. * request so we can deduce rate set, etc.
  810. */
  811. if ((ni = ieee80211_dup_bss(ic, macaddr)) == NULL)
  812. return NULL;
  813. /* XXX no rate negotiation; just dup */
  814. ni->ni_rates = ic->ic_bss->ni_rates;
  815. ni->ni_txrate = 0;
  816. if (ic->ic_newassoc)
  817. (*ic->ic_newassoc)(ic, ni, 1);
  818. }
  819. return ieee80211_ref_node(ni);
  820. #else
  821. return NULL; /* can't get there */
  822. #endif /* IEEE80211_STA_ONLY */
  823. }
  824. /*
  825. * It is usually desirable to process a Rx packet using its sender's
  826. * node-record instead of the BSS record.
  827. *
  828. * - AP mode: keep a node-record for every authenticated/associated
  829. * station *in the BSS*. For future use, we also track neighboring
  830. * APs, since they might belong to the same ESS. APs in the same
  831. * ESS may bridge packets to each other, forming a Wireless
  832. * Distribution System (WDS).
  833. *
  834. * - IBSS mode: keep a node-record for every station *in the BSS*.
  835. * Also track neighboring stations by their beacons/probe responses.
  836. *
  837. * - monitor mode: keep a node-record for every sender, regardless
  838. * of BSS.
  839. *
  840. * - STA mode: the only available node-record is the BSS record,
  841. * ic->ic_bss.
  842. *
  843. * Of all the 802.11 Control packets, only the node-records for
  844. * RTS packets node-record can be looked up.
  845. *
  846. * Return non-zero if the packet's node-record is kept, zero
  847. * otherwise.
  848. */
  849. static __inline int
  850. ieee80211_needs_rxnode(struct ieee80211com *ic,
  851. const struct ieee80211_frame *wh, const u_int8_t **bssid)
  852. {
  853. int monitor, rc = 0;
  854. monitor = (ic->ic_opmode == IEEE80211_M_MONITOR);
  855. *bssid = NULL;
  856. switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) {
  857. case IEEE80211_FC0_TYPE_CTL:
  858. if (!monitor)
  859. break;
  860. return (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
  861. IEEE80211_FC0_SUBTYPE_RTS;
  862. case IEEE80211_FC0_TYPE_MGT:
  863. *bssid = wh->i_addr3;
  864. switch (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) {
  865. case IEEE80211_FC0_SUBTYPE_BEACON:
  866. case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
  867. break;
  868. default:
  869. #ifndef IEEE80211_STA_ONLY
  870. if (ic->ic_opmode == IEEE80211_M_STA)
  871. break;
  872. rc = IEEE80211_ADDR_EQ(*bssid, ic->ic_bss->ni_bssid) ||
  873. IEEE80211_ADDR_EQ(*bssid, etherbroadcastaddr);
  874. #endif
  875. break;
  876. }
  877. break;
  878. case IEEE80211_FC0_TYPE_DATA:
  879. switch (wh->i_fc[1] & IEEE80211_FC1_DIR_MASK) {
  880. case IEEE80211_FC1_DIR_NODS:
  881. *bssid = wh->i_addr3;
  882. #ifndef IEEE80211_STA_ONLY
  883. if (ic->ic_opmode == IEEE80211_M_IBSS ||
  884. ic->ic_opmode == IEEE80211_M_AHDEMO)
  885. rc = IEEE80211_ADDR_EQ(*bssid,
  886. ic->ic_bss->ni_bssid);
  887. #endif
  888. break;
  889. case IEEE80211_FC1_DIR_TODS:
  890. *bssid = wh->i_addr1;
  891. #ifndef IEEE80211_STA_ONLY
  892. if (ic->ic_opmode == IEEE80211_M_HOSTAP)
  893. rc = IEEE80211_ADDR_EQ(*bssid,
  894. ic->ic_bss->ni_bssid);
  895. #endif
  896. break;
  897. case IEEE80211_FC1_DIR_FROMDS:
  898. case IEEE80211_FC1_DIR_DSTODS:
  899. *bssid = wh->i_addr2;
  900. #ifndef IEEE80211_STA_ONLY
  901. rc = (ic->ic_opmode == IEEE80211_M_HOSTAP);
  902. #endif
  903. break;
  904. }
  905. break;
  906. }
  907. return monitor || rc;
  908. }
  909. /*
  910. * Drivers call this, so increase the reference count before returning
  911. * the node.
  912. */
  913. struct ieee80211_node *
  914. ieee80211_find_rxnode(struct ieee80211com *ic,
  915. const struct ieee80211_frame *wh)
  916. {
  917. static const u_int8_t zero[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  918. struct ieee80211_node *ni;
  919. const u_int8_t *bssid;
  920. int s;
  921. if (!ieee80211_needs_rxnode(ic, wh, &bssid))
  922. return ieee80211_ref_node(ic->ic_bss);
  923. s = splnet();
  924. ni = ieee80211_find_node(ic, wh->i_addr2);
  925. splx(s);
  926. if (ni != NULL)
  927. return ieee80211_ref_node(ni);
  928. #ifndef IEEE80211_STA_ONLY
  929. if (ic->ic_opmode == IEEE80211_M_HOSTAP)
  930. return ieee80211_ref_node(ic->ic_bss);
  931. #endif
  932. /* XXX see remarks in ieee80211_find_txnode */
  933. /* XXX no rate negotiation; just dup */
  934. if ((ni = ieee80211_dup_bss(ic, wh->i_addr2)) == NULL)
  935. return ieee80211_ref_node(ic->ic_bss);
  936. IEEE80211_ADDR_COPY(ni->ni_bssid, (bssid != NULL) ? bssid : zero);
  937. ni->ni_rates = ic->ic_bss->ni_rates;
  938. ni->ni_txrate = 0;
  939. if (ic->ic_newassoc)
  940. (*ic->ic_newassoc)(ic, ni, 1);
  941. DPRINTF(("faked-up node %p for %s\n", ni,
  942. ether_sprintf((u_int8_t *)wh->i_addr2)));
  943. return ieee80211_ref_node(ni);
  944. }
  945. struct ieee80211_node *
  946. ieee80211_find_node_for_beacon(struct ieee80211com *ic,
  947. const u_int8_t *macaddr, const struct ieee80211_channel *chan,
  948. const char *ssid, u_int8_t rssi)
  949. {
  950. struct ieee80211_node *ni, *keep = NULL;
  951. int s, score = 0;
  952. if ((ni = ieee80211_find_node(ic, macaddr)) != NULL) {
  953. s = splnet();
  954. if (ni->ni_chan != chan && ni->ni_rssi >= rssi)
  955. score++;
  956. if (ssid[1] == 0 && ni->ni_esslen != 0)
  957. score++;
  958. if (score > 0)
  959. keep = ni;
  960. splx(s);
  961. }
  962. return (keep);
  963. }
  964. void
  965. ieee80211_free_node(struct ieee80211com *ic, struct ieee80211_node *ni)
  966. {
  967. if (ni == ic->ic_bss)
  968. panic("freeing bss node");
  969. splassert(IPL_NET);
  970. DPRINTF(("%s\n", ether_sprintf(ni->ni_macaddr)));
  971. #ifndef IEEE80211_STA_ONLY
  972. timeout_del(&ni->ni_eapol_to);
  973. timeout_del(&ni->ni_sa_query_to);
  974. IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
  975. #endif
  976. RB_REMOVE(ieee80211_tree, &ic->ic_tree, ni);
  977. ic->ic_nnodes--;
  978. #ifndef IEEE80211_STA_ONLY
  979. if (!IF_IS_EMPTY(&ni->ni_savedq)) {
  980. IF_PURGE(&ni->ni_savedq);
  981. if (ic->ic_set_tim != NULL)
  982. (*ic->ic_set_tim)(ic, ni->ni_associd, 0);
  983. }
  984. #endif
  985. (*ic->ic_node_free)(ic, ni);
  986. /* TBD indicate to drivers that a new node can be allocated */
  987. }
  988. void
  989. ieee80211_release_node(struct ieee80211com *ic, struct ieee80211_node *ni)
  990. {
  991. int s;
  992. DPRINTF(("%s refcnt %u\n", ether_sprintf(ni->ni_macaddr),
  993. ni->ni_refcnt));
  994. s = splnet();
  995. if (ieee80211_node_decref(ni) == 0 &&
  996. ni->ni_state == IEEE80211_STA_COLLECT) {
  997. ieee80211_free_node(ic, ni);
  998. }
  999. splx(s);
  1000. }
  1001. void
  1002. ieee80211_free_allnodes(struct ieee80211com *ic)
  1003. {
  1004. struct ieee80211_node *ni;
  1005. int s;
  1006. DPRINTF(("freeing all nodes\n"));
  1007. s = splnet();
  1008. while ((ni = RB_MIN(ieee80211_tree, &ic->ic_tree)) != NULL)
  1009. ieee80211_free_node(ic, ni);
  1010. splx(s);
  1011. if (ic->ic_bss != NULL)
  1012. ieee80211_node_cleanup(ic, ic->ic_bss); /* for station mode */
  1013. }
  1014. void
  1015. ieee80211_clean_cached(struct ieee80211com *ic)
  1016. {
  1017. struct ieee80211_node *ni, *next_ni;
  1018. int s;
  1019. s = splnet();
  1020. for (ni = RB_MIN(ieee80211_tree, &ic->ic_tree);
  1021. ni != NULL; ni = next_ni) {
  1022. next_ni = RB_NEXT(ieee80211_tree, &ic->ic_tree, ni);
  1023. if (ni->ni_state == IEEE80211_STA_CACHE)
  1024. ieee80211_free_node(ic, ni);
  1025. }
  1026. splx(s);
  1027. }
  1028. /*
  1029. * Timeout inactive nodes.
  1030. *
  1031. * If called because of a cache timeout, which happens only in hostap and ibss
  1032. * modes, clean all inactive cached or authenticated nodes but don't de-auth
  1033. * any associated nodes.
  1034. *
  1035. * Else, this function is called because a new node must be allocated but the
  1036. * node cache is full. In this case, return as soon as a free slot was made
  1037. * available. If acting as hostap, clean cached nodes regardless of their
  1038. * recent activity and also allow de-authing of authenticated nodes older
  1039. * than one cache wait interval, and de-authing of inactive associated nodes.
  1040. */
  1041. void
  1042. ieee80211_clean_nodes(struct ieee80211com *ic, int cache_timeout)
  1043. {
  1044. struct ieee80211_node *ni, *next_ni;
  1045. u_int gen = ic->ic_scangen++; /* NB: ok 'cuz single-threaded*/
  1046. int s;
  1047. #ifndef IEEE80211_STA_ONLY
  1048. int nnodes = 0;
  1049. struct ifnet *ifp = &ic->ic_if;
  1050. #endif
  1051. s = splnet();
  1052. for (ni = RB_MIN(ieee80211_tree, &ic->ic_tree);
  1053. ni != NULL; ni = next_ni) {
  1054. next_ni = RB_NEXT(ieee80211_tree, &ic->ic_tree, ni);
  1055. if (!cache_timeout && ic->ic_nnodes < ic->ic_max_nnodes)
  1056. break;
  1057. if (ni->ni_scangen == gen) /* previously handled */
  1058. continue;
  1059. #ifndef IEEE80211_STA_ONLY
  1060. nnodes++;
  1061. #endif
  1062. ni->ni_scangen = gen;
  1063. if (ni->ni_refcnt > 0)
  1064. continue;
  1065. #ifndef IEEE80211_STA_ONLY
  1066. if ((ic->ic_opmode == IEEE80211_M_HOSTAP ||
  1067. ic->ic_opmode == IEEE80211_M_IBSS) &&
  1068. ic->ic_state == IEEE80211_S_RUN) {
  1069. if (cache_timeout) {
  1070. if (ni->ni_state != IEEE80211_STA_COLLECT &&
  1071. (ni->ni_state == IEEE80211_STA_ASSOC ||
  1072. ni->ni_inact < IEEE80211_INACT_MAX))
  1073. continue;
  1074. } else {
  1075. if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
  1076. ((ni->ni_state == IEEE80211_STA_ASSOC &&
  1077. ni->ni_inact < IEEE80211_INACT_MAX) ||
  1078. (ni->ni_state == IEEE80211_STA_AUTH &&
  1079. ni->ni_inact == 0)))
  1080. continue;
  1081. if (ic->ic_opmode == IEEE80211_M_IBSS &&
  1082. ni->ni_state != IEEE80211_STA_COLLECT &&
  1083. ni->ni_state != IEEE80211_STA_CACHE &&
  1084. ni->ni_inact < IEEE80211_INACT_MAX)
  1085. continue;
  1086. }
  1087. }
  1088. if (ifp->if_flags & IFF_DEBUG)
  1089. printf("%s: station %s purged from node cache\n",
  1090. ifp->if_xname, ether_sprintf(ni->ni_macaddr));
  1091. #endif
  1092. /*
  1093. * If we're hostap and the node is authenticated, send
  1094. * a deauthentication frame. The node will be freed when
  1095. * the driver calls ieee80211_release_node().
  1096. */
  1097. #ifndef IEEE80211_STA_ONLY
  1098. nnodes--;
  1099. if (ic->ic_opmode == IEEE80211_M_HOSTAP &&
  1100. ni->ni_state >= IEEE80211_STA_AUTH &&
  1101. ni->ni_state != IEEE80211_STA_COLLECT) {
  1102. splx(s);
  1103. IEEE80211_SEND_MGMT(ic, ni,
  1104. IEEE80211_FC0_SUBTYPE_DEAUTH,
  1105. IEEE80211_REASON_AUTH_EXPIRE);
  1106. s = splnet();
  1107. ieee80211_node_leave(ic, ni);
  1108. } else
  1109. #endif
  1110. ieee80211_free_node(ic, ni);
  1111. ic->ic_stats.is_node_timeout++;
  1112. }
  1113. #ifndef IEEE80211_STA_ONLY
  1114. /*
  1115. * During a cache timeout we iterate over all nodes.
  1116. * Check for node leaks by comparing the actual number of cached
  1117. * nodes with the ic_nnodes count, which is maintained while adding
  1118. * and removing nodes from the cache.
  1119. */
  1120. if ((ifp->if_flags & IFF_DEBUG) && cache_timeout &&
  1121. nnodes != ic->ic_nnodes)
  1122. printf("%s: number of cached nodes is %d, expected %d,"
  1123. "possible nodes leak\n", ifp->if_xname, nnodes,
  1124. ic->ic_nnodes);
  1125. #endif
  1126. splx(s);
  1127. }
  1128. void
  1129. ieee80211_iterate_nodes(struct ieee80211com *ic, ieee80211_iter_func *f,
  1130. void *arg)
  1131. {
  1132. struct ieee80211_node *ni;
  1133. int s;
  1134. s = splnet();
  1135. RB_FOREACH(ni, ieee80211_tree, &ic->ic_tree)
  1136. (*f)(arg, ni);
  1137. splx(s);
  1138. }
  1139. /*
  1140. * Install received rate set information in the node's state block.
  1141. */
  1142. int
  1143. ieee80211_setup_rates(struct ieee80211com *ic, struct ieee80211_node *ni,
  1144. const u_int8_t *rates, const u_int8_t *xrates, int flags)
  1145. {
  1146. struct ieee80211_rateset *rs = &ni->ni_rates;
  1147. memset(rs, 0, sizeof(*rs));
  1148. rs->rs_nrates = rates[1];
  1149. memcpy(rs->rs_rates, rates + 2, rs->rs_nrates);
  1150. if (xrates != NULL) {
  1151. u_int8_t nxrates;
  1152. /*
  1153. * Tack on 11g extended supported rate element.
  1154. */
  1155. nxrates = xrates[1];
  1156. if (rs->rs_nrates + nxrates > IEEE80211_RATE_MAXSIZE) {
  1157. nxrates = IEEE80211_RATE_MAXSIZE - rs->rs_nrates;
  1158. DPRINTF(("extended rate set too large; "
  1159. "only using %u of %u rates\n",
  1160. nxrates, xrates[1]));
  1161. ic->ic_stats.is_rx_rstoobig++;
  1162. }
  1163. memcpy(rs->rs_rates + rs->rs_nrates, xrates+2, nxrates);
  1164. rs->rs_nrates += nxrates;
  1165. }
  1166. return ieee80211_fix_rate(ic, ni, flags);
  1167. }
  1168. #ifndef IEEE80211_STA_ONLY
  1169. /*
  1170. * Check if the specified node supports ERP.
  1171. */
  1172. int
  1173. ieee80211_iserp_sta(const struct ieee80211_node *ni)
  1174. {
  1175. static const u_int8_t rates[] = { 2, 4, 11, 22, 12, 24, 48 };
  1176. const struct ieee80211_rateset *rs = &ni->ni_rates;
  1177. int i, j;
  1178. /*
  1179. * A STA supports ERP operation if it includes all the Clause 19
  1180. * mandatory rates in its supported rate set.
  1181. */
  1182. for (i = 0; i < nitems(rates); i++) {
  1183. for (j = 0; j < rs->rs_nrates; j++) {
  1184. if ((rs->rs_rates[j] & IEEE80211_RATE_VAL) == rates[i])
  1185. break;
  1186. }
  1187. if (j == rs->rs_nrates)
  1188. return 0;
  1189. }
  1190. return 1;
  1191. }
  1192. /*
  1193. * This function is called to notify the 802.1X PACP machine that a new
  1194. * 802.1X port is enabled and must be authenticated. For 802.11, a port
  1195. * becomes enabled whenever a STA successfully completes Open System
  1196. * authentication with an AP.
  1197. */
  1198. void
  1199. ieee80211_needs_auth(struct ieee80211com *ic, struct ieee80211_node *ni)
  1200. {
  1201. /*
  1202. * XXX this could be done via the route socket of via a dedicated
  1203. * EAP socket or another kernel->userland notification mechanism.
  1204. * The notification should include the MAC address (ni_macaddr).
  1205. */
  1206. }
  1207. #ifndef IEEE80211_NO_HT
  1208. /*
  1209. * Handle an HT STA joining an HT network.
  1210. */
  1211. void
  1212. ieee80211_node_join_ht(struct ieee80211com *ic, struct ieee80211_node *ni)
  1213. {
  1214. /* TBD */
  1215. }
  1216. #endif /* !IEEE80211_NO_HT */
  1217. /*
  1218. * Handle a station joining an RSN network.
  1219. */
  1220. void
  1221. ieee80211_node_join_rsn(struct ieee80211com *ic, struct ieee80211_node *ni)
  1222. {
  1223. DPRINTF(("station %s associated using proto %d akm 0x%x "
  1224. "cipher 0x%x groupcipher 0x%x\n", ether_sprintf(ni->ni_macaddr),
  1225. ni->ni_rsnprotos, ni->ni_rsnakms, ni->ni_rsnciphers,
  1226. ni->ni_rsngroupcipher));
  1227. ni->ni_rsn_state = RSNA_AUTHENTICATION;
  1228. ic->ic_rsnsta++;
  1229. ni->ni_key_count = 0;
  1230. ni->ni_port_valid = 0;
  1231. ni->ni_flags &= ~IEEE80211_NODE_TXRXPROT;
  1232. ni->ni_replaycnt = -1; /* XXX */
  1233. ni->ni_rsn_retries = 0;
  1234. ni->ni_rsncipher = ni->ni_rsnciphers;
  1235. ni->ni_rsn_state = RSNA_AUTHENTICATION_2;
  1236. /* generate a new authenticator nonce (ANonce) */
  1237. arc4random_buf(ni->ni_nonce, EAPOL_KEY_NONCE_LEN);
  1238. if (!ieee80211_is_8021x_akm(ni->ni_rsnakms)) {
  1239. memcpy(ni->ni_pmk, ic->ic_psk, IEEE80211_PMK_LEN);
  1240. ni->ni_flags |= IEEE80211_NODE_PMK;
  1241. (void)ieee80211_send_4way_msg1(ic, ni);
  1242. } else if (ni->ni_flags & IEEE80211_NODE_PMK) {
  1243. /* skip 802.1X auth if a cached PMK was found */
  1244. (void)ieee80211_send_4way_msg1(ic, ni);
  1245. } else {
  1246. /* no cached PMK found, needs full 802.1X auth */
  1247. ieee80211_needs_auth(ic, ni);
  1248. }
  1249. }
  1250. /*
  1251. * Handle a station joining an 11g network.
  1252. */
  1253. void
  1254. ieee80211_node_join_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
  1255. {
  1256. if (!(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)) {
  1257. /*
  1258. * Joining STA doesn't support short slot time. We must
  1259. * disable the use of short slot time for all other associated
  1260. * STAs and give the driver a chance to reconfigure the
  1261. * hardware.
  1262. */
  1263. if (++ic->ic_longslotsta == 1) {
  1264. if (ic->ic_caps & IEEE80211_C_SHSLOT)
  1265. ieee80211_set_shortslottime(ic, 0);
  1266. }
  1267. DPRINTF(("[%s] station needs long slot time, count %d\n",
  1268. ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta));
  1269. }
  1270. if (!ieee80211_iserp_sta(ni)) {
  1271. /*
  1272. * Joining STA is non-ERP.
  1273. */
  1274. ic->ic_nonerpsta++;
  1275. DPRINTF(("[%s] station is non-ERP, %d non-ERP "
  1276. "stations associated\n", ether_sprintf(ni->ni_macaddr),
  1277. ic->ic_nonerpsta));
  1278. /* must enable the use of protection */
  1279. if (ic->ic_protmode != IEEE80211_PROT_NONE) {
  1280. DPRINTF(("enable use of protection\n"));
  1281. ic->ic_flags |= IEEE80211_F_USEPROT;
  1282. }
  1283. if (!(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_PREAMBLE))
  1284. ic->ic_flags &= ~IEEE80211_F_SHPREAMBLE;
  1285. } else
  1286. ni->ni_flags |= IEEE80211_NODE_ERP;
  1287. }
  1288. void
  1289. ieee80211_node_join(struct ieee80211com *ic, struct ieee80211_node *ni,
  1290. int resp)
  1291. {
  1292. int newassoc;
  1293. if (ni->ni_associd == 0) {
  1294. u_int16_t aid;
  1295. /*
  1296. * It would be clever to search the bitmap
  1297. * more efficiently, but this will do for now.
  1298. */
  1299. for (aid = 1; aid < ic->ic_max_aid; aid++) {
  1300. if (!IEEE80211_AID_ISSET(aid,
  1301. ic->ic_aid_bitmap))
  1302. break;
  1303. }
  1304. if (aid >= ic->ic_max_aid) {
  1305. IEEE80211_SEND_MGMT(ic, ni, resp,
  1306. IEEE80211_REASON_ASSOC_TOOMANY);
  1307. ieee80211_node_leave(ic, ni);
  1308. return;
  1309. }
  1310. ni->ni_associd = aid | 0xc000;
  1311. IEEE80211_AID_SET(ni->ni_associd, ic->ic_aid_bitmap);
  1312. newassoc = 1;
  1313. if (ic->ic_curmode == IEEE80211_MODE_11G)
  1314. ieee80211_node_join_11g(ic, ni);
  1315. } else
  1316. newassoc = 0;
  1317. DPRINTF(("station %s %s associated at aid %d\n",
  1318. ether_sprintf(ni->ni_macaddr), newassoc ? "newly" : "already",
  1319. ni->ni_associd & ~0xc000));
  1320. /* give driver a chance to setup state like ni_txrate */
  1321. if (ic->ic_newassoc)
  1322. (*ic->ic_newassoc)(ic, ni, newassoc);
  1323. IEEE80211_SEND_MGMT(ic, ni, resp, IEEE80211_STATUS_SUCCESS);
  1324. ieee80211_node_newstate(ni, IEEE80211_STA_ASSOC);
  1325. if (!(ic->ic_flags & IEEE80211_F_RSNON)) {
  1326. ni->ni_port_valid = 1;
  1327. ni->ni_rsncipher = IEEE80211_CIPHER_USEGROUP;
  1328. } else
  1329. ieee80211_node_join_rsn(ic, ni);
  1330. #ifndef IEEE80211_NO_HT
  1331. if (ni->ni_flags & IEEE80211_NODE_HT)
  1332. ieee80211_node_join_ht(ic, ni);
  1333. #endif
  1334. #if NBRIDGE > 0
  1335. /*
  1336. * If the parent interface is a bridgeport, learn
  1337. * the node's address dynamically on this interface.
  1338. */
  1339. if (ic->ic_if.if_bridgeport != NULL)
  1340. bridge_update(&ic->ic_if,
  1341. (struct ether_addr *)ni->ni_macaddr, 0);
  1342. #endif
  1343. }
  1344. #ifndef IEEE80211_NO_HT
  1345. /*
  1346. * Handle an HT STA leaving an HT network.
  1347. */
  1348. void
  1349. ieee80211_node_leave_ht(struct ieee80211com *ic, struct ieee80211_node *ni)
  1350. {
  1351. struct ieee80211_rx_ba *ba;
  1352. u_int8_t tid;
  1353. int i;
  1354. /* free all Block Ack records */
  1355. for (tid = 0; tid < IEEE80211_NUM_TID; tid++) {
  1356. ba = &ni->ni_rx_ba[tid];
  1357. if (ba->ba_buf != NULL) {
  1358. for (i = 0; i < IEEE80211_BA_MAX_WINSZ; i++)
  1359. m_freem(ba->ba_buf[i].m);
  1360. free(ba->ba_buf, M_DEVBUF, 0);
  1361. ba->ba_buf = NULL;
  1362. }
  1363. }
  1364. }
  1365. #endif /* !IEEE80211_NO_HT */
  1366. /*
  1367. * Handle a station leaving an RSN network.
  1368. */
  1369. void
  1370. ieee80211_node_leave_rsn(struct ieee80211com *ic, struct ieee80211_node *ni)
  1371. {
  1372. ni->ni_rsn_state = RSNA_DISCONNECTED;
  1373. ic->ic_rsnsta--;
  1374. ni->ni_rsn_state = RSNA_INITIALIZE;
  1375. if ((ni->ni_flags & IEEE80211_NODE_REKEY) &&
  1376. --ic->ic_rsn_keydonesta == 0)
  1377. ieee80211_setkeysdone(ic);
  1378. ni->ni_flags &= ~IEEE80211_NODE_REKEY;
  1379. ni->ni_flags &= ~IEEE80211_NODE_PMK;
  1380. ni->ni_rsn_gstate = RSNA_IDLE;
  1381. timeout_del(&ni->ni_eapol_to);
  1382. timeout_del(&ni->ni_sa_query_to);
  1383. ni->ni_rsn_retries = 0;
  1384. ni->ni_flags &= ~IEEE80211_NODE_TXRXPROT;
  1385. ni->ni_port_valid = 0;
  1386. (*ic->ic_delete_key)(ic, ni, &ni->ni_pairwise_key);
  1387. }
  1388. /*
  1389. * Handle a station leaving an 11g network.
  1390. */
  1391. void
  1392. ieee80211_node_leave_11g(struct ieee80211com *ic, struct ieee80211_node *ni)
  1393. {
  1394. if (!(ni->ni_capinfo & IEEE80211_CAPINFO_SHORT_SLOTTIME)) {
  1395. #ifdef DIAGNOSTIC
  1396. if (ic->ic_longslotsta == 0) {
  1397. panic("bogus long slot station count %d",
  1398. ic->ic_longslotsta);
  1399. }
  1400. #endif
  1401. /* leaving STA did not support short slot time */
  1402. if (--ic->ic_longslotsta == 0) {
  1403. /*
  1404. * All associated STAs now support short slot time, so
  1405. * enable this feature and give the driver a chance to
  1406. * reconfigure the hardware. Notice that IBSS always
  1407. * use a long slot time.
  1408. */
  1409. if ((ic->ic_caps & IEEE80211_C_SHSLOT) &&
  1410. ic->ic_opmode != IEEE80211_M_IBSS)
  1411. ieee80211_set_shortslottime(ic, 1);
  1412. }
  1413. DPRINTF(("[%s] long slot time station leaves, count %d\n",
  1414. ether_sprintf(ni->ni_macaddr), ic->ic_longslotsta));
  1415. }
  1416. if (!(ni->ni_flags & IEEE80211_NODE_ERP)) {
  1417. #ifdef DIAGNOSTIC
  1418. if (ic->ic_nonerpsta == 0) {
  1419. panic("bogus non-ERP station count %d",
  1420. ic->ic_nonerpsta);
  1421. }
  1422. #endif
  1423. /* leaving STA was non-ERP */
  1424. if (--ic->ic_nonerpsta == 0) {
  1425. /*
  1426. * All associated STAs are now ERP capable, disable use
  1427. * of protection and re-enable short preamble support.
  1428. */
  1429. ic->ic_flags &= ~IEEE80211_F_USEPROT;
  1430. if (ic->ic_caps & IEEE80211_C_SHPREAMBLE)
  1431. ic->ic_flags |= IEEE80211_F_SHPREAMBLE;
  1432. }
  1433. DPRINTF(("[%s] non-ERP station leaves, count %d\n",
  1434. ether_sprintf(ni->ni_macaddr), ic->ic_nonerpsta));
  1435. }
  1436. }
  1437. /*
  1438. * Handle bookkeeping for station deauthentication/disassociation
  1439. * when operating as an ap.
  1440. */
  1441. void
  1442. ieee80211_node_leave(struct ieee80211com *ic, struct ieee80211_node *ni)
  1443. {
  1444. if (ic->ic_opmode != IEEE80211_M_HOSTAP)
  1445. panic("not in ap mode, mode %u", ic->ic_opmode);
  1446. /*
  1447. * If node wasn't previously associated all we need to do is
  1448. * reclaim the reference.
  1449. */
  1450. if (ni->ni_associd == 0) {
  1451. ieee80211_node_newstate(ni, IEEE80211_STA_COLLECT);
  1452. return;
  1453. }
  1454. if (ni->ni_pwrsave == IEEE80211_PS_DOZE) {
  1455. ic->ic_pssta--;
  1456. ni->ni_pwrsave = IEEE80211_PS_AWAKE;
  1457. }
  1458. if (!IF_IS_EMPTY(&ni->ni_savedq)) {
  1459. IF_PURGE(&ni->ni_savedq);
  1460. if (ic->ic_set_tim != NULL)
  1461. (*ic->ic_set_tim)(ic, ni->ni_associd, 0);
  1462. }
  1463. if (ic->ic_flags & IEEE80211_F_RSNON)
  1464. ieee80211_node_leave_rsn(ic, ni);
  1465. if (ic->ic_curmode == IEEE80211_MODE_11G)
  1466. ieee80211_node_leave_11g(ic, ni);
  1467. #ifndef IEEE80211_NO_HT
  1468. if (ni->ni_flags & IEEE80211_NODE_HT)
  1469. ieee80211_node_leave_ht(ic, ni);
  1470. #endif
  1471. if (ic->ic_node_leave != NULL)
  1472. (*ic->ic_node_leave)(ic, ni);
  1473. IEEE80211_AID_CLR(ni->ni_associd, ic->ic_aid_bitmap);
  1474. ni->ni_associd = 0;
  1475. ieee80211_node_newstate(ni, IEEE80211_STA_COLLECT);
  1476. #if NBRIDGE > 0
  1477. /*
  1478. * If the parent interface is a bridgeport, delete
  1479. * any dynamically learned address for this node.
  1480. */
  1481. if (ic->ic_if.if_bridgeport != NULL)
  1482. bridge_update(&ic->ic_if,
  1483. (struct ether_addr *)ni->ni_macaddr, 1);
  1484. #endif
  1485. }
  1486. static int
  1487. ieee80211_do_slow_print(struct ieee80211com *ic, int *did_print)
  1488. {
  1489. static const struct timeval merge_print_intvl = {
  1490. .tv_sec = 1, .tv_usec = 0
  1491. };
  1492. if ((ic->ic_if.if_flags & IFF_LINK0) == 0)
  1493. return 0;
  1494. if (!*did_print && (ic->ic_if.if_flags & IFF_DEBUG) == 0 &&
  1495. !ratecheck(&ic->ic_last_merge_print, &merge_print_intvl))
  1496. return 0;
  1497. *did_print = 1;
  1498. return 1;
  1499. }
  1500. /* ieee80211_ibss_merge helps merge 802.11 ad hoc networks. The
  1501. * convention, set by the Wireless Ethernet Compatibility Alliance
  1502. * (WECA), is that an 802.11 station will change its BSSID to match
  1503. * the "oldest" 802.11 ad hoc network, on the same channel, that
  1504. * has the station's desired SSID. The "oldest" 802.11 network
  1505. * sends beacons with the greatest TSF timestamp.
  1506. *
  1507. * Return ENETRESET if the BSSID changed, 0 otherwise.
  1508. *
  1509. * XXX Perhaps we should compensate for the time that elapses
  1510. * between the MAC receiving the beacon and the host processing it
  1511. * in ieee80211_ibss_merge.
  1512. */
  1513. int
  1514. ieee80211_ibss_merge(struct ieee80211com *ic, struct ieee80211_node *ni,
  1515. u_int64_t local_tsft)
  1516. {
  1517. u_int64_t beacon_tsft;
  1518. int did_print = 0, sign;
  1519. union {
  1520. u_int64_t word;
  1521. u_int8_t tstamp[8];
  1522. } u;
  1523. /* ensure alignment */
  1524. (void)memcpy(&u, &ni->ni_tstamp[0], sizeof(u));
  1525. beacon_tsft = letoh64(u.word);
  1526. /* we are faster, let the other guy catch up */
  1527. if (beacon_tsft < local_tsft)
  1528. sign = -1;
  1529. else
  1530. sign = 1;
  1531. if (IEEE80211_ADDR_EQ(ni->ni_bssid, ic->ic_bss->ni_bssid)) {
  1532. if (!ieee80211_do_slow_print(ic, &did_print))
  1533. return 0;
  1534. printf("%s: tsft offset %s%llu\n", ic->ic_if.if_xname,
  1535. (sign < 0) ? "-" : "",
  1536. (sign < 0)
  1537. ? (local_tsft - beacon_tsft)
  1538. : (beacon_tsft - local_tsft));
  1539. return 0;
  1540. }
  1541. if (sign < 0)
  1542. return 0;
  1543. if (ieee80211_match_bss(ic, ni) != 0)
  1544. return 0;
  1545. if (ieee80211_do_slow_print(ic, &did_print)) {
  1546. printf("%s: ieee80211_ibss_merge: bssid mismatch %s\n",
  1547. ic->ic_if.if_xname, ether_sprintf(ni->ni_bssid));
  1548. printf("%s: my tsft %llu beacon tsft %llu\n",
  1549. ic->ic_if.if_xname, local_tsft, beacon_tsft);
  1550. printf("%s: sync TSF with %s\n",
  1551. ic->ic_if.if_xname, ether_sprintf(ni->ni_macaddr));
  1552. }
  1553. ic->ic_flags &= ~IEEE80211_F_SIBSS;
  1554. /* negotiate rates with new IBSS */
  1555. ieee80211_fix_rate(ic, ni, IEEE80211_F_DOFRATE |
  1556. IEEE80211_F_DONEGO | IEEE80211_F_DODEL);
  1557. if (ni->ni_rates.rs_nrates == 0) {
  1558. if (ieee80211_do_slow_print(ic, &did_print)) {
  1559. printf("%s: rates mismatch, BSSID %s\n",
  1560. ic->ic_if.if_xname, ether_sprintf(ni->ni_bssid));
  1561. }
  1562. return 0;
  1563. }
  1564. if (ieee80211_do_slow_print(ic, &did_print)) {
  1565. printf("%s: sync BSSID %s -> ",
  1566. ic->ic_if.if_xname, ether_sprintf(ic->ic_bss->ni_bssid));
  1567. printf("%s ", ether_sprintf(ni->ni_bssid));
  1568. printf("(from %s)\n", ether_sprintf(ni->ni_macaddr));
  1569. }
  1570. ieee80211_node_newstate(ni, IEEE80211_STA_BSS);
  1571. (*ic->ic_node_copy)(ic, ic->ic_bss, ni);
  1572. return ENETRESET;
  1573. }
  1574. void
  1575. ieee80211_set_tim(struct ieee80211com *ic, int aid, int set)
  1576. {
  1577. if (set)
  1578. setbit(ic->ic_tim_bitmap, aid & ~0xc000);
  1579. else
  1580. clrbit(ic->ic_tim_bitmap, aid & ~0xc000);
  1581. }
  1582. /*
  1583. * This function shall be called by drivers immediately after every DTIM.
  1584. * Transmit all group addressed MSDUs buffered at the AP.
  1585. */
  1586. void
  1587. ieee80211_notify_dtim(struct ieee80211com *ic)
  1588. {
  1589. /* NB: group addressed MSDUs are buffered in ic_bss */
  1590. struct ieee80211_node *ni = ic->ic_bss;
  1591. struct ifnet *ifp = &ic->ic_if;
  1592. struct ieee80211_frame *wh;
  1593. struct mbuf *m;
  1594. KASSERT(ic->ic_opmode == IEEE80211_M_HOSTAP);
  1595. for (;;) {
  1596. IF_DEQUEUE(&ni->ni_savedq, m);
  1597. if (m == NULL)
  1598. break;
  1599. if (!IF_IS_EMPTY(&ni->ni_savedq)) {
  1600. /* more queued frames, set the more data bit */
  1601. wh = mtod(m, struct ieee80211_frame *);
  1602. wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
  1603. }
  1604. IF_ENQUEUE(&ic->ic_pwrsaveq, m);
  1605. (*ifp->if_start)(ifp);
  1606. }
  1607. /* XXX assumes everything has been sent */
  1608. ic->ic_tim_mcast_pending = 0;
  1609. }
  1610. #endif /* IEEE80211_STA_ONLY */
  1611. /*
  1612. * Compare nodes in the tree by lladdr
  1613. */
  1614. int
  1615. ieee80211_node_cmp(const struct ieee80211_node *b1,
  1616. const struct ieee80211_node *b2)
  1617. {
  1618. return (memcmp(b1->ni_macaddr, b2->ni_macaddr, IEEE80211_ADDR_LEN));
  1619. }
  1620. /*
  1621. * Generate red-black tree function logic
  1622. */
  1623. RB_GENERATE(ieee80211_tree, ieee80211_node, ni_node, ieee80211_node_cmp);