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

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