You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

net80211.h 33KB

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