您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

net80211.c 77KB

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