ieee80211.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. /* $OpenBSD: ieee80211.c,v 1.44 2015/03/14 03:38:51 jsg Exp $ */
  2. /* $NetBSD: ieee80211.c,v 1.19 2004/06/06 05:45:29 dyoung Exp $ */
  3. /*-
  4. * Copyright (c) 2001 Atsushi Onoe
  5. * Copyright (c) 2002, 2003 Sam Leffler, Errno Consulting
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. The name of the author may not be used to endorse or promote products
  17. * derived from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  20. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  21. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  24. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  28. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /*
  31. * IEEE 802.11 generic handler
  32. */
  33. #include "bpfilter.h"
  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. #if NBPFILTER > 0
  47. #include <net/bpf.h>
  48. #endif
  49. #include <netinet/in.h>
  50. #include <netinet/if_ether.h>
  51. #include <net80211/ieee80211_var.h>
  52. #include <net80211/ieee80211_priv.h>
  53. #ifdef IEEE80211_DEBUG
  54. int ieee80211_debug = 0;
  55. #endif
  56. int ieee80211_cache_size = IEEE80211_CACHE_SIZE;
  57. struct ieee80211com_head ieee80211com_head =
  58. LIST_HEAD_INITIALIZER(ieee80211com_head);
  59. void ieee80211_setbasicrates(struct ieee80211com *);
  60. int ieee80211_findrate(struct ieee80211com *, enum ieee80211_phymode, int);
  61. void
  62. ieee80211_ifattach(struct ifnet *ifp)
  63. {
  64. struct ieee80211com *ic = (void *)ifp;
  65. struct ieee80211_channel *c;
  66. int i;
  67. memcpy(((struct arpcom *)ifp)->ac_enaddr, ic->ic_myaddr,
  68. ETHER_ADDR_LEN);
  69. ether_ifattach(ifp);
  70. ifp->if_output = ieee80211_output;
  71. #if NBPFILTER > 0
  72. bpfattach(&ic->ic_rawbpf, ifp, DLT_IEEE802_11,
  73. sizeof(struct ieee80211_frame_addr4));
  74. #endif
  75. ieee80211_crypto_attach(ifp);
  76. /*
  77. * Fill in 802.11 available channel set, mark
  78. * all available channels as active, and pick
  79. * a default channel if not already specified.
  80. */
  81. memset(ic->ic_chan_avail, 0, sizeof(ic->ic_chan_avail));
  82. ic->ic_modecaps |= 1<<IEEE80211_MODE_AUTO;
  83. for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
  84. c = &ic->ic_channels[i];
  85. if (c->ic_flags) {
  86. /*
  87. * Verify driver passed us valid data.
  88. */
  89. if (i != ieee80211_chan2ieee(ic, c)) {
  90. printf("%s: bad channel ignored; "
  91. "freq %u flags %x number %u\n",
  92. ifp->if_xname, c->ic_freq, c->ic_flags,
  93. i);
  94. c->ic_flags = 0; /* NB: remove */
  95. continue;
  96. }
  97. setbit(ic->ic_chan_avail, i);
  98. /*
  99. * Identify mode capabilities.
  100. */
  101. if (IEEE80211_IS_CHAN_A(c))
  102. ic->ic_modecaps |= 1<<IEEE80211_MODE_11A;
  103. if (IEEE80211_IS_CHAN_B(c))
  104. ic->ic_modecaps |= 1<<IEEE80211_MODE_11B;
  105. if (IEEE80211_IS_CHAN_PUREG(c))
  106. ic->ic_modecaps |= 1<<IEEE80211_MODE_11G;
  107. if (IEEE80211_IS_CHAN_T(c))
  108. ic->ic_modecaps |= 1<<IEEE80211_MODE_TURBO;
  109. }
  110. }
  111. /* validate ic->ic_curmode */
  112. if ((ic->ic_modecaps & (1<<ic->ic_curmode)) == 0)
  113. ic->ic_curmode = IEEE80211_MODE_AUTO;
  114. ic->ic_des_chan = IEEE80211_CHAN_ANYC; /* any channel is ok */
  115. ic->ic_scan_lock = IEEE80211_SCAN_UNLOCKED;
  116. /* IEEE 802.11 defines a MTU >= 2290 */
  117. ifp->if_capabilities |= IFCAP_VLAN_MTU;
  118. ieee80211_setbasicrates(ic);
  119. (void)ieee80211_setmode(ic, ic->ic_curmode);
  120. if (ic->ic_lintval == 0)
  121. ic->ic_lintval = 100; /* default sleep */
  122. ic->ic_bmisstimeout = 7*ic->ic_lintval; /* default 7 beacons */
  123. ic->ic_dtim_period = 1; /* all TIMs are DTIMs */
  124. LIST_INSERT_HEAD(&ieee80211com_head, ic, ic_list);
  125. ieee80211_node_attach(ifp);
  126. ieee80211_proto_attach(ifp);
  127. if_addgroup(ifp, "wlan");
  128. ifp->if_priority = IF_WIRELESS_DEFAULT_PRIORITY;
  129. }
  130. void
  131. ieee80211_ifdetach(struct ifnet *ifp)
  132. {
  133. struct ieee80211com *ic = (void *)ifp;
  134. ieee80211_proto_detach(ifp);
  135. ieee80211_crypto_detach(ifp);
  136. ieee80211_node_detach(ifp);
  137. LIST_REMOVE(ic, ic_list);
  138. ifmedia_delete_instance(&ic->ic_media, IFM_INST_ANY);
  139. ether_ifdetach(ifp);
  140. }
  141. /*
  142. * Convert MHz frequency to IEEE channel number.
  143. */
  144. u_int
  145. ieee80211_mhz2ieee(u_int freq, u_int flags)
  146. {
  147. if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
  148. if (freq == 2484)
  149. return 14;
  150. if (freq < 2484)
  151. return (freq - 2407) / 5;
  152. else
  153. return 15 + ((freq - 2512) / 20);
  154. } else if (flags & IEEE80211_CHAN_5GHZ) { /* 5GHz band */
  155. return (freq - 5000) / 5;
  156. } else { /* either, guess */
  157. if (freq == 2484)
  158. return 14;
  159. if (freq < 2484)
  160. return (freq - 2407) / 5;
  161. if (freq < 5000)
  162. return 15 + ((freq - 2512) / 20);
  163. return (freq - 5000) / 5;
  164. }
  165. }
  166. /*
  167. * Convert channel to IEEE channel number.
  168. */
  169. u_int
  170. ieee80211_chan2ieee(struct ieee80211com *ic, const struct ieee80211_channel *c)
  171. {
  172. struct ifnet *ifp = &ic->ic_if;
  173. if (ic->ic_channels <= c && c <= &ic->ic_channels[IEEE80211_CHAN_MAX])
  174. return c - ic->ic_channels;
  175. else if (c == IEEE80211_CHAN_ANYC)
  176. return IEEE80211_CHAN_ANY;
  177. else if (c != NULL) {
  178. printf("%s: invalid channel freq %u flags %x\n",
  179. ifp->if_xname, c->ic_freq, c->ic_flags);
  180. return 0; /* XXX */
  181. } else {
  182. printf("%s: invalid channel (NULL)\n", ifp->if_xname);
  183. return 0; /* XXX */
  184. }
  185. }
  186. /*
  187. * Convert IEEE channel number to MHz frequency.
  188. */
  189. u_int
  190. ieee80211_ieee2mhz(u_int chan, u_int flags)
  191. {
  192. if (flags & IEEE80211_CHAN_2GHZ) { /* 2GHz band */
  193. if (chan == 14)
  194. return 2484;
  195. if (chan < 14)
  196. return 2407 + chan*5;
  197. else
  198. return 2512 + ((chan-15)*20);
  199. } else if (flags & IEEE80211_CHAN_5GHZ) {/* 5GHz band */
  200. return 5000 + (chan*5);
  201. } else { /* either, guess */
  202. if (chan == 14)
  203. return 2484;
  204. if (chan < 14) /* 0-13 */
  205. return 2407 + chan*5;
  206. if (chan < 27) /* 15-26 */
  207. return 2512 + ((chan-15)*20);
  208. return 5000 + (chan*5);
  209. }
  210. }
  211. /*
  212. * Setup the media data structures according to the channel and
  213. * rate tables. This must be called by the driver after
  214. * ieee80211_attach and before most anything else.
  215. */
  216. void
  217. ieee80211_media_init(struct ifnet *ifp,
  218. ifm_change_cb_t media_change, ifm_stat_cb_t media_stat)
  219. {
  220. #define ADD(_ic, _s, _o) \
  221. ifmedia_add(&(_ic)->ic_media, \
  222. IFM_MAKEWORD(IFM_IEEE80211, (_s), (_o), 0), 0, NULL)
  223. struct ieee80211com *ic = (void *)ifp;
  224. struct ifmediareq imr;
  225. int i, j, mode, rate, maxrate, mword, mopt, r;
  226. const struct ieee80211_rateset *rs;
  227. struct ieee80211_rateset allrates;
  228. /*
  229. * Do late attach work that must wait for any subclass
  230. * (i.e. driver) work such as overriding methods.
  231. */
  232. ieee80211_node_lateattach(ifp);
  233. /*
  234. * Fill in media characteristics.
  235. */
  236. ifmedia_init(&ic->ic_media, 0, media_change, media_stat);
  237. maxrate = 0;
  238. memset(&allrates, 0, sizeof(allrates));
  239. for (mode = IEEE80211_MODE_AUTO; mode < IEEE80211_MODE_MAX; mode++) {
  240. static const u_int mopts[] = {
  241. IFM_AUTO,
  242. IFM_IEEE80211_11A,
  243. IFM_IEEE80211_11B,
  244. IFM_IEEE80211_11G,
  245. IFM_IEEE80211_11A | IFM_IEEE80211_TURBO,
  246. };
  247. if ((ic->ic_modecaps & (1<<mode)) == 0)
  248. continue;
  249. mopt = mopts[mode];
  250. ADD(ic, IFM_AUTO, mopt); /* e.g. 11a auto */
  251. #ifndef IEEE80211_STA_ONLY
  252. if (ic->ic_caps & IEEE80211_C_IBSS)
  253. ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_IBSS);
  254. if (ic->ic_caps & IEEE80211_C_HOSTAP)
  255. ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_HOSTAP);
  256. if (ic->ic_caps & IEEE80211_C_AHDEMO)
  257. ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_ADHOC);
  258. #endif
  259. if (ic->ic_caps & IEEE80211_C_MONITOR)
  260. ADD(ic, IFM_AUTO, mopt | IFM_IEEE80211_MONITOR);
  261. if (mode == IEEE80211_MODE_AUTO)
  262. continue;
  263. rs = &ic->ic_sup_rates[mode];
  264. for (i = 0; i < rs->rs_nrates; i++) {
  265. rate = rs->rs_rates[i];
  266. mword = ieee80211_rate2media(ic, rate, mode);
  267. if (mword == 0)
  268. continue;
  269. ADD(ic, mword, mopt);
  270. #ifndef IEEE80211_STA_ONLY
  271. if (ic->ic_caps & IEEE80211_C_IBSS)
  272. ADD(ic, mword, mopt | IFM_IEEE80211_IBSS);
  273. if (ic->ic_caps & IEEE80211_C_HOSTAP)
  274. ADD(ic, mword, mopt | IFM_IEEE80211_HOSTAP);
  275. if (ic->ic_caps & IEEE80211_C_AHDEMO)
  276. ADD(ic, mword, mopt | IFM_IEEE80211_ADHOC);
  277. #endif
  278. if (ic->ic_caps & IEEE80211_C_MONITOR)
  279. ADD(ic, mword, mopt | IFM_IEEE80211_MONITOR);
  280. /*
  281. * Add rate to the collection of all rates.
  282. */
  283. r = rate & IEEE80211_RATE_VAL;
  284. for (j = 0; j < allrates.rs_nrates; j++)
  285. if (allrates.rs_rates[j] == r)
  286. break;
  287. if (j == allrates.rs_nrates) {
  288. /* unique, add to the set */
  289. allrates.rs_rates[j] = r;
  290. allrates.rs_nrates++;
  291. }
  292. rate = (rate & IEEE80211_RATE_VAL) / 2;
  293. if (rate > maxrate)
  294. maxrate = rate;
  295. }
  296. }
  297. for (i = 0; i < allrates.rs_nrates; i++) {
  298. mword = ieee80211_rate2media(ic, allrates.rs_rates[i],
  299. IEEE80211_MODE_AUTO);
  300. if (mword == 0)
  301. continue;
  302. mword = IFM_SUBTYPE(mword); /* remove media options */
  303. ADD(ic, mword, 0);
  304. #ifndef IEEE80211_STA_ONLY
  305. if (ic->ic_caps & IEEE80211_C_IBSS)
  306. ADD(ic, mword, IFM_IEEE80211_IBSS);
  307. if (ic->ic_caps & IEEE80211_C_HOSTAP)
  308. ADD(ic, mword, IFM_IEEE80211_HOSTAP);
  309. if (ic->ic_caps & IEEE80211_C_AHDEMO)
  310. ADD(ic, mword, IFM_IEEE80211_ADHOC);
  311. #endif
  312. if (ic->ic_caps & IEEE80211_C_MONITOR)
  313. ADD(ic, mword, IFM_IEEE80211_MONITOR);
  314. }
  315. ieee80211_media_status(ifp, &imr);
  316. ifmedia_set(&ic->ic_media, imr.ifm_active);
  317. if (maxrate)
  318. ifp->if_baudrate = IF_Mbps(maxrate);
  319. #undef ADD
  320. }
  321. int
  322. ieee80211_findrate(struct ieee80211com *ic, enum ieee80211_phymode mode,
  323. int rate)
  324. {
  325. #define IEEERATE(_ic,_m,_i) \
  326. ((_ic)->ic_sup_rates[_m].rs_rates[_i] & IEEE80211_RATE_VAL)
  327. int i, nrates = ic->ic_sup_rates[mode].rs_nrates;
  328. for (i = 0; i < nrates; i++)
  329. if (IEEERATE(ic, mode, i) == rate)
  330. return i;
  331. return -1;
  332. #undef IEEERATE
  333. }
  334. /*
  335. * Handle a media change request.
  336. */
  337. int
  338. ieee80211_media_change(struct ifnet *ifp)
  339. {
  340. struct ieee80211com *ic = (void *)ifp;
  341. struct ifmedia_entry *ime;
  342. enum ieee80211_opmode newopmode;
  343. enum ieee80211_phymode newphymode;
  344. int i, j, newrate, error = 0;
  345. ime = ic->ic_media.ifm_cur;
  346. /*
  347. * First, identify the phy mode.
  348. */
  349. switch (IFM_MODE(ime->ifm_media)) {
  350. case IFM_IEEE80211_11A:
  351. newphymode = IEEE80211_MODE_11A;
  352. break;
  353. case IFM_IEEE80211_11B:
  354. newphymode = IEEE80211_MODE_11B;
  355. break;
  356. case IFM_IEEE80211_11G:
  357. newphymode = IEEE80211_MODE_11G;
  358. break;
  359. case IFM_AUTO:
  360. newphymode = IEEE80211_MODE_AUTO;
  361. break;
  362. default:
  363. return EINVAL;
  364. }
  365. /*
  366. * Turbo mode is an ``option''. Eventually it
  367. * needs to be applied to 11g too.
  368. */
  369. if (ime->ifm_media & IFM_IEEE80211_TURBO) {
  370. if (newphymode != IEEE80211_MODE_11A)
  371. return EINVAL;
  372. newphymode = IEEE80211_MODE_TURBO;
  373. }
  374. /*
  375. * Validate requested mode is available.
  376. */
  377. if ((ic->ic_modecaps & (1<<newphymode)) == 0)
  378. return EINVAL;
  379. /*
  380. * Next, the fixed/variable rate.
  381. */
  382. i = -1;
  383. if (IFM_SUBTYPE(ime->ifm_media) != IFM_AUTO) {
  384. /*
  385. * Convert media subtype to rate.
  386. */
  387. newrate = ieee80211_media2rate(ime->ifm_media);
  388. if (newrate == 0)
  389. return EINVAL;
  390. /*
  391. * Check the rate table for the specified/current phy.
  392. */
  393. if (newphymode == IEEE80211_MODE_AUTO) {
  394. /*
  395. * In autoselect mode search for the rate.
  396. */
  397. for (j = IEEE80211_MODE_11A;
  398. j < IEEE80211_MODE_MAX; j++) {
  399. if ((ic->ic_modecaps & (1<<j)) == 0)
  400. continue;
  401. i = ieee80211_findrate(ic, j, newrate);
  402. if (i != -1) {
  403. /* lock mode too */
  404. newphymode = j;
  405. break;
  406. }
  407. }
  408. } else {
  409. i = ieee80211_findrate(ic, newphymode, newrate);
  410. }
  411. if (i == -1) /* mode/rate mismatch */
  412. return EINVAL;
  413. }
  414. /* NB: defer rate setting to later */
  415. /*
  416. * Deduce new operating mode but don't install it just yet.
  417. */
  418. #ifndef IEEE80211_STA_ONLY
  419. if (ime->ifm_media & IFM_IEEE80211_ADHOC)
  420. newopmode = IEEE80211_M_AHDEMO;
  421. else if (ime->ifm_media & IFM_IEEE80211_HOSTAP)
  422. newopmode = IEEE80211_M_HOSTAP;
  423. else if (ime->ifm_media & IFM_IEEE80211_IBSS)
  424. newopmode = IEEE80211_M_IBSS;
  425. else
  426. #endif
  427. if (ime->ifm_media & IFM_IEEE80211_MONITOR)
  428. newopmode = IEEE80211_M_MONITOR;
  429. else
  430. newopmode = IEEE80211_M_STA;
  431. #ifndef IEEE80211_STA_ONLY
  432. /*
  433. * Autoselect doesn't make sense when operating as an AP.
  434. * If no phy mode has been selected, pick one and lock it
  435. * down so rate tables can be used in forming beacon frames
  436. * and the like.
  437. */
  438. if (newopmode == IEEE80211_M_HOSTAP &&
  439. newphymode == IEEE80211_MODE_AUTO) {
  440. for (j = IEEE80211_MODE_11A; j < IEEE80211_MODE_MAX; j++)
  441. if (ic->ic_modecaps & (1<<j)) {
  442. newphymode = j;
  443. break;
  444. }
  445. }
  446. #endif
  447. /*
  448. * Handle phy mode change.
  449. */
  450. if (ic->ic_curmode != newphymode) { /* change phy mode */
  451. error = ieee80211_setmode(ic, newphymode);
  452. if (error != 0)
  453. return error;
  454. error = ENETRESET;
  455. }
  456. /*
  457. * Committed to changes, install the rate setting.
  458. */
  459. if (ic->ic_fixed_rate != i) {
  460. ic->ic_fixed_rate = i; /* set fixed tx rate */
  461. error = ENETRESET;
  462. }
  463. /*
  464. * Handle operating mode change.
  465. */
  466. if (ic->ic_opmode != newopmode) {
  467. ic->ic_opmode = newopmode;
  468. #ifndef IEEE80211_STA_ONLY
  469. switch (newopmode) {
  470. case IEEE80211_M_AHDEMO:
  471. case IEEE80211_M_HOSTAP:
  472. case IEEE80211_M_STA:
  473. case IEEE80211_M_MONITOR:
  474. ic->ic_flags &= ~IEEE80211_F_IBSSON;
  475. break;
  476. case IEEE80211_M_IBSS:
  477. ic->ic_flags |= IEEE80211_F_IBSSON;
  478. break;
  479. }
  480. #endif
  481. /*
  482. * Yech, slot time may change depending on the
  483. * operating mode so reset it to be sure everything
  484. * is setup appropriately.
  485. */
  486. ieee80211_reset_erp(ic);
  487. error = ENETRESET;
  488. }
  489. #ifdef notdef
  490. if (error == 0)
  491. ifp->if_baudrate = ifmedia_baudrate(ime->ifm_media);
  492. #endif
  493. return error;
  494. }
  495. void
  496. ieee80211_media_status(struct ifnet *ifp, struct ifmediareq *imr)
  497. {
  498. struct ieee80211com *ic = (void *)ifp;
  499. const struct ieee80211_node *ni = NULL;
  500. imr->ifm_status = IFM_AVALID;
  501. imr->ifm_active = IFM_IEEE80211;
  502. if (ic->ic_state == IEEE80211_S_RUN &&
  503. (ic->ic_opmode != IEEE80211_M_STA ||
  504. !(ic->ic_flags & IEEE80211_F_RSNON) ||
  505. ic->ic_bss->ni_port_valid))
  506. imr->ifm_status |= IFM_ACTIVE;
  507. imr->ifm_active |= IFM_AUTO;
  508. switch (ic->ic_opmode) {
  509. case IEEE80211_M_STA:
  510. ni = ic->ic_bss;
  511. /* calculate rate subtype */
  512. imr->ifm_active |= ieee80211_rate2media(ic,
  513. ni->ni_rates.rs_rates[ni->ni_txrate], ic->ic_curmode);
  514. break;
  515. #ifndef IEEE80211_STA_ONLY
  516. case IEEE80211_M_IBSS:
  517. imr->ifm_active |= IFM_IEEE80211_IBSS;
  518. break;
  519. case IEEE80211_M_AHDEMO:
  520. imr->ifm_active |= IFM_IEEE80211_ADHOC;
  521. break;
  522. case IEEE80211_M_HOSTAP:
  523. imr->ifm_active |= IFM_IEEE80211_HOSTAP;
  524. break;
  525. #endif
  526. case IEEE80211_M_MONITOR:
  527. imr->ifm_active |= IFM_IEEE80211_MONITOR;
  528. break;
  529. default:
  530. break;
  531. }
  532. switch (ic->ic_curmode) {
  533. case IEEE80211_MODE_11A:
  534. imr->ifm_active |= IFM_IEEE80211_11A;
  535. break;
  536. case IEEE80211_MODE_11B:
  537. imr->ifm_active |= IFM_IEEE80211_11B;
  538. break;
  539. case IEEE80211_MODE_11G:
  540. imr->ifm_active |= IFM_IEEE80211_11G;
  541. break;
  542. case IEEE80211_MODE_TURBO:
  543. imr->ifm_active |= IFM_IEEE80211_11A
  544. | IFM_IEEE80211_TURBO;
  545. break;
  546. }
  547. }
  548. void
  549. ieee80211_watchdog(struct ifnet *ifp)
  550. {
  551. struct ieee80211com *ic = (void *)ifp;
  552. if (ic->ic_mgt_timer && --ic->ic_mgt_timer == 0)
  553. ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
  554. if (ic->ic_mgt_timer != 0)
  555. ifp->if_timer = 1;
  556. }
  557. const struct ieee80211_rateset ieee80211_std_rateset_11a =
  558. { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
  559. const struct ieee80211_rateset ieee80211_std_rateset_11b =
  560. { 4, { 2, 4, 11, 22 } };
  561. const struct ieee80211_rateset ieee80211_std_rateset_11g =
  562. { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
  563. /*
  564. * Mark the basic rates for the 11g rate table based on the
  565. * operating mode. For real 11g we mark all the 11b rates
  566. * and 6, 12, and 24 OFDM. For 11b compatibility we mark only
  567. * 11b rates. There's also a pseudo 11a-mode used to mark only
  568. * the basic OFDM rates.
  569. */
  570. void
  571. ieee80211_setbasicrates(struct ieee80211com *ic)
  572. {
  573. static const struct ieee80211_rateset basic[] = {
  574. { 0 }, /* IEEE80211_MODE_AUTO */
  575. { 3, { 12, 24, 48 } }, /* IEEE80211_MODE_11A */
  576. { 2, { 2, 4 } }, /* IEEE80211_MODE_11B */
  577. { 4, { 2, 4, 11, 22 } }, /* IEEE80211_MODE_11G */
  578. { 0 }, /* IEEE80211_MODE_TURBO */
  579. };
  580. enum ieee80211_phymode mode;
  581. struct ieee80211_rateset *rs;
  582. int i, j;
  583. for (mode = 0; mode < IEEE80211_MODE_MAX; mode++) {
  584. rs = &ic->ic_sup_rates[mode];
  585. for (i = 0; i < rs->rs_nrates; i++) {
  586. rs->rs_rates[i] &= IEEE80211_RATE_VAL;
  587. for (j = 0; j < basic[mode].rs_nrates; j++) {
  588. if (basic[mode].rs_rates[j] ==
  589. rs->rs_rates[i]) {
  590. rs->rs_rates[i] |=
  591. IEEE80211_RATE_BASIC;
  592. break;
  593. }
  594. }
  595. }
  596. }
  597. }
  598. /*
  599. * Set the current phy mode and recalculate the active channel
  600. * set based on the available channels for this mode. Also
  601. * select a new default/current channel if the current one is
  602. * inappropriate for this mode.
  603. */
  604. int
  605. ieee80211_setmode(struct ieee80211com *ic, enum ieee80211_phymode mode)
  606. {
  607. struct ifnet *ifp = &ic->ic_if;
  608. static const u_int chanflags[] = {
  609. 0, /* IEEE80211_MODE_AUTO */
  610. IEEE80211_CHAN_A, /* IEEE80211_MODE_11A */
  611. IEEE80211_CHAN_B, /* IEEE80211_MODE_11B */
  612. IEEE80211_CHAN_PUREG, /* IEEE80211_MODE_11G */
  613. IEEE80211_CHAN_T, /* IEEE80211_MODE_TURBO */
  614. };
  615. const struct ieee80211_channel *c;
  616. u_int modeflags;
  617. int i;
  618. /* validate new mode */
  619. if ((ic->ic_modecaps & (1<<mode)) == 0) {
  620. DPRINTF(("mode %u not supported (caps 0x%x)\n",
  621. mode, ic->ic_modecaps));
  622. return EINVAL;
  623. }
  624. /*
  625. * Verify at least one channel is present in the available
  626. * channel list before committing to the new mode.
  627. */
  628. if (mode >= nitems(chanflags))
  629. panic("Unexpected mode %u", mode);
  630. modeflags = chanflags[mode];
  631. for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
  632. c = &ic->ic_channels[i];
  633. if (mode == IEEE80211_MODE_AUTO) {
  634. /* ignore turbo channels for autoselect */
  635. if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
  636. break;
  637. } else {
  638. if ((c->ic_flags & modeflags) == modeflags)
  639. break;
  640. }
  641. }
  642. if (i > IEEE80211_CHAN_MAX) {
  643. DPRINTF(("no channels found for mode %u\n", mode));
  644. return EINVAL;
  645. }
  646. /*
  647. * Calculate the active channel set.
  648. */
  649. memset(ic->ic_chan_active, 0, sizeof(ic->ic_chan_active));
  650. for (i = 0; i <= IEEE80211_CHAN_MAX; i++) {
  651. c = &ic->ic_channels[i];
  652. if (mode == IEEE80211_MODE_AUTO) {
  653. /* take anything but pure turbo channels */
  654. if ((c->ic_flags &~ IEEE80211_CHAN_TURBO) != 0)
  655. setbit(ic->ic_chan_active, i);
  656. } else {
  657. if ((c->ic_flags & modeflags) == modeflags)
  658. setbit(ic->ic_chan_active, i);
  659. }
  660. }
  661. /*
  662. * If no current/default channel is setup or the current
  663. * channel is wrong for the mode then pick the first
  664. * available channel from the active list. This is likely
  665. * not the right one.
  666. */
  667. if (ic->ic_ibss_chan == NULL || isclr(ic->ic_chan_active,
  668. ieee80211_chan2ieee(ic, ic->ic_ibss_chan))) {
  669. for (i = 0; i <= IEEE80211_CHAN_MAX; i++)
  670. if (isset(ic->ic_chan_active, i)) {
  671. ic->ic_ibss_chan = &ic->ic_channels[i];
  672. break;
  673. }
  674. if ((ic->ic_ibss_chan == NULL) || isclr(ic->ic_chan_active,
  675. ieee80211_chan2ieee(ic, ic->ic_ibss_chan)))
  676. panic("Bad IBSS channel %u",
  677. ieee80211_chan2ieee(ic, ic->ic_ibss_chan));
  678. }
  679. /*
  680. * Reset the scan state for the new mode. This avoids scanning
  681. * of invalid channels, ie. 5GHz channels in 11b mode.
  682. */
  683. ieee80211_reset_scan(ifp);
  684. ic->ic_curmode = mode;
  685. ieee80211_reset_erp(ic); /* reset ERP state */
  686. return 0;
  687. }
  688. enum ieee80211_phymode
  689. ieee80211_next_mode(struct ifnet *ifp)
  690. {
  691. struct ieee80211com *ic = (void *)ifp;
  692. if (IFM_MODE(ic->ic_media.ifm_cur->ifm_media) != IFM_AUTO) {
  693. /*
  694. * Reset the scan state and indicate a wrap around
  695. * if we're running in a fixed, user-specified phy mode.
  696. */
  697. ieee80211_reset_scan(ifp);
  698. return (IEEE80211_MODE_AUTO);
  699. }
  700. /*
  701. * Get the next supported mode
  702. */
  703. for (++ic->ic_curmode;
  704. ic->ic_curmode <= IEEE80211_MODE_TURBO;
  705. ic->ic_curmode++) {
  706. /* Wrap around and ignore turbo mode */
  707. if (ic->ic_curmode >= IEEE80211_MODE_TURBO) {
  708. ic->ic_curmode = IEEE80211_MODE_AUTO;
  709. break;
  710. }
  711. if (ic->ic_modecaps & (1 << ic->ic_curmode))
  712. break;
  713. }
  714. ieee80211_setmode(ic, ic->ic_curmode);
  715. return (ic->ic_curmode);
  716. }
  717. /*
  718. * Return the phy mode for with the specified channel so the
  719. * caller can select a rate set. This is problematic and the
  720. * work here assumes how things work elsewhere in this code.
  721. *
  722. * XXX never returns turbo modes -dcy
  723. */
  724. enum ieee80211_phymode
  725. ieee80211_chan2mode(struct ieee80211com *ic,
  726. const struct ieee80211_channel *chan)
  727. {
  728. /*
  729. * NB: this assumes the channel would not be supplied to us
  730. * unless it was already compatible with the current mode.
  731. */
  732. if (ic->ic_curmode != IEEE80211_MODE_AUTO ||
  733. chan == IEEE80211_CHAN_ANYC)
  734. return ic->ic_curmode;
  735. /*
  736. * In autoselect mode; deduce a mode based on the channel
  737. * characteristics. We assume that turbo-only channels
  738. * are not considered when the channel set is constructed.
  739. */
  740. if (IEEE80211_IS_CHAN_T(chan))
  741. return IEEE80211_MODE_TURBO;
  742. else if (IEEE80211_IS_CHAN_5GHZ(chan))
  743. return IEEE80211_MODE_11A;
  744. else if (chan->ic_flags & (IEEE80211_CHAN_OFDM|IEEE80211_CHAN_DYN))
  745. return IEEE80211_MODE_11G;
  746. else
  747. return IEEE80211_MODE_11B;
  748. }
  749. /*
  750. * convert IEEE80211 rate value to ifmedia subtype.
  751. * ieee80211 rate is in unit of 0.5Mbps.
  752. */
  753. int
  754. ieee80211_rate2media(struct ieee80211com *ic, int rate,
  755. enum ieee80211_phymode mode)
  756. {
  757. static const struct {
  758. u_int m; /* rate + mode */
  759. u_int r; /* if_media rate */
  760. } rates[] = {
  761. { 2 | IFM_IEEE80211_11B, IFM_IEEE80211_DS1 },
  762. { 4 | IFM_IEEE80211_11B, IFM_IEEE80211_DS2 },
  763. { 11 | IFM_IEEE80211_11B, IFM_IEEE80211_DS5 },
  764. { 22 | IFM_IEEE80211_11B, IFM_IEEE80211_DS11 },
  765. { 44 | IFM_IEEE80211_11B, IFM_IEEE80211_DS22 },
  766. { 12 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM6 },
  767. { 18 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM9 },
  768. { 24 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM12 },
  769. { 36 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM18 },
  770. { 48 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM24 },
  771. { 72 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM36 },
  772. { 96 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM48 },
  773. { 108 | IFM_IEEE80211_11A, IFM_IEEE80211_OFDM54 },
  774. { 2 | IFM_IEEE80211_11G, IFM_IEEE80211_DS1 },
  775. { 4 | IFM_IEEE80211_11G, IFM_IEEE80211_DS2 },
  776. { 11 | IFM_IEEE80211_11G, IFM_IEEE80211_DS5 },
  777. { 22 | IFM_IEEE80211_11G, IFM_IEEE80211_DS11 },
  778. { 12 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM6 },
  779. { 18 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM9 },
  780. { 24 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM12 },
  781. { 36 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM18 },
  782. { 48 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM24 },
  783. { 72 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM36 },
  784. { 96 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM48 },
  785. { 108 | IFM_IEEE80211_11G, IFM_IEEE80211_OFDM54 },
  786. /* NB: OFDM72 doesn't really exist so we don't handle it */
  787. };
  788. u_int mask, i;
  789. mask = rate & IEEE80211_RATE_VAL;
  790. switch (mode) {
  791. case IEEE80211_MODE_11A:
  792. case IEEE80211_MODE_TURBO:
  793. mask |= IFM_IEEE80211_11A;
  794. break;
  795. case IEEE80211_MODE_11B:
  796. mask |= IFM_IEEE80211_11B;
  797. break;
  798. case IEEE80211_MODE_AUTO:
  799. /* NB: hack, 11g matches both 11b+11a rates */
  800. /* FALLTHROUGH */
  801. case IEEE80211_MODE_11G:
  802. mask |= IFM_IEEE80211_11G;
  803. break;
  804. }
  805. for (i = 0; i < nitems(rates); i++)
  806. if (rates[i].m == mask)
  807. return rates[i].r;
  808. return IFM_AUTO;
  809. }
  810. int
  811. ieee80211_media2rate(int mword)
  812. {
  813. int i;
  814. static const struct {
  815. int subtype;
  816. int rate;
  817. } ieeerates[] = {
  818. { IFM_AUTO, -1 },
  819. { IFM_MANUAL, 0 },
  820. { IFM_NONE, 0 },
  821. { IFM_IEEE80211_DS1, 2 },
  822. { IFM_IEEE80211_DS2, 4 },
  823. { IFM_IEEE80211_DS5, 11 },
  824. { IFM_IEEE80211_DS11, 22 },
  825. { IFM_IEEE80211_DS22, 44 },
  826. { IFM_IEEE80211_OFDM6, 12 },
  827. { IFM_IEEE80211_OFDM9, 18 },
  828. { IFM_IEEE80211_OFDM12, 24 },
  829. { IFM_IEEE80211_OFDM18, 36 },
  830. { IFM_IEEE80211_OFDM24, 48 },
  831. { IFM_IEEE80211_OFDM36, 72 },
  832. { IFM_IEEE80211_OFDM48, 96 },
  833. { IFM_IEEE80211_OFDM54, 108 },
  834. { IFM_IEEE80211_OFDM72, 144 },
  835. };
  836. for (i = 0; i < nitems(ieeerates); i++) {
  837. if (ieeerates[i].subtype == IFM_SUBTYPE(mword))
  838. return ieeerates[i].rate;
  839. }
  840. return 0;
  841. }
  842. /*
  843. * Convert bit rate (in 0.5Mbps units) to PLCP signal (R4-R1) and vice versa.
  844. */
  845. u_int8_t
  846. ieee80211_rate2plcp(u_int8_t rate, enum ieee80211_phymode mode)
  847. {
  848. rate &= IEEE80211_RATE_VAL;
  849. if (mode == IEEE80211_MODE_11B) {
  850. /* IEEE Std 802.11b-1999 page 15, subclause 18.2.3.3 */
  851. switch (rate) {
  852. case 2: return 10;
  853. case 4: return 20;
  854. case 11: return 55;
  855. case 22: return 110;
  856. /* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */
  857. case 44: return 220;
  858. }
  859. } else if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11A) {
  860. /* IEEE Std 802.11a-1999 page 14, subclause 17.3.4.1 */
  861. switch (rate) {
  862. case 12: return 0x0b;
  863. case 18: return 0x0f;
  864. case 24: return 0x0a;
  865. case 36: return 0x0e;
  866. case 48: return 0x09;
  867. case 72: return 0x0d;
  868. case 96: return 0x08;
  869. case 108: return 0x0c;
  870. }
  871. } else
  872. panic("Unexpected mode %u", mode);
  873. DPRINTF(("unsupported rate %u\n", rate));
  874. return 0;
  875. }
  876. u_int8_t
  877. ieee80211_plcp2rate(u_int8_t plcp, enum ieee80211_phymode mode)
  878. {
  879. if (mode == IEEE80211_MODE_11B) {
  880. /* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */
  881. switch (plcp) {
  882. case 10: return 2;
  883. case 20: return 4;
  884. case 55: return 11;
  885. case 110: return 22;
  886. /* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */
  887. case 220: return 44;
  888. }
  889. } else if (mode == IEEE80211_MODE_11G || mode == IEEE80211_MODE_11A) {
  890. /* IEEE Std 802.11a-1999 page 14, subclause 17.3.4.1 */
  891. switch (plcp) {
  892. case 0x0b: return 12;
  893. case 0x0f: return 18;
  894. case 0x0a: return 24;
  895. case 0x0e: return 36;
  896. case 0x09: return 48;
  897. case 0x0d: return 72;
  898. case 0x08: return 96;
  899. case 0x0c: return 108;
  900. }
  901. } else
  902. panic("unexpected mode %u", mode);
  903. DPRINTF(("unsupported plcp %u\n", plcp));
  904. return 0;
  905. }