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

net80211.c 77KB

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