mac80211_hwsim.c 52 KB

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