You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835
  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. net80211_keep_mgmt ( ctx->dev, ctx->old_keep_mgmt );
  1348. if ( ctx->probe )
  1349. free_iob ( ctx->probe );
  1350. free ( ctx );
  1351. return beacons;
  1352. }
  1353. /**
  1354. * Free WLAN structure
  1355. *
  1356. * @v wlan WLAN structure to free
  1357. */
  1358. void net80211_free_wlan ( struct net80211_wlan *wlan )
  1359. {
  1360. if ( wlan ) {
  1361. free_iob ( wlan->beacon );
  1362. free ( wlan );
  1363. }
  1364. }
  1365. /**
  1366. * Free list of WLAN structures
  1367. *
  1368. * @v list List of WLAN structures to free
  1369. */
  1370. void net80211_free_wlanlist ( struct list_head *list )
  1371. {
  1372. struct net80211_wlan *wlan, *tmp;
  1373. if ( ! list )
  1374. return;
  1375. list_for_each_entry_safe ( wlan, tmp, list, list ) {
  1376. list_del ( &wlan->list );
  1377. net80211_free_wlan ( wlan );
  1378. }
  1379. free ( list );
  1380. }
  1381. /** Number of ticks to wait for replies to association management frames */
  1382. #define ASSOC_TIMEOUT TICKS_PER_SEC
  1383. /** Number of times to try sending a particular association management frame */
  1384. #define ASSOC_RETRIES 2
  1385. /**
  1386. * Step 802.11 association process
  1387. *
  1388. * @v dev 802.11 device
  1389. */
  1390. static void net80211_step_associate ( struct net80211_device *dev )
  1391. {
  1392. int rc = 0;
  1393. int status = dev->state & NET80211_STATUS_MASK;
  1394. /*
  1395. * We use a sort of state machine implemented using bits in
  1396. * the dev->state variable. At each call, we take the
  1397. * logically first step that has not yet succeeded; either it
  1398. * has not been tried yet, it's being retried, or it failed.
  1399. * If it failed, we return an error indication; otherwise we
  1400. * perform the step. If it succeeds, RX handling code will set
  1401. * the appropriate status bit for us.
  1402. *
  1403. * Probe works a bit differently, since we have to step it
  1404. * on every call instead of waiting for a packet to arrive
  1405. * that will set the completion bit for us.
  1406. */
  1407. /* If we're waiting for a reply, check for timeout condition */
  1408. if ( dev->state & NET80211_WAITING ) {
  1409. /* Sanity check */
  1410. if ( ! dev->associating )
  1411. return;
  1412. if ( currticks() - dev->ctx.assoc->last_packet > ASSOC_TIMEOUT ) {
  1413. /* Timed out - fail if too many retries, or retry */
  1414. dev->ctx.assoc->times_tried++;
  1415. if ( ++dev->ctx.assoc->times_tried > ASSOC_RETRIES ) {
  1416. rc = -ETIMEDOUT;
  1417. goto fail;
  1418. }
  1419. } else {
  1420. /* Didn't time out - let it keep going */
  1421. return;
  1422. }
  1423. } else {
  1424. if ( dev->state & NET80211_PROBED )
  1425. dev->ctx.assoc->times_tried = 0;
  1426. }
  1427. if ( ! ( dev->state & NET80211_PROBED ) ) {
  1428. /* state: probe */
  1429. if ( ! dev->ctx.probe ) {
  1430. /* start probe */
  1431. int active = fetch_intz_setting ( NULL,
  1432. &net80211_active_setting );
  1433. int band = dev->hw->bands;
  1434. if ( active )
  1435. band &= ~NET80211_BAND_BIT_5GHZ;
  1436. rc = net80211_prepare_probe ( dev, band, active );
  1437. if ( rc )
  1438. goto fail;
  1439. dev->ctx.probe = net80211_probe_start ( dev, dev->essid,
  1440. active );
  1441. if ( ! dev->ctx.probe ) {
  1442. dev->assoc_rc = -ENOMEM;
  1443. goto fail;
  1444. }
  1445. }
  1446. rc = net80211_probe_step ( dev->ctx.probe );
  1447. if ( ! rc ) {
  1448. return; /* still going */
  1449. }
  1450. dev->associating = net80211_probe_finish_best ( dev->ctx.probe );
  1451. dev->ctx.probe = NULL;
  1452. if ( ! dev->associating ) {
  1453. if ( rc > 0 ) /* "successful" probe found nothing */
  1454. rc = -ETIMEDOUT;
  1455. goto fail;
  1456. }
  1457. /* If we probed using a broadcast SSID, record that
  1458. fact for the settings applicator before we clobber
  1459. it with the specific SSID we've chosen. */
  1460. if ( ! dev->essid[0] )
  1461. dev->state |= NET80211_AUTO_SSID;
  1462. DBGC ( dev, "802.11 %p found network %s (%s)\n", dev,
  1463. dev->associating->essid,
  1464. eth_ntoa ( dev->associating->bssid ) );
  1465. dev->ctx.assoc = zalloc ( sizeof ( *dev->ctx.assoc ) );
  1466. if ( ! dev->ctx.assoc ) {
  1467. rc = -ENOMEM;
  1468. goto fail;
  1469. }
  1470. dev->state |= NET80211_PROBED;
  1471. dev->ctx.assoc->method = IEEE80211_AUTH_OPEN_SYSTEM;
  1472. return;
  1473. }
  1474. /* Record time of sending the packet we're about to send, for timeout */
  1475. dev->ctx.assoc->last_packet = currticks();
  1476. if ( ! ( dev->state & NET80211_AUTHENTICATED ) ) {
  1477. /* state: prepare and authenticate */
  1478. if ( status != IEEE80211_STATUS_SUCCESS ) {
  1479. /* we tried authenticating already, but failed */
  1480. int method = dev->ctx.assoc->method;
  1481. if ( method == IEEE80211_AUTH_OPEN_SYSTEM &&
  1482. ( status == IEEE80211_STATUS_AUTH_CHALL_INVALID ||
  1483. status == IEEE80211_STATUS_AUTH_ALGO_UNSUPP ) ) {
  1484. /* Maybe this network uses Shared Key? */
  1485. dev->ctx.assoc->method =
  1486. IEEE80211_AUTH_SHARED_KEY;
  1487. } else {
  1488. goto fail;
  1489. }
  1490. }
  1491. DBGC ( dev, "802.11 %p authenticating with method %d\n", dev,
  1492. dev->ctx.assoc->method );
  1493. rc = net80211_prepare_assoc ( dev, dev->associating );
  1494. if ( rc )
  1495. goto fail;
  1496. rc = net80211_send_auth ( dev, dev->associating,
  1497. dev->ctx.assoc->method );
  1498. if ( rc )
  1499. goto fail;
  1500. return;
  1501. }
  1502. if ( ! ( dev->state & NET80211_ASSOCIATED ) ) {
  1503. /* state: associate */
  1504. if ( status != IEEE80211_STATUS_SUCCESS )
  1505. goto fail;
  1506. DBGC ( dev, "802.11 %p associating\n", dev );
  1507. if ( dev->handshaker && dev->handshaker->start &&
  1508. ! dev->handshaker->started ) {
  1509. rc = dev->handshaker->start ( dev );
  1510. if ( rc < 0 )
  1511. goto fail;
  1512. dev->handshaker->started = 1;
  1513. }
  1514. rc = net80211_send_assoc ( dev, dev->associating );
  1515. if ( rc )
  1516. goto fail;
  1517. return;
  1518. }
  1519. if ( ! ( dev->state & NET80211_CRYPTO_SYNCED ) ) {
  1520. /* state: crypto sync */
  1521. DBGC ( dev, "802.11 %p security handshaking\n", dev );
  1522. if ( ! dev->handshaker || ! dev->handshaker->step ) {
  1523. dev->state |= NET80211_CRYPTO_SYNCED;
  1524. return;
  1525. }
  1526. rc = dev->handshaker->step ( dev );
  1527. if ( rc < 0 ) {
  1528. /* Only record the returned error if we're
  1529. still marked as associated, because an
  1530. asynchronous error will have already been
  1531. reported to net80211_deauthenticate() and
  1532. assoc_rc thereby set. */
  1533. if ( dev->state & NET80211_ASSOCIATED )
  1534. dev->assoc_rc = rc;
  1535. rc = 0;
  1536. goto fail;
  1537. }
  1538. if ( rc > 0 ) {
  1539. dev->assoc_rc = 0;
  1540. dev->state |= NET80211_CRYPTO_SYNCED;
  1541. }
  1542. return;
  1543. }
  1544. /* state: done! */
  1545. netdev_link_up ( dev->netdev );
  1546. dev->assoc_rc = 0;
  1547. dev->state &= ~NET80211_WORKING;
  1548. free ( dev->ctx.assoc );
  1549. dev->ctx.assoc = NULL;
  1550. net80211_free_wlan ( dev->associating );
  1551. dev->associating = NULL;
  1552. dev->rctl = rc80211_init ( dev );
  1553. process_del ( &dev->proc_assoc );
  1554. DBGC ( dev, "802.11 %p associated with %s (%s)\n", dev,
  1555. dev->essid, eth_ntoa ( dev->bssid ) );
  1556. return;
  1557. fail:
  1558. dev->state &= ~( NET80211_WORKING | NET80211_WAITING );
  1559. if ( rc )
  1560. dev->assoc_rc = rc;
  1561. netdev_link_err ( dev->netdev, dev->assoc_rc );
  1562. /* We never reach here from the middle of a probe, so we don't
  1563. need to worry about freeing dev->ctx.probe. */
  1564. if ( dev->state & NET80211_PROBED ) {
  1565. free ( dev->ctx.assoc );
  1566. dev->ctx.assoc = NULL;
  1567. }
  1568. net80211_free_wlan ( dev->associating );
  1569. dev->associating = NULL;
  1570. process_del ( &dev->proc_assoc );
  1571. DBGC ( dev, "802.11 %p association failed (state=%04x): "
  1572. "%s\n", dev, dev->state, strerror ( dev->assoc_rc ) );
  1573. /* Try it again: */
  1574. net80211_autoassociate ( dev );
  1575. }
  1576. /**
  1577. * Check for 802.11 SSID or key updates
  1578. *
  1579. * This acts as a settings applicator; if the user changes netX/ssid,
  1580. * and netX is currently open, the association task will be invoked
  1581. * again. If the user changes the encryption key, the current security
  1582. * handshaker will be asked to update its state to match; if that is
  1583. * impossible without reassociation, we reassociate.
  1584. */
  1585. static int net80211_check_settings_update ( void )
  1586. {
  1587. struct net80211_device *dev;
  1588. char ssid[IEEE80211_MAX_SSID_LEN + 1];
  1589. int key_reassoc;
  1590. list_for_each_entry ( dev, &net80211_devices, list ) {
  1591. if ( ! netdev_is_open ( dev->netdev ) )
  1592. continue;
  1593. key_reassoc = 0;
  1594. if ( dev->handshaker && dev->handshaker->change_key &&
  1595. dev->handshaker->change_key ( dev ) < 0 )
  1596. key_reassoc = 1;
  1597. fetch_string_setting ( netdev_settings ( dev->netdev ),
  1598. &net80211_ssid_setting, ssid,
  1599. IEEE80211_MAX_SSID_LEN + 1 );
  1600. if ( key_reassoc ||
  1601. ( ! ( ! ssid[0] && ( dev->state & NET80211_AUTO_SSID ) ) &&
  1602. strcmp ( ssid, dev->essid ) != 0 ) ) {
  1603. DBGC ( dev, "802.11 %p updating association: "
  1604. "%s -> %s\n", dev, dev->essid, ssid );
  1605. net80211_autoassociate ( dev );
  1606. }
  1607. }
  1608. return 0;
  1609. }
  1610. /**
  1611. * Start 802.11 association process
  1612. *
  1613. * @v dev 802.11 device
  1614. *
  1615. * If the association process is running, it will be restarted.
  1616. */
  1617. void net80211_autoassociate ( struct net80211_device *dev )
  1618. {
  1619. if ( ! ( dev->state & NET80211_WORKING ) ) {
  1620. DBGC2 ( dev, "802.11 %p spawning association process\n", dev );
  1621. process_add ( &dev->proc_assoc );
  1622. } else {
  1623. DBGC2 ( dev, "802.11 %p restarting association\n", dev );
  1624. }
  1625. /* Clean up everything an earlier association process might
  1626. have been in the middle of using */
  1627. if ( dev->associating )
  1628. net80211_free_wlan ( dev->associating );
  1629. if ( ! ( dev->state & NET80211_PROBED ) )
  1630. net80211_free_wlan (
  1631. net80211_probe_finish_best ( dev->ctx.probe ) );
  1632. else
  1633. free ( dev->ctx.assoc );
  1634. /* Reset to a clean state */
  1635. fetch_string_setting ( netdev_settings ( dev->netdev ),
  1636. &net80211_ssid_setting, dev->essid,
  1637. IEEE80211_MAX_SSID_LEN + 1 );
  1638. dev->ctx.probe = NULL;
  1639. dev->associating = NULL;
  1640. dev->assoc_rc = 0;
  1641. net80211_set_state ( dev, NET80211_PROBED, NET80211_WORKING, 0 );
  1642. }
  1643. /**
  1644. * Pick TX rate for RTS/CTS packets based on data rate
  1645. *
  1646. * @v dev 802.11 device
  1647. *
  1648. * The RTS/CTS rate is the fastest TX rate marked as "basic" that is
  1649. * not faster than the data rate.
  1650. */
  1651. static void net80211_set_rtscts_rate ( struct net80211_device *dev )
  1652. {
  1653. u16 datarate = dev->rates[dev->rate];
  1654. u16 rtsrate = 0;
  1655. int rts_idx = -1;
  1656. int i;
  1657. for ( i = 0; i < dev->nr_rates; i++ ) {
  1658. u16 rate = dev->rates[i];
  1659. if ( ! ( dev->basic_rates & ( 1 << i ) ) || rate > datarate )
  1660. continue;
  1661. if ( rate > rtsrate ) {
  1662. rtsrate = rate;
  1663. rts_idx = i;
  1664. }
  1665. }
  1666. /* If this is in initialization, we might not have any basic
  1667. rates; just use the first data rate in that case. */
  1668. if ( rts_idx < 0 )
  1669. rts_idx = 0;
  1670. dev->rtscts_rate = rts_idx;
  1671. }
  1672. /**
  1673. * Set data transmission rate for 802.11 device
  1674. *
  1675. * @v dev 802.11 device
  1676. * @v rate Rate to set, as index into @c dev->rates array
  1677. */
  1678. void net80211_set_rate_idx ( struct net80211_device *dev, int rate )
  1679. {
  1680. assert ( netdev_is_open ( dev->netdev ) );
  1681. if ( rate >= 0 && rate < dev->nr_rates && rate != dev->rate ) {
  1682. DBGC2 ( dev, "802.11 %p changing rate from %d->%d Mbps\n",
  1683. dev, dev->rates[dev->rate] / 10,
  1684. dev->rates[rate] / 10 );
  1685. dev->rate = rate;
  1686. net80211_set_rtscts_rate ( dev );
  1687. dev->op->config ( dev, NET80211_CFG_RATE );
  1688. }
  1689. }
  1690. /**
  1691. * Configure 802.11 device to transmit on a certain channel
  1692. *
  1693. * @v dev 802.11 device
  1694. * @v channel Channel number (1-11 for 2.4GHz) to transmit on
  1695. */
  1696. int net80211_change_channel ( struct net80211_device *dev, int channel )
  1697. {
  1698. int i, oldchan = dev->channel;
  1699. assert ( netdev_is_open ( dev->netdev ) );
  1700. for ( i = 0; i < dev->nr_channels; i++ ) {
  1701. if ( dev->channels[i].channel_nr == channel ) {
  1702. dev->channel = i;
  1703. break;
  1704. }
  1705. }
  1706. if ( i == dev->nr_channels )
  1707. return -ENOENT;
  1708. if ( i != oldchan )
  1709. return dev->op->config ( dev, NET80211_CFG_CHANNEL );
  1710. return 0;
  1711. }
  1712. /**
  1713. * Prepare 802.11 device channel and rate set for scanning
  1714. *
  1715. * @v dev 802.11 device
  1716. * @v band RF band(s) on which to prepare for scanning
  1717. * @v active Whether the scanning will be active
  1718. * @ret rc Return status code
  1719. */
  1720. int net80211_prepare_probe ( struct net80211_device *dev, int band,
  1721. int active )
  1722. {
  1723. assert ( netdev_is_open ( dev->netdev ) );
  1724. if ( active && ( band & NET80211_BAND_BIT_5GHZ ) ) {
  1725. DBGC ( dev, "802.11 %p cannot perform active scanning on "
  1726. "5GHz band\n", dev );
  1727. return -EINVAL_ACTIVE_SCAN;
  1728. }
  1729. if ( band == 0 ) {
  1730. /* This can happen for a 5GHz-only card with 5GHz
  1731. scanning masked out by an active request. */
  1732. DBGC ( dev, "802.11 %p asked to prepare for scanning nothing\n",
  1733. dev );
  1734. return -EINVAL_ACTIVE_SCAN;
  1735. }
  1736. dev->nr_channels = 0;
  1737. if ( active )
  1738. net80211_add_channels ( dev, 1, 11, NET80211_REG_TXPOWER );
  1739. else {
  1740. if ( band & NET80211_BAND_BIT_2GHZ )
  1741. net80211_add_channels ( dev, 1, 14,
  1742. NET80211_REG_TXPOWER );
  1743. if ( band & NET80211_BAND_BIT_5GHZ )
  1744. net80211_add_channels ( dev, 36, 8,
  1745. NET80211_REG_TXPOWER );
  1746. }
  1747. net80211_filter_hw_channels ( dev );
  1748. /* Use channel 1 for now */
  1749. dev->channel = 0;
  1750. dev->op->config ( dev, NET80211_CFG_CHANNEL );
  1751. /* Always do active probes at lowest (presumably first) speed */
  1752. dev->rate = 0;
  1753. dev->nr_rates = 1;
  1754. dev->rates[0] = dev->hw->rates[dev->channels[0].band][0];
  1755. dev->op->config ( dev, NET80211_CFG_RATE );
  1756. return 0;
  1757. }
  1758. /**
  1759. * Prepare 802.11 device channel and rate set for communication
  1760. *
  1761. * @v dev 802.11 device
  1762. * @v wlan WLAN to prepare for communication with
  1763. * @ret rc Return status code
  1764. */
  1765. int net80211_prepare_assoc ( struct net80211_device *dev,
  1766. struct net80211_wlan *wlan )
  1767. {
  1768. struct ieee80211_frame *hdr = wlan->beacon->data;
  1769. struct ieee80211_beacon *beacon =
  1770. ( struct ieee80211_beacon * ) hdr->data;
  1771. struct net80211_handshaker *handshaker;
  1772. int rc;
  1773. assert ( netdev_is_open ( dev->netdev ) );
  1774. net80211_set_state ( dev, NET80211_ASSOCIATED, 0, 0 );
  1775. memcpy ( dev->bssid, wlan->bssid, ETH_ALEN );
  1776. strcpy ( dev->essid, wlan->essid );
  1777. free ( dev->rsn_ie );
  1778. dev->rsn_ie = NULL;
  1779. dev->last_beacon_timestamp = beacon->timestamp;
  1780. dev->tx_beacon_interval = 1024 * beacon->beacon_interval;
  1781. /* Barring an IE that tells us the channel outright, assume
  1782. the channel we heard this AP best on is the channel it's
  1783. communicating on. */
  1784. net80211_change_channel ( dev, wlan->channel );
  1785. rc = net80211_process_capab ( dev, beacon->capability );
  1786. if ( rc )
  1787. return rc;
  1788. rc = net80211_process_ie ( dev, beacon->info_element,
  1789. wlan->beacon->tail );
  1790. if ( rc )
  1791. return rc;
  1792. /* Associate at the lowest rate so we know it'll get through */
  1793. dev->rate = 0;
  1794. dev->op->config ( dev, NET80211_CFG_RATE );
  1795. /* Free old handshaker and crypto, if they exist */
  1796. if ( dev->handshaker && dev->handshaker->stop &&
  1797. dev->handshaker->started )
  1798. dev->handshaker->stop ( dev );
  1799. free ( dev->handshaker );
  1800. dev->handshaker = NULL;
  1801. free ( dev->crypto );
  1802. free ( dev->gcrypto );
  1803. dev->crypto = dev->gcrypto = NULL;
  1804. /* Find new security handshaker to use */
  1805. for_each_table_entry ( handshaker, NET80211_HANDSHAKERS ) {
  1806. if ( handshaker->protocol == wlan->handshaking ) {
  1807. dev->handshaker = zalloc ( sizeof ( *handshaker ) +
  1808. handshaker->priv_len );
  1809. if ( ! dev->handshaker )
  1810. return -ENOMEM;
  1811. memcpy ( dev->handshaker, handshaker,
  1812. sizeof ( *handshaker ) );
  1813. dev->handshaker->priv = ( ( void * ) dev->handshaker +
  1814. sizeof ( *handshaker ) );
  1815. break;
  1816. }
  1817. }
  1818. if ( ( wlan->handshaking != NET80211_SECPROT_NONE ) &&
  1819. ! dev->handshaker ) {
  1820. DBGC ( dev, "802.11 %p no support for handshaking scheme %d\n",
  1821. dev, wlan->handshaking );
  1822. return -( ENOTSUP | ( wlan->handshaking << 8 ) );
  1823. }
  1824. /* Initialize security handshaker */
  1825. if ( dev->handshaker ) {
  1826. rc = dev->handshaker->init ( dev );
  1827. if ( rc < 0 )
  1828. return rc;
  1829. }
  1830. return 0;
  1831. }
  1832. /**
  1833. * Send 802.11 initial authentication frame
  1834. *
  1835. * @v dev 802.11 device
  1836. * @v wlan WLAN to authenticate with
  1837. * @v method Authentication method
  1838. * @ret rc Return status code
  1839. *
  1840. * @a method may be 0 for Open System authentication or 1 for Shared
  1841. * Key authentication. Open System provides no security in association
  1842. * whatsoever, relying on encryption for confidentiality, but Shared
  1843. * Key actively introduces security problems and is very rarely used.
  1844. */
  1845. int net80211_send_auth ( struct net80211_device *dev,
  1846. struct net80211_wlan *wlan, int method )
  1847. {
  1848. struct io_buffer *iob = alloc_iob ( 64 );
  1849. struct ieee80211_auth *auth;
  1850. net80211_set_state ( dev, 0, NET80211_WAITING, 0 );
  1851. iob_reserve ( iob, IEEE80211_TYP_FRAME_HEADER_LEN );
  1852. auth = iob_put ( iob, sizeof ( *auth ) );
  1853. auth->algorithm = method;
  1854. auth->tx_seq = 1;
  1855. auth->status = 0;
  1856. return net80211_tx_mgmt ( dev, IEEE80211_STYPE_AUTH, wlan->bssid, iob );
  1857. }
  1858. /**
  1859. * Handle receipt of 802.11 authentication frame
  1860. *
  1861. * @v dev 802.11 device
  1862. * @v iob I/O buffer
  1863. *
  1864. * If the authentication method being used is Shared Key, and the
  1865. * frame that was received included challenge text, the frame is
  1866. * encrypted using the cryptosystem currently in effect and sent back
  1867. * to the AP to complete the authentication.
  1868. */
  1869. static void net80211_handle_auth ( struct net80211_device *dev,
  1870. struct io_buffer *iob )
  1871. {
  1872. struct ieee80211_frame *hdr = iob->data;
  1873. struct ieee80211_auth *auth =
  1874. ( struct ieee80211_auth * ) hdr->data;
  1875. if ( auth->tx_seq & 1 ) {
  1876. DBGC ( dev, "802.11 %p authentication received improperly "
  1877. "directed frame (seq. %d)\n", dev, auth->tx_seq );
  1878. net80211_set_state ( dev, NET80211_WAITING, 0,
  1879. IEEE80211_STATUS_FAILURE );
  1880. return;
  1881. }
  1882. if ( auth->status != IEEE80211_STATUS_SUCCESS ) {
  1883. DBGC ( dev, "802.11 %p authentication failed: status %d\n",
  1884. dev, auth->status );
  1885. net80211_set_state ( dev, NET80211_WAITING, 0,
  1886. auth->status );
  1887. return;
  1888. }
  1889. if ( auth->algorithm == IEEE80211_AUTH_SHARED_KEY && ! dev->crypto ) {
  1890. DBGC ( dev, "802.11 %p can't perform shared-key authentication "
  1891. "without a cryptosystem\n", dev );
  1892. net80211_set_state ( dev, NET80211_WAITING, 0,
  1893. IEEE80211_STATUS_FAILURE );
  1894. return;
  1895. }
  1896. if ( auth->algorithm == IEEE80211_AUTH_SHARED_KEY &&
  1897. auth->tx_seq == 2 ) {
  1898. /* Since the iob we got is going to be freed as soon
  1899. as we return, we can do some in-place
  1900. modification. */
  1901. auth->tx_seq = 3;
  1902. auth->status = 0;
  1903. memcpy ( hdr->addr2, hdr->addr1, ETH_ALEN );
  1904. memcpy ( hdr->addr1, hdr->addr3, ETH_ALEN );
  1905. netdev_tx ( dev->netdev,
  1906. dev->crypto->encrypt ( dev->crypto, iob ) );
  1907. return;
  1908. }
  1909. net80211_set_state ( dev, NET80211_WAITING, NET80211_AUTHENTICATED,
  1910. IEEE80211_STATUS_SUCCESS );
  1911. return;
  1912. }
  1913. /**
  1914. * Send 802.11 association frame
  1915. *
  1916. * @v dev 802.11 device
  1917. * @v wlan WLAN to associate with
  1918. * @ret rc Return status code
  1919. */
  1920. int net80211_send_assoc ( struct net80211_device *dev,
  1921. struct net80211_wlan *wlan )
  1922. {
  1923. struct io_buffer *iob = alloc_iob ( 128 );
  1924. struct ieee80211_assoc_req *assoc;
  1925. union ieee80211_ie *ie;
  1926. net80211_set_state ( dev, 0, NET80211_WAITING, 0 );
  1927. iob_reserve ( iob, IEEE80211_TYP_FRAME_HEADER_LEN );
  1928. assoc = iob->data;
  1929. assoc->capability = IEEE80211_CAPAB_MANAGED;
  1930. if ( ! ( dev->hw->flags & NET80211_HW_NO_SHORT_PREAMBLE ) )
  1931. assoc->capability |= IEEE80211_CAPAB_SHORT_PMBL;
  1932. if ( ! ( dev->hw->flags & NET80211_HW_NO_SHORT_SLOT ) )
  1933. assoc->capability |= IEEE80211_CAPAB_SHORT_SLOT;
  1934. if ( wlan->crypto )
  1935. assoc->capability |= IEEE80211_CAPAB_PRIVACY;
  1936. assoc->listen_interval = 1;
  1937. ie = net80211_marshal_request_info ( dev, assoc->info_element );
  1938. DBGP ( "802.11 %p about to send association request:\n", dev );
  1939. DBGP_HD ( iob->data, ( void * ) ie - iob->data );
  1940. iob_put ( iob, ( void * ) ie - iob->data );
  1941. return net80211_tx_mgmt ( dev, IEEE80211_STYPE_ASSOC_REQ,
  1942. wlan->bssid, iob );
  1943. }
  1944. /**
  1945. * Handle receipt of 802.11 association reply frame
  1946. *
  1947. * @v dev 802.11 device
  1948. * @v iob I/O buffer
  1949. */
  1950. static void net80211_handle_assoc_reply ( struct net80211_device *dev,
  1951. struct io_buffer *iob )
  1952. {
  1953. struct ieee80211_frame *hdr = iob->data;
  1954. struct ieee80211_assoc_resp *assoc =
  1955. ( struct ieee80211_assoc_resp * ) hdr->data;
  1956. net80211_process_capab ( dev, assoc->capability );
  1957. net80211_process_ie ( dev, assoc->info_element, iob->tail );
  1958. if ( assoc->status != IEEE80211_STATUS_SUCCESS ) {
  1959. DBGC ( dev, "802.11 %p association failed: status %d\n",
  1960. dev, assoc->status );
  1961. net80211_set_state ( dev, NET80211_WAITING, 0,
  1962. assoc->status );
  1963. return;
  1964. }
  1965. /* ESSID was filled before the association request was sent */
  1966. memcpy ( dev->bssid, hdr->addr3, ETH_ALEN );
  1967. dev->aid = assoc->aid;
  1968. net80211_set_state ( dev, NET80211_WAITING, NET80211_ASSOCIATED,
  1969. IEEE80211_STATUS_SUCCESS );
  1970. }
  1971. /**
  1972. * Send 802.11 disassociation frame
  1973. *
  1974. * @v dev 802.11 device
  1975. * @v reason Reason for disassociation
  1976. * @v deauth If TRUE, send deauthentication instead of disassociation
  1977. * @ret rc Return status code
  1978. */
  1979. static int net80211_send_disassoc ( struct net80211_device *dev, int reason,
  1980. int deauth )
  1981. {
  1982. struct io_buffer *iob = alloc_iob ( 64 );
  1983. struct ieee80211_disassoc *disassoc;
  1984. if ( ! ( dev->state & NET80211_ASSOCIATED ) )
  1985. return -EINVAL;
  1986. net80211_set_state ( dev, NET80211_ASSOCIATED, 0, 0 );
  1987. iob_reserve ( iob, IEEE80211_TYP_FRAME_HEADER_LEN );
  1988. disassoc = iob_put ( iob, sizeof ( *disassoc ) );
  1989. disassoc->reason = reason;
  1990. return net80211_tx_mgmt ( dev, deauth ? IEEE80211_STYPE_DEAUTH :
  1991. IEEE80211_STYPE_DISASSOC, dev->bssid, iob );
  1992. }
  1993. /**
  1994. * Deauthenticate from current network and try again
  1995. *
  1996. * @v dev 802.11 device
  1997. * @v rc Return status code indicating reason
  1998. *
  1999. * The deauthentication will be sent using an 802.11 "unspecified
  2000. * reason", as is common, but @a rc will be set as a link-up
  2001. * error to aid the user in debugging.
  2002. */
  2003. void net80211_deauthenticate ( struct net80211_device *dev, int rc )
  2004. {
  2005. net80211_send_disassoc ( dev, IEEE80211_REASON_UNSPECIFIED, 1 );
  2006. dev->assoc_rc = rc;
  2007. netdev_link_err ( dev->netdev, rc );
  2008. net80211_autoassociate ( dev );
  2009. }
  2010. /** Smoothing factor (1-7) for link quality calculation */
  2011. #define LQ_SMOOTH 7
  2012. /**
  2013. * Update link quality information based on received beacon
  2014. *
  2015. * @v dev 802.11 device
  2016. * @v iob I/O buffer containing beacon
  2017. * @ret rc Return status code
  2018. */
  2019. static void net80211_update_link_quality ( struct net80211_device *dev,
  2020. struct io_buffer *iob )
  2021. {
  2022. struct ieee80211_frame *hdr = iob->data;
  2023. struct ieee80211_beacon *beacon;
  2024. u32 dt, rxi;
  2025. if ( ! ( dev->state & NET80211_ASSOCIATED ) )
  2026. return;
  2027. beacon = ( struct ieee80211_beacon * ) hdr->data;
  2028. dt = ( u32 ) ( beacon->timestamp - dev->last_beacon_timestamp );
  2029. rxi = dev->rx_beacon_interval;
  2030. rxi = ( LQ_SMOOTH * rxi ) + ( ( 8 - LQ_SMOOTH ) * dt );
  2031. dev->rx_beacon_interval = rxi >> 3;
  2032. dev->last_beacon_timestamp = beacon->timestamp;
  2033. }
  2034. /**
  2035. * Handle receipt of 802.11 management frame
  2036. *
  2037. * @v dev 802.11 device
  2038. * @v iob I/O buffer
  2039. * @v signal Signal strength of received frame
  2040. */
  2041. static void net80211_handle_mgmt ( struct net80211_device *dev,
  2042. struct io_buffer *iob, int signal )
  2043. {
  2044. struct ieee80211_frame *hdr = iob->data;
  2045. struct ieee80211_disassoc *disassoc;
  2046. u16 stype = hdr->fc & IEEE80211_FC_SUBTYPE;
  2047. int keep = 0;
  2048. int is_deauth = ( stype == IEEE80211_STYPE_DEAUTH );
  2049. if ( ( hdr->fc & IEEE80211_FC_TYPE ) != IEEE80211_TYPE_MGMT ) {
  2050. free_iob ( iob );
  2051. return; /* only handle management frames */
  2052. }
  2053. switch ( stype ) {
  2054. /* We reconnect on deauthentication and disassociation. */
  2055. case IEEE80211_STYPE_DEAUTH:
  2056. case IEEE80211_STYPE_DISASSOC:
  2057. disassoc = ( struct ieee80211_disassoc * ) hdr->data;
  2058. net80211_set_state ( dev, is_deauth ? NET80211_AUTHENTICATED :
  2059. NET80211_ASSOCIATED, 0,
  2060. NET80211_IS_REASON | disassoc->reason );
  2061. DBGC ( dev, "802.11 %p %s: reason %d\n",
  2062. dev, is_deauth ? "deauthenticated" : "disassociated",
  2063. disassoc->reason );
  2064. /* Try to reassociate, in case it's transient. */
  2065. net80211_autoassociate ( dev );
  2066. break;
  2067. /* We handle authentication and association. */
  2068. case IEEE80211_STYPE_AUTH:
  2069. if ( ! ( dev->state & NET80211_AUTHENTICATED ) )
  2070. net80211_handle_auth ( dev, iob );
  2071. break;
  2072. case IEEE80211_STYPE_ASSOC_RESP:
  2073. case IEEE80211_STYPE_REASSOC_RESP:
  2074. if ( ! ( dev->state & NET80211_ASSOCIATED ) )
  2075. net80211_handle_assoc_reply ( dev, iob );
  2076. break;
  2077. /* We pass probes and beacons onto network scanning
  2078. code. Pass actions for future extensibility. */
  2079. case IEEE80211_STYPE_BEACON:
  2080. net80211_update_link_quality ( dev, iob );
  2081. /* fall through */
  2082. case IEEE80211_STYPE_PROBE_RESP:
  2083. case IEEE80211_STYPE_ACTION:
  2084. if ( dev->keep_mgmt ) {
  2085. struct net80211_rx_info *rxinf;
  2086. rxinf = zalloc ( sizeof ( *rxinf ) );
  2087. if ( ! rxinf ) {
  2088. DBGC ( dev, "802.11 %p out of memory\n", dev );
  2089. break;
  2090. }
  2091. rxinf->signal = signal;
  2092. list_add_tail ( &iob->list, &dev->mgmt_queue );
  2093. list_add_tail ( &rxinf->list, &dev->mgmt_info_queue );
  2094. keep = 1;
  2095. }
  2096. break;
  2097. case IEEE80211_STYPE_PROBE_REQ:
  2098. /* Some nodes send these broadcast. Ignore them. */
  2099. break;
  2100. case IEEE80211_STYPE_ASSOC_REQ:
  2101. case IEEE80211_STYPE_REASSOC_REQ:
  2102. /* We should never receive these, only send them. */
  2103. DBGC ( dev, "802.11 %p received strange management request "
  2104. "(%04x)\n", dev, stype );
  2105. break;
  2106. default:
  2107. DBGC ( dev, "802.11 %p received unimplemented management "
  2108. "packet (%04x)\n", dev, stype );
  2109. break;
  2110. }
  2111. if ( ! keep )
  2112. free_iob ( iob );
  2113. }
  2114. /* ---------- Packet handling functions ---------- */
  2115. /**
  2116. * Free buffers used by 802.11 fragment cache entry
  2117. *
  2118. * @v dev 802.11 device
  2119. * @v fcid Fragment cache entry index
  2120. *
  2121. * After this function, the referenced entry will be marked unused.
  2122. */
  2123. static void net80211_free_frags ( struct net80211_device *dev, int fcid )
  2124. {
  2125. int j;
  2126. struct net80211_frag_cache *frag = &dev->frags[fcid];
  2127. for ( j = 0; j < 16; j++ ) {
  2128. if ( frag->iob[j] ) {
  2129. free_iob ( frag->iob[j] );
  2130. frag->iob[j] = NULL;
  2131. }
  2132. }
  2133. frag->seqnr = 0;
  2134. frag->start_ticks = 0;
  2135. frag->in_use = 0;
  2136. }
  2137. /**
  2138. * Accumulate 802.11 fragments into one I/O buffer
  2139. *
  2140. * @v dev 802.11 device
  2141. * @v fcid Fragment cache entry index
  2142. * @v nfrags Number of fragments received
  2143. * @v size Sum of sizes of all fragments, including headers
  2144. * @ret iob I/O buffer containing reassembled packet
  2145. *
  2146. * This function does not free the fragment buffers.
  2147. */
  2148. static struct io_buffer *net80211_accum_frags ( struct net80211_device *dev,
  2149. int fcid, int nfrags, int size )
  2150. {
  2151. struct net80211_frag_cache *frag = &dev->frags[fcid];
  2152. int hdrsize = IEEE80211_TYP_FRAME_HEADER_LEN;
  2153. int nsize = size - hdrsize * ( nfrags - 1 );
  2154. int i;
  2155. struct io_buffer *niob = alloc_iob ( nsize );
  2156. struct ieee80211_frame *hdr;
  2157. /* Add the header from the first one... */
  2158. memcpy ( iob_put ( niob, hdrsize ), frag->iob[0]->data, hdrsize );
  2159. /* ... and all the data from all of them. */
  2160. for ( i = 0; i < nfrags; i++ ) {
  2161. int len = iob_len ( frag->iob[i] ) - hdrsize;
  2162. memcpy ( iob_put ( niob, len ),
  2163. frag->iob[i]->data + hdrsize, len );
  2164. }
  2165. /* Turn off the fragment bit. */
  2166. hdr = niob->data;
  2167. hdr->fc &= ~IEEE80211_FC_MORE_FRAG;
  2168. return niob;
  2169. }
  2170. /**
  2171. * Handle receipt of 802.11 fragment
  2172. *
  2173. * @v dev 802.11 device
  2174. * @v iob I/O buffer containing fragment
  2175. * @v signal Signal strength with which fragment was received
  2176. */
  2177. static void net80211_rx_frag ( struct net80211_device *dev,
  2178. struct io_buffer *iob, int signal )
  2179. {
  2180. struct ieee80211_frame *hdr = iob->data;
  2181. int fragnr = IEEE80211_FRAG ( hdr->seq );
  2182. if ( fragnr == 0 && ( hdr->fc & IEEE80211_FC_MORE_FRAG ) ) {
  2183. /* start a frag cache entry */
  2184. int i, newest = -1;
  2185. u32 curr_ticks = currticks(), newest_ticks = 0;
  2186. u32 timeout = TICKS_PER_SEC * NET80211_FRAG_TIMEOUT;
  2187. for ( i = 0; i < NET80211_NR_CONCURRENT_FRAGS; i++ ) {
  2188. if ( dev->frags[i].in_use == 0 )
  2189. break;
  2190. if ( dev->frags[i].start_ticks + timeout >=
  2191. curr_ticks ) {
  2192. net80211_free_frags ( dev, i );
  2193. break;
  2194. }
  2195. if ( dev->frags[i].start_ticks > newest_ticks ) {
  2196. newest = i;
  2197. newest_ticks = dev->frags[i].start_ticks;
  2198. }
  2199. }
  2200. /* If we're being sent more concurrent fragmented
  2201. packets than we can handle, drop the newest so the
  2202. older ones have time to complete. */
  2203. if ( i == NET80211_NR_CONCURRENT_FRAGS ) {
  2204. i = newest;
  2205. net80211_free_frags ( dev, i );
  2206. }
  2207. dev->frags[i].in_use = 1;
  2208. dev->frags[i].seqnr = IEEE80211_SEQNR ( hdr->seq );
  2209. dev->frags[i].start_ticks = currticks();
  2210. dev->frags[i].iob[0] = iob;
  2211. return;
  2212. } else {
  2213. int i;
  2214. for ( i = 0; i < NET80211_NR_CONCURRENT_FRAGS; i++ ) {
  2215. if ( dev->frags[i].in_use && dev->frags[i].seqnr ==
  2216. IEEE80211_SEQNR ( hdr->seq ) )
  2217. break;
  2218. }
  2219. if ( i == NET80211_NR_CONCURRENT_FRAGS ) {
  2220. /* Drop non-first not-in-cache fragments */
  2221. DBGC ( dev, "802.11 %p dropped fragment fc=%04x "
  2222. "seq=%04x\n", dev, hdr->fc, hdr->seq );
  2223. free_iob ( iob );
  2224. return;
  2225. }
  2226. dev->frags[i].iob[fragnr] = iob;
  2227. if ( ! ( hdr->fc & IEEE80211_FC_MORE_FRAG ) ) {
  2228. int j, size = 0;
  2229. for ( j = 0; j < fragnr; j++ ) {
  2230. size += iob_len ( dev->frags[i].iob[j] );
  2231. if ( dev->frags[i].iob[j] == NULL )
  2232. break;
  2233. }
  2234. if ( j == fragnr ) {
  2235. /* We've got everything */
  2236. struct io_buffer *niob =
  2237. net80211_accum_frags ( dev, i, fragnr,
  2238. size );
  2239. net80211_free_frags ( dev, i );
  2240. net80211_rx ( dev, niob, signal, 0 );
  2241. } else {
  2242. DBGC ( dev, "802.11 %p dropping fragmented "
  2243. "packet due to out-of-order arrival, "
  2244. "fc=%04x seq=%04x\n", dev, hdr->fc,
  2245. hdr->seq );
  2246. net80211_free_frags ( dev, i );
  2247. }
  2248. }
  2249. }
  2250. }
  2251. /**
  2252. * Handle receipt of 802.11 frame
  2253. *
  2254. * @v dev 802.11 device
  2255. * @v iob I/O buffer
  2256. * @v signal Received signal strength
  2257. * @v rate Bitrate at which frame was received, in 100 kbps units
  2258. *
  2259. * If the rate or signal is unknown, 0 should be passed.
  2260. */
  2261. void net80211_rx ( struct net80211_device *dev, struct io_buffer *iob,
  2262. int signal, u16 rate )
  2263. {
  2264. struct ieee80211_frame *hdr = iob->data;
  2265. u16 type = hdr->fc & IEEE80211_FC_TYPE;
  2266. if ( ( hdr->fc & IEEE80211_FC_VERSION ) != IEEE80211_THIS_VERSION )
  2267. goto drop; /* drop invalid-version packets */
  2268. if ( type == IEEE80211_TYPE_CTRL )
  2269. goto drop; /* we don't handle control packets,
  2270. the hardware does */
  2271. if ( dev->last_rx_seq == hdr->seq )
  2272. goto drop; /* avoid duplicate packet */
  2273. dev->last_rx_seq = hdr->seq;
  2274. if ( dev->hw->flags & NET80211_HW_RX_HAS_FCS ) {
  2275. /* discard the FCS */
  2276. iob_unput ( iob, 4 );
  2277. }
  2278. /* Only decrypt packets from our BSSID, to avoid spurious errors */
  2279. if ( ( hdr->fc & IEEE80211_FC_PROTECTED ) &&
  2280. ! memcmp ( hdr->addr2, dev->bssid, ETH_ALEN ) ) {
  2281. /* Decrypt packet; record and drop if it fails */
  2282. struct io_buffer *niob;
  2283. struct net80211_crypto *crypto = dev->crypto;
  2284. if ( ! dev->crypto ) {
  2285. DBGC ( dev, "802.11 %p cannot decrypt packet "
  2286. "without a cryptosystem\n", dev );
  2287. goto drop_crypt;
  2288. }
  2289. if ( ( hdr->addr1[0] & 1 ) && dev->gcrypto ) {
  2290. /* Use group decryption if needed */
  2291. crypto = dev->gcrypto;
  2292. }
  2293. niob = crypto->decrypt ( crypto, iob );
  2294. if ( ! niob ) {
  2295. DBGC ( dev, "802.11 %p decryption error\n", dev );
  2296. goto drop_crypt;
  2297. }
  2298. free_iob ( iob );
  2299. iob = niob;
  2300. hdr = iob->data;
  2301. }
  2302. dev->last_signal = signal;
  2303. /* Fragments go into the frag cache or get dropped. */
  2304. if ( IEEE80211_FRAG ( hdr->seq ) != 0
  2305. || ( hdr->fc & IEEE80211_FC_MORE_FRAG ) ) {
  2306. net80211_rx_frag ( dev, iob, signal );
  2307. return;
  2308. }
  2309. /* Management frames get handled, enqueued, or dropped. */
  2310. if ( type == IEEE80211_TYPE_MGMT ) {
  2311. net80211_handle_mgmt ( dev, iob, signal );
  2312. return;
  2313. }
  2314. /* Data frames get dropped or sent to the net_device. */
  2315. if ( ( hdr->fc & IEEE80211_FC_SUBTYPE ) != IEEE80211_STYPE_DATA )
  2316. goto drop; /* drop QoS, CFP, or null data packets */
  2317. /* Update rate-control algorithm */
  2318. if ( dev->rctl )
  2319. rc80211_update_rx ( dev, hdr->fc & IEEE80211_FC_RETRY, rate );
  2320. /* Pass packet onward */
  2321. if ( dev->state & NET80211_ASSOCIATED ) {
  2322. netdev_rx ( dev->netdev, iob );
  2323. return;
  2324. }
  2325. /* No association? Drop it. */
  2326. goto drop;
  2327. drop_crypt:
  2328. netdev_rx_err ( dev->netdev, NULL, EINVAL_CRYPTO_REQUEST );
  2329. drop:
  2330. DBGC2 ( dev, "802.11 %p dropped packet fc=%04x seq=%04x\n", dev,
  2331. hdr->fc, hdr->seq );
  2332. free_iob ( iob );
  2333. return;
  2334. }
  2335. /** Indicate an error in receiving a packet
  2336. *
  2337. * @v dev 802.11 device
  2338. * @v iob I/O buffer with received packet, or NULL
  2339. * @v rc Error code
  2340. *
  2341. * This logs the error with the wrapping net_device, and frees iob if
  2342. * it is passed.
  2343. */
  2344. void net80211_rx_err ( struct net80211_device *dev,
  2345. struct io_buffer *iob, int rc )
  2346. {
  2347. netdev_rx_err ( dev->netdev, iob, rc );
  2348. }
  2349. /** Indicate the completed transmission of a packet
  2350. *
  2351. * @v dev 802.11 device
  2352. * @v iob I/O buffer of transmitted packet
  2353. * @v retries Number of times this packet was retransmitted
  2354. * @v rc Error code, or 0 for success
  2355. *
  2356. * This logs an error with the wrapping net_device if one occurred,
  2357. * and removes and frees the I/O buffer from its TX queue. The
  2358. * provided retry information is used to tune our transmission rate.
  2359. *
  2360. * If the packet did not need to be retransmitted because it was
  2361. * properly ACKed the first time, @a retries should be 0.
  2362. */
  2363. void net80211_tx_complete ( struct net80211_device *dev,
  2364. struct io_buffer *iob, int retries, int rc )
  2365. {
  2366. /* Update rate-control algorithm */
  2367. if ( dev->rctl )
  2368. rc80211_update_tx ( dev, retries, rc );
  2369. /* Pass completion onward */
  2370. netdev_tx_complete_err ( dev->netdev, iob, rc );
  2371. }
  2372. /** Common 802.11 errors */
  2373. struct errortab common_wireless_errors[] __errortab = {
  2374. __einfo_errortab ( EINFO_EINVAL_CRYPTO_REQUEST ),
  2375. __einfo_errortab ( EINFO_ECONNRESET_UNSPECIFIED ),
  2376. __einfo_errortab ( EINFO_ECONNRESET_INACTIVITY ),
  2377. __einfo_errortab ( EINFO_ECONNRESET_4WAY_TIMEOUT ),
  2378. __einfo_errortab ( EINFO_ECONNRESET_8021X_FAILURE ),
  2379. __einfo_errortab ( EINFO_ECONNREFUSED_FAILURE ),
  2380. __einfo_errortab ( EINFO_ECONNREFUSED_ASSOC_DENIED ),
  2381. __einfo_errortab ( EINFO_ECONNREFUSED_AUTH_ALGO_UNSUPP ),
  2382. };
  2383. /* Drag in objects via net80211_ll_protocol */
  2384. REQUIRING_SYMBOL ( net80211_ll_protocol );
  2385. /* Drag in 802.11 configuration */
  2386. REQUIRE_OBJECT ( config_net80211 );