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

net80211.c 77KB

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