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 72KB

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