mac80211_hwsim.c 62 KB

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