mac80211_hwsim.c 54 KB

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