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 77KB

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