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

net80211.c 72KB

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