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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657
  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. list_add_tail ( &dev->list, &net80211_devices );
  685. return register_netdev ( dev->netdev );
  686. }
  687. /**
  688. * Unregister 802.11 device from network stack
  689. *
  690. * @v dev 802.11 device
  691. *
  692. * After this call, the device operations are cleared so that they
  693. * will not be called.
  694. */
  695. void net80211_unregister ( struct net80211_device *dev )
  696. {
  697. unregister_netdev ( dev->netdev );
  698. list_del ( &dev->list );
  699. dev->op = &net80211_null_ops;
  700. }
  701. /**
  702. * Free 802.11 device
  703. *
  704. * @v dev 802.11 device
  705. *
  706. * The device should be unregistered before this function is called.
  707. */
  708. void net80211_free ( struct net80211_device *dev )
  709. {
  710. free ( dev->hw );
  711. rc80211_free ( dev->rctl );
  712. netdev_nullify ( dev->netdev );
  713. netdev_put ( dev->netdev );
  714. }
  715. /* ---------- 802.11 network management workhorse code ---------- */
  716. /**
  717. * Set state of 802.11 device
  718. *
  719. * @v dev 802.11 device
  720. * @v clear Bitmask of flags to clear
  721. * @v set Bitmask of flags to set
  722. * @v status Status or reason code for most recent operation
  723. *
  724. * If @a status represents a reason code, it should be OR'ed with
  725. * NET80211_IS_REASON.
  726. *
  727. * Clearing authentication also clears association; clearing
  728. * association also clears security handshaking state. Clearing
  729. * association removes the link-up flag from the wrapping net_device,
  730. * but setting it does not automatically set the flag; that is left to
  731. * the judgment of higher-level code.
  732. */
  733. static inline void net80211_set_state ( struct net80211_device *dev,
  734. short clear, short set,
  735. u16 status )
  736. {
  737. /* The conditions in this function are deliberately formulated
  738. to be decidable at compile-time in most cases. Since clear
  739. and set are generally passed as constants, the body of this
  740. function can be reduced down to a few statements by the
  741. compiler. */
  742. const int statmsk = NET80211_STATUS_MASK | NET80211_IS_REASON;
  743. if ( clear & NET80211_PROBED )
  744. clear |= NET80211_AUTHENTICATED;
  745. if ( clear & NET80211_AUTHENTICATED )
  746. clear |= NET80211_ASSOCIATED;
  747. if ( clear & NET80211_ASSOCIATED )
  748. clear |= NET80211_CRYPTO_SYNCED;
  749. dev->state = ( dev->state & ~clear ) | set;
  750. dev->state = ( dev->state & ~statmsk ) | ( status & statmsk );
  751. if ( clear & NET80211_ASSOCIATED )
  752. netdev_link_down ( dev->netdev );
  753. if ( ( clear | set ) & NET80211_ASSOCIATED )
  754. dev->op->config ( dev, NET80211_CFG_ASSOC );
  755. if ( status != 0 ) {
  756. if ( status & NET80211_IS_REASON )
  757. dev->assoc_rc = -E80211_REASON ( status );
  758. else
  759. dev->assoc_rc = -E80211_STATUS ( status );
  760. netdev_link_err ( dev->netdev, dev->assoc_rc );
  761. }
  762. }
  763. /**
  764. * Add channels to 802.11 device
  765. *
  766. * @v dev 802.11 device
  767. * @v start First channel number to add
  768. * @v len Number of channels to add
  769. * @v txpower TX power (dBm) to allow on added channels
  770. *
  771. * To replace the current list of channels instead of adding to it,
  772. * set the nr_channels field of the 802.11 device to 0 before calling
  773. * this function.
  774. */
  775. static void net80211_add_channels ( struct net80211_device *dev, int start,
  776. int len, int txpower )
  777. {
  778. int i, chan = start;
  779. for ( i = dev->nr_channels; len-- && i < NET80211_MAX_CHANNELS; i++ ) {
  780. dev->channels[i].channel_nr = chan;
  781. dev->channels[i].maxpower = txpower;
  782. dev->channels[i].hw_value = 0;
  783. if ( chan >= 1 && chan <= 14 ) {
  784. dev->channels[i].band = NET80211_BAND_2GHZ;
  785. if ( chan == 14 )
  786. dev->channels[i].center_freq = 2484;
  787. else
  788. dev->channels[i].center_freq = 2407 + 5 * chan;
  789. chan++;
  790. } else {
  791. dev->channels[i].band = NET80211_BAND_5GHZ;
  792. dev->channels[i].center_freq = 5000 + 5 * chan;
  793. chan += 4;
  794. }
  795. }
  796. dev->nr_channels = i;
  797. }
  798. /**
  799. * Filter 802.11 device channels for hardware capabilities
  800. *
  801. * @v dev 802.11 device
  802. *
  803. * Hardware may support fewer channels than regulatory restrictions
  804. * allow; this function filters out channels in dev->channels that are
  805. * not supported by the hardware list in dev->hwinfo. It also copies
  806. * over the net80211_channel::hw_value and limits maximum TX power
  807. * appropriately.
  808. *
  809. * Channels are matched based on center frequency, ignoring band and
  810. * channel number.
  811. *
  812. * If the driver specifies no supported channels, the effect will be
  813. * as though all were supported.
  814. */
  815. static void net80211_filter_hw_channels ( struct net80211_device *dev )
  816. {
  817. int delta = 0, i = 0;
  818. int old_freq = dev->channels[dev->channel].center_freq;
  819. struct net80211_channel *chan, *hwchan;
  820. if ( ! dev->hw->nr_channels )
  821. return;
  822. dev->channel = 0;
  823. for ( chan = dev->channels; chan < dev->channels + dev->nr_channels;
  824. chan++, i++ ) {
  825. int ok = 0;
  826. for ( hwchan = dev->hw->channels;
  827. hwchan < dev->hw->channels + dev->hw->nr_channels;
  828. hwchan++ ) {
  829. if ( hwchan->center_freq == chan->center_freq ) {
  830. ok = 1;
  831. break;
  832. }
  833. }
  834. if ( ! ok )
  835. delta++;
  836. else {
  837. chan->hw_value = hwchan->hw_value;
  838. if ( hwchan->maxpower != 0 &&
  839. chan->maxpower > hwchan->maxpower )
  840. chan->maxpower = hwchan->maxpower;
  841. if ( old_freq == chan->center_freq )
  842. dev->channel = i - delta;
  843. if ( delta )
  844. chan[-delta] = *chan;
  845. }
  846. }
  847. dev->nr_channels -= delta;
  848. if ( dev->channels[dev->channel].center_freq != old_freq )
  849. dev->op->config ( dev, NET80211_CFG_CHANNEL );
  850. }
  851. /**
  852. * Update 802.11 device state to reflect received capabilities field
  853. *
  854. * @v dev 802.11 device
  855. * @v capab Capabilities field in beacon, probe, or association frame
  856. * @ret rc Return status code
  857. */
  858. static int net80211_process_capab ( struct net80211_device *dev,
  859. u16 capab )
  860. {
  861. u16 old_phy = dev->phy_flags;
  862. if ( ( capab & ( IEEE80211_CAPAB_MANAGED | IEEE80211_CAPAB_ADHOC ) ) !=
  863. IEEE80211_CAPAB_MANAGED ) {
  864. DBGC ( dev, "802.11 %p cannot handle IBSS network\n", dev );
  865. return -ENOSYS;
  866. }
  867. if ( capab & IEEE80211_CAPAB_SPECTRUM_MGMT ) {
  868. DBGC ( dev, "802.11 %p cannot handle spectrum managed "
  869. "network\n", dev );
  870. return -ENOSYS;
  871. }
  872. dev->phy_flags &= ~( NET80211_PHY_USE_SHORT_PREAMBLE |
  873. NET80211_PHY_USE_SHORT_SLOT );
  874. if ( capab & IEEE80211_CAPAB_SHORT_PMBL )
  875. dev->phy_flags |= NET80211_PHY_USE_SHORT_PREAMBLE;
  876. if ( capab & IEEE80211_CAPAB_SHORT_SLOT )
  877. dev->phy_flags |= NET80211_PHY_USE_SHORT_SLOT;
  878. if ( old_phy != dev->phy_flags )
  879. dev->op->config ( dev, NET80211_CFG_PHY_PARAMS );
  880. return 0;
  881. }
  882. /**
  883. * Update 802.11 device state to reflect received information elements
  884. *
  885. * @v dev 802.11 device
  886. * @v ie Pointer to first information element
  887. * @v ie_end Pointer to tail of packet I/O buffer
  888. * @ret rc Return status code
  889. */
  890. static int net80211_process_ie ( struct net80211_device *dev,
  891. union ieee80211_ie *ie, void *ie_end )
  892. {
  893. u16 old_rate = dev->rates[dev->rate];
  894. u16 old_phy = dev->phy_flags;
  895. int have_rates = 0, i;
  896. int ds_channel = 0;
  897. int changed = 0;
  898. int band = dev->channels[dev->channel].band;
  899. if ( ( void * ) ie >= ie_end )
  900. return 0;
  901. for ( ; ie; ie = ieee80211_next_ie ( ie, ie_end ) ) {
  902. switch ( ie->id ) {
  903. case IEEE80211_IE_SSID:
  904. if ( ie->len <= 32 ) {
  905. memcpy ( dev->essid, ie->ssid, ie->len );
  906. dev->essid[ie->len] = 0;
  907. }
  908. break;
  909. case IEEE80211_IE_RATES:
  910. case IEEE80211_IE_EXT_RATES:
  911. if ( ! have_rates ) {
  912. dev->nr_rates = 0;
  913. dev->basic_rates = 0;
  914. have_rates = 1;
  915. }
  916. for ( i = 0; i < ie->len &&
  917. dev->nr_rates < NET80211_MAX_RATES; i++ ) {
  918. u8 rid = ie->rates[i];
  919. u16 rate = ( rid & 0x7f ) * 5;
  920. if ( rid & 0x80 )
  921. dev->basic_rates |=
  922. ( 1 << dev->nr_rates );
  923. dev->rates[dev->nr_rates++] = rate;
  924. }
  925. break;
  926. case IEEE80211_IE_DS_PARAM:
  927. if ( dev->channel < dev->nr_channels && ds_channel ==
  928. dev->channels[dev->channel].channel_nr )
  929. break;
  930. ds_channel = ie->ds_param.current_channel;
  931. net80211_change_channel ( dev, ds_channel );
  932. break;
  933. case IEEE80211_IE_COUNTRY:
  934. dev->nr_channels = 0;
  935. DBGC ( dev, "802.11 %p setting country regulations "
  936. "for %c%c\n", dev, ie->country.name[0],
  937. ie->country.name[1] );
  938. for ( i = 0; i < ( ie->len - 3 ) / 3; i++ ) {
  939. union ieee80211_ie_country_triplet *t =
  940. &ie->country.triplet[i];
  941. if ( t->first > 200 ) {
  942. DBGC ( dev, "802.11 %p ignoring regulatory "
  943. "extension information\n", dev );
  944. } else {
  945. net80211_add_channels ( dev,
  946. t->band.first_channel,
  947. t->band.nr_channels,
  948. t->band.max_txpower );
  949. }
  950. }
  951. net80211_filter_hw_channels ( dev );
  952. break;
  953. case IEEE80211_IE_ERP_INFO:
  954. dev->phy_flags &= ~( NET80211_PHY_USE_PROTECTION |
  955. NET80211_PHY_USE_SHORT_PREAMBLE );
  956. if ( ie->erp_info & IEEE80211_ERP_USE_PROTECTION )
  957. dev->phy_flags |= NET80211_PHY_USE_PROTECTION;
  958. if ( ! ( ie->erp_info & IEEE80211_ERP_BARKER_LONG ) )
  959. dev->phy_flags |= NET80211_PHY_USE_SHORT_PREAMBLE;
  960. break;
  961. case IEEE80211_IE_RSN:
  962. /* XXX need to implement WPA stuff */
  963. break;
  964. }
  965. }
  966. if ( have_rates ) {
  967. /* Allow only those rates that are also supported by
  968. the hardware. */
  969. int delta = 0, j;
  970. dev->rate = 0;
  971. for ( i = 0; i < dev->nr_rates; i++ ) {
  972. int ok = 0;
  973. for ( j = 0; j < dev->hw->nr_rates[band]; j++ ) {
  974. if ( dev->hw->rates[band][j] == dev->rates[i] ){
  975. ok = 1;
  976. break;
  977. }
  978. }
  979. if ( ! ok )
  980. delta++;
  981. else {
  982. dev->rates[i - delta] = dev->rates[i];
  983. if ( old_rate == dev->rates[i] )
  984. dev->rate = i - delta;
  985. }
  986. }
  987. dev->nr_rates -= delta;
  988. /* Sort available rates - sorted subclumps tend to already
  989. exist, so insertion sort works well. */
  990. for ( i = 1; i < dev->nr_rates; i++ ) {
  991. u16 rate = dev->rates[i];
  992. for ( j = i - 1; j >= 0 && dev->rates[j] >= rate; j-- )
  993. dev->rates[j + 1] = dev->rates[j];
  994. dev->rates[j + 1] = rate;
  995. }
  996. net80211_set_rtscts_rate ( dev );
  997. if ( dev->rates[dev->rate] != old_rate )
  998. changed |= NET80211_CFG_RATE;
  999. }
  1000. if ( dev->hw->flags & NET80211_HW_NO_SHORT_PREAMBLE )
  1001. dev->phy_flags &= ~NET80211_PHY_USE_SHORT_PREAMBLE;
  1002. if ( dev->hw->flags & NET80211_HW_NO_SHORT_SLOT )
  1003. dev->phy_flags &= ~NET80211_PHY_USE_SHORT_SLOT;
  1004. if ( old_phy != dev->phy_flags )
  1005. changed |= NET80211_CFG_PHY_PARAMS;
  1006. if ( changed )
  1007. dev->op->config ( dev, changed );
  1008. return 0;
  1009. }
  1010. /**
  1011. * Create information elements for outgoing probe or association packet
  1012. *
  1013. * @v dev 802.11 device
  1014. * @v ie Pointer to start of information element area
  1015. * @ret next_ie Pointer to first byte after added information elements
  1016. */
  1017. static union ieee80211_ie *
  1018. net80211_marshal_request_info ( struct net80211_device *dev,
  1019. union ieee80211_ie *ie )
  1020. {
  1021. int i;
  1022. ie->id = IEEE80211_IE_SSID;
  1023. ie->len = strlen ( dev->essid );
  1024. memcpy ( ie->ssid, dev->essid, ie->len );
  1025. ie = ieee80211_next_ie ( ie, NULL );
  1026. ie->id = IEEE80211_IE_RATES;
  1027. ie->len = dev->nr_rates;
  1028. for ( i = 0; i < ie->len; i++ ) {
  1029. ie->rates[i] = dev->rates[i] / 5;
  1030. if ( dev->basic_rates & ( 1 << i ) )
  1031. ie->rates[i] |= 0x80;
  1032. }
  1033. if ( ie->len > 8 ) {
  1034. /* 802.11 requires we use an Extended Basic Rates IE
  1035. for the rates beyond the eighth. */
  1036. int rates = ie->len;
  1037. memmove ( ( void * ) ie + 2 + 8 + 2, ( void * ) ie + 2 + 8,
  1038. rates - 8 );
  1039. ie->len = 8;
  1040. ie = ieee80211_next_ie ( ie, NULL );
  1041. ie->id = IEEE80211_IE_EXT_RATES;
  1042. ie->len = rates - 8;
  1043. }
  1044. ie = ieee80211_next_ie ( ie, NULL );
  1045. return ie;
  1046. }
  1047. /** Seconds to wait after finding a network, to possibly find better APs for it
  1048. *
  1049. * This is used when a specific SSID to scan for is specified.
  1050. */
  1051. #define NET80211_PROBE_GATHER 1
  1052. /** Seconds to wait after finding a network, to possibly find other networks
  1053. *
  1054. * This is used when an empty SSID is specified, to scan for all
  1055. * networks.
  1056. */
  1057. #define NET80211_PROBE_GATHER_ALL 2
  1058. /** Seconds to allow a probe to take if no network has been found */
  1059. #define NET80211_PROBE_TIMEOUT 6
  1060. /**
  1061. * Begin probe of 802.11 networks
  1062. *
  1063. * @v dev 802.11 device
  1064. * @v essid SSID to probe for, or "" to accept any (may not be NULL)
  1065. * @v active Whether to use active scanning
  1066. * @ret ctx Probe context
  1067. *
  1068. * Active scanning may only be used on channels 1-11 in the 2.4GHz
  1069. * band, due to gPXE's lack of a complete regulatory database. If
  1070. * active scanning is used, probe packets will be sent on each
  1071. * channel; this can allow association with hidden-SSID networks if
  1072. * the SSID is properly specified.
  1073. *
  1074. * A @c NULL return indicates an out-of-memory condition.
  1075. *
  1076. * The returned context must be periodically passed to
  1077. * net80211_probe_step() until that function returns zero.
  1078. */
  1079. struct net80211_probe_ctx * net80211_probe_start ( struct net80211_device *dev,
  1080. const char *essid,
  1081. int active )
  1082. {
  1083. struct net80211_probe_ctx *ctx = zalloc ( sizeof ( *ctx ) );
  1084. if ( ! ctx )
  1085. return NULL;
  1086. assert ( dev->netdev->state & NETDEV_OPEN );
  1087. ctx->dev = dev;
  1088. ctx->old_keep_mgmt = net80211_keep_mgmt ( dev, 1 );
  1089. ctx->essid = essid;
  1090. if ( dev->essid != ctx->essid )
  1091. strcpy ( dev->essid, ctx->essid );
  1092. if ( active ) {
  1093. struct ieee80211_probe_req *probe_req;
  1094. union ieee80211_ie *ie;
  1095. ctx->probe = alloc_iob ( 128 );
  1096. iob_reserve ( ctx->probe, IEEE80211_TYP_FRAME_HEADER_LEN );
  1097. probe_req = ctx->probe->data;
  1098. ie = net80211_marshal_request_info ( dev,
  1099. probe_req->info_element );
  1100. ie->id = IEEE80211_IE_REQUEST;
  1101. ie->len = 3;
  1102. ie->request[0] = IEEE80211_IE_COUNTRY;
  1103. ie->request[1] = IEEE80211_IE_ERP_INFO;
  1104. ie->request[2] = IEEE80211_IE_RSN;
  1105. ie = ieee80211_next_ie ( ie, NULL );
  1106. iob_put ( ctx->probe, ( void * ) ie - ctx->probe->data );
  1107. }
  1108. ctx->ticks_start = currticks();
  1109. ctx->ticks_beacon = 0;
  1110. ctx->ticks_channel = currticks();
  1111. ctx->hop_time = ticks_per_sec() / ( active ? 2 : 6 );
  1112. /*
  1113. * Channels on 2.4GHz overlap, and the most commonly used
  1114. * are 1, 6, and 11. We'll get a result faster if we check
  1115. * every 5 channels, but in order to hit all of them the
  1116. * number of channels must be relatively prime to 5. If it's
  1117. * not, tweak the hop.
  1118. */
  1119. ctx->hop_step = 5;
  1120. while ( dev->nr_channels % ctx->hop_step == 0 && ctx->hop_step > 1 )
  1121. ctx->hop_step--;
  1122. ctx->beacons = malloc ( sizeof ( *ctx->beacons ) );
  1123. INIT_LIST_HEAD ( ctx->beacons );
  1124. dev->channel = 0;
  1125. dev->op->config ( dev, NET80211_CFG_CHANNEL );
  1126. return ctx;
  1127. }
  1128. /**
  1129. * Continue probe of 802.11 networks
  1130. *
  1131. * @v ctx Probe context returned by net80211_probe_start()
  1132. * @ret rc Probe status
  1133. *
  1134. * The return code will be 0 if the probe is still going on (and this
  1135. * function should be called again), a positive number if the probe
  1136. * completed successfully, or a negative error code if the probe
  1137. * failed for that reason.
  1138. *
  1139. * Whether the probe succeeded or failed, you must call
  1140. * net80211_probe_finish_all() or net80211_probe_finish_best()
  1141. * (depending on whether you want information on all networks or just
  1142. * the best-signal one) in order to release the probe context. A
  1143. * failed probe may still have acquired some valid data.
  1144. */
  1145. int net80211_probe_step ( struct net80211_probe_ctx *ctx )
  1146. {
  1147. struct net80211_device *dev = ctx->dev;
  1148. u32 start_timeout = NET80211_PROBE_TIMEOUT * ticks_per_sec();
  1149. u32 gather_timeout = ticks_per_sec();
  1150. u32 now = currticks();
  1151. struct io_buffer *iob;
  1152. int signal;
  1153. int rc;
  1154. char ssid[IEEE80211_MAX_SSID_LEN + 1];
  1155. gather_timeout *= ( ctx->essid[0] ? NET80211_PROBE_GATHER :
  1156. NET80211_PROBE_GATHER_ALL );
  1157. /* Time out if necessary */
  1158. if ( now >= ctx->ticks_start + start_timeout )
  1159. return list_empty ( ctx->beacons ) ? -ETIMEDOUT : +1;
  1160. if ( ctx->ticks_beacon > 0 && now >= ctx->ticks_start + gather_timeout )
  1161. return +1;
  1162. /* Change channels if necessary */
  1163. if ( now >= ctx->ticks_channel + ctx->hop_time ) {
  1164. dev->channel = ( dev->channel + ctx->hop_step )
  1165. % dev->nr_channels;
  1166. dev->op->config ( dev, NET80211_CFG_CHANNEL );
  1167. udelay ( dev->hw->channel_change_time );
  1168. ctx->ticks_channel = now;
  1169. if ( ctx->probe ) {
  1170. struct io_buffer *siob = ctx->probe; /* to send */
  1171. /* make a copy for future use */
  1172. iob = alloc_iob ( siob->tail - siob->head );
  1173. iob_reserve ( iob, iob_headroom ( siob ) );
  1174. memcpy ( iob_put ( iob, iob_len ( siob ) ),
  1175. siob->data, iob_len ( siob ) );
  1176. ctx->probe = iob;
  1177. rc = net80211_tx_mgmt ( dev, IEEE80211_STYPE_PROBE_REQ,
  1178. net80211_ll_broadcast,
  1179. iob_disown ( siob ) );
  1180. if ( rc ) {
  1181. DBGC ( dev, "802.11 %p send probe failed: "
  1182. "%s\n", dev, strerror ( rc ) );
  1183. return rc;
  1184. }
  1185. }
  1186. }
  1187. /* Check for new management packets */
  1188. while ( ( iob = net80211_mgmt_dequeue ( dev, &signal ) ) != NULL ) {
  1189. struct ieee80211_frame *hdr;
  1190. struct ieee80211_beacon *beacon;
  1191. union ieee80211_ie *ie;
  1192. struct net80211_wlan *wlan;
  1193. u16 type;
  1194. hdr = iob->data;
  1195. type = hdr->fc & IEEE80211_FC_SUBTYPE;
  1196. beacon = ( struct ieee80211_beacon * ) hdr->data;
  1197. if ( type != IEEE80211_STYPE_BEACON &&
  1198. type != IEEE80211_STYPE_PROBE_RESP ) {
  1199. DBGC2 ( dev, "802.11 %p probe: non-beacon\n", dev );
  1200. goto drop;
  1201. }
  1202. if ( ( void * ) beacon->info_element >= iob->tail ) {
  1203. DBGC ( dev, "802.11 %p probe: beacon with no IEs\n",
  1204. dev );
  1205. goto drop;
  1206. }
  1207. ie = beacon->info_element;
  1208. while ( ie && ie->id != IEEE80211_IE_SSID )
  1209. ie = ieee80211_next_ie ( ie, iob->tail );
  1210. if ( ! ie ) {
  1211. DBGC ( dev, "802.11 %p probe: beacon with no SSID\n",
  1212. dev );
  1213. goto drop;
  1214. }
  1215. memcpy ( ssid, ie->ssid, ie->len );
  1216. ssid[ie->len] = 0;
  1217. if ( ctx->essid[0] && strcmp ( ctx->essid, ssid ) != 0 ) {
  1218. DBGC2 ( dev, "802.11 %p probe: beacon with wrong SSID "
  1219. "(%s)\n", dev, ssid );
  1220. goto drop;
  1221. }
  1222. /* See if we've got an entry for this network */
  1223. list_for_each_entry ( wlan, ctx->beacons, list ) {
  1224. if ( strcmp ( wlan->essid, ssid ) != 0 )
  1225. continue;
  1226. if ( signal < wlan->signal ) {
  1227. DBGC2 ( dev, "802.11 %p probe: beacon for %s "
  1228. "(%s) with weaker signal %d\n", dev,
  1229. ssid, eth_ntoa ( hdr->addr3 ), signal );
  1230. goto drop;
  1231. }
  1232. goto fill;
  1233. }
  1234. /* No entry yet - make one */
  1235. wlan = zalloc ( sizeof ( *wlan ) );
  1236. strcpy ( wlan->essid, ssid );
  1237. list_add_tail ( &wlan->list, ctx->beacons );
  1238. /* Whether we're using an old entry or a new one, fill
  1239. it with new data. */
  1240. fill:
  1241. memcpy ( wlan->bssid, hdr->addr3, ETH_ALEN );
  1242. wlan->signal = signal;
  1243. wlan->channel = dev->channels[dev->channel].channel_nr;
  1244. /* Copy this I/O buffer into a new wlan->beacon; the
  1245. * iob we've got probably came from the device driver
  1246. * and may have the full 2.4k allocation, which we
  1247. * don't want to keep around wasting memory.
  1248. */
  1249. free_iob ( wlan->beacon );
  1250. wlan->beacon = alloc_iob ( iob_len ( iob ) );
  1251. memcpy ( iob_put ( wlan->beacon, iob_len ( iob ) ),
  1252. iob->data, iob_len ( iob ) );
  1253. /* XXX actually check capab and RSN ie to
  1254. figure this out */
  1255. wlan->handshaking = NET80211_SECPROT_NONE;
  1256. wlan->crypto = NET80211_CRYPT_NONE;
  1257. ctx->ticks_beacon = now;
  1258. DBGC2 ( dev, "802.11 %p probe: good beacon for %s (%s)\n",
  1259. dev, wlan->essid, eth_ntoa ( wlan->bssid ) );
  1260. drop:
  1261. free_iob ( iob );
  1262. }
  1263. return 0;
  1264. }
  1265. /**
  1266. * Finish probe of 802.11 networks, returning best-signal network found
  1267. *
  1268. * @v ctx Probe context
  1269. * @ret wlan Best-signal network found, or @c NULL if none were found
  1270. *
  1271. * If net80211_probe_start() was called with a particular SSID
  1272. * parameter as filter, only a network with that SSID (matching
  1273. * case-sensitively) can be returned from this function.
  1274. */
  1275. struct net80211_wlan *
  1276. net80211_probe_finish_best ( struct net80211_probe_ctx *ctx )
  1277. {
  1278. struct net80211_wlan *best = NULL, *wlan;
  1279. if ( ! ctx )
  1280. return NULL;
  1281. list_for_each_entry ( wlan, ctx->beacons, list ) {
  1282. if ( ! best || best->signal < wlan->signal )
  1283. best = wlan;
  1284. }
  1285. if ( best )
  1286. list_del ( &best->list );
  1287. else
  1288. DBGC ( ctx->dev, "802.11 %p probe: found nothing for '%s'\n",
  1289. ctx->dev, ctx->essid );
  1290. if ( ! list_empty ( ctx->beacons ) )
  1291. net80211_free_wlanlist ( ctx->beacons );
  1292. net80211_keep_mgmt ( ctx->dev, ctx->old_keep_mgmt );
  1293. if ( ctx->probe )
  1294. free_iob ( ctx->probe );
  1295. free ( ctx );
  1296. return best;
  1297. }
  1298. /**
  1299. * Finish probe of 802.11 networks, returning all networks found
  1300. *
  1301. * @v ctx Probe context
  1302. * @ret list List of net80211_wlan detailing networks found
  1303. *
  1304. * If net80211_probe_start() was called with a particular SSID
  1305. * parameter as filter, this will always return either an empty or a
  1306. * one-element list.
  1307. */
  1308. struct list_head *net80211_probe_finish_all ( struct net80211_probe_ctx *ctx )
  1309. {
  1310. struct list_head *beacons = ctx->beacons;
  1311. if ( ! ctx )
  1312. return NULL;
  1313. net80211_keep_mgmt ( ctx->dev, ctx->old_keep_mgmt );
  1314. if ( ctx->probe )
  1315. free_iob ( ctx->probe );
  1316. free ( ctx );
  1317. return beacons;
  1318. }
  1319. /**
  1320. * Free WLAN structure
  1321. *
  1322. * @v wlan WLAN structure to free
  1323. */
  1324. void net80211_free_wlan ( struct net80211_wlan *wlan )
  1325. {
  1326. if ( wlan ) {
  1327. free_iob ( wlan->beacon );
  1328. free ( wlan );
  1329. }
  1330. }
  1331. /**
  1332. * Free list of WLAN structures
  1333. *
  1334. * @v list List of WLAN structures to free
  1335. */
  1336. void net80211_free_wlanlist ( struct list_head *list )
  1337. {
  1338. struct net80211_wlan *wlan, *tmp;
  1339. if ( ! list )
  1340. return;
  1341. list_for_each_entry_safe ( wlan, tmp, list, list ) {
  1342. list_del ( &wlan->list );
  1343. net80211_free_wlan ( wlan );
  1344. }
  1345. free ( list );
  1346. }
  1347. /** Number of ticks to wait for replies to association management frames */
  1348. #define ASSOC_TIMEOUT TICKS_PER_SEC
  1349. /** Number of times to try sending a particular association management frame */
  1350. #define ASSOC_RETRIES 2
  1351. /**
  1352. * Step 802.11 association process
  1353. *
  1354. * @v proc Association process
  1355. */
  1356. static void net80211_step_associate ( struct process *proc )
  1357. {
  1358. struct net80211_device *dev =
  1359. container_of ( proc, struct net80211_device, proc_assoc );
  1360. int rc = 0;
  1361. int status = dev->state & NET80211_STATUS_MASK;
  1362. /*
  1363. * We use a sort of state machine implemented using bits in
  1364. * the dev->state variable. At each call, we take the
  1365. * logically first step that has not yet succeeded; either it
  1366. * has not been tried yet, it's being retried, or it failed.
  1367. * If it failed, we return an error indication; otherwise we
  1368. * perform the step. If it succeeds, RX handling code will set
  1369. * the appropriate status bit for us.
  1370. *
  1371. * Probe works a bit differently, since we have to step it
  1372. * on every call instead of waiting for a packet to arrive
  1373. * that will set the completion bit for us.
  1374. */
  1375. /* If we're waiting for a reply, check for timeout condition */
  1376. if ( dev->state & NET80211_WAITING ) {
  1377. /* Sanity check */
  1378. if ( ! dev->associating )
  1379. return;
  1380. if ( currticks() - dev->ctx.assoc->last_packet > ASSOC_TIMEOUT ) {
  1381. /* Timed out - fail if too many retries, or retry */
  1382. dev->ctx.assoc->times_tried++;
  1383. if ( ++dev->ctx.assoc->times_tried > ASSOC_RETRIES ) {
  1384. rc = -ETIMEDOUT;
  1385. goto fail;
  1386. }
  1387. } else {
  1388. /* Didn't time out - let it keep going */
  1389. return;
  1390. }
  1391. } else {
  1392. if ( dev->state & NET80211_PROBED )
  1393. dev->ctx.assoc->times_tried = 0;
  1394. }
  1395. if ( ! ( dev->state & NET80211_PROBED ) ) {
  1396. /* state: probe */
  1397. if ( ! dev->ctx.probe ) {
  1398. /* start probe */
  1399. int active = fetch_intz_setting ( NULL,
  1400. &net80211_active_setting );
  1401. int band = dev->hw->bands;
  1402. if ( active )
  1403. band &= ~NET80211_BAND_BIT_5GHZ;
  1404. rc = net80211_prepare_probe ( dev, band, active );
  1405. if ( rc )
  1406. goto fail;
  1407. dev->ctx.probe = net80211_probe_start ( dev, dev->essid,
  1408. active );
  1409. if ( ! dev->ctx.probe ) {
  1410. dev->assoc_rc = -ENOMEM;
  1411. goto fail;
  1412. }
  1413. }
  1414. rc = net80211_probe_step ( dev->ctx.probe );
  1415. if ( ! rc ) {
  1416. return; /* still going */
  1417. }
  1418. dev->associating = net80211_probe_finish_best ( dev->ctx.probe );
  1419. dev->ctx.probe = NULL;
  1420. if ( ! dev->associating ) {
  1421. if ( rc > 0 ) /* "successful" probe found nothing */
  1422. rc = -ETIMEDOUT;
  1423. goto fail;
  1424. }
  1425. /* If we probed using a broadcast SSID, record that
  1426. fact for the settings applicator before we clobber
  1427. it with the specific SSID we've chosen. */
  1428. if ( ! dev->essid[0] )
  1429. dev->state |= NET80211_AUTO_SSID;
  1430. DBGC ( dev, "802.11 %p found network %s (%s)\n", dev,
  1431. dev->associating->essid,
  1432. eth_ntoa ( dev->associating->bssid ) );
  1433. dev->ctx.assoc = zalloc ( sizeof ( *dev->ctx.assoc ) );
  1434. if ( ! dev->ctx.assoc ) {
  1435. rc = -ENOMEM;
  1436. goto fail;
  1437. }
  1438. dev->state |= NET80211_PROBED;
  1439. dev->ctx.assoc->method = IEEE80211_AUTH_OPEN_SYSTEM;
  1440. return;
  1441. }
  1442. /* Record time of sending the packet we're about to send, for timeout */
  1443. dev->ctx.assoc->last_packet = currticks();
  1444. if ( ! ( dev->state & NET80211_AUTHENTICATED ) ) {
  1445. /* state: prepare and authenticate */
  1446. if ( status != IEEE80211_STATUS_SUCCESS ) {
  1447. /* we tried authenticating already, but failed */
  1448. int method = dev->ctx.assoc->method;
  1449. if ( method == IEEE80211_AUTH_OPEN_SYSTEM &&
  1450. ( status == IEEE80211_STATUS_AUTH_CHALL_INVALID ||
  1451. status == IEEE80211_STATUS_AUTH_ALGO_UNSUPP ) ) {
  1452. /* Maybe this network uses Shared Key? */
  1453. dev->ctx.assoc->method =
  1454. IEEE80211_AUTH_SHARED_KEY;
  1455. } else {
  1456. goto fail;
  1457. }
  1458. }
  1459. DBGC ( dev, "802.11 %p authenticating with method %d\n", dev,
  1460. dev->ctx.assoc->method );
  1461. rc = net80211_prepare_assoc ( dev, dev->associating );
  1462. if ( rc )
  1463. goto fail;
  1464. rc = net80211_send_auth ( dev, dev->associating,
  1465. dev->ctx.assoc->method );
  1466. if ( rc )
  1467. goto fail;
  1468. return;
  1469. }
  1470. if ( ! ( dev->state & NET80211_ASSOCIATED ) ) {
  1471. /* state: associate */
  1472. if ( status != IEEE80211_STATUS_SUCCESS )
  1473. goto fail;
  1474. DBGC ( dev, "802.11 %p associating\n", dev );
  1475. rc = net80211_send_assoc ( dev, dev->associating );
  1476. if ( rc )
  1477. goto fail;
  1478. return;
  1479. }
  1480. if ( ! ( dev->state & NET80211_CRYPTO_SYNCED ) ) {
  1481. /* state: crypto sync */
  1482. DBGC ( dev, "802.11 %p security handshaking\n", dev );
  1483. dev->state |= NET80211_CRYPTO_SYNCED;
  1484. /* XXX need to actually do something here once we
  1485. support WPA */
  1486. return;
  1487. }
  1488. /* state: done! */
  1489. netdev_link_up ( dev->netdev );
  1490. dev->assoc_rc = 0;
  1491. dev->state &= ~NET80211_WORKING;
  1492. free ( dev->ctx.assoc );
  1493. dev->ctx.assoc = NULL;
  1494. net80211_free_wlan ( dev->associating );
  1495. dev->associating = NULL;
  1496. dev->rctl = rc80211_init ( dev );
  1497. process_del ( proc );
  1498. DBGC ( dev, "802.11 %p associated with %s (%s)\n", dev,
  1499. dev->essid, eth_ntoa ( dev->bssid ) );
  1500. return;
  1501. fail:
  1502. dev->state &= ~( NET80211_WORKING | NET80211_WAITING );
  1503. if ( rc )
  1504. dev->assoc_rc = rc;
  1505. netdev_link_err ( dev->netdev, dev->assoc_rc );
  1506. /* We never reach here from the middle of a probe, so we don't
  1507. need to worry about freeing dev->ctx.probe. */
  1508. if ( dev->state & NET80211_PROBED ) {
  1509. free ( dev->ctx.assoc );
  1510. dev->ctx.assoc = NULL;
  1511. }
  1512. net80211_free_wlan ( dev->associating );
  1513. dev->associating = NULL;
  1514. process_del ( proc );
  1515. DBGC ( dev, "802.11 %p association failed (state=%04x): "
  1516. "%s\n", dev, dev->state, strerror ( dev->assoc_rc ) );
  1517. /* Try it again: */
  1518. net80211_autoassociate ( dev );
  1519. }
  1520. /**
  1521. * Check for 802.11 SSID updates
  1522. *
  1523. * This acts as a settings applicator; if the user changes netX/ssid,
  1524. * and netX is currently open, the association task will be invoked
  1525. * again.
  1526. */
  1527. static int net80211_check_ssid_update ( void )
  1528. {
  1529. struct net80211_device *dev;
  1530. char ssid[IEEE80211_MAX_SSID_LEN + 1];
  1531. list_for_each_entry ( dev, &net80211_devices, list ) {
  1532. if ( ! ( dev->netdev->state & NETDEV_OPEN ) )
  1533. continue;
  1534. fetch_string_setting ( netdev_settings ( dev->netdev ),
  1535. &net80211_ssid_setting, ssid,
  1536. IEEE80211_MAX_SSID_LEN + 1 );
  1537. if ( strcmp ( ssid, dev->essid ) != 0 &&
  1538. ! ( ! ssid[0] && ( dev->state & NET80211_AUTO_SSID ) ) ) {
  1539. DBGC ( dev, "802.11 %p updating association: "
  1540. "%s -> %s\n", dev, dev->essid, ssid );
  1541. net80211_autoassociate ( dev );
  1542. }
  1543. }
  1544. return 0;
  1545. }
  1546. /**
  1547. * Start 802.11 association process
  1548. *
  1549. * @v dev 802.11 device
  1550. *
  1551. * If the association process is running, it will be restarted.
  1552. */
  1553. void net80211_autoassociate ( struct net80211_device *dev )
  1554. {
  1555. if ( ! ( dev->state & NET80211_WORKING ) ) {
  1556. DBGC2 ( dev, "802.11 %p spawning association process\n", dev );
  1557. process_add ( &dev->proc_assoc );
  1558. }
  1559. /* Clean up everything an earlier association process might
  1560. have been in the middle of using */
  1561. if ( dev->associating )
  1562. net80211_free_wlan ( dev->associating );
  1563. if ( ! ( dev->state & NET80211_PROBED ) )
  1564. net80211_free_wlan (
  1565. net80211_probe_finish_best ( dev->ctx.probe ) );
  1566. else
  1567. free ( dev->ctx.assoc );
  1568. /* Reset to a clean state */
  1569. fetch_string_setting ( netdev_settings ( dev->netdev ),
  1570. &net80211_ssid_setting, dev->essid,
  1571. IEEE80211_MAX_SSID_LEN + 1 );
  1572. dev->ctx.probe = NULL;
  1573. dev->associating = NULL;
  1574. net80211_set_state ( dev, NET80211_PROBED, NET80211_WORKING, 0 );
  1575. }
  1576. /**
  1577. * Pick TX rate for RTS/CTS packets based on data rate
  1578. *
  1579. * @v dev 802.11 device
  1580. *
  1581. * The RTS/CTS rate is the fastest TX rate marked as "basic" that is
  1582. * not faster than the data rate.
  1583. */
  1584. static void net80211_set_rtscts_rate ( struct net80211_device *dev )
  1585. {
  1586. u16 datarate = dev->rates[dev->rate];
  1587. u16 rtsrate = 0;
  1588. int rts_idx = -1;
  1589. int i;
  1590. for ( i = 0; i < dev->nr_rates; i++ ) {
  1591. u16 rate = dev->rates[i];
  1592. if ( ! ( dev->basic_rates & ( 1 << i ) ) || rate > datarate )
  1593. continue;
  1594. if ( rate > rtsrate ) {
  1595. rtsrate = rate;
  1596. rts_idx = i;
  1597. }
  1598. }
  1599. /* If this is in initialization, we might not have any basic
  1600. rates; just use the first data rate in that case. */
  1601. if ( rts_idx < 0 )
  1602. rts_idx = 0;
  1603. dev->rtscts_rate = rts_idx;
  1604. }
  1605. /**
  1606. * Set data transmission rate for 802.11 device
  1607. *
  1608. * @v dev 802.11 device
  1609. * @v rate Rate to set, as index into @c dev->rates array
  1610. */
  1611. void net80211_set_rate_idx ( struct net80211_device *dev, int rate )
  1612. {
  1613. assert ( dev->netdev->state & NETDEV_OPEN );
  1614. if ( rate >= 0 && rate < dev->nr_rates && rate != dev->rate ) {
  1615. DBGC2 ( dev, "802.11 %p changing rate from %d->%d Mbps\n",
  1616. dev, dev->rates[dev->rate] / 10,
  1617. dev->rates[rate] / 10 );
  1618. dev->rate = rate;
  1619. net80211_set_rtscts_rate ( dev );
  1620. dev->op->config ( dev, NET80211_CFG_RATE );
  1621. }
  1622. }
  1623. /**
  1624. * Configure 802.11 device to transmit on a certain channel
  1625. *
  1626. * @v dev 802.11 device
  1627. * @v channel Channel number (1-11 for 2.4GHz) to transmit on
  1628. */
  1629. int net80211_change_channel ( struct net80211_device *dev, int channel )
  1630. {
  1631. int i, oldchan = dev->channel;
  1632. assert ( dev->netdev->state & NETDEV_OPEN );
  1633. for ( i = 0; i < dev->nr_channels; i++ ) {
  1634. if ( dev->channels[i].channel_nr == channel ) {
  1635. dev->channel = i;
  1636. break;
  1637. }
  1638. }
  1639. if ( i == dev->nr_channels )
  1640. return -ENOENT;
  1641. if ( i != oldchan )
  1642. return dev->op->config ( dev, NET80211_CFG_CHANNEL );
  1643. return 0;
  1644. }
  1645. /**
  1646. * Prepare 802.11 device channel and rate set for scanning
  1647. *
  1648. * @v dev 802.11 device
  1649. * @v band RF band(s) on which to prepare for scanning
  1650. * @v active Whether the scanning will be active
  1651. * @ret rc Return status code
  1652. */
  1653. int net80211_prepare_probe ( struct net80211_device *dev, int band,
  1654. int active )
  1655. {
  1656. assert ( dev->netdev->state & NETDEV_OPEN );
  1657. if ( active && ( band & NET80211_BAND_BIT_5GHZ ) ) {
  1658. DBGC ( dev, "802.11 %p cannot perform active scanning on "
  1659. "5GHz band\n", dev );
  1660. return -EINVAL_ACTIVE_SCAN;
  1661. }
  1662. if ( band == 0 ) {
  1663. /* This can happen for a 5GHz-only card with 5GHz
  1664. scanning masked out by an active request. */
  1665. DBGC ( dev, "802.11 %p asked to prepare for scanning nothing\n",
  1666. dev );
  1667. return -EINVAL_ACTIVE_SCAN;
  1668. }
  1669. dev->nr_channels = 0;
  1670. if ( active )
  1671. net80211_add_channels ( dev, 1, 11, NET80211_REG_TXPOWER );
  1672. else {
  1673. if ( band & NET80211_BAND_BIT_2GHZ )
  1674. net80211_add_channels ( dev, 1, 14,
  1675. NET80211_REG_TXPOWER );
  1676. if ( band & NET80211_BAND_BIT_5GHZ )
  1677. net80211_add_channels ( dev, 36, 8,
  1678. NET80211_REG_TXPOWER );
  1679. }
  1680. net80211_filter_hw_channels ( dev );
  1681. /* Use channel 1 for now */
  1682. dev->channel = 0;
  1683. dev->op->config ( dev, NET80211_CFG_CHANNEL );
  1684. /* Always do active probes at lowest (presumably first) speed */
  1685. dev->rate = 0;
  1686. dev->nr_rates = 1;
  1687. dev->rates[0] = dev->hw->rates[dev->channels[0].band][0];
  1688. dev->op->config ( dev, NET80211_CFG_RATE );
  1689. return 0;
  1690. }
  1691. /**
  1692. * Prepare 802.11 device channel and rate set for communication
  1693. *
  1694. * @v dev 802.11 device
  1695. * @v wlan WLAN to prepare for communication with
  1696. * @ret rc Return status code
  1697. */
  1698. int net80211_prepare_assoc ( struct net80211_device *dev,
  1699. struct net80211_wlan *wlan )
  1700. {
  1701. struct ieee80211_frame *hdr = wlan->beacon->data;
  1702. struct ieee80211_beacon *beacon =
  1703. ( struct ieee80211_beacon * ) hdr->data;
  1704. int rc;
  1705. assert ( dev->netdev->state & NETDEV_OPEN );
  1706. net80211_set_state ( dev, NET80211_ASSOCIATED, 0, 0 );
  1707. memcpy ( dev->bssid, wlan->bssid, ETH_ALEN );
  1708. strcpy ( dev->essid, wlan->essid );
  1709. dev->last_beacon_timestamp = beacon->timestamp;
  1710. dev->tx_beacon_interval = 1024 * beacon->beacon_interval;
  1711. /* XXX do crypto setup here */
  1712. /* Barring an IE that tells us the channel outright, assume
  1713. the channel we heard this AP best on is the channel it's
  1714. communicating on. */
  1715. net80211_change_channel ( dev, wlan->channel );
  1716. rc = net80211_process_capab ( dev, beacon->capability );
  1717. if ( rc )
  1718. return rc;
  1719. rc = net80211_process_ie ( dev, beacon->info_element,
  1720. wlan->beacon->tail );
  1721. if ( rc )
  1722. return rc;
  1723. /* Associate at the lowest rate so we know it'll get through */
  1724. dev->rate = 0;
  1725. dev->op->config ( dev, NET80211_CFG_RATE );
  1726. return 0;
  1727. }
  1728. /**
  1729. * Send 802.11 initial authentication frame
  1730. *
  1731. * @v dev 802.11 device
  1732. * @v wlan WLAN to authenticate with
  1733. * @v method Authentication method
  1734. * @ret rc Return status code
  1735. *
  1736. * @a method may be 0 for Open System authentication or 1 for Shared
  1737. * Key authentication. Open System provides no security in association
  1738. * whatsoever, relying on encryption for confidentiality, but Shared
  1739. * Key actively introduces security problems and is very rarely used.
  1740. */
  1741. int net80211_send_auth ( struct net80211_device *dev,
  1742. struct net80211_wlan *wlan, int method )
  1743. {
  1744. struct io_buffer *iob = alloc_iob ( 64 );
  1745. struct ieee80211_auth *auth;
  1746. net80211_set_state ( dev, 0, NET80211_WAITING, 0 );
  1747. iob_reserve ( iob, IEEE80211_TYP_FRAME_HEADER_LEN );
  1748. auth = iob_put ( iob, sizeof ( *auth ) );
  1749. auth->algorithm = method;
  1750. auth->tx_seq = 1;
  1751. auth->status = 0;
  1752. return net80211_tx_mgmt ( dev, IEEE80211_STYPE_AUTH, wlan->bssid, iob );
  1753. }
  1754. /**
  1755. * Handle receipt of 802.11 authentication frame
  1756. *
  1757. * @v dev 802.11 device
  1758. * @v iob I/O buffer
  1759. *
  1760. * If the authentication method being used is Shared Key, and the
  1761. * frame that was received included challenge text, the frame is
  1762. * encrypted using the cryptographic algorithm currently in effect and
  1763. * sent back to the AP to complete the authentication.
  1764. */
  1765. static void net80211_handle_auth ( struct net80211_device *dev,
  1766. struct io_buffer *iob )
  1767. {
  1768. struct ieee80211_frame *hdr = iob->data;
  1769. struct ieee80211_auth *auth =
  1770. ( struct ieee80211_auth * ) hdr->data;
  1771. if ( auth->tx_seq & 1 ) {
  1772. DBGC ( dev, "802.11 %p authentication received improperly "
  1773. "directed frame (seq. %d)\n", dev, auth->tx_seq );
  1774. net80211_set_state ( dev, NET80211_WAITING, 0,
  1775. IEEE80211_STATUS_FAILURE );
  1776. return;
  1777. }
  1778. if ( auth->status != IEEE80211_STATUS_SUCCESS ) {
  1779. DBGC ( dev, "802.11 %p authentication failed: status %d\n",
  1780. dev, auth->status );
  1781. net80211_set_state ( dev, NET80211_WAITING, 0,
  1782. auth->status );
  1783. return;
  1784. }
  1785. if ( auth->algorithm == IEEE80211_AUTH_SHARED_KEY && ! dev->crypto ) {
  1786. DBGC ( dev, "802.11 %p can't perform shared-key authentication "
  1787. "without a cryptosystem\n", dev );
  1788. net80211_set_state ( dev, NET80211_WAITING, 0,
  1789. IEEE80211_STATUS_FAILURE );
  1790. return;
  1791. }
  1792. if ( auth->algorithm == IEEE80211_AUTH_SHARED_KEY &&
  1793. auth->tx_seq == 2 ) {
  1794. /* Since the iob we got is going to be freed as soon
  1795. as we return, we can do some in-place
  1796. modification. */
  1797. auth->tx_seq = 3;
  1798. auth->status = 0;
  1799. memcpy ( hdr->addr2, hdr->addr1, ETH_ALEN );
  1800. memcpy ( hdr->addr1, hdr->addr3, ETH_ALEN );
  1801. netdev_tx ( dev->netdev,
  1802. dev->crypto->encrypt ( dev->crypto, iob ) );
  1803. return;
  1804. }
  1805. net80211_set_state ( dev, NET80211_WAITING, NET80211_AUTHENTICATED,
  1806. IEEE80211_STATUS_SUCCESS );
  1807. return;
  1808. }
  1809. /**
  1810. * Send 802.11 association frame
  1811. *
  1812. * @v dev 802.11 device
  1813. * @v wlan WLAN to associate with
  1814. * @ret rc Return status code
  1815. */
  1816. int net80211_send_assoc ( struct net80211_device *dev,
  1817. struct net80211_wlan *wlan )
  1818. {
  1819. struct io_buffer *iob = alloc_iob ( 128 );
  1820. struct ieee80211_assoc_req *assoc;
  1821. union ieee80211_ie *ie;
  1822. net80211_set_state ( dev, 0, NET80211_WAITING, 0 );
  1823. iob_reserve ( iob, IEEE80211_TYP_FRAME_HEADER_LEN );
  1824. assoc = iob->data;
  1825. assoc->capability = IEEE80211_CAPAB_MANAGED;
  1826. if ( ! ( dev->hw->flags & NET80211_HW_NO_SHORT_PREAMBLE ) )
  1827. assoc->capability |= IEEE80211_CAPAB_SHORT_PMBL;
  1828. if ( ! ( dev->hw->flags & NET80211_HW_NO_SHORT_SLOT ) )
  1829. assoc->capability |= IEEE80211_CAPAB_SHORT_SLOT;
  1830. if ( wlan->crypto )
  1831. assoc->capability |= IEEE80211_CAPAB_PRIVACY;
  1832. assoc->listen_interval = 1;
  1833. ie = net80211_marshal_request_info ( dev, assoc->info_element );
  1834. DBGP ( "802.11 %p about to send association request:\n", dev );
  1835. DBGP_HD ( iob->data, ( void * ) ie - iob->data );
  1836. /* XXX add RSN ie for WPA support */
  1837. iob_put ( iob, ( void * ) ie - iob->data );
  1838. return net80211_tx_mgmt ( dev, IEEE80211_STYPE_ASSOC_REQ,
  1839. wlan->bssid, iob );
  1840. }
  1841. /**
  1842. * Handle receipt of 802.11 association reply frame
  1843. *
  1844. * @v dev 802.11 device
  1845. * @v iob I/O buffer
  1846. */
  1847. static void net80211_handle_assoc_reply ( struct net80211_device *dev,
  1848. struct io_buffer *iob )
  1849. {
  1850. struct ieee80211_frame *hdr = iob->data;
  1851. struct ieee80211_assoc_resp *assoc =
  1852. ( struct ieee80211_assoc_resp * ) hdr->data;
  1853. net80211_process_capab ( dev, assoc->capability );
  1854. net80211_process_ie ( dev, assoc->info_element, iob->tail );
  1855. if ( assoc->status != IEEE80211_STATUS_SUCCESS ) {
  1856. DBGC ( dev, "802.11 %p association failed: status %d\n",
  1857. dev, assoc->status );
  1858. net80211_set_state ( dev, NET80211_WAITING, 0,
  1859. assoc->status );
  1860. return;
  1861. }
  1862. /* ESSID was filled before the association request was sent */
  1863. memcpy ( dev->bssid, hdr->addr3, ETH_ALEN );
  1864. dev->aid = assoc->aid;
  1865. net80211_set_state ( dev, NET80211_WAITING, NET80211_ASSOCIATED,
  1866. IEEE80211_STATUS_SUCCESS );
  1867. }
  1868. /**
  1869. * Send 802.11 disassociation frame
  1870. *
  1871. * @v dev 802.11 device
  1872. * @v reason Reason for disassociation
  1873. * @ret rc Return status code
  1874. */
  1875. static int net80211_send_disassoc ( struct net80211_device *dev, int reason )
  1876. {
  1877. struct io_buffer *iob = alloc_iob ( 64 );
  1878. struct ieee80211_disassoc *disassoc;
  1879. if ( ! ( dev->state & NET80211_ASSOCIATED ) )
  1880. return -EINVAL;
  1881. net80211_set_state ( dev, NET80211_ASSOCIATED, 0, 0 );
  1882. iob_reserve ( iob, IEEE80211_TYP_FRAME_HEADER_LEN );
  1883. disassoc = iob_put ( iob, sizeof ( *disassoc ) );
  1884. disassoc->reason = reason;
  1885. return net80211_tx_mgmt ( dev, IEEE80211_STYPE_DISASSOC, dev->bssid,
  1886. iob );
  1887. }
  1888. /** Smoothing factor (1-7) for link quality calculation */
  1889. #define LQ_SMOOTH 7
  1890. /**
  1891. * Update link quality information based on received beacon
  1892. *
  1893. * @v dev 802.11 device
  1894. * @v iob I/O buffer containing beacon
  1895. * @ret rc Return status code
  1896. */
  1897. static void net80211_update_link_quality ( struct net80211_device *dev,
  1898. struct io_buffer *iob )
  1899. {
  1900. struct ieee80211_frame *hdr = iob->data;
  1901. struct ieee80211_beacon *beacon;
  1902. u32 dt, rxi;
  1903. if ( ! ( dev->state & NET80211_ASSOCIATED ) )
  1904. return;
  1905. beacon = ( struct ieee80211_beacon * ) hdr->data;
  1906. dt = ( u32 ) ( beacon->timestamp - dev->last_beacon_timestamp );
  1907. rxi = dev->rx_beacon_interval;
  1908. rxi = ( LQ_SMOOTH * rxi ) + ( ( 8 - LQ_SMOOTH ) * dt );
  1909. dev->rx_beacon_interval = rxi >> 3;
  1910. dev->last_beacon_timestamp = beacon->timestamp;
  1911. }
  1912. /**
  1913. * Handle receipt of 802.11 management frame
  1914. *
  1915. * @v dev 802.11 device
  1916. * @v iob I/O buffer
  1917. * @v signal Signal strength of received frame
  1918. */
  1919. static void net80211_handle_mgmt ( struct net80211_device *dev,
  1920. struct io_buffer *iob, int signal )
  1921. {
  1922. struct ieee80211_frame *hdr = iob->data;
  1923. struct ieee80211_disassoc *disassoc;
  1924. u16 stype = hdr->fc & IEEE80211_FC_SUBTYPE;
  1925. int keep = 0;
  1926. int is_deauth = ( stype == IEEE80211_STYPE_DEAUTH );
  1927. if ( ( hdr->fc & IEEE80211_FC_TYPE ) != IEEE80211_TYPE_MGMT ) {
  1928. free_iob ( iob );
  1929. return; /* only handle management frames */
  1930. }
  1931. switch ( stype ) {
  1932. /* We reconnect on deauthentication and disassociation. */
  1933. case IEEE80211_STYPE_DEAUTH:
  1934. case IEEE80211_STYPE_DISASSOC:
  1935. disassoc = ( struct ieee80211_disassoc * ) hdr->data;
  1936. net80211_set_state ( dev, is_deauth ? NET80211_AUTHENTICATED :
  1937. NET80211_ASSOCIATED, 0,
  1938. NET80211_IS_REASON | disassoc->reason );
  1939. DBGC ( dev, "802.11 %p %s: reason %d\n",
  1940. dev, is_deauth ? "deauthenticated" : "disassociated",
  1941. disassoc->reason );
  1942. /* Try to reassociate, in case it's transient. */
  1943. net80211_autoassociate ( dev );
  1944. break;
  1945. /* We handle authentication and association. */
  1946. case IEEE80211_STYPE_AUTH:
  1947. if ( ! ( dev->state & NET80211_AUTHENTICATED ) )
  1948. net80211_handle_auth ( dev, iob );
  1949. break;
  1950. case IEEE80211_STYPE_ASSOC_RESP:
  1951. case IEEE80211_STYPE_REASSOC_RESP:
  1952. if ( ! ( dev->state & NET80211_ASSOCIATED ) )
  1953. net80211_handle_assoc_reply ( dev, iob );
  1954. break;
  1955. /* We pass probes and beacons onto network scanning
  1956. code. Pass actions for future extensibility. */
  1957. case IEEE80211_STYPE_BEACON:
  1958. net80211_update_link_quality ( dev, iob );
  1959. /* fall through */
  1960. case IEEE80211_STYPE_PROBE_RESP:
  1961. case IEEE80211_STYPE_ACTION:
  1962. if ( dev->keep_mgmt ) {
  1963. struct net80211_rx_info *rxinf;
  1964. rxinf = zalloc ( sizeof ( *rxinf ) );
  1965. if ( ! rxinf ) {
  1966. DBGC ( dev, "802.11 %p out of memory\n", dev );
  1967. break;
  1968. }
  1969. rxinf->signal = signal;
  1970. list_add_tail ( &iob->list, &dev->mgmt_queue );
  1971. list_add_tail ( &rxinf->list, &dev->mgmt_info_queue );
  1972. keep = 1;
  1973. }
  1974. break;
  1975. case IEEE80211_STYPE_PROBE_REQ:
  1976. /* Some nodes send these broadcast. Ignore them. */
  1977. break;
  1978. case IEEE80211_STYPE_ASSOC_REQ:
  1979. case IEEE80211_STYPE_REASSOC_REQ:
  1980. /* We should never receive these, only send them. */
  1981. DBGC ( dev, "802.11 %p received strange management request "
  1982. "(%04x)\n", dev, stype );
  1983. break;
  1984. default:
  1985. DBGC ( dev, "802.11 %p received unimplemented management "
  1986. "packet (%04x)\n", dev, stype );
  1987. break;
  1988. }
  1989. if ( ! keep )
  1990. free_iob ( iob );
  1991. }
  1992. /* ---------- Packet handling functions ---------- */
  1993. /**
  1994. * Free buffers used by 802.11 fragment cache entry
  1995. *
  1996. * @v dev 802.11 device
  1997. * @v fcid Fragment cache entry index
  1998. *
  1999. * After this function, the referenced entry will be marked unused.
  2000. */
  2001. static void net80211_free_frags ( struct net80211_device *dev, int fcid )
  2002. {
  2003. int j;
  2004. struct net80211_frag_cache *frag = &dev->frags[fcid];
  2005. for ( j = 0; j < 16; j++ ) {
  2006. if ( frag->iob[j] ) {
  2007. free_iob ( frag->iob[j] );
  2008. frag->iob[j] = NULL;
  2009. }
  2010. }
  2011. frag->seqnr = 0;
  2012. frag->start_ticks = 0;
  2013. frag->in_use = 0;
  2014. }
  2015. /**
  2016. * Accumulate 802.11 fragments into one I/O buffer
  2017. *
  2018. * @v dev 802.11 device
  2019. * @v fcid Fragment cache entry index
  2020. * @v nfrags Number of fragments received
  2021. * @v size Sum of sizes of all fragments, including headers
  2022. * @ret iob I/O buffer containing reassembled packet
  2023. *
  2024. * This function does not free the fragment buffers.
  2025. */
  2026. static struct io_buffer *net80211_accum_frags ( struct net80211_device *dev,
  2027. int fcid, int nfrags, int size )
  2028. {
  2029. struct net80211_frag_cache *frag = &dev->frags[fcid];
  2030. int hdrsize = IEEE80211_TYP_FRAME_HEADER_LEN;
  2031. int nsize = size - hdrsize * ( nfrags - 1 );
  2032. int i;
  2033. struct io_buffer *niob = alloc_iob ( nsize );
  2034. struct ieee80211_frame *hdr;
  2035. /* Add the header from the first one... */
  2036. memcpy ( iob_put ( niob, hdrsize ), frag->iob[0]->data, hdrsize );
  2037. /* ... and all the data from all of them. */
  2038. for ( i = 0; i < nfrags; i++ ) {
  2039. int len = iob_len ( frag->iob[i] ) - hdrsize;
  2040. memcpy ( iob_put ( niob, len ),
  2041. frag->iob[i]->data + hdrsize, len );
  2042. }
  2043. /* Turn off the fragment bit. */
  2044. hdr = niob->data;
  2045. hdr->fc &= ~IEEE80211_FC_MORE_FRAG;
  2046. return niob;
  2047. }
  2048. /**
  2049. * Handle receipt of 802.11 fragment
  2050. *
  2051. * @v dev 802.11 device
  2052. * @v iob I/O buffer containing fragment
  2053. * @v signal Signal strength with which fragment was received
  2054. */
  2055. static void net80211_rx_frag ( struct net80211_device *dev,
  2056. struct io_buffer *iob, int signal )
  2057. {
  2058. struct ieee80211_frame *hdr = iob->data;
  2059. int fragnr = IEEE80211_FRAG ( hdr->seq );
  2060. if ( fragnr == 0 && ( hdr->fc & IEEE80211_FC_MORE_FRAG ) ) {
  2061. /* start a frag cache entry */
  2062. int i, newest = -1;
  2063. u32 curr_ticks = currticks(), newest_ticks = 0;
  2064. u32 timeout = ticks_per_sec() * NET80211_FRAG_TIMEOUT;
  2065. for ( i = 0; i < NET80211_NR_CONCURRENT_FRAGS; i++ ) {
  2066. if ( dev->frags[i].in_use == 0 )
  2067. break;
  2068. if ( dev->frags[i].start_ticks + timeout >=
  2069. curr_ticks ) {
  2070. net80211_free_frags ( dev, i );
  2071. break;
  2072. }
  2073. if ( dev->frags[i].start_ticks > newest_ticks ) {
  2074. newest = i;
  2075. newest_ticks = dev->frags[i].start_ticks;
  2076. }
  2077. }
  2078. /* If we're being sent more concurrent fragmented
  2079. packets than we can handle, drop the newest so the
  2080. older ones have time to complete. */
  2081. if ( i == NET80211_NR_CONCURRENT_FRAGS ) {
  2082. i = newest;
  2083. net80211_free_frags ( dev, i );
  2084. }
  2085. dev->frags[i].in_use = 1;
  2086. dev->frags[i].seqnr = IEEE80211_SEQNR ( hdr->seq );
  2087. dev->frags[i].start_ticks = currticks();
  2088. dev->frags[i].iob[0] = iob;
  2089. return;
  2090. } else {
  2091. int i;
  2092. for ( i = 0; i < NET80211_NR_CONCURRENT_FRAGS; i++ ) {
  2093. if ( dev->frags[i].in_use && dev->frags[i].seqnr ==
  2094. IEEE80211_SEQNR ( hdr->seq ) )
  2095. break;
  2096. }
  2097. if ( i == NET80211_NR_CONCURRENT_FRAGS ) {
  2098. /* Drop non-first not-in-cache fragments */
  2099. DBGC ( dev, "802.11 %p dropped fragment fc=%04x "
  2100. "seq=%04x\n", dev, hdr->fc, hdr->seq );
  2101. free_iob ( iob );
  2102. return;
  2103. }
  2104. dev->frags[i].iob[fragnr] = iob;
  2105. if ( ! ( hdr->fc & IEEE80211_FC_MORE_FRAG ) ) {
  2106. int j, size = 0;
  2107. for ( j = 0; j < fragnr; j++ ) {
  2108. size += iob_len ( dev->frags[i].iob[j] );
  2109. if ( dev->frags[i].iob[j] == NULL )
  2110. break;
  2111. }
  2112. if ( j == fragnr ) {
  2113. /* We've got everything */
  2114. struct io_buffer *niob =
  2115. net80211_accum_frags ( dev, i, fragnr,
  2116. size );
  2117. net80211_free_frags ( dev, i );
  2118. net80211_rx ( dev, niob, signal, 0 );
  2119. } else {
  2120. DBGC ( dev, "802.11 %p dropping fragmented "
  2121. "packet due to out-of-order arrival, "
  2122. "fc=%04x seq=%04x\n", dev, hdr->fc,
  2123. hdr->seq );
  2124. net80211_free_frags ( dev, i );
  2125. }
  2126. }
  2127. }
  2128. }
  2129. /**
  2130. * Handle receipt of 802.11 frame
  2131. *
  2132. * @v dev 802.11 device
  2133. * @v iob I/O buffer
  2134. * @v signal Received signal strength
  2135. * @v rate Bitrate at which frame was received, in 100 kbps units
  2136. *
  2137. * If the rate or signal is unknown, 0 should be passed.
  2138. */
  2139. void net80211_rx ( struct net80211_device *dev, struct io_buffer *iob,
  2140. int signal, u16 rate )
  2141. {
  2142. struct ieee80211_frame *hdr = iob->data;
  2143. u16 type = hdr->fc & IEEE80211_FC_TYPE;
  2144. if ( ( hdr->fc & IEEE80211_FC_VERSION ) != IEEE80211_THIS_VERSION )
  2145. goto drop; /* drop invalid-version packets */
  2146. if ( type == IEEE80211_TYPE_CTRL )
  2147. goto drop; /* we don't handle control packets,
  2148. the hardware does */
  2149. if ( dev->last_rx_seq == hdr->seq )
  2150. goto drop; /* avoid duplicate packet */
  2151. dev->last_rx_seq = hdr->seq;
  2152. if ( dev->hw->flags & NET80211_HW_RX_HAS_FCS ) {
  2153. /* discard the FCS */
  2154. iob_unput ( iob, 4 );
  2155. }
  2156. if ( hdr->fc & IEEE80211_FC_PROTECTED ) {
  2157. struct io_buffer *niob;
  2158. if ( ! dev->crypto )
  2159. goto drop; /* can't decrypt packets on an open network */
  2160. niob = dev->crypto->decrypt ( dev->crypto, iob );
  2161. if ( ! niob )
  2162. goto drop; /* drop failed decryption */
  2163. free_iob ( iob );
  2164. iob = niob;
  2165. }
  2166. dev->last_signal = signal;
  2167. /* Fragments go into the frag cache or get dropped. */
  2168. if ( IEEE80211_FRAG ( hdr->seq ) != 0
  2169. || ( hdr->fc & IEEE80211_FC_MORE_FRAG ) ) {
  2170. net80211_rx_frag ( dev, iob, signal );
  2171. return;
  2172. }
  2173. /* Management frames get handled, enqueued, or dropped. */
  2174. if ( type == IEEE80211_TYPE_MGMT ) {
  2175. net80211_handle_mgmt ( dev, iob, signal );
  2176. return;
  2177. }
  2178. /* Data frames get dropped or sent to the net_device. */
  2179. if ( ( hdr->fc & IEEE80211_FC_SUBTYPE ) != IEEE80211_STYPE_DATA )
  2180. goto drop; /* drop QoS, CFP, or null data packets */
  2181. /* Update rate-control algorithm */
  2182. if ( dev->rctl )
  2183. rc80211_update_rx ( dev, hdr->fc & IEEE80211_FC_RETRY, rate );
  2184. /* Pass packet onward */
  2185. if ( netdev_link_ok ( dev->netdev ) ) {
  2186. netdev_rx ( dev->netdev, iob );
  2187. return;
  2188. }
  2189. drop:
  2190. DBGC2 ( dev, "802.11 %p dropped packet fc=%04x seq=%04x\n", dev,
  2191. hdr->fc, hdr->seq );
  2192. free_iob ( iob );
  2193. return;
  2194. }
  2195. /** Indicate an error in receiving a packet
  2196. *
  2197. * @v dev 802.11 device
  2198. * @v iob I/O buffer with received packet, or NULL
  2199. * @v rc Error code
  2200. *
  2201. * This logs the error with the wrapping net_device, and frees iob if
  2202. * it is passed.
  2203. */
  2204. void net80211_rx_err ( struct net80211_device *dev,
  2205. struct io_buffer *iob, int rc )
  2206. {
  2207. netdev_rx_err ( dev->netdev, iob, rc );
  2208. }
  2209. /** Indicate the completed transmission of a packet
  2210. *
  2211. * @v dev 802.11 device
  2212. * @v iob I/O buffer of transmitted packet
  2213. * @v retries Number of times this packet was retransmitted
  2214. * @v rc Error code, or 0 for success
  2215. *
  2216. * This logs an error with the wrapping net_device if one occurred,
  2217. * and removes and frees the I/O buffer from its TX queue. The
  2218. * provided retry information is used to tune our transmission rate.
  2219. *
  2220. * If the packet did not need to be retransmitted because it was
  2221. * properly ACKed the first time, @a retries should be 0.
  2222. */
  2223. void net80211_tx_complete ( struct net80211_device *dev,
  2224. struct io_buffer *iob, int retries, int rc )
  2225. {
  2226. /* Update rate-control algorithm */
  2227. if ( dev->rctl )
  2228. rc80211_update_tx ( dev, retries, rc );
  2229. /* Pass completion onward */
  2230. netdev_tx_complete_err ( dev->netdev, iob, rc );
  2231. }