Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

net80211.c 76KB

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