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

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