mac80211_hwsim.c 65 KB

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