Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

net80211.h 39KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. #ifndef _GPXE_NET80211_H
  2. #define _GPXE_NET80211_H
  3. #include <gpxe/process.h>
  4. #include <gpxe/ieee80211.h>
  5. #include <gpxe/iobuf.h>
  6. #include <gpxe/netdevice.h>
  7. #include <gpxe/rc80211.h>
  8. /** @file
  9. *
  10. * The gPXE 802.11 MAC layer.
  11. */
  12. /*
  13. * Major things NOT YET supported:
  14. * - any type of security
  15. * - 802.11n
  16. *
  17. * Major things that probably will NEVER be supported, barring a
  18. * compelling use case and/or corporate sponsorship:
  19. * - QoS
  20. * - 802.1X authentication ("WPA Enterprise")
  21. * - Contention-free periods
  22. * - "ad-hoc" networks (IBSS), monitor mode, host AP mode
  23. * - hidden networks on the 5GHz band due to regulatory issues
  24. * - spectrum management on the 5GHz band (TPC and DFS), as required
  25. * in some non-US regulatory domains
  26. * - Clause 14 PHYs (Frequency-Hopping Spread Spectrum on 2.4GHz)
  27. * and Clause 16 PHYs (infrared) - I'm not aware of any real-world
  28. * use of these.
  29. */
  30. FILE_LICENCE ( GPL2_OR_LATER );
  31. /* All 802.11 devices are handled using a generic "802.11 device"
  32. net_device, with a link in its `priv' field to a net80211_device
  33. which we use to handle 802.11-specific details. */
  34. /** @defgroup net80211_band RF bands on which an 802.11 device can transmit */
  35. /** @{ */
  36. /** The 2.4 GHz ISM band, unlicensed in most countries */
  37. #define NET80211_BAND_2GHZ 0
  38. /** The band from 4.9 GHz to 5.7 GHz, which tends to be more restricted */
  39. #define NET80211_BAND_5GHZ 1
  40. /** Number of RF bands */
  41. #define NET80211_NR_BANDS 2
  42. /** Bitmask for the 2GHz band */
  43. #define NET80211_BAND_BIT_2GHZ (1 << 0)
  44. /** Bitmask for the 5GHz band */
  45. #define NET80211_BAND_BIT_5GHZ (1 << 1)
  46. /** @} */
  47. /** @defgroup net80211_mode 802.11 operation modes supported by hardware */
  48. /** @{ */
  49. /** 802.11a: 54 Mbps operation using OFDM signaling on the 5GHz band */
  50. #define NET80211_MODE_A (1 << 0)
  51. /** 802.11b: 1-11 Mbps operation using DSSS/CCK signaling on the 2.4GHz band */
  52. #define NET80211_MODE_B (1 << 1)
  53. /** 802.11g: 54 Mbps operation using ERP/OFDM signaling on the 2.4GHz band */
  54. #define NET80211_MODE_G (1 << 2)
  55. /** 802.11n: High-rate operation using MIMO technology on 2.4GHz or 5GHz */
  56. #define NET80211_MODE_N (1 << 3)
  57. /** @} */
  58. /** @defgroup net80211_cfg Constants for the net80211 config callback */
  59. /** @{ */
  60. /** Channel choice (@c dev->channel) or regulatory parameters have changed */
  61. #define NET80211_CFG_CHANNEL (1 << 0)
  62. /** Requested transmission rate (@c dev->rate) has changed */
  63. #define NET80211_CFG_RATE (1 << 1)
  64. /** Association has been established with a new BSS (@c dev->bssid) */
  65. #define NET80211_CFG_ASSOC (1 << 2)
  66. /** Low-level link parameters (short preamble, protection, etc) have changed */
  67. #define NET80211_CFG_PHY_PARAMS (1 << 3)
  68. /** @} */
  69. /** An 802.11 security handshaking protocol */
  70. enum net80211_security_proto {
  71. /** No security handshaking
  72. *
  73. * This might be used with an open network or with WEP, as
  74. * WEP does not have a cryptographic handshaking phase.
  75. */
  76. NET80211_SECPROT_NONE = 0,
  77. /** Pre-shared key handshaking
  78. *
  79. * This implements the "WPA Personal" handshake. 802.1X
  80. * authentication is not performed -- the user supplies a
  81. * pre-shared key directly -- but there is a 4-way handshake
  82. * between client and AP to verify that both have the same key
  83. * without revealing the contents of that key.
  84. */
  85. NET80211_SECPROT_PSK = 1,
  86. /** Full EAP 802.1X handshaking
  87. *
  88. * This implements the "WPA Enterprise" handshake, connecting
  89. * to an 802.1X authentication server to provide credentials
  90. * and receive a pairwise master key (PMK), which is then used
  91. * in the same 4-way handshake as the PSK method.
  92. */
  93. NET80211_SECPROT_EAP = 2,
  94. /** Dummy value used when the handshaking type can't be detected */
  95. NET80211_SECPROT_UNKNOWN = 3,
  96. };
  97. /** An 802.11 data encryption algorithm */
  98. enum net80211_crypto_alg {
  99. /** No security, an "Open" network */
  100. NET80211_CRYPT_NONE = 0,
  101. /** Network protected with WEP (awful RC4-based system)
  102. *
  103. * WEP uses a naive application of RC4, with a monotonically
  104. * increasing initialization vector that is prepended to the
  105. * key to initialize the RC4 keystream. It is highly insecure
  106. * and can be completely cracked or subverted using automated,
  107. * robust, freely available tools (aircrack-ng) in minutes.
  108. *
  109. * 40-bit and 104-bit WEP are differentiated only by the size
  110. * of the key. They may be advertised as 64-bit and 128-bit,
  111. * counting the non-random IV as part of the key bits.
  112. */
  113. NET80211_CRYPT_WEP = 1,
  114. /** Network protected with TKIP (better RC4-based system)
  115. *
  116. * Usually known by its trade name of WPA (Wi-Fi Protected
  117. * Access), TKIP implements a message integrity code (MIC)
  118. * called Michael, a timestamp counter for replay prevention,
  119. * and a key mixing function that together remove almost all
  120. * the security problems with WEP. Countermeasures are
  121. * implemented to prevent high data-rate attacks.
  122. *
  123. * There exists one known attack on TKIP, that allows one to
  124. * send between 7 and 15 arbitrary short data packets on a
  125. * QoS-enabled network given about an hour of data
  126. * gathering. Since gPXE does not support QoS for 802.11
  127. * networks, this is not a threat to us. The only other method
  128. * is a brute-force passphrase attack.
  129. */
  130. NET80211_CRYPT_TKIP = 2,
  131. /** Network protected with CCMP (AES-based system)
  132. *
  133. * Often called WPA2 in commerce, or RSNA (Robust Security
  134. * Network Architecture) in the 802.11 standard, CCMP is
  135. * highly secure and does not have any known attack vectors.
  136. * Since it is based on a block cipher, the statistical
  137. * correlation and "chopchop" attacks used with great success
  138. * against WEP and minor success against TKIP fail.
  139. */
  140. NET80211_CRYPT_CCMP = 3,
  141. /** Dummy value used when the cryptosystem can't be detected */
  142. NET80211_CRYPT_UNKNOWN = 4,
  143. };
  144. /** @defgroup net80211_state Bits for the 802.11 association state field */
  145. /** @{ */
  146. /** An error code indicating the failure mode, or 0 if successful */
  147. #define NET80211_STATUS_MASK 0x7F
  148. /** Whether the error code provided is a "reason" code, not a "status" code */
  149. #define NET80211_IS_REASON 0x80
  150. /** Whether we have found the network we will be associating with */
  151. #define NET80211_PROBED (1 << 8)
  152. /** Whether we have successfully authenticated with the network
  153. *
  154. * This usually has nothing to do with actual security; it is a
  155. * holdover from older 802.11 implementation ideas.
  156. */
  157. #define NET80211_AUTHENTICATED (1 << 9)
  158. /** Whether we have successfully associated with the network */
  159. #define NET80211_ASSOCIATED (1 << 10)
  160. /** Whether we have completed security handshaking with the network
  161. *
  162. * Once this is set, we can send data packets. For that reason this
  163. * bit is set even in cases where no security handshaking is
  164. * required.
  165. */
  166. #define NET80211_CRYPTO_SYNCED (1 << 11)
  167. /** Whether the auto-association task is running */
  168. #define NET80211_WORKING (1 << 12)
  169. /** Whether the auto-association task is waiting for a reply from the AP */
  170. #define NET80211_WAITING (1 << 13)
  171. /** Whether the auto-association task should be suppressed
  172. *
  173. * This is set by the `iwlist' command so that it can open the device
  174. * without starting another probe process that will interfere with its
  175. * own.
  176. */
  177. #define NET80211_NO_ASSOC (1 << 14)
  178. /** Whether this association was performed using a broadcast SSID
  179. *
  180. * If the user opened this device without netX/ssid set, the device's
  181. * SSID will be set to that of the network it chooses to associate
  182. * with, but the netX/ssid setting will remain blank. If we don't
  183. * remember that we started from no specified SSID, it will appear
  184. * every time settings are updated (e.g. after DHCP) that we need to
  185. * reassociate due to the difference between the set SSID and our own.
  186. */
  187. #define NET80211_AUTO_SSID (1 << 15)
  188. /** @} */
  189. /** @defgroup net80211_phy 802.11 physical layer flags */
  190. /** @{ */
  191. /** Whether to use RTS/CTS or CTS-to-self protection for transmissions
  192. *
  193. * Since the RTS or CTS is transmitted using 802.11b signaling, and
  194. * includes a field indicating the amount of time that will be used by
  195. * transmission of the following packet, this serves as an effective
  196. * protection mechanism to avoid 802.11b clients interfering with
  197. * 802.11g clients on mixed networks.
  198. */
  199. #define NET80211_PHY_USE_PROTECTION (1 << 1)
  200. /** Whether to use 802.11b short preamble operation
  201. *
  202. * Short-preamble operation can moderately increase throughput on
  203. * 802.11b networks operating between 2Mbps and 11Mbps. It is
  204. * irrelevant for 802.11g data rates, since they use a different
  205. * modulation scheme.
  206. */
  207. #define NET80211_PHY_USE_SHORT_PREAMBLE (1 << 2)
  208. /** Whether to use 802.11g short slot operation
  209. *
  210. * This affects a low-level timing parameter of 802.11g transmissions.
  211. */
  212. #define NET80211_PHY_USE_SHORT_SLOT (1 << 3)
  213. /** @} */
  214. /** The maximum number of TX rates we allow to be configured simultaneously */
  215. #define NET80211_MAX_RATES 16
  216. /** The maximum number of channels we allow to be configured simultaneously */
  217. #define NET80211_MAX_CHANNELS 32
  218. /** Seconds we'll wait to get all fragments of a packet */
  219. #define NET80211_FRAG_TIMEOUT 2
  220. /** The number of fragments we can receive at once
  221. *
  222. * The 802.11 standard requires that this be at least 3.
  223. */
  224. #define NET80211_NR_CONCURRENT_FRAGS 3
  225. /** Maximum TX power to allow (dBm), if we don't get a regulatory hint */
  226. #define NET80211_REG_TXPOWER 20
  227. struct net80211_device;
  228. /** Operations that must be implemented by an 802.11 driver */
  229. struct net80211_device_operations {
  230. /** Open 802.11 device
  231. *
  232. * @v dev 802.11 device
  233. * @ret rc Return status code
  234. *
  235. * This method should allocate RX I/O buffers and enable the
  236. * hardware to start transmitting and receiving packets on the
  237. * channels its net80211_register() call indicated it could
  238. * handle. It does not need to tune the antenna to receive
  239. * packets on any particular channel.
  240. */
  241. int ( * open ) ( struct net80211_device *dev );
  242. /** Close 802.11 network device
  243. *
  244. * @v dev 802.11 device
  245. *
  246. * This method should stop the flow of packets, and call
  247. * net80211_tx_complete() for any packets remaining in the
  248. * device's TX queue.
  249. */
  250. void ( * close ) ( struct net80211_device *dev );
  251. /** Transmit packet on 802.11 network device
  252. *
  253. * @v dev 802.11 device
  254. * @v iobuf I/O buffer
  255. * @ret rc Return status code
  256. *
  257. * This method should cause the hardware to initiate
  258. * transmission of the I/O buffer, using the channel and rate
  259. * most recently indicated by an appropriate call to the
  260. * @c config callback. The 802.11 layer guarantees that said
  261. * channel and rate will be the same as those currently
  262. * reflected in the fields of @a dev.
  263. *
  264. * If this method returns success, the I/O buffer remains
  265. * owned by the network layer's TX queue, and the driver must
  266. * eventually call net80211_tx_complete() to free the buffer
  267. * whether transmission succeeded or not. If this method
  268. * returns failure, it will be interpreted as "failure to
  269. * enqueue buffer" and the I/O buffer will be immediately
  270. * released.
  271. *
  272. * This method is guaranteed to be called only when the device
  273. * is open.
  274. */
  275. int ( * transmit ) ( struct net80211_device *dev,
  276. struct io_buffer *iobuf );
  277. /** Poll for completed and received packets
  278. *
  279. * @v dev 802.11 device
  280. *
  281. * This method should cause the hardware to check for
  282. * completed transmissions and received packets. Any received
  283. * packets should be delivered via net80211_rx(), and
  284. * completed transmissions should be indicated using
  285. * net80211_tx_complete().
  286. *
  287. * This method is guaranteed to be called only when the device
  288. * is open.
  289. */
  290. void ( * poll ) ( struct net80211_device *dev );
  291. /** Enable or disable interrupts
  292. *
  293. * @v dev 802.11 device
  294. * @v enable If TRUE, interrupts should be enabled
  295. */
  296. void ( * irq ) ( struct net80211_device *dev, int enable );
  297. /** Update hardware state to match 802.11 layer state
  298. *
  299. * @v dev 802.11 device
  300. * @v changed Set of flags indicating what may have changed
  301. * @ret rc Return status code
  302. *
  303. * This method should cause the hardware state to be
  304. * reinitialized from the state indicated in fields of
  305. * net80211_device, in the areas indicated by bits set in
  306. * @a changed. If the hardware is unable to do so, this method
  307. * may return an appropriate error indication.
  308. *
  309. * This method is guaranteed to be called only when the device
  310. * is open.
  311. */
  312. int ( * config ) ( struct net80211_device *dev, int changed );
  313. };
  314. /** An 802.11 RF channel. */
  315. struct net80211_channel
  316. {
  317. /** The band with which this channel is associated */
  318. u8 band;
  319. /** A channel number interpreted according to the band
  320. *
  321. * The 2.4GHz band uses channel numbers from 1-13 at 5MHz
  322. * intervals such that channel 1 is 2407 MHz; channel 14,
  323. * legal for use only in Japan, is defined separately as 2484
  324. * MHz. Adjacent channels will overlap, since 802.11
  325. * transmissions use a 20 MHz (4-channel) bandwidth. Most
  326. * commonly, channels 1, 6, and 11 are used.
  327. *
  328. * The 5GHz band uses channel numbers derived directly from
  329. * the frequency; channel 0 is 5000 MHz, and channels are
  330. * always spaced 5 MHz apart. Channel numbers over 180 are
  331. * relative to 4GHz instead of 5GHz, but these are rarely
  332. * seen. Most channels are not legal for use.
  333. */
  334. u8 channel_nr;
  335. /** The center frequency for this channel
  336. *
  337. * Currently a bandwidth of 20 MHz is assumed.
  338. */
  339. u16 center_freq;
  340. /** Hardware channel value */
  341. u16 hw_value;
  342. /** Maximum allowable transmit power, in dBm
  343. *
  344. * This should be interpreted as EIRP, the power supplied to
  345. * an ideal isotropic antenna in order to achieve the same
  346. * average signal intensity as the real hardware at a
  347. * particular distance.
  348. *
  349. * Currently no provision is made for directional antennas.
  350. */
  351. u8 maxpower;
  352. };
  353. /** Information on the capabilities of an 802.11 hardware device
  354. *
  355. * In its probe callback, an 802.11 driver must read hardware
  356. * registers to determine the appropriate contents of this structure,
  357. * fill it, and pass it to net80211_register() so that the 802.11
  358. * layer knows how to treat the hardware and what to advertise as
  359. * supported to access points.
  360. */
  361. struct net80211_hw_info
  362. {
  363. /** Default hardware MAC address.
  364. *
  365. * The user may change this by setting the @c netX/mac setting
  366. * before the driver's open function is called; in that case
  367. * the driver must set the hardware MAC address to the address
  368. * contained in the wrapping net_device's ll_addr field, or if
  369. * that is impossible, set that ll_addr field back to the
  370. * unchangeable hardware MAC address.
  371. */
  372. u8 hwaddr[ETH_ALEN];
  373. /** A bitwise OR of the 802.11x modes supported by this device */
  374. int modes;
  375. /** A bitwise OR of the bands on which this device can communicate */
  376. int bands;
  377. /** A set of flags indicating peculiarities of this device. */
  378. enum {
  379. /** Received frames include a frame check sequence. */
  380. NET80211_HW_RX_HAS_FCS = (1 << 1),
  381. /** Hardware doesn't support 2.4GHz short preambles
  382. *
  383. * This is only relevant for 802.11b operation above
  384. * 2Mbps. All 802.11g devices support short preambles.
  385. */
  386. NET80211_HW_NO_SHORT_PREAMBLE = (1 << 2),
  387. /** Hardware doesn't support 802.11g short slot operation */
  388. NET80211_HW_NO_SHORT_SLOT = (1 << 3),
  389. } flags;
  390. /** Signal strength information that can be provided by the device
  391. *
  392. * Signal strength is passed to net80211_rx(), primarily to
  393. * allow determination of the closest access point for a
  394. * multi-AP network. The units are provided for completeness
  395. * of status displays.
  396. */
  397. enum {
  398. /** No signal strength information supported */
  399. NET80211_SIGNAL_NONE = 0,
  400. /** Signal strength in arbitrary units */
  401. NET80211_SIGNAL_ARBITRARY,
  402. /** Signal strength in decibels relative to arbitrary base */
  403. NET80211_SIGNAL_DB,
  404. /** Signal strength in decibels relative to 1mW */
  405. NET80211_SIGNAL_DBM,
  406. } signal_type;
  407. /** Maximum signal in arbitrary cases
  408. *
  409. * If signal_type is NET80211_SIGNAL_ARBITRARY or
  410. * NET80211_SIGNAL_DB, the driver should report it on a scale
  411. * from 0 to signal_max.
  412. */
  413. unsigned signal_max;
  414. /** List of RF channels supported by the card */
  415. struct net80211_channel channels[NET80211_MAX_CHANNELS];
  416. /** Number of supported channels */
  417. int nr_channels;
  418. /** List of transmission rates supported by the card, indexed by band
  419. *
  420. * Rates should be in 100kbps increments (e.g. 11 Mbps would
  421. * be represented as the number 110).
  422. */
  423. u16 rates[NET80211_NR_BANDS][NET80211_MAX_RATES];
  424. /** Number of supported rates, indexed by band */
  425. int nr_rates[NET80211_NR_BANDS];
  426. /** Estimate of the time required to change channels, in microseconds
  427. *
  428. * If this is not known, a guess on the order of a few
  429. * milliseconds (value of 1000-5000) is reasonable.
  430. */
  431. unsigned channel_change_time;
  432. };
  433. /** Structure tracking received fragments for a packet
  434. *
  435. * We set up a fragment cache entry when we receive a packet marked as
  436. * fragment 0 with the "more fragments" bit set in its frame control
  437. * header. We are required by the 802.11 standard to track 3
  438. * fragmented packets arriving simultaneously; if we receive more we
  439. * may drop some. Upon receipt of a new fragment-0 packet, if no entry
  440. * is available or expired, we take over the most @e recent entry for
  441. * the new packet, since we don't want to starve old entries from ever
  442. * finishing at all. If we get a fragment after the zeroth with no
  443. * cache entry for its packet, we drop it.
  444. */
  445. struct net80211_frag_cache
  446. {
  447. /** Whether this cache entry is in use */
  448. u8 in_use;
  449. /** Sequence number of this MSDU (packet) */
  450. u16 seqnr;
  451. /** Timestamp from point at which first fragment was collected */
  452. u32 start_ticks;
  453. /** Buffers for each fragment */
  454. struct io_buffer *iob[16];
  455. };
  456. /** Interface to an 802.11 security handshaking protocol
  457. *
  458. * Security handshaking protocols handle parsing a user-specified key
  459. * into a suitable input to the encryption algorithm, and for WPA and
  460. * better systems, manage performing whatever authentication with the
  461. * network is necessary.
  462. *
  463. * At all times when any method in this structure is called with a
  464. * net80211_device argument @a dev, a dynamically allocated copy of
  465. * the handshaker structure itself with space for the requested amount
  466. * of private data may be accessed as @c dev->handshaker. The
  467. * structure will not be modified, and will only be freed during
  468. * reassociation and device closing after the @a stop method has been
  469. * called.
  470. */
  471. struct net80211_handshaker
  472. {
  473. /** The security handshaking protocol implemented */
  474. enum net80211_security_proto protocol;
  475. /** Initialize security handshaking protocol
  476. *
  477. * @v dev 802.11 device
  478. * @ret rc Return status code
  479. *
  480. * This method is expected to access @c netX/key or other
  481. * applicable settings to determine the parameters for
  482. * handshaking. If no handshaking is required, it should call
  483. * sec80211_install() with the cryptosystem and key that are
  484. * to be used, and @c start and @c step should be set to @c
  485. * NULL.
  486. *
  487. * This is always called just before association is performed,
  488. * but after its parameters have been set; in particular, you
  489. * may rely on the contents of the @a essid field in @a dev.
  490. */
  491. int ( * init ) ( struct net80211_device *dev );
  492. /** Start handshaking
  493. *
  494. * @v dev 802.11 device
  495. * @ret rc Return status code
  496. *
  497. * This method is expected to set up internal state so that
  498. * packets sent immediately after association, before @a step
  499. * can be called, will be handled appropriately.
  500. *
  501. * This is always called just before association is attempted.
  502. */
  503. int ( * start ) ( struct net80211_device *dev );
  504. /** Process handshaking state
  505. *
  506. * @v dev 802.11 device
  507. * @ret rc Return status code, or positive if done
  508. *
  509. * This method is expected to perform as much progress on the
  510. * protocol it implements as is possible without blocking. It
  511. * should return 0 if it wishes to be called again, a negative
  512. * return status code on error, or a positive value if
  513. * handshaking is complete. In the case of a positive return,
  514. * net80211_crypto_install() must have been called.
  515. *
  516. * If handshaking may require further action (e.g. an AP that
  517. * might decide to rekey), handlers must be installed by this
  518. * function that will act without further calls to @a step.
  519. */
  520. int ( * step ) ( struct net80211_device *dev );
  521. /** Change cryptographic key based on setting
  522. *
  523. * @v dev 802.11 device
  524. * @ret rc Return status code
  525. *
  526. * This method is called whenever the @c netX/key setting
  527. * @e may have been changed. It is expected to determine
  528. * whether it did in fact change, and if so, to install the
  529. * new key using net80211_crypto_install(). If it is not
  530. * possible to do this immediately, this method should return
  531. * an error; in that case the 802.11 stack will reassociate,
  532. * following the usual init/start/step sequence.
  533. *
  534. * This method is only relevant when it is possible to
  535. * associate successfully with an incorrect key. When it is
  536. * not, a failed association will be retried until the user
  537. * changes the key setting, and a successful association will
  538. * not be dropped due to such a change. When association with
  539. * an incorrect key is impossible, this function should return
  540. * 0 after performing no action.
  541. */
  542. int ( * change_key ) ( struct net80211_device *dev );
  543. /** Stop security handshaking handlers
  544. *
  545. * @v dev 802.11 device
  546. *
  547. * This method is called just before freeing a security
  548. * handshaker; it could, for example, delete a process that @a
  549. * start had created to manage the security of the connection.
  550. * If not needed it may be set to NULL.
  551. */
  552. void ( * stop ) ( struct net80211_device *dev );
  553. /** Amount of private data requested
  554. *
  555. * Before @c init is called for the first time, this structure's
  556. * @c priv pointer will point to this many bytes of allocated
  557. * data, where the allocation will be performed separately for
  558. * each net80211_device.
  559. */
  560. int priv_len;
  561. /** Whether @a start has been called
  562. *
  563. * Reset to 0 after @a stop is called.
  564. */
  565. int started;
  566. /** Pointer to private data
  567. *
  568. * In initializing this structure statically for a linker
  569. * table, set this to NULL.
  570. */
  571. void *priv;
  572. };
  573. #define NET80211_HANDSHAKERS __table ( struct net80211_handshaker, \
  574. "net80211_handshakers" )
  575. #define __net80211_handshaker __table_entry ( NET80211_HANDSHAKERS, 01 )
  576. /** Interface to an 802.11 cryptosystem
  577. *
  578. * Cryptosystems define a net80211_crypto structure statically, using
  579. * a gPXE linker table to make it available to the 802.11 layer. When
  580. * the cryptosystem needs to be used, the 802.11 code will allocate a
  581. * copy of the static definition plus whatever space the algorithm has
  582. * requested for private state, and point net80211_device::crypto or
  583. * net80211_device::gcrypto at it.
  584. */
  585. struct net80211_crypto
  586. {
  587. /** The cryptographic algorithm implemented */
  588. enum net80211_crypto_alg algorithm;
  589. /** Initialize cryptosystem using a given key
  590. *
  591. * @v crypto 802.11 cryptosystem
  592. * @v key Pointer to key bytes
  593. * @v keylen Number of key bytes
  594. * @v rsc Initial receive sequence counter, if applicable
  595. * @ret rc Return status code
  596. *
  597. * This method is passed the communication key provided by the
  598. * security handshake handler, which will already be in the
  599. * low-level form required. It may not store a pointer to the
  600. * key after returning; it must copy it to its private storage.
  601. */
  602. int ( * init ) ( struct net80211_crypto *crypto, const void *key,
  603. int keylen, const void *rsc );
  604. /** Encrypt a frame using the cryptosystem
  605. *
  606. * @v crypto 802.11 cryptosystem
  607. * @v iob I/O buffer
  608. * @ret eiob Newly allocated I/O buffer with encrypted packet
  609. *
  610. * This method is called to encrypt a single frame. It is
  611. * guaranteed that initialize() will have completed
  612. * successfully before this method is called.
  613. *
  614. * The frame passed already has an 802.11 header prepended,
  615. * but the PROTECTED bit in the frame control field will not
  616. * be set; this method is responsible for setting it. The
  617. * returned I/O buffer should contain a complete copy of @a
  618. * iob, including the 802.11 header, but with the PROTECTED
  619. * bit set, the data encrypted, and whatever encryption
  620. * headers/trailers are necessary added.
  621. *
  622. * This method should never free the passed I/O buffer.
  623. *
  624. * Return NULL if the packet could not be encrypted, due to
  625. * memory limitations or otherwise.
  626. */
  627. struct io_buffer * ( * encrypt ) ( struct net80211_crypto *crypto,
  628. struct io_buffer *iob );
  629. /** Decrypt a frame using the cryptosystem
  630. *
  631. * @v crypto 802.11 cryptosystem
  632. * @v eiob Encrypted I/O buffer
  633. * @ret iob Newly allocated I/O buffer with decrypted packet
  634. *
  635. * This method is called to decrypt a single frame. It is
  636. * guaranteed that initialize() will have completed
  637. * successfully before this method is called.
  638. *
  639. * Decryption follows the reverse of the pattern used for
  640. * encryption: this method must copy the 802.11 header into
  641. * the returned packet, decrypt the data stream, remove any
  642. * encryption header or trailer, and clear the PROTECTED bit
  643. * in the frame control header.
  644. *
  645. * This method should never free the passed I/O buffer.
  646. *
  647. * Return NULL if memory was not available for decryption, if
  648. * a consistency or integrity check on the decrypted frame
  649. * failed, or if the decrypted frame should not be processed
  650. * by the network stack for any other reason.
  651. */
  652. struct io_buffer * ( * decrypt ) ( struct net80211_crypto *crypto,
  653. struct io_buffer *iob );
  654. /** Length of private data requested to be allocated */
  655. int priv_len;
  656. /** Private data for the algorithm to store key and state info */
  657. void *priv;
  658. };
  659. #define NET80211_CRYPTOS __table ( struct net80211_crypto, "net80211_cryptos" )
  660. #define __net80211_crypto __table_entry ( NET80211_CRYPTOS, 01 )
  661. struct net80211_probe_ctx;
  662. struct net80211_assoc_ctx;
  663. /** Structure encapsulating the complete state of an 802.11 device
  664. *
  665. * An 802.11 device is always wrapped by a network device, and this
  666. * network device is always pointed to by the @a netdev field. In
  667. * general, operations should never be performed by 802.11 code using
  668. * netdev functions directly. It is usually the case that the 802.11
  669. * layer might need to do some processing or bookkeeping on top of
  670. * what the netdevice code will do.
  671. */
  672. struct net80211_device
  673. {
  674. /** The net_device that wraps us. */
  675. struct net_device *netdev;
  676. /** List of 802.11 devices. */
  677. struct list_head list;
  678. /** 802.11 device operations */
  679. struct net80211_device_operations *op;
  680. /** Driver private data */
  681. void *priv;
  682. /** Information about the hardware, provided to net80211_register() */
  683. struct net80211_hw_info *hw;
  684. /* ---------- Channel and rate fields ---------- */
  685. /** A list of all possible channels we might use */
  686. struct net80211_channel channels[NET80211_MAX_CHANNELS];
  687. /** The number of channels in the channels array */
  688. u8 nr_channels;
  689. /** The channel currently in use, as an index into the channels array */
  690. u8 channel;
  691. /** A list of all possible TX rates we might use
  692. *
  693. * Rates are in units of 100 kbps.
  694. */
  695. u16 rates[NET80211_MAX_RATES];
  696. /** The number of transmission rates in the rates array */
  697. u8 nr_rates;
  698. /** The rate currently in use, as an index into the rates array */
  699. u8 rate;
  700. /** The rate to use for RTS/CTS transmissions
  701. *
  702. * This is always the fastest basic rate that is not faster
  703. * than the data rate in use. Also an index into the rates array.
  704. */
  705. u8 rtscts_rate;
  706. /** Bitmask of basic rates
  707. *
  708. * If bit N is set in this value, with the LSB considered to
  709. * be bit 0, then rate N in the rates array is a "basic" rate.
  710. *
  711. * We don't decide which rates are "basic"; our AP does, and
  712. * we respect its wishes. We need to be able to identify basic
  713. * rates in order to calculate the duration of a CTS packet
  714. * used for 802.11 g/b interoperability.
  715. */
  716. u32 basic_rates;
  717. /* ---------- Association fields ---------- */
  718. /** The asynchronous association process.
  719. *
  720. * When an 802.11 netdev is opened, or when the user changes
  721. * the SSID setting on an open 802.11 device, an
  722. * autoassociation task is started by net80211_autoassocate()
  723. * to associate with the new best network. The association is
  724. * asynchronous, but no packets can be transmitted until it is
  725. * complete. If it is successful, the wrapping net_device is
  726. * set as "link up". If it fails, @c assoc_rc will be set with
  727. * an error indication.
  728. */
  729. struct process proc_assoc;
  730. /** Network with which we are associating
  731. *
  732. * This will be NULL when we are not actively in the process
  733. * of associating with a network we have already successfully
  734. * probed for.
  735. */
  736. struct net80211_wlan *associating;
  737. /** Context for the association process
  738. *
  739. * This is a probe_ctx if the @c PROBED flag is not set in @c
  740. * state, and an assoc_ctx otherwise.
  741. */
  742. union {
  743. struct net80211_probe_ctx *probe;
  744. struct net80211_assoc_ctx *assoc;
  745. } ctx;
  746. /** Security handshaker being used */
  747. struct net80211_handshaker *handshaker;
  748. /** State of our association to the network
  749. *
  750. * Since the association process happens asynchronously, it's
  751. * necessary to have some channel of communication so the
  752. * driver can say "I got an association reply and we're OK" or
  753. * similar. This variable provides that link. It is a bitmask
  754. * of any of NET80211_PROBED, NET80211_AUTHENTICATED,
  755. * NET80211_ASSOCIATED, NET80211_CRYPTO_SYNCED to indicate how
  756. * far along in associating we are; NET80211_WORKING if the
  757. * association task is running; and NET80211_WAITING if a
  758. * packet has been sent that we're waiting for a reply to. We
  759. * can only be crypto-synced if we're associated, we can
  760. * only be associated if we're authenticated, we can only be
  761. * authenticated if we've probed.
  762. *
  763. * If an association process fails (that is, we receive a
  764. * packet with an error indication), the error code is copied
  765. * into bits 6-0 of this variable and bit 7 is set to specify
  766. * what type of error code it is. An AP can provide either a
  767. * "status code" (0-51 are defined) explaining why it refused
  768. * an association immediately, or a "reason code" (0-45 are
  769. * defined) explaining why it canceled an association after it
  770. * had originally OK'ed it. Status and reason codes serve
  771. * similar functions, but they use separate error message
  772. * tables. A gPXE-formatted return status code (negative) is
  773. * placed in @c assoc_rc.
  774. *
  775. * If the failure to associate is indicated by a status code,
  776. * the NET80211_IS_REASON bit will be clear; if it is
  777. * indicated by a reason code, the bit will be set. If we were
  778. * successful, both zero status and zero reason mean success,
  779. * so there is no ambiguity.
  780. *
  781. * To prevent association when opening the device, user code
  782. * can set the NET80211_NO_ASSOC bit. The final bit in this
  783. * variable, NET80211_AUTO_SSID, is used to remember whether
  784. * we picked our SSID through automated probing as opposed to
  785. * user specification; the distinction becomes relevant in the
  786. * settings applicator.
  787. */
  788. u16 state;
  789. /** Return status code associated with @c state */
  790. int assoc_rc;
  791. /** RSN or WPA information element to include with association
  792. *
  793. * If set to @c NULL, none will be included. It is expected
  794. * that this will be set by the @a init function of a security
  795. * handshaker if it is needed.
  796. */
  797. union ieee80211_ie *rsn_ie;
  798. /* ---------- Parameters of currently associated network ---------- */
  799. /** 802.11 cryptosystem for our current network
  800. *
  801. * For an open network, this will be set to NULL.
  802. */
  803. struct net80211_crypto *crypto;
  804. /** 802.11 cryptosystem for multicast and broadcast frames
  805. *
  806. * If this is NULL, the cryptosystem used for receiving
  807. * unicast frames will also be used for receiving multicast
  808. * and broadcast frames. Transmitted multicast and broadcast
  809. * frames are always sent unicast to the AP, who multicasts
  810. * them on our behalf; thus they always use the unicast
  811. * cryptosystem.
  812. */
  813. struct net80211_crypto *gcrypto;
  814. /** MAC address of the access point most recently associated */
  815. u8 bssid[ETH_ALEN];
  816. /** SSID of the access point we are or will be associated with
  817. *
  818. * Although the SSID field in 802.11 packets is generally not
  819. * NUL-terminated, here and in net80211_wlan we add a NUL for
  820. * convenience.
  821. */
  822. char essid[IEEE80211_MAX_SSID_LEN+1];
  823. /** Association ID given to us by the AP */
  824. u16 aid;
  825. /** TSFT value for last beacon received, microseconds */
  826. u64 last_beacon_timestamp;
  827. /** Time between AP sending beacons, microseconds */
  828. u32 tx_beacon_interval;
  829. /** Smoothed average time between beacons, microseconds */
  830. u32 rx_beacon_interval;
  831. /* ---------- Physical layer information ---------- */
  832. /** Physical layer options
  833. *
  834. * These control the use of CTS protection, short preambles,
  835. * and short-slot operation.
  836. */
  837. int phy_flags;
  838. /** Signal strength of last received packet */
  839. int last_signal;
  840. /** Rate control state */
  841. struct rc80211_ctx *rctl;
  842. /* ---------- Packet handling state ---------- */
  843. /** Fragment reassembly state */
  844. struct net80211_frag_cache frags[NET80211_NR_CONCURRENT_FRAGS];
  845. /** The sequence number of the last packet we sent */
  846. u16 last_tx_seqnr;
  847. /** Packet duplication elimination state
  848. *
  849. * We are only required to handle immediate duplicates for
  850. * each direct sender, and since we can only have one direct
  851. * sender (the AP), we need only keep the sequence control
  852. * field from the most recent packet we've received. Thus,
  853. * this field stores the last sequence control field we've
  854. * received for a packet from the AP.
  855. */
  856. u16 last_rx_seq;
  857. /** RX management packet queue
  858. *
  859. * Sometimes we want to keep probe, beacon, and action packets
  860. * that we receive, such as when we're scanning for networks.
  861. * Ordinarily we drop them because they are sent at a large
  862. * volume (ten beacons per second per AP, broadcast) and we
  863. * have no need of them except when we're scanning.
  864. *
  865. * When keep_mgmt is TRUE, received probe, beacon, and action
  866. * management packets will be stored in this queue.
  867. */
  868. struct list_head mgmt_queue;
  869. /** RX management packet info queue
  870. *
  871. * We need to keep track of the signal strength for management
  872. * packets we're keeping, because that provides the only way
  873. * to distinguish between multiple APs for the same network.
  874. * Since we can't extend io_buffer to store signal, this field
  875. * heads a linked list of "RX packet info" structures that
  876. * contain that signal strength field. Its entries always
  877. * parallel the entries in mgmt_queue, because the two queues
  878. * are always added to or removed from in parallel.
  879. */
  880. struct list_head mgmt_info_queue;
  881. /** Whether to store management packets
  882. *
  883. * Received beacon, probe, and action packets will be added to
  884. * mgmt_queue (and their signal strengths added to
  885. * mgmt_info_queue) only when this variable is TRUE. It should
  886. * be set by net80211_keep_mgmt() (which returns the old
  887. * value) only when calling code is prepared to poll the
  888. * management queue frequently, because packets will otherwise
  889. * pile up and exhaust memory.
  890. */
  891. int keep_mgmt;
  892. };
  893. /** Structure representing a probed network.
  894. *
  895. * This is returned from the net80211_probe_finish functions and
  896. * passed to the low-level association functions. At least essid,
  897. * bssid, channel, beacon, and security must be filled in if you want
  898. * to build this structure manually.
  899. */
  900. struct net80211_wlan
  901. {
  902. /** The human-readable ESSID (network name)
  903. *
  904. * Although the 802.11 SSID field is generally not
  905. * NUL-terminated, the gPXE code adds an extra NUL (and
  906. * expects one in this structure) for convenience.
  907. */
  908. char essid[IEEE80211_MAX_SSID_LEN+1];
  909. /** MAC address of the strongest-signal access point for this ESSID */
  910. u8 bssid[ETH_ALEN];
  911. /** Signal strength of beacon frame from that access point */
  912. int signal;
  913. /** The channel on which that access point communicates
  914. *
  915. * This is a raw channel number (net80211_channel::channel_nr),
  916. * so that it will not be affected by reconfiguration of the
  917. * device channels array.
  918. */
  919. int channel;
  920. /** The complete beacon or probe-response frame received */
  921. struct io_buffer *beacon;
  922. /** Security handshaking method used on the network */
  923. enum net80211_security_proto handshaking;
  924. /** Cryptographic algorithm used on the network */
  925. enum net80211_crypto_alg crypto;
  926. /** Link to allow chaining multiple structures into a list to
  927. be returned from net80211_probe_finish_all(). */
  928. struct list_head list;
  929. };
  930. /** 802.11 encryption key setting */
  931. extern struct setting net80211_key_setting __setting;
  932. /**
  933. * @defgroup net80211_probe 802.11 network location API
  934. * @{
  935. */
  936. int net80211_prepare_probe ( struct net80211_device *dev, int band,
  937. int active );
  938. struct net80211_probe_ctx * net80211_probe_start ( struct net80211_device *dev,
  939. const char *essid,
  940. int active );
  941. int net80211_probe_step ( struct net80211_probe_ctx *ctx );
  942. struct net80211_wlan *
  943. net80211_probe_finish_best ( struct net80211_probe_ctx *ctx );
  944. struct list_head *net80211_probe_finish_all ( struct net80211_probe_ctx *ctx );
  945. void net80211_free_wlan ( struct net80211_wlan *wlan );
  946. void net80211_free_wlanlist ( struct list_head *list );
  947. /** @} */
  948. /**
  949. * @defgroup net80211_mgmt 802.11 network management API
  950. * @{
  951. */
  952. struct net80211_device * net80211_get ( struct net_device *netdev );
  953. void net80211_autoassociate ( struct net80211_device *dev );
  954. int net80211_change_channel ( struct net80211_device *dev, int channel );
  955. void net80211_set_rate_idx ( struct net80211_device *dev, int rate );
  956. int net80211_keep_mgmt ( struct net80211_device *dev, int enable );
  957. struct io_buffer * net80211_mgmt_dequeue ( struct net80211_device *dev,
  958. int *signal );
  959. int net80211_tx_mgmt ( struct net80211_device *dev, u16 fc,
  960. u8 bssid[ETH_ALEN], struct io_buffer *iob );
  961. /** @} */
  962. /**
  963. * @defgroup net80211_assoc 802.11 network association API
  964. * @{
  965. */
  966. int net80211_prepare_assoc ( struct net80211_device *dev,
  967. struct net80211_wlan *wlan );
  968. int net80211_send_auth ( struct net80211_device *dev,
  969. struct net80211_wlan *wlan, int method );
  970. int net80211_send_assoc ( struct net80211_device *dev,
  971. struct net80211_wlan *wlan );
  972. void net80211_deauthenticate ( struct net80211_device *dev, int rc );
  973. /** @} */
  974. /**
  975. * @defgroup net80211_driver 802.11 driver interface API
  976. * @{
  977. */
  978. struct net80211_device *net80211_alloc ( size_t priv_size );
  979. int net80211_register ( struct net80211_device *dev,
  980. struct net80211_device_operations *ops,
  981. struct net80211_hw_info *hw );
  982. u16 net80211_duration ( struct net80211_device *dev, int bytes, u16 rate );
  983. void net80211_rx ( struct net80211_device *dev, struct io_buffer *iob,
  984. int signal, u16 rate );
  985. void net80211_rx_err ( struct net80211_device *dev,
  986. struct io_buffer *iob, int rc );
  987. void net80211_tx_complete ( struct net80211_device *dev,
  988. struct io_buffer *iob, int retries, int rc );
  989. void net80211_unregister ( struct net80211_device *dev );
  990. void net80211_free ( struct net80211_device *dev );
  991. /** @} */
  992. /**
  993. * Calculate duration field for a CTS control frame
  994. *
  995. * @v dev 802.11 device
  996. * @v size Size of the packet being cleared to send
  997. *
  998. * A CTS control frame's duration field captures the frame being
  999. * protected and its 10-byte ACK.
  1000. */
  1001. static inline u16 net80211_cts_duration ( struct net80211_device *dev,
  1002. int size )
  1003. {
  1004. return ( net80211_duration ( dev, 10,
  1005. dev->rates[dev->rtscts_rate] ) +
  1006. net80211_duration ( dev, size, dev->rates[dev->rate] ) );
  1007. }
  1008. #endif