mac80211_hwsim.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  1. /*
  2. * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211
  3. * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. /*
  10. * TODO:
  11. * - IBSS mode simulation (Beacon transmission with competition for "air time")
  12. * - RX filtering based on filter configuration (data->rx_filter)
  13. */
  14. #include <linux/list.h>
  15. #include <linux/spinlock.h>
  16. #include <net/dst.h>
  17. #include <net/xfrm.h>
  18. #include <net/mac80211.h>
  19. #include <net/ieee80211_radiotap.h>
  20. #include <linux/if_arp.h>
  21. #include <linux/rtnetlink.h>
  22. #include <linux/etherdevice.h>
  23. #include <linux/debugfs.h>
  24. MODULE_AUTHOR("Jouni Malinen");
  25. MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
  26. MODULE_LICENSE("GPL");
  27. static int radios = 2;
  28. module_param(radios, int, 0444);
  29. MODULE_PARM_DESC(radios, "Number of simulated radios");
  30. static bool fake_hw_scan;
  31. module_param(fake_hw_scan, bool, 0444);
  32. MODULE_PARM_DESC(fake_hw_scan, "Install fake (no-op) hw-scan handler");
  33. /**
  34. * enum hwsim_regtest - the type of regulatory tests we offer
  35. *
  36. * These are the different values you can use for the regtest
  37. * module parameter. This is useful to help test world roaming
  38. * and the driver regulatory_hint() call and combinations of these.
  39. * If you want to do specific alpha2 regulatory domain tests simply
  40. * use the userspace regulatory request as that will be respected as
  41. * well without the need of this module parameter. This is designed
  42. * only for testing the driver regulatory request, world roaming
  43. * and all possible combinations.
  44. *
  45. * @HWSIM_REGTEST_DISABLED: No regulatory tests are performed,
  46. * this is the default value.
  47. * @HWSIM_REGTEST_DRIVER_REG_FOLLOW: Used for testing the driver regulatory
  48. * hint, only one driver regulatory hint will be sent as such the
  49. * secondary radios are expected to follow.
  50. * @HWSIM_REGTEST_DRIVER_REG_ALL: Used for testing the driver regulatory
  51. * request with all radios reporting the same regulatory domain.
  52. * @HWSIM_REGTEST_DIFF_COUNTRY: Used for testing the drivers calling
  53. * different regulatory domains requests. Expected behaviour is for
  54. * an intersection to occur but each device will still use their
  55. * respective regulatory requested domains. Subsequent radios will
  56. * use the resulting intersection.
  57. * @HWSIM_REGTEST_WORLD_ROAM: Used for testing the world roaming. We acomplish
  58. * this by using a custom beacon-capable regulatory domain for the first
  59. * radio. All other device world roam.
  60. * @HWSIM_REGTEST_CUSTOM_WORLD: Used for testing the custom world regulatory
  61. * domain requests. All radios will adhere to this custom world regulatory
  62. * domain.
  63. * @HWSIM_REGTEST_CUSTOM_WORLD_2: Used for testing 2 custom world regulatory
  64. * domain requests. The first radio will adhere to the first custom world
  65. * regulatory domain, the second one to the second custom world regulatory
  66. * domain. All other devices will world roam.
  67. * @HWSIM_REGTEST_STRICT_FOLLOW_: Used for testing strict regulatory domain
  68. * settings, only the first radio will send a regulatory domain request
  69. * and use strict settings. The rest of the radios are expected to follow.
  70. * @HWSIM_REGTEST_STRICT_ALL: Used for testing strict regulatory domain
  71. * settings. All radios will adhere to this.
  72. * @HWSIM_REGTEST_STRICT_AND_DRIVER_REG: Used for testing strict regulatory
  73. * domain settings, combined with secondary driver regulatory domain
  74. * settings. The first radio will get a strict regulatory domain setting
  75. * using the first driver regulatory request and the second radio will use
  76. * non-strict settings using the second driver regulatory request. All
  77. * other devices should follow the intersection created between the
  78. * first two.
  79. * @HWSIM_REGTEST_ALL: Used for testing every possible mix. You will need
  80. * at least 6 radios for a complete test. We will test in this order:
  81. * 1 - driver custom world regulatory domain
  82. * 2 - second custom world regulatory domain
  83. * 3 - first driver regulatory domain request
  84. * 4 - second driver regulatory domain request
  85. * 5 - strict regulatory domain settings using the third driver regulatory
  86. * domain request
  87. * 6 and on - should follow the intersection of the 3rd, 4rth and 5th radio
  88. * regulatory requests.
  89. */
  90. enum hwsim_regtest {
  91. HWSIM_REGTEST_DISABLED = 0,
  92. HWSIM_REGTEST_DRIVER_REG_FOLLOW = 1,
  93. HWSIM_REGTEST_DRIVER_REG_ALL = 2,
  94. HWSIM_REGTEST_DIFF_COUNTRY = 3,
  95. HWSIM_REGTEST_WORLD_ROAM = 4,
  96. HWSIM_REGTEST_CUSTOM_WORLD = 5,
  97. HWSIM_REGTEST_CUSTOM_WORLD_2 = 6,
  98. HWSIM_REGTEST_STRICT_FOLLOW = 7,
  99. HWSIM_REGTEST_STRICT_ALL = 8,
  100. HWSIM_REGTEST_STRICT_AND_DRIVER_REG = 9,
  101. HWSIM_REGTEST_ALL = 10,
  102. };
  103. /* Set to one of the HWSIM_REGTEST_* values above */
  104. static int regtest = HWSIM_REGTEST_DISABLED;
  105. module_param(regtest, int, 0444);
  106. MODULE_PARM_DESC(regtest, "The type of regulatory test we want to run");
  107. static const char *hwsim_alpha2s[] = {
  108. "FI",
  109. "AL",
  110. "US",
  111. "DE",
  112. "JP",
  113. "AL",
  114. };
  115. static const struct ieee80211_regdomain hwsim_world_regdom_custom_01 = {
  116. .n_reg_rules = 4,
  117. .alpha2 = "99",
  118. .reg_rules = {
  119. REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
  120. REG_RULE(2484-10, 2484+10, 40, 0, 20, 0),
  121. REG_RULE(5150-10, 5240+10, 40, 0, 30, 0),
  122. REG_RULE(5745-10, 5825+10, 40, 0, 30, 0),
  123. }
  124. };
  125. static const struct ieee80211_regdomain hwsim_world_regdom_custom_02 = {
  126. .n_reg_rules = 2,
  127. .alpha2 = "99",
  128. .reg_rules = {
  129. REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
  130. REG_RULE(5725-10, 5850+10, 40, 0, 30,
  131. NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_IBSS),
  132. }
  133. };
  134. struct hwsim_vif_priv {
  135. u32 magic;
  136. u8 bssid[ETH_ALEN];
  137. bool assoc;
  138. u16 aid;
  139. };
  140. #define HWSIM_VIF_MAGIC 0x69537748
  141. static inline void hwsim_check_magic(struct ieee80211_vif *vif)
  142. {
  143. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  144. WARN_ON(vp->magic != HWSIM_VIF_MAGIC);
  145. }
  146. static inline void hwsim_set_magic(struct ieee80211_vif *vif)
  147. {
  148. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  149. vp->magic = HWSIM_VIF_MAGIC;
  150. }
  151. static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
  152. {
  153. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  154. vp->magic = 0;
  155. }
  156. struct hwsim_sta_priv {
  157. u32 magic;
  158. };
  159. #define HWSIM_STA_MAGIC 0x6d537748
  160. static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
  161. {
  162. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  163. WARN_ON(sp->magic != HWSIM_STA_MAGIC);
  164. }
  165. static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
  166. {
  167. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  168. sp->magic = HWSIM_STA_MAGIC;
  169. }
  170. static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
  171. {
  172. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  173. sp->magic = 0;
  174. }
  175. static struct class *hwsim_class;
  176. static struct net_device *hwsim_mon; /* global monitor netdev */
  177. #define CHAN2G(_freq) { \
  178. .band = IEEE80211_BAND_2GHZ, \
  179. .center_freq = (_freq), \
  180. .hw_value = (_freq), \
  181. .max_power = 20, \
  182. }
  183. #define CHAN5G(_freq) { \
  184. .band = IEEE80211_BAND_5GHZ, \
  185. .center_freq = (_freq), \
  186. .hw_value = (_freq), \
  187. .max_power = 20, \
  188. }
  189. static const struct ieee80211_channel hwsim_channels_2ghz[] = {
  190. CHAN2G(2412), /* Channel 1 */
  191. CHAN2G(2417), /* Channel 2 */
  192. CHAN2G(2422), /* Channel 3 */
  193. CHAN2G(2427), /* Channel 4 */
  194. CHAN2G(2432), /* Channel 5 */
  195. CHAN2G(2437), /* Channel 6 */
  196. CHAN2G(2442), /* Channel 7 */
  197. CHAN2G(2447), /* Channel 8 */
  198. CHAN2G(2452), /* Channel 9 */
  199. CHAN2G(2457), /* Channel 10 */
  200. CHAN2G(2462), /* Channel 11 */
  201. CHAN2G(2467), /* Channel 12 */
  202. CHAN2G(2472), /* Channel 13 */
  203. CHAN2G(2484), /* Channel 14 */
  204. };
  205. static const struct ieee80211_channel hwsim_channels_5ghz[] = {
  206. CHAN5G(5180), /* Channel 36 */
  207. CHAN5G(5200), /* Channel 40 */
  208. CHAN5G(5220), /* Channel 44 */
  209. CHAN5G(5240), /* Channel 48 */
  210. CHAN5G(5260), /* Channel 52 */
  211. CHAN5G(5280), /* Channel 56 */
  212. CHAN5G(5300), /* Channel 60 */
  213. CHAN5G(5320), /* Channel 64 */
  214. CHAN5G(5500), /* Channel 100 */
  215. CHAN5G(5520), /* Channel 104 */
  216. CHAN5G(5540), /* Channel 108 */
  217. CHAN5G(5560), /* Channel 112 */
  218. CHAN5G(5580), /* Channel 116 */
  219. CHAN5G(5600), /* Channel 120 */
  220. CHAN5G(5620), /* Channel 124 */
  221. CHAN5G(5640), /* Channel 128 */
  222. CHAN5G(5660), /* Channel 132 */
  223. CHAN5G(5680), /* Channel 136 */
  224. CHAN5G(5700), /* Channel 140 */
  225. CHAN5G(5745), /* Channel 149 */
  226. CHAN5G(5765), /* Channel 153 */
  227. CHAN5G(5785), /* Channel 157 */
  228. CHAN5G(5805), /* Channel 161 */
  229. CHAN5G(5825), /* Channel 165 */
  230. };
  231. static const struct ieee80211_rate hwsim_rates[] = {
  232. { .bitrate = 10 },
  233. { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  234. { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  235. { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  236. { .bitrate = 60 },
  237. { .bitrate = 90 },
  238. { .bitrate = 120 },
  239. { .bitrate = 180 },
  240. { .bitrate = 240 },
  241. { .bitrate = 360 },
  242. { .bitrate = 480 },
  243. { .bitrate = 540 }
  244. };
  245. static spinlock_t hwsim_radio_lock;
  246. static struct list_head hwsim_radios;
  247. struct mac80211_hwsim_data {
  248. struct list_head list;
  249. struct ieee80211_hw *hw;
  250. struct device *dev;
  251. struct ieee80211_supported_band bands[2];
  252. struct ieee80211_channel channels_2ghz[ARRAY_SIZE(hwsim_channels_2ghz)];
  253. struct ieee80211_channel channels_5ghz[ARRAY_SIZE(hwsim_channels_5ghz)];
  254. struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
  255. struct mac_address addresses[2];
  256. struct ieee80211_channel *channel;
  257. unsigned long beacon_int; /* in jiffies unit */
  258. unsigned int rx_filter;
  259. bool started, idle, scanning;
  260. struct mutex mutex;
  261. struct timer_list beacon_timer;
  262. enum ps_mode {
  263. PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
  264. } ps;
  265. bool ps_poll_pending;
  266. struct dentry *debugfs;
  267. struct dentry *debugfs_ps;
  268. /*
  269. * Only radios in the same group can communicate together (the
  270. * channel has to match too). Each bit represents a group. A
  271. * radio can be in more then one group.
  272. */
  273. u64 group;
  274. struct dentry *debugfs_group;
  275. };
  276. struct hwsim_radiotap_hdr {
  277. struct ieee80211_radiotap_header hdr;
  278. u8 rt_flags;
  279. u8 rt_rate;
  280. __le16 rt_channel;
  281. __le16 rt_chbitmask;
  282. } __attribute__ ((packed));
  283. static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
  284. struct net_device *dev)
  285. {
  286. /* TODO: allow packet injection */
  287. dev_kfree_skb(skb);
  288. return NETDEV_TX_OK;
  289. }
  290. static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
  291. struct sk_buff *tx_skb)
  292. {
  293. struct mac80211_hwsim_data *data = hw->priv;
  294. struct sk_buff *skb;
  295. struct hwsim_radiotap_hdr *hdr;
  296. u16 flags;
  297. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
  298. struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
  299. if (!netif_running(hwsim_mon))
  300. return;
  301. skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
  302. if (skb == NULL)
  303. return;
  304. hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr));
  305. hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  306. hdr->hdr.it_pad = 0;
  307. hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
  308. hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  309. (1 << IEEE80211_RADIOTAP_RATE) |
  310. (1 << IEEE80211_RADIOTAP_CHANNEL));
  311. hdr->rt_flags = 0;
  312. hdr->rt_rate = txrate->bitrate / 5;
  313. hdr->rt_channel = cpu_to_le16(data->channel->center_freq);
  314. flags = IEEE80211_CHAN_2GHZ;
  315. if (txrate->flags & IEEE80211_RATE_ERP_G)
  316. flags |= IEEE80211_CHAN_OFDM;
  317. else
  318. flags |= IEEE80211_CHAN_CCK;
  319. hdr->rt_chbitmask = cpu_to_le16(flags);
  320. skb->dev = hwsim_mon;
  321. skb_set_mac_header(skb, 0);
  322. skb->ip_summed = CHECKSUM_UNNECESSARY;
  323. skb->pkt_type = PACKET_OTHERHOST;
  324. skb->protocol = htons(ETH_P_802_2);
  325. memset(skb->cb, 0, sizeof(skb->cb));
  326. netif_rx(skb);
  327. }
  328. static void mac80211_hwsim_monitor_ack(struct ieee80211_hw *hw, const u8 *addr)
  329. {
  330. struct mac80211_hwsim_data *data = hw->priv;
  331. struct sk_buff *skb;
  332. struct hwsim_radiotap_hdr *hdr;
  333. u16 flags;
  334. struct ieee80211_hdr *hdr11;
  335. if (!netif_running(hwsim_mon))
  336. return;
  337. skb = dev_alloc_skb(100);
  338. if (skb == NULL)
  339. return;
  340. hdr = (struct hwsim_radiotap_hdr *) skb_put(skb, sizeof(*hdr));
  341. hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  342. hdr->hdr.it_pad = 0;
  343. hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
  344. hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  345. (1 << IEEE80211_RADIOTAP_CHANNEL));
  346. hdr->rt_flags = 0;
  347. hdr->rt_rate = 0;
  348. hdr->rt_channel = cpu_to_le16(data->channel->center_freq);
  349. flags = IEEE80211_CHAN_2GHZ;
  350. hdr->rt_chbitmask = cpu_to_le16(flags);
  351. hdr11 = (struct ieee80211_hdr *) skb_put(skb, 10);
  352. hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  353. IEEE80211_STYPE_ACK);
  354. hdr11->duration_id = cpu_to_le16(0);
  355. memcpy(hdr11->addr1, addr, ETH_ALEN);
  356. skb->dev = hwsim_mon;
  357. skb_set_mac_header(skb, 0);
  358. skb->ip_summed = CHECKSUM_UNNECESSARY;
  359. skb->pkt_type = PACKET_OTHERHOST;
  360. skb->protocol = htons(ETH_P_802_2);
  361. memset(skb->cb, 0, sizeof(skb->cb));
  362. netif_rx(skb);
  363. }
  364. static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
  365. struct sk_buff *skb)
  366. {
  367. switch (data->ps) {
  368. case PS_DISABLED:
  369. return true;
  370. case PS_ENABLED:
  371. return false;
  372. case PS_AUTO_POLL:
  373. /* TODO: accept (some) Beacons by default and other frames only
  374. * if pending PS-Poll has been sent */
  375. return true;
  376. case PS_MANUAL_POLL:
  377. /* Allow unicast frames to own address if there is a pending
  378. * PS-Poll */
  379. if (data->ps_poll_pending &&
  380. memcmp(data->hw->wiphy->perm_addr, skb->data + 4,
  381. ETH_ALEN) == 0) {
  382. data->ps_poll_pending = false;
  383. return true;
  384. }
  385. return false;
  386. }
  387. return true;
  388. }
  389. struct mac80211_hwsim_addr_match_data {
  390. bool ret;
  391. const u8 *addr;
  392. };
  393. static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
  394. struct ieee80211_vif *vif)
  395. {
  396. struct mac80211_hwsim_addr_match_data *md = data;
  397. if (memcmp(mac, md->addr, ETH_ALEN) == 0)
  398. md->ret = true;
  399. }
  400. static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
  401. const u8 *addr)
  402. {
  403. struct mac80211_hwsim_addr_match_data md;
  404. if (memcmp(addr, data->hw->wiphy->perm_addr, ETH_ALEN) == 0)
  405. return true;
  406. md.ret = false;
  407. md.addr = addr;
  408. ieee80211_iterate_active_interfaces_atomic(data->hw,
  409. mac80211_hwsim_addr_iter,
  410. &md);
  411. return md.ret;
  412. }
  413. static bool mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
  414. struct sk_buff *skb)
  415. {
  416. struct mac80211_hwsim_data *data = hw->priv, *data2;
  417. bool ack = false;
  418. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  419. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  420. struct ieee80211_rx_status rx_status;
  421. if (data->idle) {
  422. printk(KERN_DEBUG "%s: Trying to TX when idle - reject\n",
  423. wiphy_name(hw->wiphy));
  424. return false;
  425. }
  426. memset(&rx_status, 0, sizeof(rx_status));
  427. /* TODO: set mactime */
  428. rx_status.freq = data->channel->center_freq;
  429. rx_status.band = data->channel->band;
  430. rx_status.rate_idx = info->control.rates[0].idx;
  431. /* TODO: simulate real signal strength (and optional packet loss) */
  432. rx_status.signal = -50;
  433. if (data->ps != PS_DISABLED)
  434. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  435. /* release the skb's source info */
  436. skb_orphan(skb);
  437. skb_dst_drop(skb);
  438. skb->mark = 0;
  439. secpath_reset(skb);
  440. nf_reset(skb);
  441. /* Copy skb to all enabled radios that are on the current frequency */
  442. spin_lock(&hwsim_radio_lock);
  443. list_for_each_entry(data2, &hwsim_radios, list) {
  444. struct sk_buff *nskb;
  445. if (data == data2)
  446. continue;
  447. if (data2->idle || !data2->started ||
  448. !hwsim_ps_rx_ok(data2, skb) ||
  449. !data->channel || !data2->channel ||
  450. data->channel->center_freq != data2->channel->center_freq ||
  451. !(data->group & data2->group))
  452. continue;
  453. nskb = skb_copy(skb, GFP_ATOMIC);
  454. if (nskb == NULL)
  455. continue;
  456. if (mac80211_hwsim_addr_match(data2, hdr->addr1))
  457. ack = true;
  458. memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
  459. ieee80211_rx_irqsafe(data2->hw, nskb);
  460. }
  461. spin_unlock(&hwsim_radio_lock);
  462. return ack;
  463. }
  464. static int mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  465. {
  466. bool ack;
  467. struct ieee80211_tx_info *txi;
  468. mac80211_hwsim_monitor_rx(hw, skb);
  469. if (skb->len < 10) {
  470. /* Should not happen; just a sanity check for addr1 use */
  471. dev_kfree_skb(skb);
  472. return NETDEV_TX_OK;
  473. }
  474. ack = mac80211_hwsim_tx_frame(hw, skb);
  475. if (ack && skb->len >= 16) {
  476. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  477. mac80211_hwsim_monitor_ack(hw, hdr->addr2);
  478. }
  479. txi = IEEE80211_SKB_CB(skb);
  480. if (txi->control.vif)
  481. hwsim_check_magic(txi->control.vif);
  482. if (txi->control.sta)
  483. hwsim_check_sta_magic(txi->control.sta);
  484. ieee80211_tx_info_clear_status(txi);
  485. if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
  486. txi->flags |= IEEE80211_TX_STAT_ACK;
  487. ieee80211_tx_status_irqsafe(hw, skb);
  488. return NETDEV_TX_OK;
  489. }
  490. static int mac80211_hwsim_start(struct ieee80211_hw *hw)
  491. {
  492. struct mac80211_hwsim_data *data = hw->priv;
  493. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  494. data->started = 1;
  495. return 0;
  496. }
  497. static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
  498. {
  499. struct mac80211_hwsim_data *data = hw->priv;
  500. data->started = 0;
  501. del_timer(&data->beacon_timer);
  502. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  503. }
  504. static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
  505. struct ieee80211_vif *vif)
  506. {
  507. printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%pM)\n",
  508. wiphy_name(hw->wiphy), __func__, vif->type,
  509. vif->addr);
  510. hwsim_set_magic(vif);
  511. return 0;
  512. }
  513. static void mac80211_hwsim_remove_interface(
  514. struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  515. {
  516. printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%pM)\n",
  517. wiphy_name(hw->wiphy), __func__, vif->type,
  518. vif->addr);
  519. hwsim_check_magic(vif);
  520. hwsim_clear_magic(vif);
  521. }
  522. static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
  523. struct ieee80211_vif *vif)
  524. {
  525. struct ieee80211_hw *hw = arg;
  526. struct sk_buff *skb;
  527. struct ieee80211_tx_info *info;
  528. hwsim_check_magic(vif);
  529. if (vif->type != NL80211_IFTYPE_AP &&
  530. vif->type != NL80211_IFTYPE_MESH_POINT)
  531. return;
  532. skb = ieee80211_beacon_get(hw, vif);
  533. if (skb == NULL)
  534. return;
  535. info = IEEE80211_SKB_CB(skb);
  536. mac80211_hwsim_monitor_rx(hw, skb);
  537. mac80211_hwsim_tx_frame(hw, skb);
  538. dev_kfree_skb(skb);
  539. }
  540. static void mac80211_hwsim_beacon(unsigned long arg)
  541. {
  542. struct ieee80211_hw *hw = (struct ieee80211_hw *) arg;
  543. struct mac80211_hwsim_data *data = hw->priv;
  544. if (!data->started)
  545. return;
  546. ieee80211_iterate_active_interfaces_atomic(
  547. hw, mac80211_hwsim_beacon_tx, hw);
  548. data->beacon_timer.expires = jiffies + data->beacon_int;
  549. add_timer(&data->beacon_timer);
  550. }
  551. static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
  552. {
  553. struct mac80211_hwsim_data *data = hw->priv;
  554. struct ieee80211_conf *conf = &hw->conf;
  555. static const char *chantypes[4] = {
  556. [NL80211_CHAN_NO_HT] = "noht",
  557. [NL80211_CHAN_HT20] = "ht20",
  558. [NL80211_CHAN_HT40MINUS] = "ht40-",
  559. [NL80211_CHAN_HT40PLUS] = "ht40+",
  560. };
  561. static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
  562. [IEEE80211_SMPS_AUTOMATIC] = "auto",
  563. [IEEE80211_SMPS_OFF] = "off",
  564. [IEEE80211_SMPS_STATIC] = "static",
  565. [IEEE80211_SMPS_DYNAMIC] = "dynamic",
  566. };
  567. printk(KERN_DEBUG "%s:%s (freq=%d/%s idle=%d ps=%d smps=%s)\n",
  568. wiphy_name(hw->wiphy), __func__,
  569. conf->channel->center_freq,
  570. chantypes[conf->channel_type],
  571. !!(conf->flags & IEEE80211_CONF_IDLE),
  572. !!(conf->flags & IEEE80211_CONF_PS),
  573. smps_modes[conf->smps_mode]);
  574. data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
  575. data->channel = conf->channel;
  576. if (!data->started || !data->beacon_int)
  577. del_timer(&data->beacon_timer);
  578. else
  579. mod_timer(&data->beacon_timer, jiffies + data->beacon_int);
  580. return 0;
  581. }
  582. static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
  583. unsigned int changed_flags,
  584. unsigned int *total_flags,u64 multicast)
  585. {
  586. struct mac80211_hwsim_data *data = hw->priv;
  587. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  588. data->rx_filter = 0;
  589. if (*total_flags & FIF_PROMISC_IN_BSS)
  590. data->rx_filter |= FIF_PROMISC_IN_BSS;
  591. if (*total_flags & FIF_ALLMULTI)
  592. data->rx_filter |= FIF_ALLMULTI;
  593. *total_flags = data->rx_filter;
  594. }
  595. static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
  596. struct ieee80211_vif *vif,
  597. struct ieee80211_bss_conf *info,
  598. u32 changed)
  599. {
  600. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  601. struct mac80211_hwsim_data *data = hw->priv;
  602. hwsim_check_magic(vif);
  603. printk(KERN_DEBUG "%s:%s(changed=0x%x)\n",
  604. wiphy_name(hw->wiphy), __func__, changed);
  605. if (changed & BSS_CHANGED_BSSID) {
  606. printk(KERN_DEBUG "%s:%s: BSSID changed: %pM\n",
  607. wiphy_name(hw->wiphy), __func__,
  608. info->bssid);
  609. memcpy(vp->bssid, info->bssid, ETH_ALEN);
  610. }
  611. if (changed & BSS_CHANGED_ASSOC) {
  612. printk(KERN_DEBUG " %s: ASSOC: assoc=%d aid=%d\n",
  613. wiphy_name(hw->wiphy), info->assoc, info->aid);
  614. vp->assoc = info->assoc;
  615. vp->aid = info->aid;
  616. }
  617. if (changed & BSS_CHANGED_BEACON_INT) {
  618. printk(KERN_DEBUG " %s: BCNINT: %d\n",
  619. wiphy_name(hw->wiphy), info->beacon_int);
  620. data->beacon_int = 1024 * info->beacon_int / 1000 * HZ / 1000;
  621. if (WARN_ON(!data->beacon_int))
  622. data->beacon_int = 1;
  623. if (data->started)
  624. mod_timer(&data->beacon_timer,
  625. jiffies + data->beacon_int);
  626. }
  627. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  628. printk(KERN_DEBUG " %s: ERP_CTS_PROT: %d\n",
  629. wiphy_name(hw->wiphy), info->use_cts_prot);
  630. }
  631. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  632. printk(KERN_DEBUG " %s: ERP_PREAMBLE: %d\n",
  633. wiphy_name(hw->wiphy), info->use_short_preamble);
  634. }
  635. if (changed & BSS_CHANGED_ERP_SLOT) {
  636. printk(KERN_DEBUG " %s: ERP_SLOT: %d\n",
  637. wiphy_name(hw->wiphy), info->use_short_slot);
  638. }
  639. if (changed & BSS_CHANGED_HT) {
  640. printk(KERN_DEBUG " %s: HT: op_mode=0x%x\n",
  641. wiphy_name(hw->wiphy),
  642. info->ht_operation_mode);
  643. }
  644. if (changed & BSS_CHANGED_BASIC_RATES) {
  645. printk(KERN_DEBUG " %s: BASIC_RATES: 0x%llx\n",
  646. wiphy_name(hw->wiphy),
  647. (unsigned long long) info->basic_rates);
  648. }
  649. }
  650. static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
  651. struct ieee80211_vif *vif,
  652. struct ieee80211_sta *sta)
  653. {
  654. hwsim_check_magic(vif);
  655. hwsim_set_sta_magic(sta);
  656. return 0;
  657. }
  658. static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
  659. struct ieee80211_vif *vif,
  660. struct ieee80211_sta *sta)
  661. {
  662. hwsim_check_magic(vif);
  663. hwsim_clear_sta_magic(sta);
  664. return 0;
  665. }
  666. static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
  667. struct ieee80211_vif *vif,
  668. enum sta_notify_cmd cmd,
  669. struct ieee80211_sta *sta)
  670. {
  671. hwsim_check_magic(vif);
  672. switch (cmd) {
  673. case STA_NOTIFY_SLEEP:
  674. case STA_NOTIFY_AWAKE:
  675. /* TODO: make good use of these flags */
  676. break;
  677. default:
  678. WARN(1, "Invalid sta notify: %d\n", cmd);
  679. break;
  680. }
  681. }
  682. static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
  683. struct ieee80211_sta *sta,
  684. bool set)
  685. {
  686. hwsim_check_sta_magic(sta);
  687. return 0;
  688. }
  689. static int mac80211_hwsim_conf_tx(
  690. struct ieee80211_hw *hw, u16 queue,
  691. const struct ieee80211_tx_queue_params *params)
  692. {
  693. printk(KERN_DEBUG "%s:%s (queue=%d txop=%d cw_min=%d cw_max=%d "
  694. "aifs=%d)\n",
  695. wiphy_name(hw->wiphy), __func__, queue,
  696. params->txop, params->cw_min, params->cw_max, params->aifs);
  697. return 0;
  698. }
  699. static int mac80211_hwsim_get_survey(
  700. struct ieee80211_hw *hw, int idx,
  701. struct survey_info *survey)
  702. {
  703. struct ieee80211_conf *conf = &hw->conf;
  704. printk(KERN_DEBUG "%s:%s (idx=%d)\n",
  705. wiphy_name(hw->wiphy), __func__, idx);
  706. if (idx != 0)
  707. return -ENOENT;
  708. /* Current channel */
  709. survey->channel = conf->channel;
  710. /*
  711. * Magically conjured noise level --- this is only ok for simulated hardware.
  712. *
  713. * A real driver which cannot determine the real channel noise MUST NOT
  714. * report any noise, especially not a magically conjured one :-)
  715. */
  716. survey->filled = SURVEY_INFO_NOISE_DBM;
  717. survey->noise = -92;
  718. return 0;
  719. }
  720. #ifdef CONFIG_NL80211_TESTMODE
  721. /*
  722. * This section contains example code for using netlink
  723. * attributes with the testmode command in nl80211.
  724. */
  725. /* These enums need to be kept in sync with userspace */
  726. enum hwsim_testmode_attr {
  727. __HWSIM_TM_ATTR_INVALID = 0,
  728. HWSIM_TM_ATTR_CMD = 1,
  729. HWSIM_TM_ATTR_PS = 2,
  730. /* keep last */
  731. __HWSIM_TM_ATTR_AFTER_LAST,
  732. HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
  733. };
  734. enum hwsim_testmode_cmd {
  735. HWSIM_TM_CMD_SET_PS = 0,
  736. HWSIM_TM_CMD_GET_PS = 1,
  737. };
  738. static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
  739. [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
  740. [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
  741. };
  742. static int hwsim_fops_ps_write(void *dat, u64 val);
  743. static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
  744. void *data, int len)
  745. {
  746. struct mac80211_hwsim_data *hwsim = hw->priv;
  747. struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
  748. struct sk_buff *skb;
  749. int err, ps;
  750. err = nla_parse(tb, HWSIM_TM_ATTR_MAX, data, len,
  751. hwsim_testmode_policy);
  752. if (err)
  753. return err;
  754. if (!tb[HWSIM_TM_ATTR_CMD])
  755. return -EINVAL;
  756. switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
  757. case HWSIM_TM_CMD_SET_PS:
  758. if (!tb[HWSIM_TM_ATTR_PS])
  759. return -EINVAL;
  760. ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
  761. return hwsim_fops_ps_write(hwsim, ps);
  762. case HWSIM_TM_CMD_GET_PS:
  763. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
  764. nla_total_size(sizeof(u32)));
  765. if (!skb)
  766. return -ENOMEM;
  767. NLA_PUT_U32(skb, HWSIM_TM_ATTR_PS, hwsim->ps);
  768. return cfg80211_testmode_reply(skb);
  769. default:
  770. return -EOPNOTSUPP;
  771. }
  772. nla_put_failure:
  773. kfree_skb(skb);
  774. return -ENOBUFS;
  775. }
  776. #endif
  777. static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
  778. struct ieee80211_vif *vif,
  779. enum ieee80211_ampdu_mlme_action action,
  780. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  781. {
  782. switch (action) {
  783. case IEEE80211_AMPDU_TX_START:
  784. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  785. break;
  786. case IEEE80211_AMPDU_TX_STOP:
  787. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  788. break;
  789. case IEEE80211_AMPDU_TX_OPERATIONAL:
  790. break;
  791. case IEEE80211_AMPDU_RX_START:
  792. case IEEE80211_AMPDU_RX_STOP:
  793. break;
  794. default:
  795. return -EOPNOTSUPP;
  796. }
  797. return 0;
  798. }
  799. static void mac80211_hwsim_flush(struct ieee80211_hw *hw, bool drop)
  800. {
  801. /*
  802. * In this special case, there's nothing we need to
  803. * do because hwsim does transmission synchronously.
  804. * In the future, when it does transmissions via
  805. * userspace, we may need to do something.
  806. */
  807. }
  808. struct hw_scan_done {
  809. struct delayed_work w;
  810. struct ieee80211_hw *hw;
  811. };
  812. static void hw_scan_done(struct work_struct *work)
  813. {
  814. struct hw_scan_done *hsd =
  815. container_of(work, struct hw_scan_done, w.work);
  816. ieee80211_scan_completed(hsd->hw, false);
  817. kfree(hsd);
  818. }
  819. static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
  820. struct ieee80211_vif *vif,
  821. struct cfg80211_scan_request *req)
  822. {
  823. struct hw_scan_done *hsd = kzalloc(sizeof(*hsd), GFP_KERNEL);
  824. int i;
  825. if (!hsd)
  826. return -ENOMEM;
  827. hsd->hw = hw;
  828. INIT_DELAYED_WORK(&hsd->w, hw_scan_done);
  829. printk(KERN_DEBUG "hwsim hw_scan request\n");
  830. for (i = 0; i < req->n_channels; i++)
  831. printk(KERN_DEBUG "hwsim hw_scan freq %d\n",
  832. req->channels[i]->center_freq);
  833. ieee80211_queue_delayed_work(hw, &hsd->w, 2 * HZ);
  834. return 0;
  835. }
  836. static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw)
  837. {
  838. struct mac80211_hwsim_data *hwsim = hw->priv;
  839. mutex_lock(&hwsim->mutex);
  840. if (hwsim->scanning) {
  841. printk(KERN_DEBUG "two hwsim sw_scans detected!\n");
  842. goto out;
  843. }
  844. printk(KERN_DEBUG "hwsim sw_scan request, prepping stuff\n");
  845. hwsim->scanning = true;
  846. out:
  847. mutex_unlock(&hwsim->mutex);
  848. }
  849. static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw)
  850. {
  851. struct mac80211_hwsim_data *hwsim = hw->priv;
  852. mutex_lock(&hwsim->mutex);
  853. printk(KERN_DEBUG "hwsim sw_scan_complete\n");
  854. hwsim->scanning = true;
  855. mutex_unlock(&hwsim->mutex);
  856. }
  857. static struct ieee80211_ops mac80211_hwsim_ops =
  858. {
  859. .tx = mac80211_hwsim_tx,
  860. .start = mac80211_hwsim_start,
  861. .stop = mac80211_hwsim_stop,
  862. .add_interface = mac80211_hwsim_add_interface,
  863. .remove_interface = mac80211_hwsim_remove_interface,
  864. .config = mac80211_hwsim_config,
  865. .configure_filter = mac80211_hwsim_configure_filter,
  866. .bss_info_changed = mac80211_hwsim_bss_info_changed,
  867. .sta_add = mac80211_hwsim_sta_add,
  868. .sta_remove = mac80211_hwsim_sta_remove,
  869. .sta_notify = mac80211_hwsim_sta_notify,
  870. .set_tim = mac80211_hwsim_set_tim,
  871. .conf_tx = mac80211_hwsim_conf_tx,
  872. .get_survey = mac80211_hwsim_get_survey,
  873. CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd)
  874. .ampdu_action = mac80211_hwsim_ampdu_action,
  875. .sw_scan_start = mac80211_hwsim_sw_scan,
  876. .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
  877. .flush = mac80211_hwsim_flush,
  878. };
  879. static void mac80211_hwsim_free(void)
  880. {
  881. struct list_head tmplist, *i, *tmp;
  882. struct mac80211_hwsim_data *data, *tmpdata;
  883. INIT_LIST_HEAD(&tmplist);
  884. spin_lock_bh(&hwsim_radio_lock);
  885. list_for_each_safe(i, tmp, &hwsim_radios)
  886. list_move(i, &tmplist);
  887. spin_unlock_bh(&hwsim_radio_lock);
  888. list_for_each_entry_safe(data, tmpdata, &tmplist, list) {
  889. debugfs_remove(data->debugfs_group);
  890. debugfs_remove(data->debugfs_ps);
  891. debugfs_remove(data->debugfs);
  892. ieee80211_unregister_hw(data->hw);
  893. device_unregister(data->dev);
  894. ieee80211_free_hw(data->hw);
  895. }
  896. class_destroy(hwsim_class);
  897. }
  898. static struct device_driver mac80211_hwsim_driver = {
  899. .name = "mac80211_hwsim"
  900. };
  901. static const struct net_device_ops hwsim_netdev_ops = {
  902. .ndo_start_xmit = hwsim_mon_xmit,
  903. .ndo_change_mtu = eth_change_mtu,
  904. .ndo_set_mac_address = eth_mac_addr,
  905. .ndo_validate_addr = eth_validate_addr,
  906. };
  907. static void hwsim_mon_setup(struct net_device *dev)
  908. {
  909. dev->netdev_ops = &hwsim_netdev_ops;
  910. dev->destructor = free_netdev;
  911. ether_setup(dev);
  912. dev->tx_queue_len = 0;
  913. dev->type = ARPHRD_IEEE80211_RADIOTAP;
  914. memset(dev->dev_addr, 0, ETH_ALEN);
  915. dev->dev_addr[0] = 0x12;
  916. }
  917. static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
  918. {
  919. struct mac80211_hwsim_data *data = dat;
  920. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  921. struct sk_buff *skb;
  922. struct ieee80211_pspoll *pspoll;
  923. if (!vp->assoc)
  924. return;
  925. printk(KERN_DEBUG "%s:%s: send PS-Poll to %pM for aid %d\n",
  926. wiphy_name(data->hw->wiphy), __func__, vp->bssid, vp->aid);
  927. skb = dev_alloc_skb(sizeof(*pspoll));
  928. if (!skb)
  929. return;
  930. pspoll = (void *) skb_put(skb, sizeof(*pspoll));
  931. pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  932. IEEE80211_STYPE_PSPOLL |
  933. IEEE80211_FCTL_PM);
  934. pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
  935. memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
  936. memcpy(pspoll->ta, mac, ETH_ALEN);
  937. if (!mac80211_hwsim_tx_frame(data->hw, skb))
  938. printk(KERN_DEBUG "%s: PS-Poll frame not ack'ed\n", __func__);
  939. dev_kfree_skb(skb);
  940. }
  941. static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
  942. struct ieee80211_vif *vif, int ps)
  943. {
  944. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  945. struct sk_buff *skb;
  946. struct ieee80211_hdr *hdr;
  947. if (!vp->assoc)
  948. return;
  949. printk(KERN_DEBUG "%s:%s: send data::nullfunc to %pM ps=%d\n",
  950. wiphy_name(data->hw->wiphy), __func__, vp->bssid, ps);
  951. skb = dev_alloc_skb(sizeof(*hdr));
  952. if (!skb)
  953. return;
  954. hdr = (void *) skb_put(skb, sizeof(*hdr) - ETH_ALEN);
  955. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  956. IEEE80211_STYPE_NULLFUNC |
  957. (ps ? IEEE80211_FCTL_PM : 0));
  958. hdr->duration_id = cpu_to_le16(0);
  959. memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
  960. memcpy(hdr->addr2, mac, ETH_ALEN);
  961. memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
  962. if (!mac80211_hwsim_tx_frame(data->hw, skb))
  963. printk(KERN_DEBUG "%s: nullfunc frame not ack'ed\n", __func__);
  964. dev_kfree_skb(skb);
  965. }
  966. static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
  967. struct ieee80211_vif *vif)
  968. {
  969. struct mac80211_hwsim_data *data = dat;
  970. hwsim_send_nullfunc(data, mac, vif, 1);
  971. }
  972. static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
  973. struct ieee80211_vif *vif)
  974. {
  975. struct mac80211_hwsim_data *data = dat;
  976. hwsim_send_nullfunc(data, mac, vif, 0);
  977. }
  978. static int hwsim_fops_ps_read(void *dat, u64 *val)
  979. {
  980. struct mac80211_hwsim_data *data = dat;
  981. *val = data->ps;
  982. return 0;
  983. }
  984. static int hwsim_fops_ps_write(void *dat, u64 val)
  985. {
  986. struct mac80211_hwsim_data *data = dat;
  987. enum ps_mode old_ps;
  988. if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
  989. val != PS_MANUAL_POLL)
  990. return -EINVAL;
  991. old_ps = data->ps;
  992. data->ps = val;
  993. if (val == PS_MANUAL_POLL) {
  994. ieee80211_iterate_active_interfaces(data->hw,
  995. hwsim_send_ps_poll, data);
  996. data->ps_poll_pending = true;
  997. } else if (old_ps == PS_DISABLED && val != PS_DISABLED) {
  998. ieee80211_iterate_active_interfaces(data->hw,
  999. hwsim_send_nullfunc_ps,
  1000. data);
  1001. } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
  1002. ieee80211_iterate_active_interfaces(data->hw,
  1003. hwsim_send_nullfunc_no_ps,
  1004. data);
  1005. }
  1006. return 0;
  1007. }
  1008. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
  1009. "%llu\n");
  1010. static int hwsim_fops_group_read(void *dat, u64 *val)
  1011. {
  1012. struct mac80211_hwsim_data *data = dat;
  1013. *val = data->group;
  1014. return 0;
  1015. }
  1016. static int hwsim_fops_group_write(void *dat, u64 val)
  1017. {
  1018. struct mac80211_hwsim_data *data = dat;
  1019. data->group = val;
  1020. return 0;
  1021. }
  1022. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
  1023. hwsim_fops_group_read, hwsim_fops_group_write,
  1024. "%llx\n");
  1025. static int __init init_mac80211_hwsim(void)
  1026. {
  1027. int i, err = 0;
  1028. u8 addr[ETH_ALEN];
  1029. struct mac80211_hwsim_data *data;
  1030. struct ieee80211_hw *hw;
  1031. enum ieee80211_band band;
  1032. if (radios < 1 || radios > 100)
  1033. return -EINVAL;
  1034. if (fake_hw_scan) {
  1035. mac80211_hwsim_ops.hw_scan = mac80211_hwsim_hw_scan;
  1036. mac80211_hwsim_ops.sw_scan_start = NULL;
  1037. mac80211_hwsim_ops.sw_scan_complete = NULL;
  1038. }
  1039. spin_lock_init(&hwsim_radio_lock);
  1040. INIT_LIST_HEAD(&hwsim_radios);
  1041. hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
  1042. if (IS_ERR(hwsim_class))
  1043. return PTR_ERR(hwsim_class);
  1044. memset(addr, 0, ETH_ALEN);
  1045. addr[0] = 0x02;
  1046. for (i = 0; i < radios; i++) {
  1047. printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
  1048. i);
  1049. hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
  1050. if (!hw) {
  1051. printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
  1052. "failed\n");
  1053. err = -ENOMEM;
  1054. goto failed;
  1055. }
  1056. data = hw->priv;
  1057. data->hw = hw;
  1058. data->dev = device_create(hwsim_class, NULL, 0, hw,
  1059. "hwsim%d", i);
  1060. if (IS_ERR(data->dev)) {
  1061. printk(KERN_DEBUG
  1062. "mac80211_hwsim: device_create "
  1063. "failed (%ld)\n", PTR_ERR(data->dev));
  1064. err = -ENOMEM;
  1065. goto failed_drvdata;
  1066. }
  1067. data->dev->driver = &mac80211_hwsim_driver;
  1068. SET_IEEE80211_DEV(hw, data->dev);
  1069. addr[3] = i >> 8;
  1070. addr[4] = i;
  1071. memcpy(data->addresses[0].addr, addr, ETH_ALEN);
  1072. memcpy(data->addresses[1].addr, addr, ETH_ALEN);
  1073. data->addresses[1].addr[0] |= 0x40;
  1074. hw->wiphy->n_addresses = 2;
  1075. hw->wiphy->addresses = data->addresses;
  1076. hw->channel_change_time = 1;
  1077. hw->queues = 4;
  1078. hw->wiphy->interface_modes =
  1079. BIT(NL80211_IFTYPE_STATION) |
  1080. BIT(NL80211_IFTYPE_AP) |
  1081. BIT(NL80211_IFTYPE_MESH_POINT);
  1082. hw->flags = IEEE80211_HW_MFP_CAPABLE |
  1083. IEEE80211_HW_SIGNAL_DBM |
  1084. IEEE80211_HW_SUPPORTS_STATIC_SMPS |
  1085. IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
  1086. IEEE80211_HW_AMPDU_AGGREGATION;
  1087. /* ask mac80211 to reserve space for magic */
  1088. hw->vif_data_size = sizeof(struct hwsim_vif_priv);
  1089. hw->sta_data_size = sizeof(struct hwsim_sta_priv);
  1090. memcpy(data->channels_2ghz, hwsim_channels_2ghz,
  1091. sizeof(hwsim_channels_2ghz));
  1092. memcpy(data->channels_5ghz, hwsim_channels_5ghz,
  1093. sizeof(hwsim_channels_5ghz));
  1094. memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
  1095. for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
  1096. struct ieee80211_supported_band *sband = &data->bands[band];
  1097. switch (band) {
  1098. case IEEE80211_BAND_2GHZ:
  1099. sband->channels = data->channels_2ghz;
  1100. sband->n_channels =
  1101. ARRAY_SIZE(hwsim_channels_2ghz);
  1102. sband->bitrates = data->rates;
  1103. sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
  1104. break;
  1105. case IEEE80211_BAND_5GHZ:
  1106. sband->channels = data->channels_5ghz;
  1107. sband->n_channels =
  1108. ARRAY_SIZE(hwsim_channels_5ghz);
  1109. sband->bitrates = data->rates + 4;
  1110. sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
  1111. break;
  1112. default:
  1113. break;
  1114. }
  1115. sband->ht_cap.ht_supported = true;
  1116. sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  1117. IEEE80211_HT_CAP_GRN_FLD |
  1118. IEEE80211_HT_CAP_SGI_40 |
  1119. IEEE80211_HT_CAP_DSSSCCK40;
  1120. sband->ht_cap.ampdu_factor = 0x3;
  1121. sband->ht_cap.ampdu_density = 0x6;
  1122. memset(&sband->ht_cap.mcs, 0,
  1123. sizeof(sband->ht_cap.mcs));
  1124. sband->ht_cap.mcs.rx_mask[0] = 0xff;
  1125. sband->ht_cap.mcs.rx_mask[1] = 0xff;
  1126. sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  1127. hw->wiphy->bands[band] = sband;
  1128. }
  1129. /* By default all radios are belonging to the first group */
  1130. data->group = 1;
  1131. mutex_init(&data->mutex);
  1132. /* Work to be done prior to ieee80211_register_hw() */
  1133. switch (regtest) {
  1134. case HWSIM_REGTEST_DISABLED:
  1135. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  1136. case HWSIM_REGTEST_DRIVER_REG_ALL:
  1137. case HWSIM_REGTEST_DIFF_COUNTRY:
  1138. /*
  1139. * Nothing to be done for driver regulatory domain
  1140. * hints prior to ieee80211_register_hw()
  1141. */
  1142. break;
  1143. case HWSIM_REGTEST_WORLD_ROAM:
  1144. if (i == 0) {
  1145. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1146. wiphy_apply_custom_regulatory(hw->wiphy,
  1147. &hwsim_world_regdom_custom_01);
  1148. }
  1149. break;
  1150. case HWSIM_REGTEST_CUSTOM_WORLD:
  1151. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1152. wiphy_apply_custom_regulatory(hw->wiphy,
  1153. &hwsim_world_regdom_custom_01);
  1154. break;
  1155. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  1156. if (i == 0) {
  1157. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1158. wiphy_apply_custom_regulatory(hw->wiphy,
  1159. &hwsim_world_regdom_custom_01);
  1160. } else if (i == 1) {
  1161. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1162. wiphy_apply_custom_regulatory(hw->wiphy,
  1163. &hwsim_world_regdom_custom_02);
  1164. }
  1165. break;
  1166. case HWSIM_REGTEST_STRICT_ALL:
  1167. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1168. break;
  1169. case HWSIM_REGTEST_STRICT_FOLLOW:
  1170. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  1171. if (i == 0)
  1172. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1173. break;
  1174. case HWSIM_REGTEST_ALL:
  1175. if (i == 0) {
  1176. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1177. wiphy_apply_custom_regulatory(hw->wiphy,
  1178. &hwsim_world_regdom_custom_01);
  1179. } else if (i == 1) {
  1180. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1181. wiphy_apply_custom_regulatory(hw->wiphy,
  1182. &hwsim_world_regdom_custom_02);
  1183. } else if (i == 4)
  1184. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1185. break;
  1186. default:
  1187. break;
  1188. }
  1189. /* give the regulatory workqueue a chance to run */
  1190. if (regtest)
  1191. schedule_timeout_interruptible(1);
  1192. err = ieee80211_register_hw(hw);
  1193. if (err < 0) {
  1194. printk(KERN_DEBUG "mac80211_hwsim: "
  1195. "ieee80211_register_hw failed (%d)\n", err);
  1196. goto failed_hw;
  1197. }
  1198. /* Work to be done after to ieee80211_register_hw() */
  1199. switch (regtest) {
  1200. case HWSIM_REGTEST_WORLD_ROAM:
  1201. case HWSIM_REGTEST_DISABLED:
  1202. break;
  1203. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  1204. if (!i)
  1205. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1206. break;
  1207. case HWSIM_REGTEST_DRIVER_REG_ALL:
  1208. case HWSIM_REGTEST_STRICT_ALL:
  1209. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1210. break;
  1211. case HWSIM_REGTEST_DIFF_COUNTRY:
  1212. if (i < ARRAY_SIZE(hwsim_alpha2s))
  1213. regulatory_hint(hw->wiphy, hwsim_alpha2s[i]);
  1214. break;
  1215. case HWSIM_REGTEST_CUSTOM_WORLD:
  1216. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  1217. /*
  1218. * Nothing to be done for custom world regulatory
  1219. * domains after to ieee80211_register_hw
  1220. */
  1221. break;
  1222. case HWSIM_REGTEST_STRICT_FOLLOW:
  1223. if (i == 0)
  1224. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1225. break;
  1226. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  1227. if (i == 0)
  1228. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1229. else if (i == 1)
  1230. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  1231. break;
  1232. case HWSIM_REGTEST_ALL:
  1233. if (i == 2)
  1234. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1235. else if (i == 3)
  1236. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  1237. else if (i == 4)
  1238. regulatory_hint(hw->wiphy, hwsim_alpha2s[2]);
  1239. break;
  1240. default:
  1241. break;
  1242. }
  1243. printk(KERN_DEBUG "%s: hwaddr %pM registered\n",
  1244. wiphy_name(hw->wiphy),
  1245. hw->wiphy->perm_addr);
  1246. data->debugfs = debugfs_create_dir("hwsim",
  1247. hw->wiphy->debugfsdir);
  1248. data->debugfs_ps = debugfs_create_file("ps", 0666,
  1249. data->debugfs, data,
  1250. &hwsim_fops_ps);
  1251. data->debugfs_group = debugfs_create_file("group", 0666,
  1252. data->debugfs, data,
  1253. &hwsim_fops_group);
  1254. setup_timer(&data->beacon_timer, mac80211_hwsim_beacon,
  1255. (unsigned long) hw);
  1256. list_add_tail(&data->list, &hwsim_radios);
  1257. }
  1258. hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
  1259. if (hwsim_mon == NULL)
  1260. goto failed;
  1261. rtnl_lock();
  1262. err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
  1263. if (err < 0)
  1264. goto failed_mon;
  1265. err = register_netdevice(hwsim_mon);
  1266. if (err < 0)
  1267. goto failed_mon;
  1268. rtnl_unlock();
  1269. return 0;
  1270. failed_mon:
  1271. rtnl_unlock();
  1272. free_netdev(hwsim_mon);
  1273. mac80211_hwsim_free();
  1274. return err;
  1275. failed_hw:
  1276. device_unregister(data->dev);
  1277. failed_drvdata:
  1278. ieee80211_free_hw(hw);
  1279. failed:
  1280. mac80211_hwsim_free();
  1281. return err;
  1282. }
  1283. static void __exit exit_mac80211_hwsim(void)
  1284. {
  1285. printk(KERN_DEBUG "mac80211_hwsim: unregister radios\n");
  1286. mac80211_hwsim_free();
  1287. unregister_netdev(hwsim_mon);
  1288. }
  1289. module_init(init_mac80211_hwsim);
  1290. module_exit(exit_mac80211_hwsim);