mac80211_hwsim.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531
  1. /*
  2. * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211
  3. * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2011, Javier Lopez <jlopex@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. /*
  11. * TODO:
  12. * - Add TSF sync and fix IBSS beacon transmission by adding
  13. * competition for "air time" at TBTT
  14. * - RX filtering based on filter configuration (data->rx_filter)
  15. */
  16. #include <linux/list.h>
  17. #include <linux/slab.h>
  18. #include <linux/spinlock.h>
  19. #include <net/dst.h>
  20. #include <net/xfrm.h>
  21. #include <net/mac80211.h>
  22. #include <net/ieee80211_radiotap.h>
  23. #include <linux/if_arp.h>
  24. #include <linux/rtnetlink.h>
  25. #include <linux/etherdevice.h>
  26. #include <linux/debugfs.h>
  27. #include <linux/module.h>
  28. #include <linux/ktime.h>
  29. #include <net/genetlink.h>
  30. #include "mac80211_hwsim.h"
  31. #define WARN_QUEUE 100
  32. #define MAX_QUEUE 200
  33. MODULE_AUTHOR("Jouni Malinen");
  34. MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
  35. MODULE_LICENSE("GPL");
  36. static u32 wmediumd_portid;
  37. static int radios = 2;
  38. module_param(radios, int, 0444);
  39. MODULE_PARM_DESC(radios, "Number of simulated radios");
  40. static int channels = 1;
  41. module_param(channels, int, 0444);
  42. MODULE_PARM_DESC(channels, "Number of concurrent channels");
  43. static bool paged_rx = false;
  44. module_param(paged_rx, bool, 0644);
  45. MODULE_PARM_DESC(paged_rx, "Use paged SKBs for RX instead of linear ones");
  46. /**
  47. * enum hwsim_regtest - the type of regulatory tests we offer
  48. *
  49. * These are the different values you can use for the regtest
  50. * module parameter. This is useful to help test world roaming
  51. * and the driver regulatory_hint() call and combinations of these.
  52. * If you want to do specific alpha2 regulatory domain tests simply
  53. * use the userspace regulatory request as that will be respected as
  54. * well without the need of this module parameter. This is designed
  55. * only for testing the driver regulatory request, world roaming
  56. * and all possible combinations.
  57. *
  58. * @HWSIM_REGTEST_DISABLED: No regulatory tests are performed,
  59. * this is the default value.
  60. * @HWSIM_REGTEST_DRIVER_REG_FOLLOW: Used for testing the driver regulatory
  61. * hint, only one driver regulatory hint will be sent as such the
  62. * secondary radios are expected to follow.
  63. * @HWSIM_REGTEST_DRIVER_REG_ALL: Used for testing the driver regulatory
  64. * request with all radios reporting the same regulatory domain.
  65. * @HWSIM_REGTEST_DIFF_COUNTRY: Used for testing the drivers calling
  66. * different regulatory domains requests. Expected behaviour is for
  67. * an intersection to occur but each device will still use their
  68. * respective regulatory requested domains. Subsequent radios will
  69. * use the resulting intersection.
  70. * @HWSIM_REGTEST_WORLD_ROAM: Used for testing the world roaming. We accomplish
  71. * this by using a custom beacon-capable regulatory domain for the first
  72. * radio. All other device world roam.
  73. * @HWSIM_REGTEST_CUSTOM_WORLD: Used for testing the custom world regulatory
  74. * domain requests. All radios will adhere to this custom world regulatory
  75. * domain.
  76. * @HWSIM_REGTEST_CUSTOM_WORLD_2: Used for testing 2 custom world regulatory
  77. * domain requests. The first radio will adhere to the first custom world
  78. * regulatory domain, the second one to the second custom world regulatory
  79. * domain. All other devices will world roam.
  80. * @HWSIM_REGTEST_STRICT_FOLLOW_: Used for testing strict regulatory domain
  81. * settings, only the first radio will send a regulatory domain request
  82. * and use strict settings. The rest of the radios are expected to follow.
  83. * @HWSIM_REGTEST_STRICT_ALL: Used for testing strict regulatory domain
  84. * settings. All radios will adhere to this.
  85. * @HWSIM_REGTEST_STRICT_AND_DRIVER_REG: Used for testing strict regulatory
  86. * domain settings, combined with secondary driver regulatory domain
  87. * settings. The first radio will get a strict regulatory domain setting
  88. * using the first driver regulatory request and the second radio will use
  89. * non-strict settings using the second driver regulatory request. All
  90. * other devices should follow the intersection created between the
  91. * first two.
  92. * @HWSIM_REGTEST_ALL: Used for testing every possible mix. You will need
  93. * at least 6 radios for a complete test. We will test in this order:
  94. * 1 - driver custom world regulatory domain
  95. * 2 - second custom world regulatory domain
  96. * 3 - first driver regulatory domain request
  97. * 4 - second driver regulatory domain request
  98. * 5 - strict regulatory domain settings using the third driver regulatory
  99. * domain request
  100. * 6 and on - should follow the intersection of the 3rd, 4rth and 5th radio
  101. * regulatory requests.
  102. */
  103. enum hwsim_regtest {
  104. HWSIM_REGTEST_DISABLED = 0,
  105. HWSIM_REGTEST_DRIVER_REG_FOLLOW = 1,
  106. HWSIM_REGTEST_DRIVER_REG_ALL = 2,
  107. HWSIM_REGTEST_DIFF_COUNTRY = 3,
  108. HWSIM_REGTEST_WORLD_ROAM = 4,
  109. HWSIM_REGTEST_CUSTOM_WORLD = 5,
  110. HWSIM_REGTEST_CUSTOM_WORLD_2 = 6,
  111. HWSIM_REGTEST_STRICT_FOLLOW = 7,
  112. HWSIM_REGTEST_STRICT_ALL = 8,
  113. HWSIM_REGTEST_STRICT_AND_DRIVER_REG = 9,
  114. HWSIM_REGTEST_ALL = 10,
  115. };
  116. /* Set to one of the HWSIM_REGTEST_* values above */
  117. static int regtest = HWSIM_REGTEST_DISABLED;
  118. module_param(regtest, int, 0444);
  119. MODULE_PARM_DESC(regtest, "The type of regulatory test we want to run");
  120. static const char *hwsim_alpha2s[] = {
  121. "FI",
  122. "AL",
  123. "US",
  124. "DE",
  125. "JP",
  126. "AL",
  127. };
  128. static const struct ieee80211_regdomain hwsim_world_regdom_custom_01 = {
  129. .n_reg_rules = 4,
  130. .alpha2 = "99",
  131. .reg_rules = {
  132. REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
  133. REG_RULE(2484-10, 2484+10, 40, 0, 20, 0),
  134. REG_RULE(5150-10, 5240+10, 40, 0, 30, 0),
  135. REG_RULE(5745-10, 5825+10, 40, 0, 30, 0),
  136. }
  137. };
  138. static const struct ieee80211_regdomain hwsim_world_regdom_custom_02 = {
  139. .n_reg_rules = 2,
  140. .alpha2 = "99",
  141. .reg_rules = {
  142. REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
  143. REG_RULE(5725-10, 5850+10, 40, 0, 30,
  144. NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_IBSS),
  145. }
  146. };
  147. struct hwsim_vif_priv {
  148. u32 magic;
  149. u8 bssid[ETH_ALEN];
  150. bool assoc;
  151. u16 aid;
  152. };
  153. #define HWSIM_VIF_MAGIC 0x69537748
  154. static inline void hwsim_check_magic(struct ieee80211_vif *vif)
  155. {
  156. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  157. WARN(vp->magic != HWSIM_VIF_MAGIC,
  158. "Invalid VIF (%p) magic %#x, %pM, %d/%d\n",
  159. vif, vp->magic, vif->addr, vif->type, vif->p2p);
  160. }
  161. static inline void hwsim_set_magic(struct ieee80211_vif *vif)
  162. {
  163. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  164. vp->magic = HWSIM_VIF_MAGIC;
  165. }
  166. static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
  167. {
  168. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  169. vp->magic = 0;
  170. }
  171. struct hwsim_sta_priv {
  172. u32 magic;
  173. };
  174. #define HWSIM_STA_MAGIC 0x6d537749
  175. static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
  176. {
  177. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  178. WARN_ON(sp->magic != HWSIM_STA_MAGIC);
  179. }
  180. static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
  181. {
  182. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  183. sp->magic = HWSIM_STA_MAGIC;
  184. }
  185. static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
  186. {
  187. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  188. sp->magic = 0;
  189. }
  190. struct hwsim_chanctx_priv {
  191. u32 magic;
  192. };
  193. #define HWSIM_CHANCTX_MAGIC 0x6d53774a
  194. static inline void hwsim_check_chanctx_magic(struct ieee80211_chanctx_conf *c)
  195. {
  196. struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
  197. WARN_ON(cp->magic != HWSIM_CHANCTX_MAGIC);
  198. }
  199. static inline void hwsim_set_chanctx_magic(struct ieee80211_chanctx_conf *c)
  200. {
  201. struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
  202. cp->magic = HWSIM_CHANCTX_MAGIC;
  203. }
  204. static inline void hwsim_clear_chanctx_magic(struct ieee80211_chanctx_conf *c)
  205. {
  206. struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
  207. cp->magic = 0;
  208. }
  209. static struct class *hwsim_class;
  210. static struct net_device *hwsim_mon; /* global monitor netdev */
  211. #define CHAN2G(_freq) { \
  212. .band = IEEE80211_BAND_2GHZ, \
  213. .center_freq = (_freq), \
  214. .hw_value = (_freq), \
  215. .max_power = 20, \
  216. }
  217. #define CHAN5G(_freq) { \
  218. .band = IEEE80211_BAND_5GHZ, \
  219. .center_freq = (_freq), \
  220. .hw_value = (_freq), \
  221. .max_power = 20, \
  222. }
  223. static const struct ieee80211_channel hwsim_channels_2ghz[] = {
  224. CHAN2G(2412), /* Channel 1 */
  225. CHAN2G(2417), /* Channel 2 */
  226. CHAN2G(2422), /* Channel 3 */
  227. CHAN2G(2427), /* Channel 4 */
  228. CHAN2G(2432), /* Channel 5 */
  229. CHAN2G(2437), /* Channel 6 */
  230. CHAN2G(2442), /* Channel 7 */
  231. CHAN2G(2447), /* Channel 8 */
  232. CHAN2G(2452), /* Channel 9 */
  233. CHAN2G(2457), /* Channel 10 */
  234. CHAN2G(2462), /* Channel 11 */
  235. CHAN2G(2467), /* Channel 12 */
  236. CHAN2G(2472), /* Channel 13 */
  237. CHAN2G(2484), /* Channel 14 */
  238. };
  239. static const struct ieee80211_channel hwsim_channels_5ghz[] = {
  240. CHAN5G(5180), /* Channel 36 */
  241. CHAN5G(5200), /* Channel 40 */
  242. CHAN5G(5220), /* Channel 44 */
  243. CHAN5G(5240), /* Channel 48 */
  244. CHAN5G(5260), /* Channel 52 */
  245. CHAN5G(5280), /* Channel 56 */
  246. CHAN5G(5300), /* Channel 60 */
  247. CHAN5G(5320), /* Channel 64 */
  248. CHAN5G(5500), /* Channel 100 */
  249. CHAN5G(5520), /* Channel 104 */
  250. CHAN5G(5540), /* Channel 108 */
  251. CHAN5G(5560), /* Channel 112 */
  252. CHAN5G(5580), /* Channel 116 */
  253. CHAN5G(5600), /* Channel 120 */
  254. CHAN5G(5620), /* Channel 124 */
  255. CHAN5G(5640), /* Channel 128 */
  256. CHAN5G(5660), /* Channel 132 */
  257. CHAN5G(5680), /* Channel 136 */
  258. CHAN5G(5700), /* Channel 140 */
  259. CHAN5G(5745), /* Channel 149 */
  260. CHAN5G(5765), /* Channel 153 */
  261. CHAN5G(5785), /* Channel 157 */
  262. CHAN5G(5805), /* Channel 161 */
  263. CHAN5G(5825), /* Channel 165 */
  264. };
  265. static const struct ieee80211_rate hwsim_rates[] = {
  266. { .bitrate = 10 },
  267. { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  268. { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  269. { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  270. { .bitrate = 60 },
  271. { .bitrate = 90 },
  272. { .bitrate = 120 },
  273. { .bitrate = 180 },
  274. { .bitrate = 240 },
  275. { .bitrate = 360 },
  276. { .bitrate = 480 },
  277. { .bitrate = 540 }
  278. };
  279. static spinlock_t hwsim_radio_lock;
  280. static struct list_head hwsim_radios;
  281. struct mac80211_hwsim_data {
  282. struct list_head list;
  283. struct ieee80211_hw *hw;
  284. struct device *dev;
  285. struct ieee80211_supported_band bands[IEEE80211_NUM_BANDS];
  286. struct ieee80211_channel channels_2ghz[ARRAY_SIZE(hwsim_channels_2ghz)];
  287. struct ieee80211_channel channels_5ghz[ARRAY_SIZE(hwsim_channels_5ghz)];
  288. struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
  289. struct mac_address addresses[2];
  290. struct ieee80211_channel *tmp_chan;
  291. struct delayed_work roc_done;
  292. struct delayed_work hw_scan;
  293. struct cfg80211_scan_request *hw_scan_request;
  294. struct ieee80211_vif *hw_scan_vif;
  295. int scan_chan_idx;
  296. struct ieee80211_channel *channel;
  297. u64 beacon_int /* beacon interval in us */;
  298. unsigned int rx_filter;
  299. bool started, idle, scanning;
  300. struct mutex mutex;
  301. struct tasklet_hrtimer beacon_timer;
  302. enum ps_mode {
  303. PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
  304. } ps;
  305. bool ps_poll_pending;
  306. struct dentry *debugfs;
  307. struct dentry *debugfs_ps;
  308. struct sk_buff_head pending; /* packets pending */
  309. /*
  310. * Only radios in the same group can communicate together (the
  311. * channel has to match too). Each bit represents a group. A
  312. * radio can be in more then one group.
  313. */
  314. u64 group;
  315. struct dentry *debugfs_group;
  316. int power_level;
  317. /* difference between this hw's clock and the real clock, in usecs */
  318. s64 tsf_offset;
  319. s64 bcn_delta;
  320. /* absolute beacon transmission time. Used to cover up "tx" delay. */
  321. u64 abs_bcn_ts;
  322. };
  323. struct hwsim_radiotap_hdr {
  324. struct ieee80211_radiotap_header hdr;
  325. __le64 rt_tsft;
  326. u8 rt_flags;
  327. u8 rt_rate;
  328. __le16 rt_channel;
  329. __le16 rt_chbitmask;
  330. } __packed;
  331. /* MAC80211_HWSIM netlinf family */
  332. static struct genl_family hwsim_genl_family = {
  333. .id = GENL_ID_GENERATE,
  334. .hdrsize = 0,
  335. .name = "MAC80211_HWSIM",
  336. .version = 1,
  337. .maxattr = HWSIM_ATTR_MAX,
  338. };
  339. /* MAC80211_HWSIM netlink policy */
  340. static struct nla_policy hwsim_genl_policy[HWSIM_ATTR_MAX + 1] = {
  341. [HWSIM_ATTR_ADDR_RECEIVER] = { .type = NLA_UNSPEC,
  342. .len = 6*sizeof(u8) },
  343. [HWSIM_ATTR_ADDR_TRANSMITTER] = { .type = NLA_UNSPEC,
  344. .len = 6*sizeof(u8) },
  345. [HWSIM_ATTR_FRAME] = { .type = NLA_BINARY,
  346. .len = IEEE80211_MAX_DATA_LEN },
  347. [HWSIM_ATTR_FLAGS] = { .type = NLA_U32 },
  348. [HWSIM_ATTR_RX_RATE] = { .type = NLA_U32 },
  349. [HWSIM_ATTR_SIGNAL] = { .type = NLA_U32 },
  350. [HWSIM_ATTR_TX_INFO] = { .type = NLA_UNSPEC,
  351. .len = IEEE80211_TX_MAX_RATES*sizeof(
  352. struct hwsim_tx_rate)},
  353. [HWSIM_ATTR_COOKIE] = { .type = NLA_U64 },
  354. };
  355. static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
  356. struct net_device *dev)
  357. {
  358. /* TODO: allow packet injection */
  359. dev_kfree_skb(skb);
  360. return NETDEV_TX_OK;
  361. }
  362. static inline u64 mac80211_hwsim_get_tsf_raw(void)
  363. {
  364. return ktime_to_us(ktime_get_real());
  365. }
  366. static __le64 __mac80211_hwsim_get_tsf(struct mac80211_hwsim_data *data)
  367. {
  368. u64 now = mac80211_hwsim_get_tsf_raw();
  369. return cpu_to_le64(now + data->tsf_offset);
  370. }
  371. static u64 mac80211_hwsim_get_tsf(struct ieee80211_hw *hw,
  372. struct ieee80211_vif *vif)
  373. {
  374. struct mac80211_hwsim_data *data = hw->priv;
  375. return le64_to_cpu(__mac80211_hwsim_get_tsf(data));
  376. }
  377. static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw,
  378. struct ieee80211_vif *vif, u64 tsf)
  379. {
  380. struct mac80211_hwsim_data *data = hw->priv;
  381. u64 now = mac80211_hwsim_get_tsf(hw, vif);
  382. u32 bcn_int = data->beacon_int;
  383. s64 delta = tsf - now;
  384. data->tsf_offset += delta;
  385. /* adjust after beaconing with new timestamp at old TBTT */
  386. data->bcn_delta = do_div(delta, bcn_int);
  387. }
  388. static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
  389. struct sk_buff *tx_skb,
  390. struct ieee80211_channel *chan)
  391. {
  392. struct mac80211_hwsim_data *data = hw->priv;
  393. struct sk_buff *skb;
  394. struct hwsim_radiotap_hdr *hdr;
  395. u16 flags;
  396. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
  397. struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
  398. if (!netif_running(hwsim_mon))
  399. return;
  400. skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
  401. if (skb == NULL)
  402. return;
  403. hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr));
  404. hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  405. hdr->hdr.it_pad = 0;
  406. hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
  407. hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  408. (1 << IEEE80211_RADIOTAP_RATE) |
  409. (1 << IEEE80211_RADIOTAP_TSFT) |
  410. (1 << IEEE80211_RADIOTAP_CHANNEL));
  411. hdr->rt_tsft = __mac80211_hwsim_get_tsf(data);
  412. hdr->rt_flags = 0;
  413. hdr->rt_rate = txrate->bitrate / 5;
  414. hdr->rt_channel = cpu_to_le16(chan->center_freq);
  415. flags = IEEE80211_CHAN_2GHZ;
  416. if (txrate->flags & IEEE80211_RATE_ERP_G)
  417. flags |= IEEE80211_CHAN_OFDM;
  418. else
  419. flags |= IEEE80211_CHAN_CCK;
  420. hdr->rt_chbitmask = cpu_to_le16(flags);
  421. skb->dev = hwsim_mon;
  422. skb_set_mac_header(skb, 0);
  423. skb->ip_summed = CHECKSUM_UNNECESSARY;
  424. skb->pkt_type = PACKET_OTHERHOST;
  425. skb->protocol = htons(ETH_P_802_2);
  426. memset(skb->cb, 0, sizeof(skb->cb));
  427. netif_rx(skb);
  428. }
  429. static void mac80211_hwsim_monitor_ack(struct ieee80211_channel *chan,
  430. const u8 *addr)
  431. {
  432. struct sk_buff *skb;
  433. struct hwsim_radiotap_hdr *hdr;
  434. u16 flags;
  435. struct ieee80211_hdr *hdr11;
  436. if (!netif_running(hwsim_mon))
  437. return;
  438. skb = dev_alloc_skb(100);
  439. if (skb == NULL)
  440. return;
  441. hdr = (struct hwsim_radiotap_hdr *) skb_put(skb, sizeof(*hdr));
  442. hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  443. hdr->hdr.it_pad = 0;
  444. hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
  445. hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  446. (1 << IEEE80211_RADIOTAP_CHANNEL));
  447. hdr->rt_flags = 0;
  448. hdr->rt_rate = 0;
  449. hdr->rt_channel = cpu_to_le16(chan->center_freq);
  450. flags = IEEE80211_CHAN_2GHZ;
  451. hdr->rt_chbitmask = cpu_to_le16(flags);
  452. hdr11 = (struct ieee80211_hdr *) skb_put(skb, 10);
  453. hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  454. IEEE80211_STYPE_ACK);
  455. hdr11->duration_id = cpu_to_le16(0);
  456. memcpy(hdr11->addr1, addr, ETH_ALEN);
  457. skb->dev = hwsim_mon;
  458. skb_set_mac_header(skb, 0);
  459. skb->ip_summed = CHECKSUM_UNNECESSARY;
  460. skb->pkt_type = PACKET_OTHERHOST;
  461. skb->protocol = htons(ETH_P_802_2);
  462. memset(skb->cb, 0, sizeof(skb->cb));
  463. netif_rx(skb);
  464. }
  465. static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
  466. struct sk_buff *skb)
  467. {
  468. switch (data->ps) {
  469. case PS_DISABLED:
  470. return true;
  471. case PS_ENABLED:
  472. return false;
  473. case PS_AUTO_POLL:
  474. /* TODO: accept (some) Beacons by default and other frames only
  475. * if pending PS-Poll has been sent */
  476. return true;
  477. case PS_MANUAL_POLL:
  478. /* Allow unicast frames to own address if there is a pending
  479. * PS-Poll */
  480. if (data->ps_poll_pending &&
  481. memcmp(data->hw->wiphy->perm_addr, skb->data + 4,
  482. ETH_ALEN) == 0) {
  483. data->ps_poll_pending = false;
  484. return true;
  485. }
  486. return false;
  487. }
  488. return true;
  489. }
  490. struct mac80211_hwsim_addr_match_data {
  491. bool ret;
  492. const u8 *addr;
  493. };
  494. static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
  495. struct ieee80211_vif *vif)
  496. {
  497. struct mac80211_hwsim_addr_match_data *md = data;
  498. if (memcmp(mac, md->addr, ETH_ALEN) == 0)
  499. md->ret = true;
  500. }
  501. static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
  502. const u8 *addr)
  503. {
  504. struct mac80211_hwsim_addr_match_data md;
  505. if (memcmp(addr, data->hw->wiphy->perm_addr, ETH_ALEN) == 0)
  506. return true;
  507. md.ret = false;
  508. md.addr = addr;
  509. ieee80211_iterate_active_interfaces_atomic(data->hw,
  510. IEEE80211_IFACE_ITER_NORMAL,
  511. mac80211_hwsim_addr_iter,
  512. &md);
  513. return md.ret;
  514. }
  515. static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
  516. struct sk_buff *my_skb,
  517. int dst_portid)
  518. {
  519. struct sk_buff *skb;
  520. struct mac80211_hwsim_data *data = hw->priv;
  521. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data;
  522. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb);
  523. void *msg_head;
  524. unsigned int hwsim_flags = 0;
  525. int i;
  526. struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES];
  527. if (data->ps != PS_DISABLED)
  528. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  529. /* If the queue contains MAX_QUEUE skb's drop some */
  530. if (skb_queue_len(&data->pending) >= MAX_QUEUE) {
  531. /* Droping until WARN_QUEUE level */
  532. while (skb_queue_len(&data->pending) >= WARN_QUEUE)
  533. skb_dequeue(&data->pending);
  534. }
  535. skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  536. if (skb == NULL)
  537. goto nla_put_failure;
  538. msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
  539. HWSIM_CMD_FRAME);
  540. if (msg_head == NULL) {
  541. printk(KERN_DEBUG "mac80211_hwsim: problem with msg_head\n");
  542. goto nla_put_failure;
  543. }
  544. if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
  545. sizeof(struct mac_address), data->addresses[1].addr))
  546. goto nla_put_failure;
  547. /* We get the skb->data */
  548. if (nla_put(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data))
  549. goto nla_put_failure;
  550. /* We get the flags for this transmission, and we translate them to
  551. wmediumd flags */
  552. if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
  553. hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS;
  554. if (info->flags & IEEE80211_TX_CTL_NO_ACK)
  555. hwsim_flags |= HWSIM_TX_CTL_NO_ACK;
  556. if (nla_put_u32(skb, HWSIM_ATTR_FLAGS, hwsim_flags))
  557. goto nla_put_failure;
  558. /* We get the tx control (rate and retries) info*/
  559. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  560. tx_attempts[i].idx = info->status.rates[i].idx;
  561. tx_attempts[i].count = info->status.rates[i].count;
  562. }
  563. if (nla_put(skb, HWSIM_ATTR_TX_INFO,
  564. sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES,
  565. tx_attempts))
  566. goto nla_put_failure;
  567. /* We create a cookie to identify this skb */
  568. if (nla_put_u64(skb, HWSIM_ATTR_COOKIE, (unsigned long) my_skb))
  569. goto nla_put_failure;
  570. genlmsg_end(skb, msg_head);
  571. genlmsg_unicast(&init_net, skb, dst_portid);
  572. /* Enqueue the packet */
  573. skb_queue_tail(&data->pending, my_skb);
  574. return;
  575. nla_put_failure:
  576. printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
  577. }
  578. static bool hwsim_chans_compat(struct ieee80211_channel *c1,
  579. struct ieee80211_channel *c2)
  580. {
  581. if (!c1 || !c2)
  582. return false;
  583. return c1->center_freq == c2->center_freq;
  584. }
  585. struct tx_iter_data {
  586. struct ieee80211_channel *channel;
  587. bool receive;
  588. };
  589. static void mac80211_hwsim_tx_iter(void *_data, u8 *addr,
  590. struct ieee80211_vif *vif)
  591. {
  592. struct tx_iter_data *data = _data;
  593. if (!vif->chanctx_conf)
  594. return;
  595. if (!hwsim_chans_compat(data->channel,
  596. rcu_dereference(vif->chanctx_conf)->def.chan))
  597. return;
  598. data->receive = true;
  599. }
  600. static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw,
  601. struct sk_buff *skb,
  602. struct ieee80211_channel *chan)
  603. {
  604. struct mac80211_hwsim_data *data = hw->priv, *data2;
  605. bool ack = false;
  606. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  607. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  608. struct ieee80211_rx_status rx_status;
  609. u64 now;
  610. memset(&rx_status, 0, sizeof(rx_status));
  611. rx_status.flag |= RX_FLAG_MACTIME_START;
  612. rx_status.freq = chan->center_freq;
  613. rx_status.band = chan->band;
  614. rx_status.rate_idx = info->control.rates[0].idx;
  615. if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
  616. rx_status.flag |= RX_FLAG_HT;
  617. if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  618. rx_status.flag |= RX_FLAG_40MHZ;
  619. if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
  620. rx_status.flag |= RX_FLAG_SHORT_GI;
  621. /* TODO: simulate real signal strength (and optional packet loss) */
  622. rx_status.signal = data->power_level - 50;
  623. if (data->ps != PS_DISABLED)
  624. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  625. /* release the skb's source info */
  626. skb_orphan(skb);
  627. skb_dst_drop(skb);
  628. skb->mark = 0;
  629. secpath_reset(skb);
  630. nf_reset(skb);
  631. /*
  632. * Get absolute mactime here so all HWs RX at the "same time", and
  633. * absolute TX time for beacon mactime so the timestamp matches.
  634. * Giving beacons a different mactime than non-beacons looks messy, but
  635. * it helps the Toffset be exact and a ~10us mactime discrepancy
  636. * probably doesn't really matter.
  637. */
  638. if (ieee80211_is_beacon(hdr->frame_control) ||
  639. ieee80211_is_probe_resp(hdr->frame_control))
  640. now = data->abs_bcn_ts;
  641. else
  642. now = mac80211_hwsim_get_tsf_raw();
  643. /* Copy skb to all enabled radios that are on the current frequency */
  644. spin_lock(&hwsim_radio_lock);
  645. list_for_each_entry(data2, &hwsim_radios, list) {
  646. struct sk_buff *nskb;
  647. struct tx_iter_data tx_iter_data = {
  648. .receive = false,
  649. .channel = chan,
  650. };
  651. if (data == data2)
  652. continue;
  653. if (!data2->started || (data2->idle && !data2->tmp_chan) ||
  654. !hwsim_ps_rx_ok(data2, skb))
  655. continue;
  656. if (!(data->group & data2->group))
  657. continue;
  658. if (!hwsim_chans_compat(chan, data2->tmp_chan) &&
  659. !hwsim_chans_compat(chan, data2->channel)) {
  660. ieee80211_iterate_active_interfaces_atomic(
  661. data2->hw, IEEE80211_IFACE_ITER_NORMAL,
  662. mac80211_hwsim_tx_iter, &tx_iter_data);
  663. if (!tx_iter_data.receive)
  664. continue;
  665. }
  666. /*
  667. * reserve some space for our vendor and the normal
  668. * radiotap header, since we're copying anyway
  669. */
  670. if (skb->len < PAGE_SIZE && paged_rx) {
  671. struct page *page = alloc_page(GFP_ATOMIC);
  672. if (!page)
  673. continue;
  674. nskb = dev_alloc_skb(128);
  675. if (!nskb) {
  676. __free_page(page);
  677. continue;
  678. }
  679. memcpy(page_address(page), skb->data, skb->len);
  680. skb_add_rx_frag(nskb, 0, page, 0, skb->len, skb->len);
  681. } else {
  682. nskb = skb_copy(skb, GFP_ATOMIC);
  683. if (!nskb)
  684. continue;
  685. }
  686. if (mac80211_hwsim_addr_match(data2, hdr->addr1))
  687. ack = true;
  688. rx_status.mactime = now + data2->tsf_offset;
  689. #if 0
  690. /*
  691. * Don't enable this code by default as the OUI 00:00:00
  692. * is registered to Xerox so we shouldn't use it here, it
  693. * might find its way into pcap files.
  694. * Note that this code requires the headroom in the SKB
  695. * that was allocated earlier.
  696. */
  697. rx_status.vendor_radiotap_oui[0] = 0x00;
  698. rx_status.vendor_radiotap_oui[1] = 0x00;
  699. rx_status.vendor_radiotap_oui[2] = 0x00;
  700. rx_status.vendor_radiotap_subns = 127;
  701. /*
  702. * Radiotap vendor namespaces can (and should) also be
  703. * split into fields by using the standard radiotap
  704. * presence bitmap mechanism. Use just BIT(0) here for
  705. * the presence bitmap.
  706. */
  707. rx_status.vendor_radiotap_bitmap = BIT(0);
  708. /* We have 8 bytes of (dummy) data */
  709. rx_status.vendor_radiotap_len = 8;
  710. /* For testing, also require it to be aligned */
  711. rx_status.vendor_radiotap_align = 8;
  712. /* push the data */
  713. memcpy(skb_push(nskb, 8), "ABCDEFGH", 8);
  714. #endif
  715. memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
  716. ieee80211_rx_irqsafe(data2->hw, nskb);
  717. }
  718. spin_unlock(&hwsim_radio_lock);
  719. return ack;
  720. }
  721. static void mac80211_hwsim_tx(struct ieee80211_hw *hw,
  722. struct ieee80211_tx_control *control,
  723. struct sk_buff *skb)
  724. {
  725. struct mac80211_hwsim_data *data = hw->priv;
  726. struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
  727. struct ieee80211_chanctx_conf *chanctx_conf;
  728. struct ieee80211_channel *channel;
  729. bool ack;
  730. u32 _portid;
  731. if (WARN_ON(skb->len < 10)) {
  732. /* Should not happen; just a sanity check for addr1 use */
  733. dev_kfree_skb(skb);
  734. return;
  735. }
  736. if (channels == 1) {
  737. channel = data->channel;
  738. } else if (txi->hw_queue == 4) {
  739. channel = data->tmp_chan;
  740. } else {
  741. chanctx_conf = rcu_dereference(txi->control.vif->chanctx_conf);
  742. if (chanctx_conf)
  743. channel = chanctx_conf->def.chan;
  744. else
  745. channel = NULL;
  746. }
  747. if (WARN(!channel, "TX w/o channel - queue = %d\n", txi->hw_queue)) {
  748. dev_kfree_skb(skb);
  749. return;
  750. }
  751. if (data->idle && !data->tmp_chan) {
  752. wiphy_debug(hw->wiphy, "Trying to TX when idle - reject\n");
  753. dev_kfree_skb(skb);
  754. return;
  755. }
  756. if (txi->control.vif)
  757. hwsim_check_magic(txi->control.vif);
  758. if (control->sta)
  759. hwsim_check_sta_magic(control->sta);
  760. txi->rate_driver_data[0] = channel;
  761. mac80211_hwsim_monitor_rx(hw, skb, channel);
  762. /* wmediumd mode check */
  763. _portid = ACCESS_ONCE(wmediumd_portid);
  764. if (_portid)
  765. return mac80211_hwsim_tx_frame_nl(hw, skb, _portid);
  766. /* NO wmediumd detected, perfect medium simulation */
  767. ack = mac80211_hwsim_tx_frame_no_nl(hw, skb, channel);
  768. if (ack && skb->len >= 16) {
  769. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  770. mac80211_hwsim_monitor_ack(channel, hdr->addr2);
  771. }
  772. ieee80211_tx_info_clear_status(txi);
  773. /* frame was transmitted at most favorable rate at first attempt */
  774. txi->control.rates[0].count = 1;
  775. txi->control.rates[1].idx = -1;
  776. if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
  777. txi->flags |= IEEE80211_TX_STAT_ACK;
  778. ieee80211_tx_status_irqsafe(hw, skb);
  779. }
  780. static int mac80211_hwsim_start(struct ieee80211_hw *hw)
  781. {
  782. struct mac80211_hwsim_data *data = hw->priv;
  783. wiphy_debug(hw->wiphy, "%s\n", __func__);
  784. data->started = true;
  785. return 0;
  786. }
  787. static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
  788. {
  789. struct mac80211_hwsim_data *data = hw->priv;
  790. data->started = false;
  791. tasklet_hrtimer_cancel(&data->beacon_timer);
  792. wiphy_debug(hw->wiphy, "%s\n", __func__);
  793. }
  794. static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
  795. struct ieee80211_vif *vif)
  796. {
  797. wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
  798. __func__, ieee80211_vif_type_p2p(vif),
  799. vif->addr);
  800. hwsim_set_magic(vif);
  801. vif->cab_queue = 0;
  802. vif->hw_queue[IEEE80211_AC_VO] = 0;
  803. vif->hw_queue[IEEE80211_AC_VI] = 1;
  804. vif->hw_queue[IEEE80211_AC_BE] = 2;
  805. vif->hw_queue[IEEE80211_AC_BK] = 3;
  806. return 0;
  807. }
  808. static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
  809. struct ieee80211_vif *vif,
  810. enum nl80211_iftype newtype,
  811. bool newp2p)
  812. {
  813. newtype = ieee80211_iftype_p2p(newtype, newp2p);
  814. wiphy_debug(hw->wiphy,
  815. "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
  816. __func__, ieee80211_vif_type_p2p(vif),
  817. newtype, vif->addr);
  818. hwsim_check_magic(vif);
  819. /*
  820. * interface may change from non-AP to AP in
  821. * which case this needs to be set up again
  822. */
  823. vif->cab_queue = 0;
  824. return 0;
  825. }
  826. static void mac80211_hwsim_remove_interface(
  827. struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  828. {
  829. wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
  830. __func__, ieee80211_vif_type_p2p(vif),
  831. vif->addr);
  832. hwsim_check_magic(vif);
  833. hwsim_clear_magic(vif);
  834. }
  835. static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
  836. struct sk_buff *skb,
  837. struct ieee80211_channel *chan)
  838. {
  839. u32 _pid = ACCESS_ONCE(wmediumd_portid);
  840. mac80211_hwsim_monitor_rx(hw, skb, chan);
  841. if (_pid)
  842. return mac80211_hwsim_tx_frame_nl(hw, skb, _pid);
  843. mac80211_hwsim_tx_frame_no_nl(hw, skb, chan);
  844. dev_kfree_skb(skb);
  845. }
  846. static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
  847. struct ieee80211_vif *vif)
  848. {
  849. struct mac80211_hwsim_data *data = arg;
  850. struct ieee80211_hw *hw = data->hw;
  851. struct ieee80211_tx_info *info;
  852. struct ieee80211_rate *txrate;
  853. struct ieee80211_mgmt *mgmt;
  854. struct sk_buff *skb;
  855. hwsim_check_magic(vif);
  856. if (vif->type != NL80211_IFTYPE_AP &&
  857. vif->type != NL80211_IFTYPE_MESH_POINT &&
  858. vif->type != NL80211_IFTYPE_ADHOC)
  859. return;
  860. skb = ieee80211_beacon_get(hw, vif);
  861. if (skb == NULL)
  862. return;
  863. info = IEEE80211_SKB_CB(skb);
  864. txrate = ieee80211_get_tx_rate(hw, info);
  865. mgmt = (struct ieee80211_mgmt *) skb->data;
  866. /* fake header transmission time */
  867. data->abs_bcn_ts = mac80211_hwsim_get_tsf_raw();
  868. mgmt->u.beacon.timestamp = cpu_to_le64(data->abs_bcn_ts +
  869. data->tsf_offset +
  870. 24 * 8 * 10 / txrate->bitrate);
  871. mac80211_hwsim_tx_frame(hw, skb,
  872. rcu_dereference(vif->chanctx_conf)->def.chan);
  873. }
  874. static enum hrtimer_restart
  875. mac80211_hwsim_beacon(struct hrtimer *timer)
  876. {
  877. struct mac80211_hwsim_data *data =
  878. container_of(timer, struct mac80211_hwsim_data,
  879. beacon_timer.timer);
  880. struct ieee80211_hw *hw = data->hw;
  881. u64 bcn_int = data->beacon_int;
  882. ktime_t next_bcn;
  883. if (!data->started)
  884. goto out;
  885. ieee80211_iterate_active_interfaces_atomic(
  886. hw, IEEE80211_IFACE_ITER_NORMAL,
  887. mac80211_hwsim_beacon_tx, data);
  888. /* beacon at new TBTT + beacon interval */
  889. if (data->bcn_delta) {
  890. bcn_int -= data->bcn_delta;
  891. data->bcn_delta = 0;
  892. }
  893. next_bcn = ktime_add(hrtimer_get_expires(timer),
  894. ns_to_ktime(bcn_int * 1000));
  895. tasklet_hrtimer_start(&data->beacon_timer, next_bcn, HRTIMER_MODE_ABS);
  896. out:
  897. return HRTIMER_NORESTART;
  898. }
  899. static const char * const hwsim_chanwidths[] = {
  900. [NL80211_CHAN_WIDTH_20_NOHT] = "noht",
  901. [NL80211_CHAN_WIDTH_20] = "ht20",
  902. [NL80211_CHAN_WIDTH_40] = "ht40",
  903. [NL80211_CHAN_WIDTH_80] = "vht80",
  904. [NL80211_CHAN_WIDTH_80P80] = "vht80p80",
  905. [NL80211_CHAN_WIDTH_160] = "vht160",
  906. };
  907. static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
  908. {
  909. struct mac80211_hwsim_data *data = hw->priv;
  910. struct ieee80211_conf *conf = &hw->conf;
  911. static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
  912. [IEEE80211_SMPS_AUTOMATIC] = "auto",
  913. [IEEE80211_SMPS_OFF] = "off",
  914. [IEEE80211_SMPS_STATIC] = "static",
  915. [IEEE80211_SMPS_DYNAMIC] = "dynamic",
  916. };
  917. if (conf->chandef.chan)
  918. wiphy_debug(hw->wiphy,
  919. "%s (freq=%d(%d - %d)/%s idle=%d ps=%d smps=%s)\n",
  920. __func__,
  921. conf->chandef.chan->center_freq,
  922. conf->chandef.center_freq1,
  923. conf->chandef.center_freq2,
  924. hwsim_chanwidths[conf->chandef.width],
  925. !!(conf->flags & IEEE80211_CONF_IDLE),
  926. !!(conf->flags & IEEE80211_CONF_PS),
  927. smps_modes[conf->smps_mode]);
  928. else
  929. wiphy_debug(hw->wiphy,
  930. "%s (freq=0 idle=%d ps=%d smps=%s)\n",
  931. __func__,
  932. !!(conf->flags & IEEE80211_CONF_IDLE),
  933. !!(conf->flags & IEEE80211_CONF_PS),
  934. smps_modes[conf->smps_mode]);
  935. data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
  936. data->channel = conf->chandef.chan;
  937. WARN_ON(data->channel && channels > 1);
  938. data->power_level = conf->power_level;
  939. if (!data->started || !data->beacon_int)
  940. tasklet_hrtimer_cancel(&data->beacon_timer);
  941. else if (!hrtimer_is_queued(&data->beacon_timer.timer)) {
  942. u64 tsf = mac80211_hwsim_get_tsf(hw, NULL);
  943. u32 bcn_int = data->beacon_int;
  944. u64 until_tbtt = bcn_int - do_div(tsf, bcn_int);
  945. tasklet_hrtimer_start(&data->beacon_timer,
  946. ns_to_ktime(until_tbtt * 1000),
  947. HRTIMER_MODE_REL);
  948. }
  949. return 0;
  950. }
  951. static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
  952. unsigned int changed_flags,
  953. unsigned int *total_flags,u64 multicast)
  954. {
  955. struct mac80211_hwsim_data *data = hw->priv;
  956. wiphy_debug(hw->wiphy, "%s\n", __func__);
  957. data->rx_filter = 0;
  958. if (*total_flags & FIF_PROMISC_IN_BSS)
  959. data->rx_filter |= FIF_PROMISC_IN_BSS;
  960. if (*total_flags & FIF_ALLMULTI)
  961. data->rx_filter |= FIF_ALLMULTI;
  962. *total_flags = data->rx_filter;
  963. }
  964. static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
  965. struct ieee80211_vif *vif,
  966. struct ieee80211_bss_conf *info,
  967. u32 changed)
  968. {
  969. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  970. struct mac80211_hwsim_data *data = hw->priv;
  971. hwsim_check_magic(vif);
  972. wiphy_debug(hw->wiphy, "%s(changed=0x%x)\n", __func__, changed);
  973. if (changed & BSS_CHANGED_BSSID) {
  974. wiphy_debug(hw->wiphy, "%s: BSSID changed: %pM\n",
  975. __func__, info->bssid);
  976. memcpy(vp->bssid, info->bssid, ETH_ALEN);
  977. }
  978. if (changed & BSS_CHANGED_ASSOC) {
  979. wiphy_debug(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
  980. info->assoc, info->aid);
  981. vp->assoc = info->assoc;
  982. vp->aid = info->aid;
  983. }
  984. if (changed & BSS_CHANGED_BEACON_INT) {
  985. wiphy_debug(hw->wiphy, " BCNINT: %d\n", info->beacon_int);
  986. data->beacon_int = info->beacon_int * 1024;
  987. }
  988. if (changed & BSS_CHANGED_BEACON_ENABLED) {
  989. wiphy_debug(hw->wiphy, " BCN EN: %d\n", info->enable_beacon);
  990. if (data->started &&
  991. !hrtimer_is_queued(&data->beacon_timer.timer) &&
  992. info->enable_beacon) {
  993. u64 tsf, until_tbtt;
  994. u32 bcn_int;
  995. if (WARN_ON(!data->beacon_int))
  996. data->beacon_int = 1000 * 1024;
  997. tsf = mac80211_hwsim_get_tsf(hw, vif);
  998. bcn_int = data->beacon_int;
  999. until_tbtt = bcn_int - do_div(tsf, bcn_int);
  1000. tasklet_hrtimer_start(&data->beacon_timer,
  1001. ns_to_ktime(until_tbtt * 1000),
  1002. HRTIMER_MODE_REL);
  1003. } else if (!info->enable_beacon)
  1004. tasklet_hrtimer_cancel(&data->beacon_timer);
  1005. }
  1006. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  1007. wiphy_debug(hw->wiphy, " ERP_CTS_PROT: %d\n",
  1008. info->use_cts_prot);
  1009. }
  1010. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  1011. wiphy_debug(hw->wiphy, " ERP_PREAMBLE: %d\n",
  1012. info->use_short_preamble);
  1013. }
  1014. if (changed & BSS_CHANGED_ERP_SLOT) {
  1015. wiphy_debug(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
  1016. }
  1017. if (changed & BSS_CHANGED_HT) {
  1018. wiphy_debug(hw->wiphy, " HT: op_mode=0x%x\n",
  1019. info->ht_operation_mode);
  1020. }
  1021. if (changed & BSS_CHANGED_BASIC_RATES) {
  1022. wiphy_debug(hw->wiphy, " BASIC_RATES: 0x%llx\n",
  1023. (unsigned long long) info->basic_rates);
  1024. }
  1025. if (changed & BSS_CHANGED_TXPOWER)
  1026. wiphy_debug(hw->wiphy, " TX Power: %d dBm\n", info->txpower);
  1027. }
  1028. static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
  1029. struct ieee80211_vif *vif,
  1030. struct ieee80211_sta *sta)
  1031. {
  1032. hwsim_check_magic(vif);
  1033. hwsim_set_sta_magic(sta);
  1034. return 0;
  1035. }
  1036. static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
  1037. struct ieee80211_vif *vif,
  1038. struct ieee80211_sta *sta)
  1039. {
  1040. hwsim_check_magic(vif);
  1041. hwsim_clear_sta_magic(sta);
  1042. return 0;
  1043. }
  1044. static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
  1045. struct ieee80211_vif *vif,
  1046. enum sta_notify_cmd cmd,
  1047. struct ieee80211_sta *sta)
  1048. {
  1049. hwsim_check_magic(vif);
  1050. switch (cmd) {
  1051. case STA_NOTIFY_SLEEP:
  1052. case STA_NOTIFY_AWAKE:
  1053. /* TODO: make good use of these flags */
  1054. break;
  1055. default:
  1056. WARN(1, "Invalid sta notify: %d\n", cmd);
  1057. break;
  1058. }
  1059. }
  1060. static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
  1061. struct ieee80211_sta *sta,
  1062. bool set)
  1063. {
  1064. hwsim_check_sta_magic(sta);
  1065. return 0;
  1066. }
  1067. static int mac80211_hwsim_conf_tx(
  1068. struct ieee80211_hw *hw,
  1069. struct ieee80211_vif *vif, u16 queue,
  1070. const struct ieee80211_tx_queue_params *params)
  1071. {
  1072. wiphy_debug(hw->wiphy,
  1073. "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
  1074. __func__, queue,
  1075. params->txop, params->cw_min,
  1076. params->cw_max, params->aifs);
  1077. return 0;
  1078. }
  1079. static int mac80211_hwsim_get_survey(
  1080. struct ieee80211_hw *hw, int idx,
  1081. struct survey_info *survey)
  1082. {
  1083. struct ieee80211_conf *conf = &hw->conf;
  1084. wiphy_debug(hw->wiphy, "%s (idx=%d)\n", __func__, idx);
  1085. if (idx != 0)
  1086. return -ENOENT;
  1087. /* Current channel */
  1088. survey->channel = conf->chandef.chan;
  1089. /*
  1090. * Magically conjured noise level --- this is only ok for simulated hardware.
  1091. *
  1092. * A real driver which cannot determine the real channel noise MUST NOT
  1093. * report any noise, especially not a magically conjured one :-)
  1094. */
  1095. survey->filled = SURVEY_INFO_NOISE_DBM;
  1096. survey->noise = -92;
  1097. return 0;
  1098. }
  1099. #ifdef CONFIG_NL80211_TESTMODE
  1100. /*
  1101. * This section contains example code for using netlink
  1102. * attributes with the testmode command in nl80211.
  1103. */
  1104. /* These enums need to be kept in sync with userspace */
  1105. enum hwsim_testmode_attr {
  1106. __HWSIM_TM_ATTR_INVALID = 0,
  1107. HWSIM_TM_ATTR_CMD = 1,
  1108. HWSIM_TM_ATTR_PS = 2,
  1109. /* keep last */
  1110. __HWSIM_TM_ATTR_AFTER_LAST,
  1111. HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
  1112. };
  1113. enum hwsim_testmode_cmd {
  1114. HWSIM_TM_CMD_SET_PS = 0,
  1115. HWSIM_TM_CMD_GET_PS = 1,
  1116. HWSIM_TM_CMD_STOP_QUEUES = 2,
  1117. HWSIM_TM_CMD_WAKE_QUEUES = 3,
  1118. };
  1119. static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
  1120. [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
  1121. [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
  1122. };
  1123. static int hwsim_fops_ps_write(void *dat, u64 val);
  1124. static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
  1125. void *data, int len)
  1126. {
  1127. struct mac80211_hwsim_data *hwsim = hw->priv;
  1128. struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
  1129. struct sk_buff *skb;
  1130. int err, ps;
  1131. err = nla_parse(tb, HWSIM_TM_ATTR_MAX, data, len,
  1132. hwsim_testmode_policy);
  1133. if (err)
  1134. return err;
  1135. if (!tb[HWSIM_TM_ATTR_CMD])
  1136. return -EINVAL;
  1137. switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
  1138. case HWSIM_TM_CMD_SET_PS:
  1139. if (!tb[HWSIM_TM_ATTR_PS])
  1140. return -EINVAL;
  1141. ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
  1142. return hwsim_fops_ps_write(hwsim, ps);
  1143. case HWSIM_TM_CMD_GET_PS:
  1144. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
  1145. nla_total_size(sizeof(u32)));
  1146. if (!skb)
  1147. return -ENOMEM;
  1148. if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
  1149. goto nla_put_failure;
  1150. return cfg80211_testmode_reply(skb);
  1151. case HWSIM_TM_CMD_STOP_QUEUES:
  1152. ieee80211_stop_queues(hw);
  1153. return 0;
  1154. case HWSIM_TM_CMD_WAKE_QUEUES:
  1155. ieee80211_wake_queues(hw);
  1156. return 0;
  1157. default:
  1158. return -EOPNOTSUPP;
  1159. }
  1160. nla_put_failure:
  1161. kfree_skb(skb);
  1162. return -ENOBUFS;
  1163. }
  1164. #endif
  1165. static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
  1166. struct ieee80211_vif *vif,
  1167. enum ieee80211_ampdu_mlme_action action,
  1168. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  1169. u8 buf_size)
  1170. {
  1171. switch (action) {
  1172. case IEEE80211_AMPDU_TX_START:
  1173. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1174. break;
  1175. case IEEE80211_AMPDU_TX_STOP_CONT:
  1176. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  1177. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  1178. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1179. break;
  1180. case IEEE80211_AMPDU_TX_OPERATIONAL:
  1181. break;
  1182. case IEEE80211_AMPDU_RX_START:
  1183. case IEEE80211_AMPDU_RX_STOP:
  1184. break;
  1185. default:
  1186. return -EOPNOTSUPP;
  1187. }
  1188. return 0;
  1189. }
  1190. static void mac80211_hwsim_flush(struct ieee80211_hw *hw, u32 queues, bool drop)
  1191. {
  1192. /* Not implemented, queues only on kernel side */
  1193. }
  1194. static void hw_scan_work(struct work_struct *work)
  1195. {
  1196. struct mac80211_hwsim_data *hwsim =
  1197. container_of(work, struct mac80211_hwsim_data, hw_scan.work);
  1198. struct cfg80211_scan_request *req = hwsim->hw_scan_request;
  1199. int dwell, i;
  1200. mutex_lock(&hwsim->mutex);
  1201. if (hwsim->scan_chan_idx >= req->n_channels) {
  1202. wiphy_debug(hwsim->hw->wiphy, "hw scan complete\n");
  1203. ieee80211_scan_completed(hwsim->hw, false);
  1204. hwsim->hw_scan_request = NULL;
  1205. hwsim->hw_scan_vif = NULL;
  1206. hwsim->tmp_chan = NULL;
  1207. mutex_unlock(&hwsim->mutex);
  1208. return;
  1209. }
  1210. wiphy_debug(hwsim->hw->wiphy, "hw scan %d MHz\n",
  1211. req->channels[hwsim->scan_chan_idx]->center_freq);
  1212. hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx];
  1213. if (hwsim->tmp_chan->flags & IEEE80211_CHAN_PASSIVE_SCAN ||
  1214. !req->n_ssids) {
  1215. dwell = 120;
  1216. } else {
  1217. dwell = 30;
  1218. /* send probes */
  1219. for (i = 0; i < req->n_ssids; i++) {
  1220. struct sk_buff *probe;
  1221. probe = ieee80211_probereq_get(hwsim->hw,
  1222. hwsim->hw_scan_vif,
  1223. req->ssids[i].ssid,
  1224. req->ssids[i].ssid_len,
  1225. req->ie_len);
  1226. if (!probe)
  1227. continue;
  1228. if (req->ie_len)
  1229. memcpy(skb_put(probe, req->ie_len), req->ie,
  1230. req->ie_len);
  1231. local_bh_disable();
  1232. mac80211_hwsim_tx_frame(hwsim->hw, probe,
  1233. hwsim->tmp_chan);
  1234. local_bh_enable();
  1235. }
  1236. }
  1237. ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
  1238. msecs_to_jiffies(dwell));
  1239. hwsim->scan_chan_idx++;
  1240. mutex_unlock(&hwsim->mutex);
  1241. }
  1242. static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
  1243. struct ieee80211_vif *vif,
  1244. struct cfg80211_scan_request *req)
  1245. {
  1246. struct mac80211_hwsim_data *hwsim = hw->priv;
  1247. mutex_lock(&hwsim->mutex);
  1248. if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
  1249. mutex_unlock(&hwsim->mutex);
  1250. return -EBUSY;
  1251. }
  1252. hwsim->hw_scan_request = req;
  1253. hwsim->hw_scan_vif = vif;
  1254. hwsim->scan_chan_idx = 0;
  1255. mutex_unlock(&hwsim->mutex);
  1256. wiphy_debug(hw->wiphy, "hwsim hw_scan request\n");
  1257. ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
  1258. return 0;
  1259. }
  1260. static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
  1261. struct ieee80211_vif *vif)
  1262. {
  1263. struct mac80211_hwsim_data *hwsim = hw->priv;
  1264. wiphy_debug(hw->wiphy, "hwsim cancel_hw_scan\n");
  1265. cancel_delayed_work_sync(&hwsim->hw_scan);
  1266. mutex_lock(&hwsim->mutex);
  1267. ieee80211_scan_completed(hwsim->hw, true);
  1268. hwsim->tmp_chan = NULL;
  1269. hwsim->hw_scan_request = NULL;
  1270. hwsim->hw_scan_vif = NULL;
  1271. mutex_unlock(&hwsim->mutex);
  1272. }
  1273. static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw)
  1274. {
  1275. struct mac80211_hwsim_data *hwsim = hw->priv;
  1276. mutex_lock(&hwsim->mutex);
  1277. if (hwsim->scanning) {
  1278. printk(KERN_DEBUG "two hwsim sw_scans detected!\n");
  1279. goto out;
  1280. }
  1281. printk(KERN_DEBUG "hwsim sw_scan request, prepping stuff\n");
  1282. hwsim->scanning = true;
  1283. out:
  1284. mutex_unlock(&hwsim->mutex);
  1285. }
  1286. static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw)
  1287. {
  1288. struct mac80211_hwsim_data *hwsim = hw->priv;
  1289. mutex_lock(&hwsim->mutex);
  1290. printk(KERN_DEBUG "hwsim sw_scan_complete\n");
  1291. hwsim->scanning = false;
  1292. mutex_unlock(&hwsim->mutex);
  1293. }
  1294. static void hw_roc_done(struct work_struct *work)
  1295. {
  1296. struct mac80211_hwsim_data *hwsim =
  1297. container_of(work, struct mac80211_hwsim_data, roc_done.work);
  1298. mutex_lock(&hwsim->mutex);
  1299. ieee80211_remain_on_channel_expired(hwsim->hw);
  1300. hwsim->tmp_chan = NULL;
  1301. mutex_unlock(&hwsim->mutex);
  1302. wiphy_debug(hwsim->hw->wiphy, "hwsim ROC expired\n");
  1303. }
  1304. static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
  1305. struct ieee80211_vif *vif,
  1306. struct ieee80211_channel *chan,
  1307. int duration,
  1308. enum ieee80211_roc_type type)
  1309. {
  1310. struct mac80211_hwsim_data *hwsim = hw->priv;
  1311. mutex_lock(&hwsim->mutex);
  1312. if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
  1313. mutex_unlock(&hwsim->mutex);
  1314. return -EBUSY;
  1315. }
  1316. hwsim->tmp_chan = chan;
  1317. mutex_unlock(&hwsim->mutex);
  1318. wiphy_debug(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
  1319. chan->center_freq, duration);
  1320. ieee80211_ready_on_channel(hw);
  1321. ieee80211_queue_delayed_work(hw, &hwsim->roc_done,
  1322. msecs_to_jiffies(duration));
  1323. return 0;
  1324. }
  1325. static int mac80211_hwsim_croc(struct ieee80211_hw *hw)
  1326. {
  1327. struct mac80211_hwsim_data *hwsim = hw->priv;
  1328. cancel_delayed_work_sync(&hwsim->roc_done);
  1329. mutex_lock(&hwsim->mutex);
  1330. hwsim->tmp_chan = NULL;
  1331. mutex_unlock(&hwsim->mutex);
  1332. wiphy_debug(hw->wiphy, "hwsim ROC canceled\n");
  1333. return 0;
  1334. }
  1335. static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
  1336. struct ieee80211_chanctx_conf *ctx)
  1337. {
  1338. hwsim_set_chanctx_magic(ctx);
  1339. wiphy_debug(hw->wiphy,
  1340. "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
  1341. ctx->def.chan->center_freq, ctx->def.width,
  1342. ctx->def.center_freq1, ctx->def.center_freq2);
  1343. return 0;
  1344. }
  1345. static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
  1346. struct ieee80211_chanctx_conf *ctx)
  1347. {
  1348. wiphy_debug(hw->wiphy,
  1349. "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
  1350. ctx->def.chan->center_freq, ctx->def.width,
  1351. ctx->def.center_freq1, ctx->def.center_freq2);
  1352. hwsim_check_chanctx_magic(ctx);
  1353. hwsim_clear_chanctx_magic(ctx);
  1354. }
  1355. static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
  1356. struct ieee80211_chanctx_conf *ctx,
  1357. u32 changed)
  1358. {
  1359. hwsim_check_chanctx_magic(ctx);
  1360. wiphy_debug(hw->wiphy,
  1361. "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
  1362. ctx->def.chan->center_freq, ctx->def.width,
  1363. ctx->def.center_freq1, ctx->def.center_freq2);
  1364. }
  1365. static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
  1366. struct ieee80211_vif *vif,
  1367. struct ieee80211_chanctx_conf *ctx)
  1368. {
  1369. hwsim_check_magic(vif);
  1370. hwsim_check_chanctx_magic(ctx);
  1371. return 0;
  1372. }
  1373. static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
  1374. struct ieee80211_vif *vif,
  1375. struct ieee80211_chanctx_conf *ctx)
  1376. {
  1377. hwsim_check_magic(vif);
  1378. hwsim_check_chanctx_magic(ctx);
  1379. }
  1380. static struct ieee80211_ops mac80211_hwsim_ops =
  1381. {
  1382. .tx = mac80211_hwsim_tx,
  1383. .start = mac80211_hwsim_start,
  1384. .stop = mac80211_hwsim_stop,
  1385. .add_interface = mac80211_hwsim_add_interface,
  1386. .change_interface = mac80211_hwsim_change_interface,
  1387. .remove_interface = mac80211_hwsim_remove_interface,
  1388. .config = mac80211_hwsim_config,
  1389. .configure_filter = mac80211_hwsim_configure_filter,
  1390. .bss_info_changed = mac80211_hwsim_bss_info_changed,
  1391. .sta_add = mac80211_hwsim_sta_add,
  1392. .sta_remove = mac80211_hwsim_sta_remove,
  1393. .sta_notify = mac80211_hwsim_sta_notify,
  1394. .set_tim = mac80211_hwsim_set_tim,
  1395. .conf_tx = mac80211_hwsim_conf_tx,
  1396. .get_survey = mac80211_hwsim_get_survey,
  1397. CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd)
  1398. .ampdu_action = mac80211_hwsim_ampdu_action,
  1399. .sw_scan_start = mac80211_hwsim_sw_scan,
  1400. .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
  1401. .flush = mac80211_hwsim_flush,
  1402. .get_tsf = mac80211_hwsim_get_tsf,
  1403. .set_tsf = mac80211_hwsim_set_tsf,
  1404. };
  1405. static void mac80211_hwsim_free(void)
  1406. {
  1407. struct list_head tmplist, *i, *tmp;
  1408. struct mac80211_hwsim_data *data, *tmpdata;
  1409. INIT_LIST_HEAD(&tmplist);
  1410. spin_lock_bh(&hwsim_radio_lock);
  1411. list_for_each_safe(i, tmp, &hwsim_radios)
  1412. list_move(i, &tmplist);
  1413. spin_unlock_bh(&hwsim_radio_lock);
  1414. list_for_each_entry_safe(data, tmpdata, &tmplist, list) {
  1415. debugfs_remove(data->debugfs_group);
  1416. debugfs_remove(data->debugfs_ps);
  1417. debugfs_remove(data->debugfs);
  1418. ieee80211_unregister_hw(data->hw);
  1419. device_unregister(data->dev);
  1420. ieee80211_free_hw(data->hw);
  1421. }
  1422. class_destroy(hwsim_class);
  1423. }
  1424. static struct device_driver mac80211_hwsim_driver = {
  1425. .name = "mac80211_hwsim"
  1426. };
  1427. static const struct net_device_ops hwsim_netdev_ops = {
  1428. .ndo_start_xmit = hwsim_mon_xmit,
  1429. .ndo_change_mtu = eth_change_mtu,
  1430. .ndo_set_mac_address = eth_mac_addr,
  1431. .ndo_validate_addr = eth_validate_addr,
  1432. };
  1433. static void hwsim_mon_setup(struct net_device *dev)
  1434. {
  1435. dev->netdev_ops = &hwsim_netdev_ops;
  1436. dev->destructor = free_netdev;
  1437. ether_setup(dev);
  1438. dev->tx_queue_len = 0;
  1439. dev->type = ARPHRD_IEEE80211_RADIOTAP;
  1440. memset(dev->dev_addr, 0, ETH_ALEN);
  1441. dev->dev_addr[0] = 0x12;
  1442. }
  1443. static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
  1444. {
  1445. struct mac80211_hwsim_data *data = dat;
  1446. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  1447. struct sk_buff *skb;
  1448. struct ieee80211_pspoll *pspoll;
  1449. if (!vp->assoc)
  1450. return;
  1451. wiphy_debug(data->hw->wiphy,
  1452. "%s: send PS-Poll to %pM for aid %d\n",
  1453. __func__, vp->bssid, vp->aid);
  1454. skb = dev_alloc_skb(sizeof(*pspoll));
  1455. if (!skb)
  1456. return;
  1457. pspoll = (void *) skb_put(skb, sizeof(*pspoll));
  1458. pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  1459. IEEE80211_STYPE_PSPOLL |
  1460. IEEE80211_FCTL_PM);
  1461. pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
  1462. memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
  1463. memcpy(pspoll->ta, mac, ETH_ALEN);
  1464. rcu_read_lock();
  1465. mac80211_hwsim_tx_frame(data->hw, skb,
  1466. rcu_dereference(vif->chanctx_conf)->def.chan);
  1467. rcu_read_unlock();
  1468. }
  1469. static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
  1470. struct ieee80211_vif *vif, int ps)
  1471. {
  1472. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  1473. struct sk_buff *skb;
  1474. struct ieee80211_hdr *hdr;
  1475. if (!vp->assoc)
  1476. return;
  1477. wiphy_debug(data->hw->wiphy,
  1478. "%s: send data::nullfunc to %pM ps=%d\n",
  1479. __func__, vp->bssid, ps);
  1480. skb = dev_alloc_skb(sizeof(*hdr));
  1481. if (!skb)
  1482. return;
  1483. hdr = (void *) skb_put(skb, sizeof(*hdr) - ETH_ALEN);
  1484. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  1485. IEEE80211_STYPE_NULLFUNC |
  1486. (ps ? IEEE80211_FCTL_PM : 0));
  1487. hdr->duration_id = cpu_to_le16(0);
  1488. memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
  1489. memcpy(hdr->addr2, mac, ETH_ALEN);
  1490. memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
  1491. rcu_read_lock();
  1492. mac80211_hwsim_tx_frame(data->hw, skb,
  1493. rcu_dereference(vif->chanctx_conf)->def.chan);
  1494. rcu_read_unlock();
  1495. }
  1496. static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
  1497. struct ieee80211_vif *vif)
  1498. {
  1499. struct mac80211_hwsim_data *data = dat;
  1500. hwsim_send_nullfunc(data, mac, vif, 1);
  1501. }
  1502. static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
  1503. struct ieee80211_vif *vif)
  1504. {
  1505. struct mac80211_hwsim_data *data = dat;
  1506. hwsim_send_nullfunc(data, mac, vif, 0);
  1507. }
  1508. static int hwsim_fops_ps_read(void *dat, u64 *val)
  1509. {
  1510. struct mac80211_hwsim_data *data = dat;
  1511. *val = data->ps;
  1512. return 0;
  1513. }
  1514. static int hwsim_fops_ps_write(void *dat, u64 val)
  1515. {
  1516. struct mac80211_hwsim_data *data = dat;
  1517. enum ps_mode old_ps;
  1518. if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
  1519. val != PS_MANUAL_POLL)
  1520. return -EINVAL;
  1521. old_ps = data->ps;
  1522. data->ps = val;
  1523. if (val == PS_MANUAL_POLL) {
  1524. ieee80211_iterate_active_interfaces(data->hw,
  1525. IEEE80211_IFACE_ITER_NORMAL,
  1526. hwsim_send_ps_poll, data);
  1527. data->ps_poll_pending = true;
  1528. } else if (old_ps == PS_DISABLED && val != PS_DISABLED) {
  1529. ieee80211_iterate_active_interfaces(data->hw,
  1530. IEEE80211_IFACE_ITER_NORMAL,
  1531. hwsim_send_nullfunc_ps,
  1532. data);
  1533. } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
  1534. ieee80211_iterate_active_interfaces(data->hw,
  1535. IEEE80211_IFACE_ITER_NORMAL,
  1536. hwsim_send_nullfunc_no_ps,
  1537. data);
  1538. }
  1539. return 0;
  1540. }
  1541. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
  1542. "%llu\n");
  1543. static int hwsim_fops_group_read(void *dat, u64 *val)
  1544. {
  1545. struct mac80211_hwsim_data *data = dat;
  1546. *val = data->group;
  1547. return 0;
  1548. }
  1549. static int hwsim_fops_group_write(void *dat, u64 val)
  1550. {
  1551. struct mac80211_hwsim_data *data = dat;
  1552. data->group = val;
  1553. return 0;
  1554. }
  1555. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
  1556. hwsim_fops_group_read, hwsim_fops_group_write,
  1557. "%llx\n");
  1558. static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(
  1559. struct mac_address *addr)
  1560. {
  1561. struct mac80211_hwsim_data *data;
  1562. bool _found = false;
  1563. spin_lock_bh(&hwsim_radio_lock);
  1564. list_for_each_entry(data, &hwsim_radios, list) {
  1565. if (memcmp(data->addresses[1].addr, addr,
  1566. sizeof(struct mac_address)) == 0) {
  1567. _found = true;
  1568. break;
  1569. }
  1570. }
  1571. spin_unlock_bh(&hwsim_radio_lock);
  1572. if (!_found)
  1573. return NULL;
  1574. return data;
  1575. }
  1576. static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
  1577. struct genl_info *info)
  1578. {
  1579. struct ieee80211_hdr *hdr;
  1580. struct mac80211_hwsim_data *data2;
  1581. struct ieee80211_tx_info *txi;
  1582. struct hwsim_tx_rate *tx_attempts;
  1583. unsigned long ret_skb_ptr;
  1584. struct sk_buff *skb, *tmp;
  1585. struct mac_address *src;
  1586. unsigned int hwsim_flags;
  1587. int i;
  1588. bool found = false;
  1589. if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
  1590. !info->attrs[HWSIM_ATTR_FLAGS] ||
  1591. !info->attrs[HWSIM_ATTR_COOKIE] ||
  1592. !info->attrs[HWSIM_ATTR_TX_INFO])
  1593. goto out;
  1594. src = (struct mac_address *)nla_data(
  1595. info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
  1596. hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
  1597. ret_skb_ptr = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
  1598. data2 = get_hwsim_data_ref_from_addr(src);
  1599. if (data2 == NULL)
  1600. goto out;
  1601. /* look for the skb matching the cookie passed back from user */
  1602. skb_queue_walk_safe(&data2->pending, skb, tmp) {
  1603. if ((unsigned long)skb == ret_skb_ptr) {
  1604. skb_unlink(skb, &data2->pending);
  1605. found = true;
  1606. break;
  1607. }
  1608. }
  1609. /* not found */
  1610. if (!found)
  1611. goto out;
  1612. /* Tx info received because the frame was broadcasted on user space,
  1613. so we get all the necessary info: tx attempts and skb control buff */
  1614. tx_attempts = (struct hwsim_tx_rate *)nla_data(
  1615. info->attrs[HWSIM_ATTR_TX_INFO]);
  1616. /* now send back TX status */
  1617. txi = IEEE80211_SKB_CB(skb);
  1618. ieee80211_tx_info_clear_status(txi);
  1619. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  1620. txi->status.rates[i].idx = tx_attempts[i].idx;
  1621. txi->status.rates[i].count = tx_attempts[i].count;
  1622. /*txi->status.rates[i].flags = 0;*/
  1623. }
  1624. txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
  1625. if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
  1626. (hwsim_flags & HWSIM_TX_STAT_ACK)) {
  1627. if (skb->len >= 16) {
  1628. hdr = (struct ieee80211_hdr *) skb->data;
  1629. mac80211_hwsim_monitor_ack(txi->rate_driver_data[0],
  1630. hdr->addr2);
  1631. }
  1632. txi->flags |= IEEE80211_TX_STAT_ACK;
  1633. }
  1634. ieee80211_tx_status_irqsafe(data2->hw, skb);
  1635. return 0;
  1636. out:
  1637. return -EINVAL;
  1638. }
  1639. static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
  1640. struct genl_info *info)
  1641. {
  1642. struct mac80211_hwsim_data *data2;
  1643. struct ieee80211_rx_status rx_status;
  1644. struct mac_address *dst;
  1645. int frame_data_len;
  1646. char *frame_data;
  1647. struct sk_buff *skb = NULL;
  1648. if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
  1649. !info->attrs[HWSIM_ATTR_FRAME] ||
  1650. !info->attrs[HWSIM_ATTR_RX_RATE] ||
  1651. !info->attrs[HWSIM_ATTR_SIGNAL])
  1652. goto out;
  1653. dst = (struct mac_address *)nla_data(
  1654. info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
  1655. frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
  1656. frame_data = (char *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
  1657. /* Allocate new skb here */
  1658. skb = alloc_skb(frame_data_len, GFP_KERNEL);
  1659. if (skb == NULL)
  1660. goto err;
  1661. if (frame_data_len <= IEEE80211_MAX_DATA_LEN) {
  1662. /* Copy the data */
  1663. memcpy(skb_put(skb, frame_data_len), frame_data,
  1664. frame_data_len);
  1665. } else
  1666. goto err;
  1667. data2 = get_hwsim_data_ref_from_addr(dst);
  1668. if (data2 == NULL)
  1669. goto out;
  1670. /* check if radio is configured properly */
  1671. if (data2->idle || !data2->started)
  1672. goto out;
  1673. /*A frame is received from user space*/
  1674. memset(&rx_status, 0, sizeof(rx_status));
  1675. rx_status.freq = data2->channel->center_freq;
  1676. rx_status.band = data2->channel->band;
  1677. rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
  1678. rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
  1679. memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
  1680. ieee80211_rx_irqsafe(data2->hw, skb);
  1681. return 0;
  1682. err:
  1683. printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
  1684. goto out;
  1685. out:
  1686. dev_kfree_skb(skb);
  1687. return -EINVAL;
  1688. }
  1689. static int hwsim_register_received_nl(struct sk_buff *skb_2,
  1690. struct genl_info *info)
  1691. {
  1692. if (info == NULL)
  1693. goto out;
  1694. wmediumd_portid = info->snd_portid;
  1695. printk(KERN_DEBUG "mac80211_hwsim: received a REGISTER, "
  1696. "switching to wmediumd mode with pid %d\n", info->snd_portid);
  1697. return 0;
  1698. out:
  1699. printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
  1700. return -EINVAL;
  1701. }
  1702. /* Generic Netlink operations array */
  1703. static struct genl_ops hwsim_ops[] = {
  1704. {
  1705. .cmd = HWSIM_CMD_REGISTER,
  1706. .policy = hwsim_genl_policy,
  1707. .doit = hwsim_register_received_nl,
  1708. .flags = GENL_ADMIN_PERM,
  1709. },
  1710. {
  1711. .cmd = HWSIM_CMD_FRAME,
  1712. .policy = hwsim_genl_policy,
  1713. .doit = hwsim_cloned_frame_received_nl,
  1714. },
  1715. {
  1716. .cmd = HWSIM_CMD_TX_INFO_FRAME,
  1717. .policy = hwsim_genl_policy,
  1718. .doit = hwsim_tx_info_frame_received_nl,
  1719. },
  1720. };
  1721. static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
  1722. unsigned long state,
  1723. void *_notify)
  1724. {
  1725. struct netlink_notify *notify = _notify;
  1726. if (state != NETLINK_URELEASE)
  1727. return NOTIFY_DONE;
  1728. if (notify->portid == wmediumd_portid) {
  1729. printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
  1730. " socket, switching to perfect channel medium\n");
  1731. wmediumd_portid = 0;
  1732. }
  1733. return NOTIFY_DONE;
  1734. }
  1735. static struct notifier_block hwsim_netlink_notifier = {
  1736. .notifier_call = mac80211_hwsim_netlink_notify,
  1737. };
  1738. static int hwsim_init_netlink(void)
  1739. {
  1740. int rc;
  1741. /* userspace test API hasn't been adjusted for multi-channel */
  1742. if (channels > 1)
  1743. return 0;
  1744. printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
  1745. rc = genl_register_family_with_ops(&hwsim_genl_family,
  1746. hwsim_ops, ARRAY_SIZE(hwsim_ops));
  1747. if (rc)
  1748. goto failure;
  1749. rc = netlink_register_notifier(&hwsim_netlink_notifier);
  1750. if (rc)
  1751. goto failure;
  1752. return 0;
  1753. failure:
  1754. printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
  1755. return -EINVAL;
  1756. }
  1757. static void hwsim_exit_netlink(void)
  1758. {
  1759. int ret;
  1760. /* userspace test API hasn't been adjusted for multi-channel */
  1761. if (channels > 1)
  1762. return;
  1763. printk(KERN_INFO "mac80211_hwsim: closing netlink\n");
  1764. /* unregister the notifier */
  1765. netlink_unregister_notifier(&hwsim_netlink_notifier);
  1766. /* unregister the family */
  1767. ret = genl_unregister_family(&hwsim_genl_family);
  1768. if (ret)
  1769. printk(KERN_DEBUG "mac80211_hwsim: "
  1770. "unregister family %i\n", ret);
  1771. }
  1772. static const struct ieee80211_iface_limit hwsim_if_limits[] = {
  1773. { .max = 1, .types = BIT(NL80211_IFTYPE_ADHOC) },
  1774. { .max = 2048, .types = BIT(NL80211_IFTYPE_STATION) |
  1775. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  1776. #ifdef CONFIG_MAC80211_MESH
  1777. BIT(NL80211_IFTYPE_MESH_POINT) |
  1778. #endif
  1779. BIT(NL80211_IFTYPE_AP) |
  1780. BIT(NL80211_IFTYPE_P2P_GO) },
  1781. { .max = 1, .types = BIT(NL80211_IFTYPE_P2P_DEVICE) },
  1782. };
  1783. static struct ieee80211_iface_combination hwsim_if_comb = {
  1784. .limits = hwsim_if_limits,
  1785. .n_limits = ARRAY_SIZE(hwsim_if_limits),
  1786. .max_interfaces = 2048,
  1787. .num_different_channels = 1,
  1788. };
  1789. static int __init init_mac80211_hwsim(void)
  1790. {
  1791. int i, err = 0;
  1792. u8 addr[ETH_ALEN];
  1793. struct mac80211_hwsim_data *data;
  1794. struct ieee80211_hw *hw;
  1795. enum ieee80211_band band;
  1796. if (radios < 1 || radios > 100)
  1797. return -EINVAL;
  1798. if (channels < 1)
  1799. return -EINVAL;
  1800. if (channels > 1) {
  1801. hwsim_if_comb.num_different_channels = channels;
  1802. mac80211_hwsim_ops.hw_scan = mac80211_hwsim_hw_scan;
  1803. mac80211_hwsim_ops.cancel_hw_scan =
  1804. mac80211_hwsim_cancel_hw_scan;
  1805. mac80211_hwsim_ops.sw_scan_start = NULL;
  1806. mac80211_hwsim_ops.sw_scan_complete = NULL;
  1807. mac80211_hwsim_ops.remain_on_channel =
  1808. mac80211_hwsim_roc;
  1809. mac80211_hwsim_ops.cancel_remain_on_channel =
  1810. mac80211_hwsim_croc;
  1811. mac80211_hwsim_ops.add_chanctx =
  1812. mac80211_hwsim_add_chanctx;
  1813. mac80211_hwsim_ops.remove_chanctx =
  1814. mac80211_hwsim_remove_chanctx;
  1815. mac80211_hwsim_ops.change_chanctx =
  1816. mac80211_hwsim_change_chanctx;
  1817. mac80211_hwsim_ops.assign_vif_chanctx =
  1818. mac80211_hwsim_assign_vif_chanctx;
  1819. mac80211_hwsim_ops.unassign_vif_chanctx =
  1820. mac80211_hwsim_unassign_vif_chanctx;
  1821. }
  1822. spin_lock_init(&hwsim_radio_lock);
  1823. INIT_LIST_HEAD(&hwsim_radios);
  1824. hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
  1825. if (IS_ERR(hwsim_class))
  1826. return PTR_ERR(hwsim_class);
  1827. memset(addr, 0, ETH_ALEN);
  1828. addr[0] = 0x02;
  1829. for (i = 0; i < radios; i++) {
  1830. printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
  1831. i);
  1832. hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
  1833. if (!hw) {
  1834. printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
  1835. "failed\n");
  1836. err = -ENOMEM;
  1837. goto failed;
  1838. }
  1839. data = hw->priv;
  1840. data->hw = hw;
  1841. data->dev = device_create(hwsim_class, NULL, 0, hw,
  1842. "hwsim%d", i);
  1843. if (IS_ERR(data->dev)) {
  1844. printk(KERN_DEBUG
  1845. "mac80211_hwsim: device_create "
  1846. "failed (%ld)\n", PTR_ERR(data->dev));
  1847. err = -ENOMEM;
  1848. goto failed_drvdata;
  1849. }
  1850. data->dev->driver = &mac80211_hwsim_driver;
  1851. skb_queue_head_init(&data->pending);
  1852. SET_IEEE80211_DEV(hw, data->dev);
  1853. addr[3] = i >> 8;
  1854. addr[4] = i;
  1855. memcpy(data->addresses[0].addr, addr, ETH_ALEN);
  1856. memcpy(data->addresses[1].addr, addr, ETH_ALEN);
  1857. data->addresses[1].addr[0] |= 0x40;
  1858. hw->wiphy->n_addresses = 2;
  1859. hw->wiphy->addresses = data->addresses;
  1860. hw->wiphy->iface_combinations = &hwsim_if_comb;
  1861. hw->wiphy->n_iface_combinations = 1;
  1862. if (channels > 1) {
  1863. hw->wiphy->max_scan_ssids = 255;
  1864. hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
  1865. hw->wiphy->max_remain_on_channel_duration = 1000;
  1866. }
  1867. INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
  1868. INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
  1869. hw->channel_change_time = 1;
  1870. hw->queues = 5;
  1871. hw->offchannel_tx_hw_queue = 4;
  1872. hw->wiphy->interface_modes =
  1873. BIT(NL80211_IFTYPE_STATION) |
  1874. BIT(NL80211_IFTYPE_AP) |
  1875. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  1876. BIT(NL80211_IFTYPE_P2P_GO) |
  1877. BIT(NL80211_IFTYPE_ADHOC) |
  1878. BIT(NL80211_IFTYPE_MESH_POINT) |
  1879. BIT(NL80211_IFTYPE_P2P_DEVICE);
  1880. hw->flags = IEEE80211_HW_MFP_CAPABLE |
  1881. IEEE80211_HW_SIGNAL_DBM |
  1882. IEEE80211_HW_SUPPORTS_STATIC_SMPS |
  1883. IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
  1884. IEEE80211_HW_AMPDU_AGGREGATION |
  1885. IEEE80211_HW_WANT_MONITOR_VIF |
  1886. IEEE80211_HW_QUEUE_CONTROL;
  1887. hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
  1888. WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
  1889. /* ask mac80211 to reserve space for magic */
  1890. hw->vif_data_size = sizeof(struct hwsim_vif_priv);
  1891. hw->sta_data_size = sizeof(struct hwsim_sta_priv);
  1892. hw->chanctx_data_size = sizeof(struct hwsim_chanctx_priv);
  1893. memcpy(data->channels_2ghz, hwsim_channels_2ghz,
  1894. sizeof(hwsim_channels_2ghz));
  1895. memcpy(data->channels_5ghz, hwsim_channels_5ghz,
  1896. sizeof(hwsim_channels_5ghz));
  1897. memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
  1898. for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
  1899. struct ieee80211_supported_band *sband = &data->bands[band];
  1900. switch (band) {
  1901. case IEEE80211_BAND_2GHZ:
  1902. sband->channels = data->channels_2ghz;
  1903. sband->n_channels =
  1904. ARRAY_SIZE(hwsim_channels_2ghz);
  1905. sband->bitrates = data->rates;
  1906. sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
  1907. break;
  1908. case IEEE80211_BAND_5GHZ:
  1909. sband->channels = data->channels_5ghz;
  1910. sband->n_channels =
  1911. ARRAY_SIZE(hwsim_channels_5ghz);
  1912. sband->bitrates = data->rates + 4;
  1913. sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
  1914. break;
  1915. default:
  1916. continue;
  1917. }
  1918. sband->ht_cap.ht_supported = true;
  1919. sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  1920. IEEE80211_HT_CAP_GRN_FLD |
  1921. IEEE80211_HT_CAP_SGI_40 |
  1922. IEEE80211_HT_CAP_DSSSCCK40;
  1923. sband->ht_cap.ampdu_factor = 0x3;
  1924. sband->ht_cap.ampdu_density = 0x6;
  1925. memset(&sband->ht_cap.mcs, 0,
  1926. sizeof(sband->ht_cap.mcs));
  1927. sband->ht_cap.mcs.rx_mask[0] = 0xff;
  1928. sband->ht_cap.mcs.rx_mask[1] = 0xff;
  1929. sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  1930. hw->wiphy->bands[band] = sband;
  1931. sband->vht_cap.vht_supported = true;
  1932. sband->vht_cap.cap =
  1933. IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
  1934. IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
  1935. IEEE80211_VHT_CAP_RXLDPC |
  1936. IEEE80211_VHT_CAP_SHORT_GI_80 |
  1937. IEEE80211_VHT_CAP_SHORT_GI_160 |
  1938. IEEE80211_VHT_CAP_TXSTBC |
  1939. IEEE80211_VHT_CAP_RXSTBC_1 |
  1940. IEEE80211_VHT_CAP_RXSTBC_2 |
  1941. IEEE80211_VHT_CAP_RXSTBC_3 |
  1942. IEEE80211_VHT_CAP_RXSTBC_4 |
  1943. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
  1944. sband->vht_cap.vht_mcs.rx_mcs_map =
  1945. cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_8 << 0 |
  1946. IEEE80211_VHT_MCS_SUPPORT_0_8 << 2 |
  1947. IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
  1948. IEEE80211_VHT_MCS_SUPPORT_0_8 << 6 |
  1949. IEEE80211_VHT_MCS_SUPPORT_0_8 << 8 |
  1950. IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |
  1951. IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |
  1952. IEEE80211_VHT_MCS_SUPPORT_0_8 << 14);
  1953. sband->vht_cap.vht_mcs.tx_mcs_map =
  1954. sband->vht_cap.vht_mcs.rx_mcs_map;
  1955. }
  1956. /* By default all radios are belonging to the first group */
  1957. data->group = 1;
  1958. mutex_init(&data->mutex);
  1959. /* Enable frame retransmissions for lossy channels */
  1960. hw->max_rates = 4;
  1961. hw->max_rate_tries = 11;
  1962. /* Work to be done prior to ieee80211_register_hw() */
  1963. switch (regtest) {
  1964. case HWSIM_REGTEST_DISABLED:
  1965. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  1966. case HWSIM_REGTEST_DRIVER_REG_ALL:
  1967. case HWSIM_REGTEST_DIFF_COUNTRY:
  1968. /*
  1969. * Nothing to be done for driver regulatory domain
  1970. * hints prior to ieee80211_register_hw()
  1971. */
  1972. break;
  1973. case HWSIM_REGTEST_WORLD_ROAM:
  1974. if (i == 0) {
  1975. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1976. wiphy_apply_custom_regulatory(hw->wiphy,
  1977. &hwsim_world_regdom_custom_01);
  1978. }
  1979. break;
  1980. case HWSIM_REGTEST_CUSTOM_WORLD:
  1981. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1982. wiphy_apply_custom_regulatory(hw->wiphy,
  1983. &hwsim_world_regdom_custom_01);
  1984. break;
  1985. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  1986. if (i == 0) {
  1987. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1988. wiphy_apply_custom_regulatory(hw->wiphy,
  1989. &hwsim_world_regdom_custom_01);
  1990. } else if (i == 1) {
  1991. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1992. wiphy_apply_custom_regulatory(hw->wiphy,
  1993. &hwsim_world_regdom_custom_02);
  1994. }
  1995. break;
  1996. case HWSIM_REGTEST_STRICT_ALL:
  1997. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1998. break;
  1999. case HWSIM_REGTEST_STRICT_FOLLOW:
  2000. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  2001. if (i == 0)
  2002. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  2003. break;
  2004. case HWSIM_REGTEST_ALL:
  2005. if (i == 0) {
  2006. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  2007. wiphy_apply_custom_regulatory(hw->wiphy,
  2008. &hwsim_world_regdom_custom_01);
  2009. } else if (i == 1) {
  2010. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  2011. wiphy_apply_custom_regulatory(hw->wiphy,
  2012. &hwsim_world_regdom_custom_02);
  2013. } else if (i == 4)
  2014. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  2015. break;
  2016. default:
  2017. break;
  2018. }
  2019. /* give the regulatory workqueue a chance to run */
  2020. if (regtest)
  2021. schedule_timeout_interruptible(1);
  2022. err = ieee80211_register_hw(hw);
  2023. if (err < 0) {
  2024. printk(KERN_DEBUG "mac80211_hwsim: "
  2025. "ieee80211_register_hw failed (%d)\n", err);
  2026. goto failed_hw;
  2027. }
  2028. /* Work to be done after to ieee80211_register_hw() */
  2029. switch (regtest) {
  2030. case HWSIM_REGTEST_WORLD_ROAM:
  2031. case HWSIM_REGTEST_DISABLED:
  2032. break;
  2033. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  2034. if (!i)
  2035. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  2036. break;
  2037. case HWSIM_REGTEST_DRIVER_REG_ALL:
  2038. case HWSIM_REGTEST_STRICT_ALL:
  2039. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  2040. break;
  2041. case HWSIM_REGTEST_DIFF_COUNTRY:
  2042. if (i < ARRAY_SIZE(hwsim_alpha2s))
  2043. regulatory_hint(hw->wiphy, hwsim_alpha2s[i]);
  2044. break;
  2045. case HWSIM_REGTEST_CUSTOM_WORLD:
  2046. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  2047. /*
  2048. * Nothing to be done for custom world regulatory
  2049. * domains after to ieee80211_register_hw
  2050. */
  2051. break;
  2052. case HWSIM_REGTEST_STRICT_FOLLOW:
  2053. if (i == 0)
  2054. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  2055. break;
  2056. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  2057. if (i == 0)
  2058. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  2059. else if (i == 1)
  2060. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  2061. break;
  2062. case HWSIM_REGTEST_ALL:
  2063. if (i == 2)
  2064. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  2065. else if (i == 3)
  2066. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  2067. else if (i == 4)
  2068. regulatory_hint(hw->wiphy, hwsim_alpha2s[2]);
  2069. break;
  2070. default:
  2071. break;
  2072. }
  2073. wiphy_debug(hw->wiphy, "hwaddr %pm registered\n",
  2074. hw->wiphy->perm_addr);
  2075. data->debugfs = debugfs_create_dir("hwsim",
  2076. hw->wiphy->debugfsdir);
  2077. data->debugfs_ps = debugfs_create_file("ps", 0666,
  2078. data->debugfs, data,
  2079. &hwsim_fops_ps);
  2080. data->debugfs_group = debugfs_create_file("group", 0666,
  2081. data->debugfs, data,
  2082. &hwsim_fops_group);
  2083. tasklet_hrtimer_init(&data->beacon_timer,
  2084. mac80211_hwsim_beacon,
  2085. CLOCK_REALTIME, HRTIMER_MODE_ABS);
  2086. list_add_tail(&data->list, &hwsim_radios);
  2087. }
  2088. hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
  2089. if (hwsim_mon == NULL)
  2090. goto failed;
  2091. rtnl_lock();
  2092. err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
  2093. if (err < 0)
  2094. goto failed_mon;
  2095. err = register_netdevice(hwsim_mon);
  2096. if (err < 0)
  2097. goto failed_mon;
  2098. rtnl_unlock();
  2099. err = hwsim_init_netlink();
  2100. if (err < 0)
  2101. goto failed_nl;
  2102. return 0;
  2103. failed_nl:
  2104. printk(KERN_DEBUG "mac_80211_hwsim: failed initializing netlink\n");
  2105. return err;
  2106. failed_mon:
  2107. rtnl_unlock();
  2108. free_netdev(hwsim_mon);
  2109. mac80211_hwsim_free();
  2110. return err;
  2111. failed_hw:
  2112. device_unregister(data->dev);
  2113. failed_drvdata:
  2114. ieee80211_free_hw(hw);
  2115. failed:
  2116. mac80211_hwsim_free();
  2117. return err;
  2118. }
  2119. module_init(init_mac80211_hwsim);
  2120. static void __exit exit_mac80211_hwsim(void)
  2121. {
  2122. printk(KERN_DEBUG "mac80211_hwsim: unregister radios\n");
  2123. hwsim_exit_netlink();
  2124. mac80211_hwsim_free();
  2125. unregister_netdev(hwsim_mon);
  2126. }
  2127. module_exit(exit_mac80211_hwsim);