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.c 76KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816
  1. /*
  2. * The iPXE 802.11 MAC layer.
  3. *
  4. * Copyright (c) 2009 Joshua Oreman <oremanj@rwcr.net>.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of the
  9. * License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. FILE_LICENCE ( GPL2_OR_LATER );
  21. #include <string.h>
  22. #include <byteswap.h>
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. #include <errno.h>
  26. #include <ipxe/settings.h>
  27. #include <ipxe/if_arp.h>
  28. #include <ipxe/ethernet.h>
  29. #include <ipxe/ieee80211.h>
  30. #include <ipxe/netdevice.h>
  31. #include <ipxe/net80211.h>
  32. #include <ipxe/sec80211.h>
  33. #include <ipxe/timer.h>
  34. #include <ipxe/nap.h>
  35. #include <ipxe/errortab.h>
  36. #include <ipxe/net80211_err.h>
  37. /** @file
  38. *
  39. * 802.11 device management
  40. */
  41. /** List of 802.11 devices */
  42. static struct list_head net80211_devices = LIST_HEAD_INIT ( net80211_devices );
  43. /** Set of device operations that does nothing */
  44. static struct net80211_device_operations net80211_null_ops;
  45. /** Information associated with a received management packet
  46. *
  47. * This is used to keep beacon signal strengths in a parallel queue to
  48. * the beacons themselves.
  49. */
  50. struct net80211_rx_info {
  51. int signal;
  52. struct list_head list;
  53. };
  54. /** Context for a probe operation */
  55. struct net80211_probe_ctx {
  56. /** 802.11 device to probe on */
  57. struct net80211_device *dev;
  58. /** Value of keep_mgmt before probe was started */
  59. int old_keep_mgmt;
  60. /** If scanning actively, pointer to probe packet to send */
  61. struct io_buffer *probe;
  62. /** If non-"", the ESSID to limit ourselves to */
  63. const char *essid;
  64. /** Time probe was started */
  65. u32 ticks_start;
  66. /** Time last useful beacon was received */
  67. u32 ticks_beacon;
  68. /** Time channel was last changed */
  69. u32 ticks_channel;
  70. /** Time to stay on each channel */
  71. u32 hop_time;
  72. /** Channels to hop by when changing channel */
  73. int hop_step;
  74. /** List of best beacons for each network found so far */
  75. struct list_head *beacons;
  76. };
  77. /** Context for the association task */
  78. struct net80211_assoc_ctx {
  79. /** Next authentication method to try using */
  80. int method;
  81. /** Time (in ticks) of the last sent association-related packet */
  82. int last_packet;
  83. /** Number of times we have tried sending it */
  84. int times_tried;
  85. };
  86. /**
  87. * Detect secure 802.11 network when security support is not available
  88. *
  89. * @return -ENOTSUP, always.
  90. */
  91. __weak int sec80211_detect ( struct io_buffer *iob __unused,
  92. enum net80211_security_proto *secprot __unused,
  93. enum net80211_crypto_alg *crypt __unused ) {
  94. return -ENOTSUP;
  95. }
  96. /**
  97. * @defgroup net80211_netdev Network device interface functions
  98. * @{
  99. */
  100. static int net80211_netdev_open ( struct net_device *netdev );
  101. static void net80211_netdev_close ( struct net_device *netdev );
  102. static int net80211_netdev_transmit ( struct net_device *netdev,
  103. struct io_buffer *iobuf );
  104. static void net80211_netdev_poll ( struct net_device *netdev );
  105. static void net80211_netdev_irq ( struct net_device *netdev, int enable );
  106. /** @} */
  107. /**
  108. * @defgroup net80211_linklayer 802.11 link-layer protocol functions
  109. * @{
  110. */
  111. static int net80211_ll_push ( struct net_device *netdev,
  112. struct io_buffer *iobuf, const void *ll_dest,
  113. const void *ll_source, uint16_t net_proto );
  114. static int net80211_ll_pull ( struct net_device *netdev,
  115. struct io_buffer *iobuf, const void **ll_dest,
  116. const void **ll_source, uint16_t * net_proto );
  117. /** @} */
  118. /**
  119. * @defgroup net80211_help 802.11 helper functions
  120. * @{
  121. */
  122. static void net80211_add_channels ( struct net80211_device *dev, int start,
  123. int len, int txpower );
  124. static void net80211_filter_hw_channels ( struct net80211_device *dev );
  125. static void net80211_set_rtscts_rate ( struct net80211_device *dev );
  126. static int net80211_process_capab ( struct net80211_device *dev,
  127. u16 capab );
  128. static int net80211_process_ie ( struct net80211_device *dev,
  129. union ieee80211_ie *ie, void *ie_end );
  130. static union ieee80211_ie *
  131. net80211_marshal_request_info ( struct net80211_device *dev,
  132. union ieee80211_ie *ie );
  133. /** @} */
  134. /**
  135. * @defgroup net80211_assoc_ll 802.11 association handling functions
  136. * @{
  137. */
  138. static void net80211_step_associate ( struct process *proc );
  139. static void net80211_handle_auth ( struct net80211_device *dev,
  140. struct io_buffer *iob );
  141. static void net80211_handle_assoc_reply ( struct net80211_device *dev,
  142. struct io_buffer *iob );
  143. static int net80211_send_disassoc ( struct net80211_device *dev, int reason,
  144. int deauth );
  145. static void net80211_handle_mgmt ( struct net80211_device *dev,
  146. struct io_buffer *iob, int signal );
  147. /** @} */
  148. /**
  149. * @defgroup net80211_frag 802.11 fragment handling functions
  150. * @{
  151. */
  152. static void net80211_free_frags ( struct net80211_device *dev, int fcid );
  153. static struct io_buffer *net80211_accum_frags ( struct net80211_device *dev,
  154. int fcid, int nfrags, int size );
  155. static void net80211_rx_frag ( struct net80211_device *dev,
  156. struct io_buffer *iob, int signal );
  157. /** @} */
  158. /**
  159. * @defgroup net80211_settings 802.11 settings handlers
  160. * @{
  161. */
  162. static int net80211_check_settings_update ( void );
  163. /** 802.11 settings applicator
  164. *
  165. * When the SSID is changed, this will cause any open devices to
  166. * re-associate; when the encryption key is changed, we similarly
  167. * update their state.
  168. */
  169. struct settings_applicator net80211_applicator __settings_applicator = {
  170. .apply = net80211_check_settings_update,
  171. };
  172. /** The network name to associate with
  173. *
  174. * If this is blank, we scan for all networks and use the one with the
  175. * greatest signal strength.
  176. */
  177. struct setting net80211_ssid_setting __setting = {
  178. .name = "ssid",
  179. .description = "802.11 SSID (network name)",
  180. .type = &setting_type_string,
  181. };
  182. /** Whether to use active scanning
  183. *
  184. * In order to associate with a hidden SSID, it's necessary to use an
  185. * active scan (send probe packets). If this setting is nonzero, an
  186. * active scan on the 2.4GHz band will be used to associate.
  187. */
  188. struct setting net80211_active_setting __setting = {
  189. .name = "active-scan",
  190. .description = "Use an active scan during 802.11 association",
  191. .type = &setting_type_int8,
  192. };
  193. /** The cryptographic key to use
  194. *
  195. * For hex WEP keys, as is common, this must be entered using the
  196. * normal iPXE method for entering hex settings; an ASCII string of
  197. * hex characters will not behave as expected.
  198. */
  199. struct setting net80211_key_setting __setting = {
  200. .name = "key",
  201. .description = "Encryption key for protected 802.11 networks",
  202. .type = &setting_type_string,
  203. };
  204. /** @} */
  205. /* ---------- net_device wrapper ---------- */
  206. /**
  207. * Open 802.11 device and start association
  208. *
  209. * @v netdev Wrapping network device
  210. * @ret rc Return status code
  211. *
  212. * This sets up a default conservative set of channels for probing,
  213. * and starts the auto-association task unless the @c
  214. * NET80211_NO_ASSOC flag is set in the wrapped 802.11 device's @c
  215. * state field.
  216. */
  217. static int net80211_netdev_open ( struct net_device *netdev )
  218. {
  219. struct net80211_device *dev = netdev->priv;
  220. int rc = 0;
  221. if ( dev->op == &net80211_null_ops )
  222. return -EFAULT;
  223. if ( dev->op->open )
  224. rc = dev->op->open ( dev );
  225. if ( rc < 0 )
  226. return rc;
  227. if ( ! ( dev->state & NET80211_NO_ASSOC ) )
  228. net80211_autoassociate ( dev );
  229. return 0;
  230. }
  231. /**
  232. * Close 802.11 device
  233. *
  234. * @v netdev Wrapping network device.
  235. *
  236. * If the association task is running, this will stop it.
  237. */
  238. static void net80211_netdev_close ( struct net_device *netdev )
  239. {
  240. struct net80211_device *dev = netdev->priv;
  241. if ( dev->state & NET80211_WORKING )
  242. process_del ( &dev->proc_assoc );
  243. /* Send disassociation frame to AP, to be polite */
  244. if ( dev->state & NET80211_ASSOCIATED )
  245. net80211_send_disassoc ( dev, IEEE80211_REASON_LEAVING, 0 );
  246. if ( dev->handshaker && dev->handshaker->stop &&
  247. dev->handshaker->started )
  248. dev->handshaker->stop ( dev );
  249. free ( dev->crypto );
  250. free ( dev->handshaker );
  251. dev->crypto = NULL;
  252. dev->handshaker = NULL;
  253. netdev_link_down ( netdev );
  254. dev->state = 0;
  255. if ( dev->op->close )
  256. dev->op->close ( dev );
  257. }
  258. /**
  259. * Transmit packet on 802.11 device
  260. *
  261. * @v netdev Wrapping network device
  262. * @v iobuf I/O buffer
  263. * @ret rc Return status code
  264. *
  265. * If encryption is enabled for the currently associated network, the
  266. * packet will be encrypted prior to transmission.
  267. */
  268. static int net80211_netdev_transmit ( struct net_device *netdev,
  269. struct io_buffer *iobuf )
  270. {
  271. struct net80211_device *dev = netdev->priv;
  272. struct ieee80211_frame *hdr = iobuf->data;
  273. int rc = -ENOSYS;
  274. if ( dev->crypto && ! ( hdr->fc & IEEE80211_FC_PROTECTED ) &&
  275. ( ( hdr->fc & IEEE80211_FC_TYPE ) == IEEE80211_TYPE_DATA ) ) {
  276. struct io_buffer *niob = dev->crypto->encrypt ( dev->crypto,
  277. iobuf );
  278. if ( ! niob )
  279. return -ENOMEM; /* only reason encryption could fail */
  280. /* Free the non-encrypted iob */
  281. netdev_tx_complete ( netdev, iobuf );
  282. /* Transmit the encrypted iob; the Protected flag is
  283. set, so we won't recurse into here again */
  284. netdev_tx ( netdev, niob );
  285. /* Don't transmit the freed packet */
  286. return 0;
  287. }
  288. if ( dev->op->transmit )
  289. rc = dev->op->transmit ( dev, iobuf );
  290. return rc;
  291. }
  292. /**
  293. * Poll 802.11 device for received packets and completed transmissions
  294. *
  295. * @v netdev Wrapping network device
  296. */
  297. static void net80211_netdev_poll ( struct net_device *netdev )
  298. {
  299. struct net80211_device *dev = netdev->priv;
  300. if ( dev->op->poll )
  301. dev->op->poll ( dev );
  302. }
  303. /**
  304. * Enable or disable interrupts for 802.11 device
  305. *
  306. * @v netdev Wrapping network device
  307. * @v enable Whether to enable interrupts
  308. */
  309. static void net80211_netdev_irq ( struct net_device *netdev, int enable )
  310. {
  311. struct net80211_device *dev = netdev->priv;
  312. if ( dev->op->irq )
  313. dev->op->irq ( dev, enable );
  314. }
  315. /** Network device operations for a wrapped 802.11 device */
  316. static struct net_device_operations net80211_netdev_ops = {
  317. .open = net80211_netdev_open,
  318. .close = net80211_netdev_close,
  319. .transmit = net80211_netdev_transmit,
  320. .poll = net80211_netdev_poll,
  321. .irq = net80211_netdev_irq,
  322. };
  323. /* ---------- 802.11 link-layer protocol ---------- */
  324. /** 802.11 broadcast MAC address */
  325. static u8 net80211_ll_broadcast[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  326. /**
  327. * Determine whether a transmission rate uses ERP/OFDM
  328. *
  329. * @v rate Rate in 100 kbps units
  330. * @ret is_erp TRUE if the rate is an ERP/OFDM rate
  331. *
  332. * 802.11b supports rates of 1.0, 2.0, 5.5, and 11.0 Mbps; any other
  333. * rate than these on the 2.4GHz spectrum is an ERP (802.11g) rate.
  334. */
  335. static inline int net80211_rate_is_erp ( u16 rate )
  336. {
  337. if ( rate == 10 || rate == 20 || rate == 55 || rate == 110 )
  338. return 0;
  339. return 1;
  340. }
  341. /**
  342. * Calculate one frame's contribution to 802.11 duration field
  343. *
  344. * @v dev 802.11 device
  345. * @v bytes Amount of data to calculate duration for
  346. * @ret dur Duration field in microseconds
  347. *
  348. * To avoid multiple stations attempting to transmit at once, 802.11
  349. * provides that every packet shall include a duration field
  350. * specifying a length of time for which the wireless medium will be
  351. * reserved after it is transmitted. The duration is measured in
  352. * microseconds and is calculated with respect to the current
  353. * physical-layer parameters of the 802.11 device.
  354. *
  355. * For an unfragmented data or management frame, or the last fragment
  356. * of a fragmented frame, the duration captures only the 10 data bytes
  357. * of one ACK; call once with bytes = 10.
  358. *
  359. * For a fragment of a data or management rame that will be followed
  360. * by more fragments, the duration captures an ACK, the following
  361. * fragment, and its ACK; add the results of three calls, two with
  362. * bytes = 10 and one with bytes set to the next fragment's size.
  363. *
  364. * For an RTS control frame, the duration captures the responding CTS,
  365. * the frame being sent, and its ACK; add the results of three calls,
  366. * two with bytes = 10 and one with bytes set to the next frame's size
  367. * (assuming unfragmented).
  368. *
  369. * For a CTS-to-self control frame, the duration captures the frame
  370. * being protected and its ACK; add the results of two calls, one with
  371. * bytes = 10 and one with bytes set to the next frame's size.
  372. *
  373. * No other frame types are currently supported by iPXE.
  374. */
  375. u16 net80211_duration ( struct net80211_device *dev, int bytes, u16 rate )
  376. {
  377. struct net80211_channel *chan = &dev->channels[dev->channel];
  378. u32 kbps = rate * 100;
  379. if ( chan->band == NET80211_BAND_5GHZ || net80211_rate_is_erp ( rate ) ) {
  380. /* OFDM encoding (802.11a/g) */
  381. int bits_per_symbol = ( kbps * 4 ) / 1000; /* 4us/symbol */
  382. int bits = 22 + ( bytes << 3 ); /* 22-bit PLCP */
  383. int symbols = ( bits + bits_per_symbol - 1 ) / bits_per_symbol;
  384. return 16 + 20 + ( symbols * 4 ); /* 16us SIFS, 20us preamble */
  385. } else {
  386. /* CCK encoding (802.11b) */
  387. int phy_time = 144 + 48; /* preamble + PLCP */
  388. int bits = bytes << 3;
  389. int data_time = ( bits * 1000 + kbps - 1 ) / kbps;
  390. if ( dev->phy_flags & NET80211_PHY_USE_SHORT_PREAMBLE )
  391. phy_time >>= 1;
  392. return 10 + phy_time + data_time; /* 10us SIFS */
  393. }
  394. }
  395. /**
  396. * Add 802.11 link-layer header
  397. *
  398. * @v netdev Wrapping network device
  399. * @v iobuf I/O buffer
  400. * @v ll_dest Link-layer destination address
  401. * @v ll_source Link-layer source address
  402. * @v net_proto Network-layer protocol, in network byte order
  403. * @ret rc Return status code
  404. *
  405. * This adds both the 802.11 frame header and the 802.2 LLC/SNAP
  406. * header used on data packets.
  407. *
  408. * We also check here for state of the link that would make it invalid
  409. * to send a data packet; every data packet must pass through here,
  410. * and no non-data packet (e.g. management frame) should.
  411. */
  412. static int net80211_ll_push ( struct net_device *netdev,
  413. struct io_buffer *iobuf, const void *ll_dest,
  414. const void *ll_source, uint16_t net_proto )
  415. {
  416. struct net80211_device *dev = netdev->priv;
  417. struct ieee80211_frame *hdr = iob_push ( iobuf,
  418. IEEE80211_LLC_HEADER_LEN +
  419. IEEE80211_TYP_FRAME_HEADER_LEN );
  420. struct ieee80211_llc_snap_header *lhdr =
  421. ( void * ) hdr + IEEE80211_TYP_FRAME_HEADER_LEN;
  422. /* We can't send data packets if we're not associated. */
  423. if ( ! ( dev->state & NET80211_ASSOCIATED ) ) {
  424. if ( dev->assoc_rc )
  425. return dev->assoc_rc;
  426. return -ENETUNREACH;
  427. }
  428. hdr->fc = IEEE80211_THIS_VERSION | IEEE80211_TYPE_DATA |
  429. IEEE80211_STYPE_DATA | IEEE80211_FC_TODS;
  430. /* We don't send fragmented frames, so duration is the time
  431. for an SIFS + 10-byte ACK. */
  432. hdr->duration = net80211_duration ( dev, 10, dev->rates[dev->rate] );
  433. memcpy ( hdr->addr1, dev->bssid, ETH_ALEN );
  434. memcpy ( hdr->addr2, ll_source, ETH_ALEN );
  435. memcpy ( hdr->addr3, ll_dest, ETH_ALEN );
  436. hdr->seq = IEEE80211_MAKESEQ ( ++dev->last_tx_seqnr, 0 );
  437. lhdr->dsap = IEEE80211_LLC_DSAP;
  438. lhdr->ssap = IEEE80211_LLC_SSAP;
  439. lhdr->ctrl = IEEE80211_LLC_CTRL;
  440. memset ( lhdr->oui, 0x00, 3 );
  441. lhdr->ethertype = net_proto;
  442. return 0;
  443. }
  444. /**
  445. * Remove 802.11 link-layer header
  446. *
  447. * @v netdev Wrapping network device
  448. * @v iobuf I/O buffer
  449. * @ret ll_dest Link-layer destination address
  450. * @ret ll_source Link-layer source
  451. * @ret net_proto Network-layer protocol, in network byte order
  452. * @ret rc Return status code
  453. *
  454. * This expects and removes both the 802.11 frame header and the 802.2
  455. * LLC/SNAP header that are used on data packets.
  456. */
  457. static int net80211_ll_pull ( struct net_device *netdev __unused,
  458. struct io_buffer *iobuf,
  459. const void **ll_dest, const void **ll_source,
  460. uint16_t * net_proto )
  461. {
  462. struct ieee80211_frame *hdr = iobuf->data;
  463. struct ieee80211_llc_snap_header *lhdr =
  464. ( void * ) hdr + IEEE80211_TYP_FRAME_HEADER_LEN;
  465. /* Bunch of sanity checks */
  466. if ( iob_len ( iobuf ) < IEEE80211_TYP_FRAME_HEADER_LEN +
  467. IEEE80211_LLC_HEADER_LEN ) {
  468. DBGC ( netdev->priv, "802.11 %p packet too short (%zd bytes)\n",
  469. netdev->priv, iob_len ( iobuf ) );
  470. return -EINVAL_PKT_TOO_SHORT;
  471. }
  472. if ( ( hdr->fc & IEEE80211_FC_VERSION ) != IEEE80211_THIS_VERSION ) {
  473. DBGC ( netdev->priv, "802.11 %p packet invalid version %04x\n",
  474. netdev->priv, hdr->fc & IEEE80211_FC_VERSION );
  475. return -EINVAL_PKT_VERSION;
  476. }
  477. if ( ( hdr->fc & IEEE80211_FC_TYPE ) != IEEE80211_TYPE_DATA ||
  478. ( hdr->fc & IEEE80211_FC_SUBTYPE ) != IEEE80211_STYPE_DATA ) {
  479. DBGC ( netdev->priv, "802.11 %p packet not data/data (fc=%04x)\n",
  480. netdev->priv, hdr->fc );
  481. return -EINVAL_PKT_NOT_DATA;
  482. }
  483. if ( ( hdr->fc & ( IEEE80211_FC_TODS | IEEE80211_FC_FROMDS ) ) !=
  484. IEEE80211_FC_FROMDS ) {
  485. DBGC ( netdev->priv, "802.11 %p packet not from DS (fc=%04x)\n",
  486. netdev->priv, hdr->fc );
  487. return -EINVAL_PKT_NOT_FROMDS;
  488. }
  489. if ( lhdr->dsap != IEEE80211_LLC_DSAP || lhdr->ssap != IEEE80211_LLC_SSAP ||
  490. lhdr->ctrl != IEEE80211_LLC_CTRL || lhdr->oui[0] || lhdr->oui[1] ||
  491. lhdr->oui[2] ) {
  492. DBGC ( netdev->priv, "802.11 %p LLC header is not plain EtherType "
  493. "encapsulator: %02x->%02x [%02x] %02x:%02x:%02x %04x\n",
  494. netdev->priv, lhdr->dsap, lhdr->ssap, lhdr->ctrl,
  495. lhdr->oui[0], lhdr->oui[1], lhdr->oui[2], lhdr->ethertype );
  496. return -EINVAL_PKT_LLC_HEADER;
  497. }
  498. iob_pull ( iobuf, sizeof ( *hdr ) + sizeof ( *lhdr ) );
  499. *ll_dest = hdr->addr1;
  500. *ll_source = hdr->addr3;
  501. *net_proto = lhdr->ethertype;
  502. return 0;
  503. }
  504. /** 802.11 link-layer protocol */
  505. static struct ll_protocol net80211_ll_protocol __ll_protocol = {
  506. .name = "802.11",
  507. .push = net80211_ll_push,
  508. .pull = net80211_ll_pull,
  509. .init_addr = eth_init_addr,
  510. .ntoa = eth_ntoa,
  511. .mc_hash = eth_mc_hash,
  512. .eth_addr = eth_eth_addr,
  513. .ll_proto = htons ( ARPHRD_ETHER ), /* "encapsulated Ethernet" */
  514. .hw_addr_len = ETH_ALEN,
  515. .ll_addr_len = ETH_ALEN,
  516. .ll_header_len = IEEE80211_TYP_FRAME_HEADER_LEN +
  517. IEEE80211_LLC_HEADER_LEN,
  518. };
  519. /* ---------- 802.11 network management API ---------- */
  520. /**
  521. * Get 802.11 device from wrapping network device
  522. *
  523. * @v netdev Wrapping network device
  524. * @ret dev 802.11 device wrapped by network device, or NULL
  525. *
  526. * Returns NULL if the network device does not wrap an 802.11 device.
  527. */
  528. struct net80211_device * net80211_get ( struct net_device *netdev )
  529. {
  530. struct net80211_device *dev;
  531. list_for_each_entry ( dev, &net80211_devices, list ) {
  532. if ( netdev->priv == dev )
  533. return netdev->priv;
  534. }
  535. return NULL;
  536. }
  537. /**
  538. * Set state of 802.11 device keeping management frames
  539. *
  540. * @v dev 802.11 device
  541. * @v enable Whether to keep management frames
  542. * @ret oldenab Whether management frames were enabled before this call
  543. *
  544. * If enable is TRUE, beacon, probe, and action frames will be kept
  545. * and may be retrieved by calling net80211_mgmt_dequeue().
  546. */
  547. int net80211_keep_mgmt ( struct net80211_device *dev, int enable )
  548. {
  549. int oldenab = dev->keep_mgmt;
  550. dev->keep_mgmt = enable;
  551. return oldenab;
  552. }
  553. /**
  554. * Get 802.11 management frame
  555. *
  556. * @v dev 802.11 device
  557. * @ret signal Signal strength of returned management frame
  558. * @ret iob I/O buffer, or NULL if no management frame is queued
  559. *
  560. * Frames will only be returned by this function if
  561. * net80211_keep_mgmt() has been previously called with enable set to
  562. * TRUE.
  563. *
  564. * The calling function takes ownership of the returned I/O buffer.
  565. */
  566. struct io_buffer * net80211_mgmt_dequeue ( struct net80211_device *dev,
  567. int *signal )
  568. {
  569. struct io_buffer *iobuf;
  570. struct net80211_rx_info *rxi;
  571. list_for_each_entry ( rxi, &dev->mgmt_info_queue, list ) {
  572. list_del ( &rxi->list );
  573. if ( signal )
  574. *signal = rxi->signal;
  575. free ( rxi );
  576. list_for_each_entry ( iobuf, &dev->mgmt_queue, list ) {
  577. list_del ( &iobuf->list );
  578. return iobuf;
  579. }
  580. assert ( 0 );
  581. }
  582. return NULL;
  583. }
  584. /**
  585. * Transmit 802.11 management frame
  586. *
  587. * @v dev 802.11 device
  588. * @v fc Frame Control flags for management frame
  589. * @v dest Destination access point
  590. * @v iob I/O buffer
  591. * @ret rc Return status code
  592. *
  593. * The @a fc argument must contain at least an IEEE 802.11 management
  594. * subtype number (e.g. IEEE80211_STYPE_PROBE_REQ). If it contains
  595. * IEEE80211_FC_PROTECTED, the frame will be encrypted prior to
  596. * transmission.
  597. *
  598. * It is required that @a iob have at least 24 bytes of headroom
  599. * reserved before its data start.
  600. */
  601. int net80211_tx_mgmt ( struct net80211_device *dev, u16 fc, u8 dest[6],
  602. struct io_buffer *iob )
  603. {
  604. struct ieee80211_frame *hdr = iob_push ( iob,
  605. IEEE80211_TYP_FRAME_HEADER_LEN );
  606. hdr->fc = IEEE80211_THIS_VERSION | IEEE80211_TYPE_MGMT |
  607. ( fc & ~IEEE80211_FC_PROTECTED );
  608. hdr->duration = net80211_duration ( dev, 10, dev->rates[dev->rate] );
  609. hdr->seq = IEEE80211_MAKESEQ ( ++dev->last_tx_seqnr, 0 );
  610. memcpy ( hdr->addr1, dest, ETH_ALEN ); /* DA = RA */
  611. memcpy ( hdr->addr2, dev->netdev->ll_addr, ETH_ALEN ); /* SA = TA */
  612. memcpy ( hdr->addr3, dest, ETH_ALEN ); /* BSSID */
  613. if ( fc & IEEE80211_FC_PROTECTED ) {
  614. if ( ! dev->crypto )
  615. return -EINVAL_CRYPTO_REQUEST;
  616. struct io_buffer *eiob = dev->crypto->encrypt ( dev->crypto,
  617. iob );
  618. free_iob ( iob );
  619. iob = eiob;
  620. }
  621. return netdev_tx ( dev->netdev, iob );
  622. }
  623. /* ---------- Driver API ---------- */
  624. /**
  625. * Allocate 802.11 device
  626. *
  627. * @v priv_size Size of driver-private allocation area
  628. * @ret dev Newly allocated 802.11 device
  629. *
  630. * This function allocates a net_device with space in its private area
  631. * for both the net80211_device it will wrap and the driver-private
  632. * data space requested. It initializes the link-layer-specific parts
  633. * of the net_device, and links the net80211_device to the net_device
  634. * appropriately.
  635. */
  636. struct net80211_device * net80211_alloc ( size_t priv_size )
  637. {
  638. struct net80211_device *dev;
  639. struct net_device *netdev =
  640. alloc_netdev ( sizeof ( *dev ) + priv_size );
  641. if ( ! netdev )
  642. return NULL;
  643. netdev->ll_protocol = &net80211_ll_protocol;
  644. netdev->ll_broadcast = net80211_ll_broadcast;
  645. netdev->max_pkt_len = IEEE80211_MAX_DATA_LEN;
  646. netdev_init ( netdev, &net80211_netdev_ops );
  647. dev = netdev->priv;
  648. dev->netdev = netdev;
  649. dev->priv = ( u8 * ) dev + sizeof ( *dev );
  650. dev->op = &net80211_null_ops;
  651. process_init_stopped ( &dev->proc_assoc, net80211_step_associate,
  652. &netdev->refcnt );
  653. INIT_LIST_HEAD ( &dev->mgmt_queue );
  654. INIT_LIST_HEAD ( &dev->mgmt_info_queue );
  655. return dev;
  656. }
  657. /**
  658. * Register 802.11 device with network stack
  659. *
  660. * @v dev 802.11 device
  661. * @v ops 802.11 device operations
  662. * @v hw 802.11 hardware information
  663. *
  664. * This also registers the wrapping net_device with the higher network
  665. * layers.
  666. */
  667. int net80211_register ( struct net80211_device *dev,
  668. struct net80211_device_operations *ops,
  669. struct net80211_hw_info *hw )
  670. {
  671. dev->op = ops;
  672. dev->hw = malloc ( sizeof ( *hw ) );
  673. if ( ! dev->hw )
  674. return -ENOMEM;
  675. memcpy ( dev->hw, hw, sizeof ( *hw ) );
  676. memcpy ( dev->netdev->hw_addr, hw->hwaddr, ETH_ALEN );
  677. /* Set some sensible channel defaults for driver's open() function */
  678. memcpy ( dev->channels, dev->hw->channels,
  679. NET80211_MAX_CHANNELS * sizeof ( dev->channels[0] ) );
  680. dev->channel = 0;
  681. list_add_tail ( &dev->list, &net80211_devices );
  682. return register_netdev ( dev->netdev );
  683. }
  684. /**
  685. * Unregister 802.11 device from network stack
  686. *
  687. * @v dev 802.11 device
  688. *
  689. * After this call, the device operations are cleared so that they
  690. * will not be called.
  691. */
  692. void net80211_unregister ( struct net80211_device *dev )
  693. {
  694. unregister_netdev ( dev->netdev );
  695. list_del ( &dev->list );
  696. dev->op = &net80211_null_ops;
  697. }
  698. /**
  699. * Free 802.11 device
  700. *
  701. * @v dev 802.11 device
  702. *
  703. * The device should be unregistered before this function is called.
  704. */
  705. void net80211_free ( struct net80211_device *dev )
  706. {
  707. free ( dev->hw );
  708. rc80211_free ( dev->rctl );
  709. netdev_nullify ( dev->netdev );
  710. netdev_put ( dev->netdev );
  711. }
  712. /* ---------- 802.11 network management workhorse code ---------- */
  713. /**
  714. * Set state of 802.11 device
  715. *
  716. * @v dev 802.11 device
  717. * @v clear Bitmask of flags to clear
  718. * @v set Bitmask of flags to set
  719. * @v status Status or reason code for most recent operation
  720. *
  721. * If @a status represents a reason code, it should be OR'ed with
  722. * NET80211_IS_REASON.
  723. *
  724. * Clearing authentication also clears association; clearing
  725. * association also clears security handshaking state. Clearing
  726. * association removes the link-up flag from the wrapping net_device,
  727. * but setting it does not automatically set the flag; that is left to
  728. * the judgment of higher-level code.
  729. */
  730. static inline void net80211_set_state ( struct net80211_device *dev,
  731. short clear, short set,
  732. u16 status )
  733. {
  734. /* The conditions in this function are deliberately formulated
  735. to be decidable at compile-time in most cases. Since clear
  736. and set are generally passed as constants, the body of this
  737. function can be reduced down to a few statements by the
  738. compiler. */
  739. const int statmsk = NET80211_STATUS_MASK | NET80211_IS_REASON;
  740. if ( clear & NET80211_PROBED )
  741. clear |= NET80211_AUTHENTICATED;
  742. if ( clear & NET80211_AUTHENTICATED )
  743. clear |= NET80211_ASSOCIATED;
  744. if ( clear & NET80211_ASSOCIATED )
  745. clear |= NET80211_CRYPTO_SYNCED;
  746. dev->state = ( dev->state & ~clear ) | set;
  747. dev->state = ( dev->state & ~statmsk ) | ( status & statmsk );
  748. if ( clear & NET80211_ASSOCIATED )
  749. netdev_link_down ( dev->netdev );
  750. if ( ( clear | set ) & NET80211_ASSOCIATED )
  751. dev->op->config ( dev, NET80211_CFG_ASSOC );
  752. if ( status != 0 ) {
  753. if ( status & NET80211_IS_REASON )
  754. dev->assoc_rc = -E80211_REASON ( status );
  755. else
  756. dev->assoc_rc = -E80211_STATUS ( status );
  757. netdev_link_err ( dev->netdev, dev->assoc_rc );
  758. }
  759. }
  760. /**
  761. * Add channels to 802.11 device
  762. *
  763. * @v dev 802.11 device
  764. * @v start First channel number to add
  765. * @v len Number of channels to add
  766. * @v txpower TX power (dBm) to allow on added channels
  767. *
  768. * To replace the current list of channels instead of adding to it,
  769. * set the nr_channels field of the 802.11 device to 0 before calling
  770. * this function.
  771. */
  772. static void net80211_add_channels ( struct net80211_device *dev, int start,
  773. int len, int txpower )
  774. {
  775. int i, chan = start;
  776. for ( i = dev->nr_channels; len-- && i < NET80211_MAX_CHANNELS; i++ ) {
  777. dev->channels[i].channel_nr = chan;
  778. dev->channels[i].maxpower = txpower;
  779. dev->channels[i].hw_value = 0;
  780. if ( chan >= 1 && chan <= 14 ) {
  781. dev->channels[i].band = NET80211_BAND_2GHZ;
  782. if ( chan == 14 )
  783. dev->channels[i].center_freq = 2484;
  784. else
  785. dev->channels[i].center_freq = 2407 + 5 * chan;
  786. chan++;
  787. } else {
  788. dev->channels[i].band = NET80211_BAND_5GHZ;
  789. dev->channels[i].center_freq = 5000 + 5 * chan;
  790. chan += 4;
  791. }
  792. }
  793. dev->nr_channels = i;
  794. }
  795. /**
  796. * Filter 802.11 device channels for hardware capabilities
  797. *
  798. * @v dev 802.11 device
  799. *
  800. * Hardware may support fewer channels than regulatory restrictions
  801. * allow; this function filters out channels in dev->channels that are
  802. * not supported by the hardware list in dev->hwinfo. It also copies
  803. * over the net80211_channel::hw_value and limits maximum TX power
  804. * appropriately.
  805. *
  806. * Channels are matched based on center frequency, ignoring band and
  807. * channel number.
  808. *
  809. * If the driver specifies no supported channels, the effect will be
  810. * as though all were supported.
  811. */
  812. static void net80211_filter_hw_channels ( struct net80211_device *dev )
  813. {
  814. int delta = 0, i = 0;
  815. int old_freq = dev->channels[dev->channel].center_freq;
  816. struct net80211_channel *chan, *hwchan;
  817. if ( ! dev->hw->nr_channels )
  818. return;
  819. dev->channel = 0;
  820. for ( chan = dev->channels; chan < dev->channels + dev->nr_channels;
  821. chan++, i++ ) {
  822. int ok = 0;
  823. for ( hwchan = dev->hw->channels;
  824. hwchan < dev->hw->channels + dev->hw->nr_channels;
  825. hwchan++ ) {
  826. if ( hwchan->center_freq == chan->center_freq ) {
  827. ok = 1;
  828. break;
  829. }
  830. }
  831. if ( ! ok )
  832. delta++;
  833. else {
  834. chan->hw_value = hwchan->hw_value;
  835. if ( hwchan->maxpower != 0 &&
  836. chan->maxpower > hwchan->maxpower )
  837. chan->maxpower = hwchan->maxpower;
  838. if ( old_freq == chan->center_freq )
  839. dev->channel = i - delta;
  840. if ( delta )
  841. chan[-delta] = *chan;
  842. }
  843. }
  844. dev->nr_channels -= delta;
  845. if ( dev->channels[dev->channel].center_freq != old_freq )
  846. dev->op->config ( dev, NET80211_CFG_CHANNEL );
  847. }
  848. /**
  849. * Update 802.11 device state to reflect received capabilities field
  850. *
  851. * @v dev 802.11 device
  852. * @v capab Capabilities field in beacon, probe, or association frame
  853. * @ret rc Return status code
  854. */
  855. static int net80211_process_capab ( struct net80211_device *dev,
  856. u16 capab )
  857. {
  858. u16 old_phy = dev->phy_flags;
  859. if ( ( capab & ( IEEE80211_CAPAB_MANAGED | IEEE80211_CAPAB_ADHOC ) ) !=
  860. IEEE80211_CAPAB_MANAGED ) {
  861. DBGC ( dev, "802.11 %p cannot handle IBSS network\n", dev );
  862. return -ENOSYS;
  863. }
  864. dev->phy_flags &= ~( NET80211_PHY_USE_SHORT_PREAMBLE |
  865. NET80211_PHY_USE_SHORT_SLOT );
  866. if ( capab & IEEE80211_CAPAB_SHORT_PMBL )
  867. dev->phy_flags |= NET80211_PHY_USE_SHORT_PREAMBLE;
  868. if ( capab & IEEE80211_CAPAB_SHORT_SLOT )
  869. dev->phy_flags |= NET80211_PHY_USE_SHORT_SLOT;
  870. if ( old_phy != dev->phy_flags )
  871. dev->op->config ( dev, NET80211_CFG_PHY_PARAMS );
  872. return 0;
  873. }
  874. /**
  875. * Update 802.11 device state to reflect received information elements
  876. *
  877. * @v dev 802.11 device
  878. * @v ie Pointer to first information element
  879. * @v ie_end Pointer to tail of packet I/O buffer
  880. * @ret rc Return status code
  881. */
  882. static int net80211_process_ie ( struct net80211_device *dev,
  883. union ieee80211_ie *ie, void *ie_end )
  884. {
  885. u16 old_rate = dev->rates[dev->rate];
  886. u16 old_phy = dev->phy_flags;
  887. int have_rates = 0, i;
  888. int ds_channel = 0;
  889. int changed = 0;
  890. int band = dev->channels[dev->channel].band;
  891. if ( ! ieee80211_ie_bound ( ie, ie_end ) )
  892. return 0;
  893. for ( ; ie; ie = ieee80211_next_ie ( ie, ie_end ) ) {
  894. switch ( ie->id ) {
  895. case IEEE80211_IE_SSID:
  896. if ( ie->len <= 32 ) {
  897. memcpy ( dev->essid, ie->ssid, ie->len );
  898. dev->essid[ie->len] = 0;
  899. }
  900. break;
  901. case IEEE80211_IE_RATES:
  902. case IEEE80211_IE_EXT_RATES:
  903. if ( ! have_rates ) {
  904. dev->nr_rates = 0;
  905. dev->basic_rates = 0;
  906. have_rates = 1;
  907. }
  908. for ( i = 0; i < ie->len &&
  909. dev->nr_rates < NET80211_MAX_RATES; i++ ) {
  910. u8 rid = ie->rates[i];
  911. u16 rate = ( rid & 0x7f ) * 5;
  912. if ( rid & 0x80 )
  913. dev->basic_rates |=
  914. ( 1 << dev->nr_rates );
  915. dev->rates[dev->nr_rates++] = rate;
  916. }
  917. break;
  918. case IEEE80211_IE_DS_PARAM:
  919. if ( dev->channel < dev->nr_channels && ds_channel ==
  920. dev->channels[dev->channel].channel_nr )
  921. break;
  922. ds_channel = ie->ds_param.current_channel;
  923. net80211_change_channel ( dev, ds_channel );
  924. break;
  925. case IEEE80211_IE_COUNTRY:
  926. dev->nr_channels = 0;
  927. DBGC ( dev, "802.11 %p setting country regulations "
  928. "for %c%c\n", dev, ie->country.name[0],
  929. ie->country.name[1] );
  930. for ( i = 0; i < ( ie->len - 3 ) / 3; i++ ) {
  931. union ieee80211_ie_country_triplet *t =
  932. &ie->country.triplet[i];
  933. if ( t->first > 200 ) {
  934. DBGC ( dev, "802.11 %p ignoring regulatory "
  935. "extension information\n", dev );
  936. } else {
  937. net80211_add_channels ( dev,
  938. t->band.first_channel,
  939. t->band.nr_channels,
  940. t->band.max_txpower );
  941. }
  942. }
  943. net80211_filter_hw_channels ( dev );
  944. break;
  945. case IEEE80211_IE_ERP_INFO:
  946. dev->phy_flags &= ~( NET80211_PHY_USE_PROTECTION |
  947. NET80211_PHY_USE_SHORT_PREAMBLE );
  948. if ( ie->erp_info & IEEE80211_ERP_USE_PROTECTION )
  949. dev->phy_flags |= NET80211_PHY_USE_PROTECTION;
  950. if ( ! ( ie->erp_info & IEEE80211_ERP_BARKER_LONG ) )
  951. dev->phy_flags |= NET80211_PHY_USE_SHORT_PREAMBLE;
  952. break;
  953. }
  954. }
  955. if ( have_rates ) {
  956. /* Allow only those rates that are also supported by
  957. the hardware. */
  958. int delta = 0, j;
  959. dev->rate = 0;
  960. for ( i = 0; i < dev->nr_rates; i++ ) {
  961. int ok = 0;
  962. for ( j = 0; j < dev->hw->nr_rates[band]; j++ ) {
  963. if ( dev->hw->rates[band][j] == dev->rates[i] ){
  964. ok = 1;
  965. break;
  966. }
  967. }
  968. if ( ! ok )
  969. delta++;
  970. else {
  971. dev->rates[i - delta] = dev->rates[i];
  972. if ( old_rate == dev->rates[i] )
  973. dev->rate = i - delta;
  974. }
  975. }
  976. dev->nr_rates -= delta;
  977. /* Sort available rates - sorted subclumps tend to already
  978. exist, so insertion sort works well. */
  979. for ( i = 1; i < dev->nr_rates; i++ ) {
  980. u16 rate = dev->rates[i];
  981. u32 tmp, br, mask;
  982. for ( j = i - 1; j >= 0 && dev->rates[j] >= rate; j-- )
  983. dev->rates[j + 1] = dev->rates[j];
  984. dev->rates[j + 1] = rate;
  985. /* Adjust basic_rates to match by rotating the
  986. bits from bit j+1 to bit i left one position. */
  987. mask = ( ( 1 << i ) - 1 ) & ~( ( 1 << ( j + 1 ) ) - 1 );
  988. br = dev->basic_rates;
  989. tmp = br & ( 1 << i );
  990. br = ( br & ~( mask | tmp ) ) | ( ( br & mask ) << 1 );
  991. br |= ( tmp >> ( i - j - 1 ) );
  992. dev->basic_rates = br;
  993. }
  994. net80211_set_rtscts_rate ( dev );
  995. if ( dev->rates[dev->rate] != old_rate )
  996. changed |= NET80211_CFG_RATE;
  997. }
  998. if ( dev->hw->flags & NET80211_HW_NO_SHORT_PREAMBLE )
  999. dev->phy_flags &= ~NET80211_PHY_USE_SHORT_PREAMBLE;
  1000. if ( dev->hw->flags & NET80211_HW_NO_SHORT_SLOT )
  1001. dev->phy_flags &= ~NET80211_PHY_USE_SHORT_SLOT;
  1002. if ( old_phy != dev->phy_flags )
  1003. changed |= NET80211_CFG_PHY_PARAMS;
  1004. if ( changed )
  1005. dev->op->config ( dev, changed );
  1006. return 0;
  1007. }
  1008. /**
  1009. * Create information elements for outgoing probe or association packet
  1010. *
  1011. * @v dev 802.11 device
  1012. * @v ie Pointer to start of information element area
  1013. * @ret next_ie Pointer to first byte after added information elements
  1014. */
  1015. static union ieee80211_ie *
  1016. net80211_marshal_request_info ( struct net80211_device *dev,
  1017. union ieee80211_ie *ie )
  1018. {
  1019. int i;
  1020. ie->id = IEEE80211_IE_SSID;
  1021. ie->len = strlen ( dev->essid );
  1022. memcpy ( ie->ssid, dev->essid, ie->len );
  1023. ie = ieee80211_next_ie ( ie, NULL );
  1024. ie->id = IEEE80211_IE_RATES;
  1025. ie->len = dev->nr_rates;
  1026. if ( ie->len > 8 )
  1027. ie->len = 8;
  1028. for ( i = 0; i < ie->len; i++ ) {
  1029. ie->rates[i] = dev->rates[i] / 5;
  1030. if ( dev->basic_rates & ( 1 << i ) )
  1031. ie->rates[i] |= 0x80;
  1032. }
  1033. ie = ieee80211_next_ie ( ie, NULL );
  1034. if ( dev->rsn_ie && dev->rsn_ie->id == IEEE80211_IE_RSN ) {
  1035. memcpy ( ie, dev->rsn_ie, dev->rsn_ie->len + 2 );
  1036. ie = ieee80211_next_ie ( ie, NULL );
  1037. }
  1038. if ( dev->nr_rates > 8 ) {
  1039. /* 802.11 requires we use an Extended Basic Rates IE
  1040. for the rates beyond the eighth. */
  1041. ie->id = IEEE80211_IE_EXT_RATES;
  1042. ie->len = dev->nr_rates - 8;
  1043. for ( ; i < dev->nr_rates; i++ ) {
  1044. ie->rates[i - 8] = dev->rates[i] / 5;
  1045. if ( dev->basic_rates & ( 1 << i ) )
  1046. ie->rates[i - 8] |= 0x80;
  1047. }
  1048. ie = ieee80211_next_ie ( ie, NULL );
  1049. }
  1050. if ( dev->rsn_ie && dev->rsn_ie->id == IEEE80211_IE_VENDOR ) {
  1051. memcpy ( ie, dev->rsn_ie, dev->rsn_ie->len + 2 );
  1052. ie = ieee80211_next_ie ( ie, NULL );
  1053. }
  1054. return ie;
  1055. }
  1056. /** Seconds to wait after finding a network, to possibly find better APs for it
  1057. *
  1058. * This is used when a specific SSID to scan for is specified.
  1059. */
  1060. #define NET80211_PROBE_GATHER 1
  1061. /** Seconds to wait after finding a network, to possibly find other networks
  1062. *
  1063. * This is used when an empty SSID is specified, to scan for all
  1064. * networks.
  1065. */
  1066. #define NET80211_PROBE_GATHER_ALL 2
  1067. /** Seconds to allow a probe to take if no network has been found */
  1068. #define NET80211_PROBE_TIMEOUT 6
  1069. /**
  1070. * Begin probe of 802.11 networks
  1071. *
  1072. * @v dev 802.11 device
  1073. * @v essid SSID to probe for, or "" to accept any (may not be NULL)
  1074. * @v active Whether to use active scanning
  1075. * @ret ctx Probe context
  1076. *
  1077. * Active scanning may only be used on channels 1-11 in the 2.4GHz
  1078. * band, due to iPXE's lack of a complete regulatory database. If
  1079. * active scanning is used, probe packets will be sent on each
  1080. * channel; this can allow association with hidden-SSID networks if
  1081. * the SSID is properly specified.
  1082. *
  1083. * A @c NULL return indicates an out-of-memory condition.
  1084. *
  1085. * The returned context must be periodically passed to
  1086. * net80211_probe_step() until that function returns zero.
  1087. */
  1088. struct net80211_probe_ctx * net80211_probe_start ( struct net80211_device *dev,
  1089. const char *essid,
  1090. int active )
  1091. {
  1092. struct net80211_probe_ctx *ctx = zalloc ( sizeof ( *ctx ) );
  1093. if ( ! ctx )
  1094. return NULL;
  1095. assert ( netdev_is_open ( dev->netdev ) );
  1096. ctx->dev = dev;
  1097. ctx->old_keep_mgmt = net80211_keep_mgmt ( dev, 1 );
  1098. ctx->essid = essid;
  1099. if ( dev->essid != ctx->essid )
  1100. strcpy ( dev->essid, ctx->essid );
  1101. if ( active ) {
  1102. struct ieee80211_probe_req *probe_req;
  1103. union ieee80211_ie *ie;
  1104. ctx->probe = alloc_iob ( 128 );
  1105. iob_reserve ( ctx->probe, IEEE80211_TYP_FRAME_HEADER_LEN );
  1106. probe_req = ctx->probe->data;
  1107. ie = net80211_marshal_request_info ( dev,
  1108. probe_req->info_element );
  1109. iob_put ( ctx->probe, ( void * ) ie - ctx->probe->data );
  1110. }
  1111. ctx->ticks_start = currticks();
  1112. ctx->ticks_beacon = 0;
  1113. ctx->ticks_channel = currticks();
  1114. ctx->hop_time = ticks_per_sec() / ( active ? 2 : 6 );
  1115. /*
  1116. * Channels on 2.4GHz overlap, and the most commonly used
  1117. * are 1, 6, and 11. We'll get a result faster if we check
  1118. * every 5 channels, but in order to hit all of them the
  1119. * number of channels must be relatively prime to 5. If it's
  1120. * not, tweak the hop.
  1121. */
  1122. ctx->hop_step = 5;
  1123. while ( dev->nr_channels % ctx->hop_step == 0 && ctx->hop_step > 1 )
  1124. ctx->hop_step--;
  1125. ctx->beacons = malloc ( sizeof ( *ctx->beacons ) );
  1126. INIT_LIST_HEAD ( ctx->beacons );
  1127. dev->channel = 0;
  1128. dev->op->config ( dev, NET80211_CFG_CHANNEL );
  1129. return ctx;
  1130. }
  1131. /**
  1132. * Continue probe of 802.11 networks
  1133. *
  1134. * @v ctx Probe context returned by net80211_probe_start()
  1135. * @ret rc Probe status
  1136. *
  1137. * The return code will be 0 if the probe is still going on (and this
  1138. * function should be called again), a positive number if the probe
  1139. * completed successfully, or a negative error code if the probe
  1140. * failed for that reason.
  1141. *
  1142. * Whether the probe succeeded or failed, you must call
  1143. * net80211_probe_finish_all() or net80211_probe_finish_best()
  1144. * (depending on whether you want information on all networks or just
  1145. * the best-signal one) in order to release the probe context. A
  1146. * failed probe may still have acquired some valid data.
  1147. */
  1148. int net80211_probe_step ( struct net80211_probe_ctx *ctx )
  1149. {
  1150. struct net80211_device *dev = ctx->dev;
  1151. u32 start_timeout = NET80211_PROBE_TIMEOUT * ticks_per_sec();
  1152. u32 gather_timeout = ticks_per_sec();
  1153. u32 now = currticks();
  1154. struct io_buffer *iob;
  1155. int signal;
  1156. int rc;
  1157. char ssid[IEEE80211_MAX_SSID_LEN + 1];
  1158. gather_timeout *= ( ctx->essid[0] ? NET80211_PROBE_GATHER :
  1159. NET80211_PROBE_GATHER_ALL );
  1160. /* Time out if necessary */
  1161. if ( now >= ctx->ticks_start + start_timeout )
  1162. return list_empty ( ctx->beacons ) ? -ETIMEDOUT : +1;
  1163. if ( ctx->ticks_beacon > 0 && now >= ctx->ticks_start + gather_timeout )
  1164. return +1;
  1165. /* Change channels if necessary */
  1166. if ( now >= ctx->ticks_channel + ctx->hop_time ) {
  1167. dev->channel = ( dev->channel + ctx->hop_step )
  1168. % dev->nr_channels;
  1169. dev->op->config ( dev, NET80211_CFG_CHANNEL );
  1170. udelay ( dev->hw->channel_change_time );
  1171. ctx->ticks_channel = now;
  1172. if ( ctx->probe ) {
  1173. struct io_buffer *siob = ctx->probe; /* to send */
  1174. /* make a copy for future use */
  1175. iob = alloc_iob ( siob->tail - siob->head );
  1176. iob_reserve ( iob, iob_headroom ( siob ) );
  1177. memcpy ( iob_put ( iob, iob_len ( siob ) ),
  1178. siob->data, iob_len ( siob ) );
  1179. ctx->probe = iob;
  1180. rc = net80211_tx_mgmt ( dev, IEEE80211_STYPE_PROBE_REQ,
  1181. net80211_ll_broadcast,
  1182. iob_disown ( siob ) );
  1183. if ( rc ) {
  1184. DBGC ( dev, "802.11 %p send probe failed: "
  1185. "%s\n", dev, strerror ( rc ) );
  1186. return rc;
  1187. }
  1188. }
  1189. }
  1190. /* Check for new management packets */
  1191. while ( ( iob = net80211_mgmt_dequeue ( dev, &signal ) ) != NULL ) {
  1192. struct ieee80211_frame *hdr;
  1193. struct ieee80211_beacon *beacon;
  1194. union ieee80211_ie *ie;
  1195. struct net80211_wlan *wlan;
  1196. u16 type;
  1197. hdr = iob->data;
  1198. type = hdr->fc & IEEE80211_FC_SUBTYPE;
  1199. beacon = ( struct ieee80211_beacon * ) hdr->data;
  1200. if ( type != IEEE80211_STYPE_BEACON &&
  1201. type != IEEE80211_STYPE_PROBE_RESP ) {
  1202. DBGC2 ( dev, "802.11 %p probe: non-beacon\n", dev );
  1203. goto drop;
  1204. }
  1205. if ( ( void * ) beacon->info_element >= iob->tail ) {
  1206. DBGC ( dev, "802.11 %p probe: beacon with no IEs\n",
  1207. dev );
  1208. goto drop;
  1209. }
  1210. ie = beacon->info_element;
  1211. if ( ! ieee80211_ie_bound ( ie, iob->tail ) )
  1212. ie = NULL;
  1213. while ( ie && ie->id != IEEE80211_IE_SSID )
  1214. ie = ieee80211_next_ie ( ie, iob->tail );
  1215. if ( ! ie ) {
  1216. DBGC ( dev, "802.11 %p probe: beacon with no SSID\n",
  1217. dev );
  1218. goto drop;
  1219. }
  1220. memcpy ( ssid, ie->ssid, ie->len );
  1221. ssid[ie->len] = 0;
  1222. if ( ctx->essid[0] && strcmp ( ctx->essid, ssid ) != 0 ) {
  1223. DBGC2 ( dev, "802.11 %p probe: beacon with wrong SSID "
  1224. "(%s)\n", dev, ssid );
  1225. goto drop;
  1226. }
  1227. /* See if we've got an entry for this network */
  1228. list_for_each_entry ( wlan, ctx->beacons, list ) {
  1229. if ( strcmp ( wlan->essid, ssid ) != 0 )
  1230. continue;
  1231. if ( signal < wlan->signal ) {
  1232. DBGC2 ( dev, "802.11 %p probe: beacon for %s "
  1233. "(%s) with weaker signal %d\n", dev,
  1234. ssid, eth_ntoa ( hdr->addr3 ), signal );
  1235. goto drop;
  1236. }
  1237. goto fill;
  1238. }
  1239. /* No entry yet - make one */
  1240. wlan = zalloc ( sizeof ( *wlan ) );
  1241. strcpy ( wlan->essid, ssid );
  1242. list_add_tail ( &wlan->list, ctx->beacons );
  1243. /* Whether we're using an old entry or a new one, fill
  1244. it with new data. */
  1245. fill:
  1246. memcpy ( wlan->bssid, hdr->addr3, ETH_ALEN );
  1247. wlan->signal = signal;
  1248. wlan->channel = dev->channels[dev->channel].channel_nr;
  1249. /* Copy this I/O buffer into a new wlan->beacon; the
  1250. * iob we've got probably came from the device driver
  1251. * and may have the full 2.4k allocation, which we
  1252. * don't want to keep around wasting memory.
  1253. */
  1254. free_iob ( wlan->beacon );
  1255. wlan->beacon = alloc_iob ( iob_len ( iob ) );
  1256. memcpy ( iob_put ( wlan->beacon, iob_len ( iob ) ),
  1257. iob->data, iob_len ( iob ) );
  1258. if ( ( rc = sec80211_detect ( wlan->beacon, &wlan->handshaking,
  1259. &wlan->crypto ) ) == -ENOTSUP ) {
  1260. struct ieee80211_beacon *beacon =
  1261. ( struct ieee80211_beacon * ) hdr->data;
  1262. if ( beacon->capability & IEEE80211_CAPAB_PRIVACY ) {
  1263. DBG ( "802.11 %p probe: secured network %s but "
  1264. "encryption support not compiled in\n",
  1265. dev, wlan->essid );
  1266. wlan->handshaking = NET80211_SECPROT_UNKNOWN;
  1267. wlan->crypto = NET80211_CRYPT_UNKNOWN;
  1268. } else {
  1269. wlan->handshaking = NET80211_SECPROT_NONE;
  1270. wlan->crypto = NET80211_CRYPT_NONE;
  1271. }
  1272. } else if ( rc != 0 ) {
  1273. DBGC ( dev, "802.11 %p probe warning: network "
  1274. "%s with unidentifiable security "
  1275. "settings: %s\n", dev, wlan->essid,
  1276. strerror ( rc ) );
  1277. }
  1278. ctx->ticks_beacon = now;
  1279. DBGC2 ( dev, "802.11 %p probe: good beacon for %s (%s)\n",
  1280. dev, wlan->essid, eth_ntoa ( wlan->bssid ) );
  1281. drop:
  1282. free_iob ( iob );
  1283. }
  1284. return 0;
  1285. }
  1286. /**
  1287. * Finish probe of 802.11 networks, returning best-signal network found
  1288. *
  1289. * @v ctx Probe context
  1290. * @ret wlan Best-signal network found, or @c NULL if none were found
  1291. *
  1292. * If net80211_probe_start() was called with a particular SSID
  1293. * parameter as filter, only a network with that SSID (matching
  1294. * case-sensitively) can be returned from this function.
  1295. */
  1296. struct net80211_wlan *
  1297. net80211_probe_finish_best ( struct net80211_probe_ctx *ctx )
  1298. {
  1299. struct net80211_wlan *best = NULL, *wlan;
  1300. if ( ! ctx )
  1301. return NULL;
  1302. list_for_each_entry ( wlan, ctx->beacons, list ) {
  1303. if ( ! best || best->signal < wlan->signal )
  1304. best = wlan;
  1305. }
  1306. if ( best )
  1307. list_del ( &best->list );
  1308. else
  1309. DBGC ( ctx->dev, "802.11 %p probe: found nothing for '%s'\n",
  1310. ctx->dev, ctx->essid );
  1311. net80211_free_wlanlist ( ctx->beacons );
  1312. net80211_keep_mgmt ( ctx->dev, ctx->old_keep_mgmt );
  1313. if ( ctx->probe )
  1314. free_iob ( ctx->probe );
  1315. free ( ctx );
  1316. return best;
  1317. }
  1318. /**
  1319. * Finish probe of 802.11 networks, returning all networks found
  1320. *
  1321. * @v ctx Probe context
  1322. * @ret list List of net80211_wlan detailing networks found
  1323. *
  1324. * If net80211_probe_start() was called with a particular SSID
  1325. * parameter as filter, this will always return either an empty or a
  1326. * one-element list.
  1327. */
  1328. struct list_head *net80211_probe_finish_all ( struct net80211_probe_ctx *ctx )
  1329. {
  1330. struct list_head *beacons = ctx->beacons;
  1331. if ( ! ctx )
  1332. return NULL;
  1333. net80211_keep_mgmt ( ctx->dev, ctx->old_keep_mgmt );
  1334. if ( ctx->probe )
  1335. free_iob ( ctx->probe );
  1336. free ( ctx );
  1337. return beacons;
  1338. }
  1339. /**
  1340. * Free WLAN structure
  1341. *
  1342. * @v wlan WLAN structure to free
  1343. */
  1344. void net80211_free_wlan ( struct net80211_wlan *wlan )
  1345. {
  1346. if ( wlan ) {
  1347. free_iob ( wlan->beacon );
  1348. free ( wlan );
  1349. }
  1350. }
  1351. /**
  1352. * Free list of WLAN structures
  1353. *
  1354. * @v list List of WLAN structures to free
  1355. */
  1356. void net80211_free_wlanlist ( struct list_head *list )
  1357. {
  1358. struct net80211_wlan *wlan, *tmp;
  1359. if ( ! list )
  1360. return;
  1361. list_for_each_entry_safe ( wlan, tmp, list, list ) {
  1362. list_del ( &wlan->list );
  1363. net80211_free_wlan ( wlan );
  1364. }
  1365. free ( list );
  1366. }
  1367. /** Number of ticks to wait for replies to association management frames */
  1368. #define ASSOC_TIMEOUT TICKS_PER_SEC
  1369. /** Number of times to try sending a particular association management frame */
  1370. #define ASSOC_RETRIES 2
  1371. /**
  1372. * Step 802.11 association process
  1373. *
  1374. * @v proc Association process
  1375. */
  1376. static void net80211_step_associate ( struct process *proc )
  1377. {
  1378. struct net80211_device *dev =
  1379. container_of ( proc, struct net80211_device, proc_assoc );
  1380. int rc = 0;
  1381. int status = dev->state & NET80211_STATUS_MASK;
  1382. /*
  1383. * We use a sort of state machine implemented using bits in
  1384. * the dev->state variable. At each call, we take the
  1385. * logically first step that has not yet succeeded; either it
  1386. * has not been tried yet, it's being retried, or it failed.
  1387. * If it failed, we return an error indication; otherwise we
  1388. * perform the step. If it succeeds, RX handling code will set
  1389. * the appropriate status bit for us.
  1390. *
  1391. * Probe works a bit differently, since we have to step it
  1392. * on every call instead of waiting for a packet to arrive
  1393. * that will set the completion bit for us.
  1394. */
  1395. /* If we're waiting for a reply, check for timeout condition */
  1396. if ( dev->state & NET80211_WAITING ) {
  1397. /* Sanity check */
  1398. if ( ! dev->associating )
  1399. return;
  1400. if ( currticks() - dev->ctx.assoc->last_packet > ASSOC_TIMEOUT ) {
  1401. /* Timed out - fail if too many retries, or retry */
  1402. dev->ctx.assoc->times_tried++;
  1403. if ( ++dev->ctx.assoc->times_tried > ASSOC_RETRIES ) {
  1404. rc = -ETIMEDOUT;
  1405. goto fail;
  1406. }
  1407. } else {
  1408. /* Didn't time out - let it keep going */
  1409. return;
  1410. }
  1411. } else {
  1412. if ( dev->state & NET80211_PROBED )
  1413. dev->ctx.assoc->times_tried = 0;
  1414. }
  1415. if ( ! ( dev->state & NET80211_PROBED ) ) {
  1416. /* state: probe */
  1417. if ( ! dev->ctx.probe ) {
  1418. /* start probe */
  1419. int active = fetch_intz_setting ( NULL,
  1420. &net80211_active_setting );
  1421. int band = dev->hw->bands;
  1422. if ( active )
  1423. band &= ~NET80211_BAND_BIT_5GHZ;
  1424. rc = net80211_prepare_probe ( dev, band, active );
  1425. if ( rc )
  1426. goto fail;
  1427. dev->ctx.probe = net80211_probe_start ( dev, dev->essid,
  1428. active );
  1429. if ( ! dev->ctx.probe ) {
  1430. dev->assoc_rc = -ENOMEM;
  1431. goto fail;
  1432. }
  1433. }
  1434. rc = net80211_probe_step ( dev->ctx.probe );
  1435. if ( ! rc ) {
  1436. return; /* still going */
  1437. }
  1438. dev->associating = net80211_probe_finish_best ( dev->ctx.probe );
  1439. dev->ctx.probe = NULL;
  1440. if ( ! dev->associating ) {
  1441. if ( rc > 0 ) /* "successful" probe found nothing */
  1442. rc = -ETIMEDOUT;
  1443. goto fail;
  1444. }
  1445. /* If we probed using a broadcast SSID, record that
  1446. fact for the settings applicator before we clobber
  1447. it with the specific SSID we've chosen. */
  1448. if ( ! dev->essid[0] )
  1449. dev->state |= NET80211_AUTO_SSID;
  1450. DBGC ( dev, "802.11 %p found network %s (%s)\n", dev,
  1451. dev->associating->essid,
  1452. eth_ntoa ( dev->associating->bssid ) );
  1453. dev->ctx.assoc = zalloc ( sizeof ( *dev->ctx.assoc ) );
  1454. if ( ! dev->ctx.assoc ) {
  1455. rc = -ENOMEM;
  1456. goto fail;
  1457. }
  1458. dev->state |= NET80211_PROBED;
  1459. dev->ctx.assoc->method = IEEE80211_AUTH_OPEN_SYSTEM;
  1460. return;
  1461. }
  1462. /* Record time of sending the packet we're about to send, for timeout */
  1463. dev->ctx.assoc->last_packet = currticks();
  1464. if ( ! ( dev->state & NET80211_AUTHENTICATED ) ) {
  1465. /* state: prepare and authenticate */
  1466. if ( status != IEEE80211_STATUS_SUCCESS ) {
  1467. /* we tried authenticating already, but failed */
  1468. int method = dev->ctx.assoc->method;
  1469. if ( method == IEEE80211_AUTH_OPEN_SYSTEM &&
  1470. ( status == IEEE80211_STATUS_AUTH_CHALL_INVALID ||
  1471. status == IEEE80211_STATUS_AUTH_ALGO_UNSUPP ) ) {
  1472. /* Maybe this network uses Shared Key? */
  1473. dev->ctx.assoc->method =
  1474. IEEE80211_AUTH_SHARED_KEY;
  1475. } else {
  1476. goto fail;
  1477. }
  1478. }
  1479. DBGC ( dev, "802.11 %p authenticating with method %d\n", dev,
  1480. dev->ctx.assoc->method );
  1481. rc = net80211_prepare_assoc ( dev, dev->associating );
  1482. if ( rc )
  1483. goto fail;
  1484. rc = net80211_send_auth ( dev, dev->associating,
  1485. dev->ctx.assoc->method );
  1486. if ( rc )
  1487. goto fail;
  1488. return;
  1489. }
  1490. if ( ! ( dev->state & NET80211_ASSOCIATED ) ) {
  1491. /* state: associate */
  1492. if ( status != IEEE80211_STATUS_SUCCESS )
  1493. goto fail;
  1494. DBGC ( dev, "802.11 %p associating\n", dev );
  1495. if ( dev->handshaker && dev->handshaker->start &&
  1496. ! dev->handshaker->started ) {
  1497. rc = dev->handshaker->start ( dev );
  1498. if ( rc < 0 )
  1499. goto fail;
  1500. dev->handshaker->started = 1;
  1501. }
  1502. rc = net80211_send_assoc ( dev, dev->associating );
  1503. if ( rc )
  1504. goto fail;
  1505. return;
  1506. }
  1507. if ( ! ( dev->state & NET80211_CRYPTO_SYNCED ) ) {
  1508. /* state: crypto sync */
  1509. DBGC ( dev, "802.11 %p security handshaking\n", dev );
  1510. if ( ! dev->handshaker || ! dev->handshaker->step ) {
  1511. dev->state |= NET80211_CRYPTO_SYNCED;
  1512. return;
  1513. }
  1514. rc = dev->handshaker->step ( dev );
  1515. if ( rc < 0 ) {
  1516. /* Only record the returned error if we're
  1517. still marked as associated, because an
  1518. asynchronous error will have already been
  1519. reported to net80211_deauthenticate() and
  1520. assoc_rc thereby set. */
  1521. if ( dev->state & NET80211_ASSOCIATED )
  1522. dev->assoc_rc = rc;
  1523. rc = 0;
  1524. goto fail;
  1525. }
  1526. if ( rc > 0 ) {
  1527. dev->assoc_rc = 0;
  1528. dev->state |= NET80211_CRYPTO_SYNCED;
  1529. }
  1530. return;
  1531. }
  1532. /* state: done! */
  1533. netdev_link_up ( dev->netdev );
  1534. dev->assoc_rc = 0;
  1535. dev->state &= ~NET80211_WORKING;
  1536. free ( dev->ctx.assoc );
  1537. dev->ctx.assoc = NULL;
  1538. net80211_free_wlan ( dev->associating );
  1539. dev->associating = NULL;
  1540. dev->rctl = rc80211_init ( dev );
  1541. process_del ( proc );
  1542. DBGC ( dev, "802.11 %p associated with %s (%s)\n", dev,
  1543. dev->essid, eth_ntoa ( dev->bssid ) );
  1544. return;
  1545. fail:
  1546. dev->state &= ~( NET80211_WORKING | NET80211_WAITING );
  1547. if ( rc )
  1548. dev->assoc_rc = rc;
  1549. netdev_link_err ( dev->netdev, dev->assoc_rc );
  1550. /* We never reach here from the middle of a probe, so we don't
  1551. need to worry about freeing dev->ctx.probe. */
  1552. if ( dev->state & NET80211_PROBED ) {
  1553. free ( dev->ctx.assoc );
  1554. dev->ctx.assoc = NULL;
  1555. }
  1556. net80211_free_wlan ( dev->associating );
  1557. dev->associating = NULL;
  1558. process_del ( proc );
  1559. DBGC ( dev, "802.11 %p association failed (state=%04x): "
  1560. "%s\n", dev, dev->state, strerror ( dev->assoc_rc ) );
  1561. /* Try it again: */
  1562. net80211_autoassociate ( dev );
  1563. }
  1564. /**
  1565. * Check for 802.11 SSID or key updates
  1566. *
  1567. * This acts as a settings applicator; if the user changes netX/ssid,
  1568. * and netX is currently open, the association task will be invoked
  1569. * again. If the user changes the encryption key, the current security
  1570. * handshaker will be asked to update its state to match; if that is
  1571. * impossible without reassociation, we reassociate.
  1572. */
  1573. static int net80211_check_settings_update ( void )
  1574. {
  1575. struct net80211_device *dev;
  1576. char ssid[IEEE80211_MAX_SSID_LEN + 1];
  1577. int key_reassoc;
  1578. list_for_each_entry ( dev, &net80211_devices, list ) {
  1579. if ( ! netdev_is_open ( dev->netdev ) )
  1580. continue;
  1581. key_reassoc = 0;
  1582. if ( dev->handshaker && dev->handshaker->change_key &&
  1583. dev->handshaker->change_key ( dev ) < 0 )
  1584. key_reassoc = 1;
  1585. fetch_string_setting ( netdev_settings ( dev->netdev ),
  1586. &net80211_ssid_setting, ssid,
  1587. IEEE80211_MAX_SSID_LEN + 1 );
  1588. if ( key_reassoc ||
  1589. ( ! ( ! ssid[0] && ( dev->state & NET80211_AUTO_SSID ) ) &&
  1590. strcmp ( ssid, dev->essid ) != 0 ) ) {
  1591. DBGC ( dev, "802.11 %p updating association: "
  1592. "%s -> %s\n", dev, dev->essid, ssid );
  1593. net80211_autoassociate ( dev );
  1594. }
  1595. }
  1596. return 0;
  1597. }
  1598. /**
  1599. * Start 802.11 association process
  1600. *
  1601. * @v dev 802.11 device
  1602. *
  1603. * If the association process is running, it will be restarted.
  1604. */
  1605. void net80211_autoassociate ( struct net80211_device *dev )
  1606. {
  1607. if ( ! ( dev->state & NET80211_WORKING ) ) {
  1608. DBGC2 ( dev, "802.11 %p spawning association process\n", dev );
  1609. process_add ( &dev->proc_assoc );
  1610. } else {
  1611. DBGC2 ( dev, "802.11 %p restarting association\n", dev );
  1612. }
  1613. /* Clean up everything an earlier association process might
  1614. have been in the middle of using */
  1615. if ( dev->associating )
  1616. net80211_free_wlan ( dev->associating );
  1617. if ( ! ( dev->state & NET80211_PROBED ) )
  1618. net80211_free_wlan (
  1619. net80211_probe_finish_best ( dev->ctx.probe ) );
  1620. else
  1621. free ( dev->ctx.assoc );
  1622. /* Reset to a clean state */
  1623. fetch_string_setting ( netdev_settings ( dev->netdev ),
  1624. &net80211_ssid_setting, dev->essid,
  1625. IEEE80211_MAX_SSID_LEN + 1 );
  1626. dev->ctx.probe = NULL;
  1627. dev->associating = NULL;
  1628. dev->assoc_rc = 0;
  1629. net80211_set_state ( dev, NET80211_PROBED, NET80211_WORKING, 0 );
  1630. }
  1631. /**
  1632. * Pick TX rate for RTS/CTS packets based on data rate
  1633. *
  1634. * @v dev 802.11 device
  1635. *
  1636. * The RTS/CTS rate is the fastest TX rate marked as "basic" that is
  1637. * not faster than the data rate.
  1638. */
  1639. static void net80211_set_rtscts_rate ( struct net80211_device *dev )
  1640. {
  1641. u16 datarate = dev->rates[dev->rate];
  1642. u16 rtsrate = 0;
  1643. int rts_idx = -1;
  1644. int i;
  1645. for ( i = 0; i < dev->nr_rates; i++ ) {
  1646. u16 rate = dev->rates[i];
  1647. if ( ! ( dev->basic_rates & ( 1 << i ) ) || rate > datarate )
  1648. continue;
  1649. if ( rate > rtsrate ) {
  1650. rtsrate = rate;
  1651. rts_idx = i;
  1652. }
  1653. }
  1654. /* If this is in initialization, we might not have any basic
  1655. rates; just use the first data rate in that case. */
  1656. if ( rts_idx < 0 )
  1657. rts_idx = 0;
  1658. dev->rtscts_rate = rts_idx;
  1659. }
  1660. /**
  1661. * Set data transmission rate for 802.11 device
  1662. *
  1663. * @v dev 802.11 device
  1664. * @v rate Rate to set, as index into @c dev->rates array
  1665. */
  1666. void net80211_set_rate_idx ( struct net80211_device *dev, int rate )
  1667. {
  1668. assert ( netdev_is_open ( dev->netdev ) );
  1669. if ( rate >= 0 && rate < dev->nr_rates && rate != dev->rate ) {
  1670. DBGC2 ( dev, "802.11 %p changing rate from %d->%d Mbps\n",
  1671. dev, dev->rates[dev->rate] / 10,
  1672. dev->rates[rate] / 10 );
  1673. dev->rate = rate;
  1674. net80211_set_rtscts_rate ( dev );
  1675. dev->op->config ( dev, NET80211_CFG_RATE );
  1676. }
  1677. }
  1678. /**
  1679. * Configure 802.11 device to transmit on a certain channel
  1680. *
  1681. * @v dev 802.11 device
  1682. * @v channel Channel number (1-11 for 2.4GHz) to transmit on
  1683. */
  1684. int net80211_change_channel ( struct net80211_device *dev, int channel )
  1685. {
  1686. int i, oldchan = dev->channel;
  1687. assert ( netdev_is_open ( dev->netdev ) );
  1688. for ( i = 0; i < dev->nr_channels; i++ ) {
  1689. if ( dev->channels[i].channel_nr == channel ) {
  1690. dev->channel = i;
  1691. break;
  1692. }
  1693. }
  1694. if ( i == dev->nr_channels )
  1695. return -ENOENT;
  1696. if ( i != oldchan )
  1697. return dev->op->config ( dev, NET80211_CFG_CHANNEL );
  1698. return 0;
  1699. }
  1700. /**
  1701. * Prepare 802.11 device channel and rate set for scanning
  1702. *
  1703. * @v dev 802.11 device
  1704. * @v band RF band(s) on which to prepare for scanning
  1705. * @v active Whether the scanning will be active
  1706. * @ret rc Return status code
  1707. */
  1708. int net80211_prepare_probe ( struct net80211_device *dev, int band,
  1709. int active )
  1710. {
  1711. assert ( netdev_is_open ( dev->netdev ) );
  1712. if ( active && ( band & NET80211_BAND_BIT_5GHZ ) ) {
  1713. DBGC ( dev, "802.11 %p cannot perform active scanning on "
  1714. "5GHz band\n", dev );
  1715. return -EINVAL_ACTIVE_SCAN;
  1716. }
  1717. if ( band == 0 ) {
  1718. /* This can happen for a 5GHz-only card with 5GHz
  1719. scanning masked out by an active request. */
  1720. DBGC ( dev, "802.11 %p asked to prepare for scanning nothing\n",
  1721. dev );
  1722. return -EINVAL_ACTIVE_SCAN;
  1723. }
  1724. dev->nr_channels = 0;
  1725. if ( active )
  1726. net80211_add_channels ( dev, 1, 11, NET80211_REG_TXPOWER );
  1727. else {
  1728. if ( band & NET80211_BAND_BIT_2GHZ )
  1729. net80211_add_channels ( dev, 1, 14,
  1730. NET80211_REG_TXPOWER );
  1731. if ( band & NET80211_BAND_BIT_5GHZ )
  1732. net80211_add_channels ( dev, 36, 8,
  1733. NET80211_REG_TXPOWER );
  1734. }
  1735. net80211_filter_hw_channels ( dev );
  1736. /* Use channel 1 for now */
  1737. dev->channel = 0;
  1738. dev->op->config ( dev, NET80211_CFG_CHANNEL );
  1739. /* Always do active probes at lowest (presumably first) speed */
  1740. dev->rate = 0;
  1741. dev->nr_rates = 1;
  1742. dev->rates[0] = dev->hw->rates[dev->channels[0].band][0];
  1743. dev->op->config ( dev, NET80211_CFG_RATE );
  1744. return 0;
  1745. }
  1746. /**
  1747. * Prepare 802.11 device channel and rate set for communication
  1748. *
  1749. * @v dev 802.11 device
  1750. * @v wlan WLAN to prepare for communication with
  1751. * @ret rc Return status code
  1752. */
  1753. int net80211_prepare_assoc ( struct net80211_device *dev,
  1754. struct net80211_wlan *wlan )
  1755. {
  1756. struct ieee80211_frame *hdr = wlan->beacon->data;
  1757. struct ieee80211_beacon *beacon =
  1758. ( struct ieee80211_beacon * ) hdr->data;
  1759. struct net80211_handshaker *handshaker;
  1760. int rc;
  1761. assert ( netdev_is_open ( dev->netdev ) );
  1762. net80211_set_state ( dev, NET80211_ASSOCIATED, 0, 0 );
  1763. memcpy ( dev->bssid, wlan->bssid, ETH_ALEN );
  1764. strcpy ( dev->essid, wlan->essid );
  1765. free ( dev->rsn_ie );
  1766. dev->rsn_ie = NULL;
  1767. dev->last_beacon_timestamp = beacon->timestamp;
  1768. dev->tx_beacon_interval = 1024 * beacon->beacon_interval;
  1769. /* Barring an IE that tells us the channel outright, assume
  1770. the channel we heard this AP best on is the channel it's
  1771. communicating on. */
  1772. net80211_change_channel ( dev, wlan->channel );
  1773. rc = net80211_process_capab ( dev, beacon->capability );
  1774. if ( rc )
  1775. return rc;
  1776. rc = net80211_process_ie ( dev, beacon->info_element,
  1777. wlan->beacon->tail );
  1778. if ( rc )
  1779. return rc;
  1780. /* Associate at the lowest rate so we know it'll get through */
  1781. dev->rate = 0;
  1782. dev->op->config ( dev, NET80211_CFG_RATE );
  1783. /* Free old handshaker and crypto, if they exist */
  1784. if ( dev->handshaker && dev->handshaker->stop &&
  1785. dev->handshaker->started )
  1786. dev->handshaker->stop ( dev );
  1787. free ( dev->handshaker );
  1788. dev->handshaker = NULL;
  1789. free ( dev->crypto );
  1790. free ( dev->gcrypto );
  1791. dev->crypto = dev->gcrypto = NULL;
  1792. /* Find new security handshaker to use */
  1793. for_each_table_entry ( handshaker, NET80211_HANDSHAKERS ) {
  1794. if ( handshaker->protocol == wlan->handshaking ) {
  1795. dev->handshaker = zalloc ( sizeof ( *handshaker ) +
  1796. handshaker->priv_len );
  1797. if ( ! dev->handshaker )
  1798. return -ENOMEM;
  1799. memcpy ( dev->handshaker, handshaker,
  1800. sizeof ( *handshaker ) );
  1801. dev->handshaker->priv = ( ( void * ) dev->handshaker +
  1802. sizeof ( *handshaker ) );
  1803. break;
  1804. }
  1805. }
  1806. if ( ( wlan->handshaking != NET80211_SECPROT_NONE ) &&
  1807. ! dev->handshaker ) {
  1808. DBGC ( dev, "802.11 %p no support for handshaking scheme %d\n",
  1809. dev, wlan->handshaking );
  1810. return -( ENOTSUP | ( wlan->handshaking << 8 ) );
  1811. }
  1812. /* Initialize security handshaker */
  1813. if ( dev->handshaker ) {
  1814. rc = dev->handshaker->init ( dev );
  1815. if ( rc < 0 )
  1816. return rc;
  1817. }
  1818. return 0;
  1819. }
  1820. /**
  1821. * Send 802.11 initial authentication frame
  1822. *
  1823. * @v dev 802.11 device
  1824. * @v wlan WLAN to authenticate with
  1825. * @v method Authentication method
  1826. * @ret rc Return status code
  1827. *
  1828. * @a method may be 0 for Open System authentication or 1 for Shared
  1829. * Key authentication. Open System provides no security in association
  1830. * whatsoever, relying on encryption for confidentiality, but Shared
  1831. * Key actively introduces security problems and is very rarely used.
  1832. */
  1833. int net80211_send_auth ( struct net80211_device *dev,
  1834. struct net80211_wlan *wlan, int method )
  1835. {
  1836. struct io_buffer *iob = alloc_iob ( 64 );
  1837. struct ieee80211_auth *auth;
  1838. net80211_set_state ( dev, 0, NET80211_WAITING, 0 );
  1839. iob_reserve ( iob, IEEE80211_TYP_FRAME_HEADER_LEN );
  1840. auth = iob_put ( iob, sizeof ( *auth ) );
  1841. auth->algorithm = method;
  1842. auth->tx_seq = 1;
  1843. auth->status = 0;
  1844. return net80211_tx_mgmt ( dev, IEEE80211_STYPE_AUTH, wlan->bssid, iob );
  1845. }
  1846. /**
  1847. * Handle receipt of 802.11 authentication frame
  1848. *
  1849. * @v dev 802.11 device
  1850. * @v iob I/O buffer
  1851. *
  1852. * If the authentication method being used is Shared Key, and the
  1853. * frame that was received included challenge text, the frame is
  1854. * encrypted using the cryptosystem currently in effect and sent back
  1855. * to the AP to complete the authentication.
  1856. */
  1857. static void net80211_handle_auth ( struct net80211_device *dev,
  1858. struct io_buffer *iob )
  1859. {
  1860. struct ieee80211_frame *hdr = iob->data;
  1861. struct ieee80211_auth *auth =
  1862. ( struct ieee80211_auth * ) hdr->data;
  1863. if ( auth->tx_seq & 1 ) {
  1864. DBGC ( dev, "802.11 %p authentication received improperly "
  1865. "directed frame (seq. %d)\n", dev, auth->tx_seq );
  1866. net80211_set_state ( dev, NET80211_WAITING, 0,
  1867. IEEE80211_STATUS_FAILURE );
  1868. return;
  1869. }
  1870. if ( auth->status != IEEE80211_STATUS_SUCCESS ) {
  1871. DBGC ( dev, "802.11 %p authentication failed: status %d\n",
  1872. dev, auth->status );
  1873. net80211_set_state ( dev, NET80211_WAITING, 0,
  1874. auth->status );
  1875. return;
  1876. }
  1877. if ( auth->algorithm == IEEE80211_AUTH_SHARED_KEY && ! dev->crypto ) {
  1878. DBGC ( dev, "802.11 %p can't perform shared-key authentication "
  1879. "without a cryptosystem\n", dev );
  1880. net80211_set_state ( dev, NET80211_WAITING, 0,
  1881. IEEE80211_STATUS_FAILURE );
  1882. return;
  1883. }
  1884. if ( auth->algorithm == IEEE80211_AUTH_SHARED_KEY &&
  1885. auth->tx_seq == 2 ) {
  1886. /* Since the iob we got is going to be freed as soon
  1887. as we return, we can do some in-place
  1888. modification. */
  1889. auth->tx_seq = 3;
  1890. auth->status = 0;
  1891. memcpy ( hdr->addr2, hdr->addr1, ETH_ALEN );
  1892. memcpy ( hdr->addr1, hdr->addr3, ETH_ALEN );
  1893. netdev_tx ( dev->netdev,
  1894. dev->crypto->encrypt ( dev->crypto, iob ) );
  1895. return;
  1896. }
  1897. net80211_set_state ( dev, NET80211_WAITING, NET80211_AUTHENTICATED,
  1898. IEEE80211_STATUS_SUCCESS );
  1899. return;
  1900. }
  1901. /**
  1902. * Send 802.11 association frame
  1903. *
  1904. * @v dev 802.11 device
  1905. * @v wlan WLAN to associate with
  1906. * @ret rc Return status code
  1907. */
  1908. int net80211_send_assoc ( struct net80211_device *dev,
  1909. struct net80211_wlan *wlan )
  1910. {
  1911. struct io_buffer *iob = alloc_iob ( 128 );
  1912. struct ieee80211_assoc_req *assoc;
  1913. union ieee80211_ie *ie;
  1914. net80211_set_state ( dev, 0, NET80211_WAITING, 0 );
  1915. iob_reserve ( iob, IEEE80211_TYP_FRAME_HEADER_LEN );
  1916. assoc = iob->data;
  1917. assoc->capability = IEEE80211_CAPAB_MANAGED;
  1918. if ( ! ( dev->hw->flags & NET80211_HW_NO_SHORT_PREAMBLE ) )
  1919. assoc->capability |= IEEE80211_CAPAB_SHORT_PMBL;
  1920. if ( ! ( dev->hw->flags & NET80211_HW_NO_SHORT_SLOT ) )
  1921. assoc->capability |= IEEE80211_CAPAB_SHORT_SLOT;
  1922. if ( wlan->crypto )
  1923. assoc->capability |= IEEE80211_CAPAB_PRIVACY;
  1924. assoc->listen_interval = 1;
  1925. ie = net80211_marshal_request_info ( dev, assoc->info_element );
  1926. DBGP ( "802.11 %p about to send association request:\n", dev );
  1927. DBGP_HD ( iob->data, ( void * ) ie - iob->data );
  1928. iob_put ( iob, ( void * ) ie - iob->data );
  1929. return net80211_tx_mgmt ( dev, IEEE80211_STYPE_ASSOC_REQ,
  1930. wlan->bssid, iob );
  1931. }
  1932. /**
  1933. * Handle receipt of 802.11 association reply frame
  1934. *
  1935. * @v dev 802.11 device
  1936. * @v iob I/O buffer
  1937. */
  1938. static void net80211_handle_assoc_reply ( struct net80211_device *dev,
  1939. struct io_buffer *iob )
  1940. {
  1941. struct ieee80211_frame *hdr = iob->data;
  1942. struct ieee80211_assoc_resp *assoc =
  1943. ( struct ieee80211_assoc_resp * ) hdr->data;
  1944. net80211_process_capab ( dev, assoc->capability );
  1945. net80211_process_ie ( dev, assoc->info_element, iob->tail );
  1946. if ( assoc->status != IEEE80211_STATUS_SUCCESS ) {
  1947. DBGC ( dev, "802.11 %p association failed: status %d\n",
  1948. dev, assoc->status );
  1949. net80211_set_state ( dev, NET80211_WAITING, 0,
  1950. assoc->status );
  1951. return;
  1952. }
  1953. /* ESSID was filled before the association request was sent */
  1954. memcpy ( dev->bssid, hdr->addr3, ETH_ALEN );
  1955. dev->aid = assoc->aid;
  1956. net80211_set_state ( dev, NET80211_WAITING, NET80211_ASSOCIATED,
  1957. IEEE80211_STATUS_SUCCESS );
  1958. }
  1959. /**
  1960. * Send 802.11 disassociation frame
  1961. *
  1962. * @v dev 802.11 device
  1963. * @v reason Reason for disassociation
  1964. * @v deauth If TRUE, send deauthentication instead of disassociation
  1965. * @ret rc Return status code
  1966. */
  1967. static int net80211_send_disassoc ( struct net80211_device *dev, int reason,
  1968. int deauth )
  1969. {
  1970. struct io_buffer *iob = alloc_iob ( 64 );
  1971. struct ieee80211_disassoc *disassoc;
  1972. if ( ! ( dev->state & NET80211_ASSOCIATED ) )
  1973. return -EINVAL;
  1974. net80211_set_state ( dev, NET80211_ASSOCIATED, 0, 0 );
  1975. iob_reserve ( iob, IEEE80211_TYP_FRAME_HEADER_LEN );
  1976. disassoc = iob_put ( iob, sizeof ( *disassoc ) );
  1977. disassoc->reason = reason;
  1978. return net80211_tx_mgmt ( dev, deauth ? IEEE80211_STYPE_DEAUTH :
  1979. IEEE80211_STYPE_DISASSOC, dev->bssid, iob );
  1980. }
  1981. /**
  1982. * Deauthenticate from current network and try again
  1983. *
  1984. * @v dev 802.11 device
  1985. * @v rc Return status code indicating reason
  1986. *
  1987. * The deauthentication will be sent using an 802.11 "unspecified
  1988. * reason", as is common, but @a rc will be set as a link-up
  1989. * error to aid the user in debugging.
  1990. */
  1991. void net80211_deauthenticate ( struct net80211_device *dev, int rc )
  1992. {
  1993. net80211_send_disassoc ( dev, IEEE80211_REASON_UNSPECIFIED, 1 );
  1994. dev->assoc_rc = rc;
  1995. netdev_link_err ( dev->netdev, rc );
  1996. net80211_autoassociate ( dev );
  1997. }
  1998. /** Smoothing factor (1-7) for link quality calculation */
  1999. #define LQ_SMOOTH 7
  2000. /**
  2001. * Update link quality information based on received beacon
  2002. *
  2003. * @v dev 802.11 device
  2004. * @v iob I/O buffer containing beacon
  2005. * @ret rc Return status code
  2006. */
  2007. static void net80211_update_link_quality ( struct net80211_device *dev,
  2008. struct io_buffer *iob )
  2009. {
  2010. struct ieee80211_frame *hdr = iob->data;
  2011. struct ieee80211_beacon *beacon;
  2012. u32 dt, rxi;
  2013. if ( ! ( dev->state & NET80211_ASSOCIATED ) )
  2014. return;
  2015. beacon = ( struct ieee80211_beacon * ) hdr->data;
  2016. dt = ( u32 ) ( beacon->timestamp - dev->last_beacon_timestamp );
  2017. rxi = dev->rx_beacon_interval;
  2018. rxi = ( LQ_SMOOTH * rxi ) + ( ( 8 - LQ_SMOOTH ) * dt );
  2019. dev->rx_beacon_interval = rxi >> 3;
  2020. dev->last_beacon_timestamp = beacon->timestamp;
  2021. }
  2022. /**
  2023. * Handle receipt of 802.11 management frame
  2024. *
  2025. * @v dev 802.11 device
  2026. * @v iob I/O buffer
  2027. * @v signal Signal strength of received frame
  2028. */
  2029. static void net80211_handle_mgmt ( struct net80211_device *dev,
  2030. struct io_buffer *iob, int signal )
  2031. {
  2032. struct ieee80211_frame *hdr = iob->data;
  2033. struct ieee80211_disassoc *disassoc;
  2034. u16 stype = hdr->fc & IEEE80211_FC_SUBTYPE;
  2035. int keep = 0;
  2036. int is_deauth = ( stype == IEEE80211_STYPE_DEAUTH );
  2037. if ( ( hdr->fc & IEEE80211_FC_TYPE ) != IEEE80211_TYPE_MGMT ) {
  2038. free_iob ( iob );
  2039. return; /* only handle management frames */
  2040. }
  2041. switch ( stype ) {
  2042. /* We reconnect on deauthentication and disassociation. */
  2043. case IEEE80211_STYPE_DEAUTH:
  2044. case IEEE80211_STYPE_DISASSOC:
  2045. disassoc = ( struct ieee80211_disassoc * ) hdr->data;
  2046. net80211_set_state ( dev, is_deauth ? NET80211_AUTHENTICATED :
  2047. NET80211_ASSOCIATED, 0,
  2048. NET80211_IS_REASON | disassoc->reason );
  2049. DBGC ( dev, "802.11 %p %s: reason %d\n",
  2050. dev, is_deauth ? "deauthenticated" : "disassociated",
  2051. disassoc->reason );
  2052. /* Try to reassociate, in case it's transient. */
  2053. net80211_autoassociate ( dev );
  2054. break;
  2055. /* We handle authentication and association. */
  2056. case IEEE80211_STYPE_AUTH:
  2057. if ( ! ( dev->state & NET80211_AUTHENTICATED ) )
  2058. net80211_handle_auth ( dev, iob );
  2059. break;
  2060. case IEEE80211_STYPE_ASSOC_RESP:
  2061. case IEEE80211_STYPE_REASSOC_RESP:
  2062. if ( ! ( dev->state & NET80211_ASSOCIATED ) )
  2063. net80211_handle_assoc_reply ( dev, iob );
  2064. break;
  2065. /* We pass probes and beacons onto network scanning
  2066. code. Pass actions for future extensibility. */
  2067. case IEEE80211_STYPE_BEACON:
  2068. net80211_update_link_quality ( dev, iob );
  2069. /* fall through */
  2070. case IEEE80211_STYPE_PROBE_RESP:
  2071. case IEEE80211_STYPE_ACTION:
  2072. if ( dev->keep_mgmt ) {
  2073. struct net80211_rx_info *rxinf;
  2074. rxinf = zalloc ( sizeof ( *rxinf ) );
  2075. if ( ! rxinf ) {
  2076. DBGC ( dev, "802.11 %p out of memory\n", dev );
  2077. break;
  2078. }
  2079. rxinf->signal = signal;
  2080. list_add_tail ( &iob->list, &dev->mgmt_queue );
  2081. list_add_tail ( &rxinf->list, &dev->mgmt_info_queue );
  2082. keep = 1;
  2083. }
  2084. break;
  2085. case IEEE80211_STYPE_PROBE_REQ:
  2086. /* Some nodes send these broadcast. Ignore them. */
  2087. break;
  2088. case IEEE80211_STYPE_ASSOC_REQ:
  2089. case IEEE80211_STYPE_REASSOC_REQ:
  2090. /* We should never receive these, only send them. */
  2091. DBGC ( dev, "802.11 %p received strange management request "
  2092. "(%04x)\n", dev, stype );
  2093. break;
  2094. default:
  2095. DBGC ( dev, "802.11 %p received unimplemented management "
  2096. "packet (%04x)\n", dev, stype );
  2097. break;
  2098. }
  2099. if ( ! keep )
  2100. free_iob ( iob );
  2101. }
  2102. /* ---------- Packet handling functions ---------- */
  2103. /**
  2104. * Free buffers used by 802.11 fragment cache entry
  2105. *
  2106. * @v dev 802.11 device
  2107. * @v fcid Fragment cache entry index
  2108. *
  2109. * After this function, the referenced entry will be marked unused.
  2110. */
  2111. static void net80211_free_frags ( struct net80211_device *dev, int fcid )
  2112. {
  2113. int j;
  2114. struct net80211_frag_cache *frag = &dev->frags[fcid];
  2115. for ( j = 0; j < 16; j++ ) {
  2116. if ( frag->iob[j] ) {
  2117. free_iob ( frag->iob[j] );
  2118. frag->iob[j] = NULL;
  2119. }
  2120. }
  2121. frag->seqnr = 0;
  2122. frag->start_ticks = 0;
  2123. frag->in_use = 0;
  2124. }
  2125. /**
  2126. * Accumulate 802.11 fragments into one I/O buffer
  2127. *
  2128. * @v dev 802.11 device
  2129. * @v fcid Fragment cache entry index
  2130. * @v nfrags Number of fragments received
  2131. * @v size Sum of sizes of all fragments, including headers
  2132. * @ret iob I/O buffer containing reassembled packet
  2133. *
  2134. * This function does not free the fragment buffers.
  2135. */
  2136. static struct io_buffer *net80211_accum_frags ( struct net80211_device *dev,
  2137. int fcid, int nfrags, int size )
  2138. {
  2139. struct net80211_frag_cache *frag = &dev->frags[fcid];
  2140. int hdrsize = IEEE80211_TYP_FRAME_HEADER_LEN;
  2141. int nsize = size - hdrsize * ( nfrags - 1 );
  2142. int i;
  2143. struct io_buffer *niob = alloc_iob ( nsize );
  2144. struct ieee80211_frame *hdr;
  2145. /* Add the header from the first one... */
  2146. memcpy ( iob_put ( niob, hdrsize ), frag->iob[0]->data, hdrsize );
  2147. /* ... and all the data from all of them. */
  2148. for ( i = 0; i < nfrags; i++ ) {
  2149. int len = iob_len ( frag->iob[i] ) - hdrsize;
  2150. memcpy ( iob_put ( niob, len ),
  2151. frag->iob[i]->data + hdrsize, len );
  2152. }
  2153. /* Turn off the fragment bit. */
  2154. hdr = niob->data;
  2155. hdr->fc &= ~IEEE80211_FC_MORE_FRAG;
  2156. return niob;
  2157. }
  2158. /**
  2159. * Handle receipt of 802.11 fragment
  2160. *
  2161. * @v dev 802.11 device
  2162. * @v iob I/O buffer containing fragment
  2163. * @v signal Signal strength with which fragment was received
  2164. */
  2165. static void net80211_rx_frag ( struct net80211_device *dev,
  2166. struct io_buffer *iob, int signal )
  2167. {
  2168. struct ieee80211_frame *hdr = iob->data;
  2169. int fragnr = IEEE80211_FRAG ( hdr->seq );
  2170. if ( fragnr == 0 && ( hdr->fc & IEEE80211_FC_MORE_FRAG ) ) {
  2171. /* start a frag cache entry */
  2172. int i, newest = -1;
  2173. u32 curr_ticks = currticks(), newest_ticks = 0;
  2174. u32 timeout = ticks_per_sec() * NET80211_FRAG_TIMEOUT;
  2175. for ( i = 0; i < NET80211_NR_CONCURRENT_FRAGS; i++ ) {
  2176. if ( dev->frags[i].in_use == 0 )
  2177. break;
  2178. if ( dev->frags[i].start_ticks + timeout >=
  2179. curr_ticks ) {
  2180. net80211_free_frags ( dev, i );
  2181. break;
  2182. }
  2183. if ( dev->frags[i].start_ticks > newest_ticks ) {
  2184. newest = i;
  2185. newest_ticks = dev->frags[i].start_ticks;
  2186. }
  2187. }
  2188. /* If we're being sent more concurrent fragmented
  2189. packets than we can handle, drop the newest so the
  2190. older ones have time to complete. */
  2191. if ( i == NET80211_NR_CONCURRENT_FRAGS ) {
  2192. i = newest;
  2193. net80211_free_frags ( dev, i );
  2194. }
  2195. dev->frags[i].in_use = 1;
  2196. dev->frags[i].seqnr = IEEE80211_SEQNR ( hdr->seq );
  2197. dev->frags[i].start_ticks = currticks();
  2198. dev->frags[i].iob[0] = iob;
  2199. return;
  2200. } else {
  2201. int i;
  2202. for ( i = 0; i < NET80211_NR_CONCURRENT_FRAGS; i++ ) {
  2203. if ( dev->frags[i].in_use && dev->frags[i].seqnr ==
  2204. IEEE80211_SEQNR ( hdr->seq ) )
  2205. break;
  2206. }
  2207. if ( i == NET80211_NR_CONCURRENT_FRAGS ) {
  2208. /* Drop non-first not-in-cache fragments */
  2209. DBGC ( dev, "802.11 %p dropped fragment fc=%04x "
  2210. "seq=%04x\n", dev, hdr->fc, hdr->seq );
  2211. free_iob ( iob );
  2212. return;
  2213. }
  2214. dev->frags[i].iob[fragnr] = iob;
  2215. if ( ! ( hdr->fc & IEEE80211_FC_MORE_FRAG ) ) {
  2216. int j, size = 0;
  2217. for ( j = 0; j < fragnr; j++ ) {
  2218. size += iob_len ( dev->frags[i].iob[j] );
  2219. if ( dev->frags[i].iob[j] == NULL )
  2220. break;
  2221. }
  2222. if ( j == fragnr ) {
  2223. /* We've got everything */
  2224. struct io_buffer *niob =
  2225. net80211_accum_frags ( dev, i, fragnr,
  2226. size );
  2227. net80211_free_frags ( dev, i );
  2228. net80211_rx ( dev, niob, signal, 0 );
  2229. } else {
  2230. DBGC ( dev, "802.11 %p dropping fragmented "
  2231. "packet due to out-of-order arrival, "
  2232. "fc=%04x seq=%04x\n", dev, hdr->fc,
  2233. hdr->seq );
  2234. net80211_free_frags ( dev, i );
  2235. }
  2236. }
  2237. }
  2238. }
  2239. /**
  2240. * Handle receipt of 802.11 frame
  2241. *
  2242. * @v dev 802.11 device
  2243. * @v iob I/O buffer
  2244. * @v signal Received signal strength
  2245. * @v rate Bitrate at which frame was received, in 100 kbps units
  2246. *
  2247. * If the rate or signal is unknown, 0 should be passed.
  2248. */
  2249. void net80211_rx ( struct net80211_device *dev, struct io_buffer *iob,
  2250. int signal, u16 rate )
  2251. {
  2252. struct ieee80211_frame *hdr = iob->data;
  2253. u16 type = hdr->fc & IEEE80211_FC_TYPE;
  2254. if ( ( hdr->fc & IEEE80211_FC_VERSION ) != IEEE80211_THIS_VERSION )
  2255. goto drop; /* drop invalid-version packets */
  2256. if ( type == IEEE80211_TYPE_CTRL )
  2257. goto drop; /* we don't handle control packets,
  2258. the hardware does */
  2259. if ( dev->last_rx_seq == hdr->seq )
  2260. goto drop; /* avoid duplicate packet */
  2261. dev->last_rx_seq = hdr->seq;
  2262. if ( dev->hw->flags & NET80211_HW_RX_HAS_FCS ) {
  2263. /* discard the FCS */
  2264. iob_unput ( iob, 4 );
  2265. }
  2266. /* Only decrypt packets from our BSSID, to avoid spurious errors */
  2267. if ( ( hdr->fc & IEEE80211_FC_PROTECTED ) &&
  2268. ! memcmp ( hdr->addr2, dev->bssid, ETH_ALEN ) ) {
  2269. /* Decrypt packet; record and drop if it fails */
  2270. struct io_buffer *niob;
  2271. struct net80211_crypto *crypto = dev->crypto;
  2272. if ( ! dev->crypto ) {
  2273. DBGC ( dev, "802.11 %p cannot decrypt packet "
  2274. "without a cryptosystem\n", dev );
  2275. goto drop_crypt;
  2276. }
  2277. if ( ( hdr->addr1[0] & 1 ) && dev->gcrypto ) {
  2278. /* Use group decryption if needed */
  2279. crypto = dev->gcrypto;
  2280. }
  2281. niob = crypto->decrypt ( crypto, iob );
  2282. if ( ! niob ) {
  2283. DBGC ( dev, "802.11 %p decryption error\n", dev );
  2284. goto drop_crypt;
  2285. }
  2286. free_iob ( iob );
  2287. iob = niob;
  2288. }
  2289. dev->last_signal = signal;
  2290. /* Fragments go into the frag cache or get dropped. */
  2291. if ( IEEE80211_FRAG ( hdr->seq ) != 0
  2292. || ( hdr->fc & IEEE80211_FC_MORE_FRAG ) ) {
  2293. net80211_rx_frag ( dev, iob, signal );
  2294. return;
  2295. }
  2296. /* Management frames get handled, enqueued, or dropped. */
  2297. if ( type == IEEE80211_TYPE_MGMT ) {
  2298. net80211_handle_mgmt ( dev, iob, signal );
  2299. return;
  2300. }
  2301. /* Data frames get dropped or sent to the net_device. */
  2302. if ( ( hdr->fc & IEEE80211_FC_SUBTYPE ) != IEEE80211_STYPE_DATA )
  2303. goto drop; /* drop QoS, CFP, or null data packets */
  2304. /* Update rate-control algorithm */
  2305. if ( dev->rctl )
  2306. rc80211_update_rx ( dev, hdr->fc & IEEE80211_FC_RETRY, rate );
  2307. /* Pass packet onward */
  2308. if ( dev->state & NET80211_ASSOCIATED ) {
  2309. netdev_rx ( dev->netdev, iob );
  2310. return;
  2311. }
  2312. /* No association? Drop it. */
  2313. goto drop;
  2314. drop_crypt:
  2315. netdev_rx_err ( dev->netdev, NULL, EINVAL_CRYPTO_REQUEST );
  2316. drop:
  2317. DBGC2 ( dev, "802.11 %p dropped packet fc=%04x seq=%04x\n", dev,
  2318. hdr->fc, hdr->seq );
  2319. free_iob ( iob );
  2320. return;
  2321. }
  2322. /** Indicate an error in receiving a packet
  2323. *
  2324. * @v dev 802.11 device
  2325. * @v iob I/O buffer with received packet, or NULL
  2326. * @v rc Error code
  2327. *
  2328. * This logs the error with the wrapping net_device, and frees iob if
  2329. * it is passed.
  2330. */
  2331. void net80211_rx_err ( struct net80211_device *dev,
  2332. struct io_buffer *iob, int rc )
  2333. {
  2334. netdev_rx_err ( dev->netdev, iob, rc );
  2335. }
  2336. /** Indicate the completed transmission of a packet
  2337. *
  2338. * @v dev 802.11 device
  2339. * @v iob I/O buffer of transmitted packet
  2340. * @v retries Number of times this packet was retransmitted
  2341. * @v rc Error code, or 0 for success
  2342. *
  2343. * This logs an error with the wrapping net_device if one occurred,
  2344. * and removes and frees the I/O buffer from its TX queue. The
  2345. * provided retry information is used to tune our transmission rate.
  2346. *
  2347. * If the packet did not need to be retransmitted because it was
  2348. * properly ACKed the first time, @a retries should be 0.
  2349. */
  2350. void net80211_tx_complete ( struct net80211_device *dev,
  2351. struct io_buffer *iob, int retries, int rc )
  2352. {
  2353. /* Update rate-control algorithm */
  2354. if ( dev->rctl )
  2355. rc80211_update_tx ( dev, retries, rc );
  2356. /* Pass completion onward */
  2357. netdev_tx_complete_err ( dev->netdev, iob, rc );
  2358. }
  2359. /** Common 802.11 errors */
  2360. struct errortab common_wireless_errors[] __errortab = {
  2361. __einfo_errortab ( EINFO_EINVAL_CRYPTO_REQUEST ),
  2362. __einfo_errortab ( EINFO_ECONNRESET_UNSPECIFIED ),
  2363. __einfo_errortab ( EINFO_ECONNRESET_INACTIVITY ),
  2364. __einfo_errortab ( EINFO_ECONNRESET_4WAY_TIMEOUT ),
  2365. __einfo_errortab ( EINFO_ECONNRESET_8021X_FAILURE ),
  2366. __einfo_errortab ( EINFO_ECONNREFUSED_FAILURE ),
  2367. __einfo_errortab ( EINFO_ECONNREFUSED_ASSOC_DENIED ),
  2368. __einfo_errortab ( EINFO_ECONNREFUSED_AUTH_ALGO_UNSUPP ),
  2369. };