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