mac80211_hwsim.c 40 KB

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