mac80211_hwsim.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560
  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. int power_level;
  278. };
  279. struct hwsim_radiotap_hdr {
  280. struct ieee80211_radiotap_header hdr;
  281. u8 rt_flags;
  282. u8 rt_rate;
  283. __le16 rt_channel;
  284. __le16 rt_chbitmask;
  285. } __packed;
  286. static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
  287. struct net_device *dev)
  288. {
  289. /* TODO: allow packet injection */
  290. dev_kfree_skb(skb);
  291. return NETDEV_TX_OK;
  292. }
  293. static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
  294. struct sk_buff *tx_skb)
  295. {
  296. struct mac80211_hwsim_data *data = hw->priv;
  297. struct sk_buff *skb;
  298. struct hwsim_radiotap_hdr *hdr;
  299. u16 flags;
  300. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
  301. struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
  302. if (!netif_running(hwsim_mon))
  303. return;
  304. skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
  305. if (skb == NULL)
  306. return;
  307. hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr));
  308. hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  309. hdr->hdr.it_pad = 0;
  310. hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
  311. hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  312. (1 << IEEE80211_RADIOTAP_RATE) |
  313. (1 << IEEE80211_RADIOTAP_CHANNEL));
  314. hdr->rt_flags = 0;
  315. hdr->rt_rate = txrate->bitrate / 5;
  316. hdr->rt_channel = cpu_to_le16(data->channel->center_freq);
  317. flags = IEEE80211_CHAN_2GHZ;
  318. if (txrate->flags & IEEE80211_RATE_ERP_G)
  319. flags |= IEEE80211_CHAN_OFDM;
  320. else
  321. flags |= IEEE80211_CHAN_CCK;
  322. hdr->rt_chbitmask = cpu_to_le16(flags);
  323. skb->dev = hwsim_mon;
  324. skb_set_mac_header(skb, 0);
  325. skb->ip_summed = CHECKSUM_UNNECESSARY;
  326. skb->pkt_type = PACKET_OTHERHOST;
  327. skb->protocol = htons(ETH_P_802_2);
  328. memset(skb->cb, 0, sizeof(skb->cb));
  329. netif_rx(skb);
  330. }
  331. static void mac80211_hwsim_monitor_ack(struct ieee80211_hw *hw, const u8 *addr)
  332. {
  333. struct mac80211_hwsim_data *data = hw->priv;
  334. struct sk_buff *skb;
  335. struct hwsim_radiotap_hdr *hdr;
  336. u16 flags;
  337. struct ieee80211_hdr *hdr11;
  338. if (!netif_running(hwsim_mon))
  339. return;
  340. skb = dev_alloc_skb(100);
  341. if (skb == NULL)
  342. return;
  343. hdr = (struct hwsim_radiotap_hdr *) skb_put(skb, sizeof(*hdr));
  344. hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  345. hdr->hdr.it_pad = 0;
  346. hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
  347. hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  348. (1 << IEEE80211_RADIOTAP_CHANNEL));
  349. hdr->rt_flags = 0;
  350. hdr->rt_rate = 0;
  351. hdr->rt_channel = cpu_to_le16(data->channel->center_freq);
  352. flags = IEEE80211_CHAN_2GHZ;
  353. hdr->rt_chbitmask = cpu_to_le16(flags);
  354. hdr11 = (struct ieee80211_hdr *) skb_put(skb, 10);
  355. hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  356. IEEE80211_STYPE_ACK);
  357. hdr11->duration_id = cpu_to_le16(0);
  358. memcpy(hdr11->addr1, addr, ETH_ALEN);
  359. skb->dev = hwsim_mon;
  360. skb_set_mac_header(skb, 0);
  361. skb->ip_summed = CHECKSUM_UNNECESSARY;
  362. skb->pkt_type = PACKET_OTHERHOST;
  363. skb->protocol = htons(ETH_P_802_2);
  364. memset(skb->cb, 0, sizeof(skb->cb));
  365. netif_rx(skb);
  366. }
  367. static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
  368. struct sk_buff *skb)
  369. {
  370. switch (data->ps) {
  371. case PS_DISABLED:
  372. return true;
  373. case PS_ENABLED:
  374. return false;
  375. case PS_AUTO_POLL:
  376. /* TODO: accept (some) Beacons by default and other frames only
  377. * if pending PS-Poll has been sent */
  378. return true;
  379. case PS_MANUAL_POLL:
  380. /* Allow unicast frames to own address if there is a pending
  381. * PS-Poll */
  382. if (data->ps_poll_pending &&
  383. memcmp(data->hw->wiphy->perm_addr, skb->data + 4,
  384. ETH_ALEN) == 0) {
  385. data->ps_poll_pending = false;
  386. return true;
  387. }
  388. return false;
  389. }
  390. return true;
  391. }
  392. struct mac80211_hwsim_addr_match_data {
  393. bool ret;
  394. const u8 *addr;
  395. };
  396. static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
  397. struct ieee80211_vif *vif)
  398. {
  399. struct mac80211_hwsim_addr_match_data *md = data;
  400. if (memcmp(mac, md->addr, ETH_ALEN) == 0)
  401. md->ret = true;
  402. }
  403. static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
  404. const u8 *addr)
  405. {
  406. struct mac80211_hwsim_addr_match_data md;
  407. if (memcmp(addr, data->hw->wiphy->perm_addr, ETH_ALEN) == 0)
  408. return true;
  409. md.ret = false;
  410. md.addr = addr;
  411. ieee80211_iterate_active_interfaces_atomic(data->hw,
  412. mac80211_hwsim_addr_iter,
  413. &md);
  414. return md.ret;
  415. }
  416. static bool mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
  417. struct sk_buff *skb)
  418. {
  419. struct mac80211_hwsim_data *data = hw->priv, *data2;
  420. bool ack = false;
  421. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  422. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  423. struct ieee80211_rx_status rx_status;
  424. if (data->idle) {
  425. wiphy_debug(hw->wiphy, "Trying to TX when idle - reject\n");
  426. return false;
  427. }
  428. memset(&rx_status, 0, sizeof(rx_status));
  429. /* TODO: set mactime */
  430. rx_status.freq = data->channel->center_freq;
  431. rx_status.band = data->channel->band;
  432. rx_status.rate_idx = info->control.rates[0].idx;
  433. /* TODO: simulate real signal strength (and optional packet loss) */
  434. rx_status.signal = data->power_level - 50;
  435. if (data->ps != PS_DISABLED)
  436. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  437. /* release the skb's source info */
  438. skb_orphan(skb);
  439. skb_dst_drop(skb);
  440. skb->mark = 0;
  441. secpath_reset(skb);
  442. nf_reset(skb);
  443. /* Copy skb to all enabled radios that are on the current frequency */
  444. spin_lock(&hwsim_radio_lock);
  445. list_for_each_entry(data2, &hwsim_radios, list) {
  446. struct sk_buff *nskb;
  447. if (data == data2)
  448. continue;
  449. if (data2->idle || !data2->started ||
  450. !hwsim_ps_rx_ok(data2, skb) ||
  451. !data->channel || !data2->channel ||
  452. data->channel->center_freq != data2->channel->center_freq ||
  453. !(data->group & data2->group))
  454. continue;
  455. nskb = skb_copy(skb, GFP_ATOMIC);
  456. if (nskb == NULL)
  457. continue;
  458. if (mac80211_hwsim_addr_match(data2, hdr->addr1))
  459. ack = true;
  460. memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
  461. ieee80211_rx_irqsafe(data2->hw, nskb);
  462. }
  463. spin_unlock(&hwsim_radio_lock);
  464. return ack;
  465. }
  466. static int mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  467. {
  468. bool ack;
  469. struct ieee80211_tx_info *txi;
  470. mac80211_hwsim_monitor_rx(hw, skb);
  471. if (skb->len < 10) {
  472. /* Should not happen; just a sanity check for addr1 use */
  473. dev_kfree_skb(skb);
  474. return NETDEV_TX_OK;
  475. }
  476. ack = mac80211_hwsim_tx_frame(hw, skb);
  477. if (ack && skb->len >= 16) {
  478. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  479. mac80211_hwsim_monitor_ack(hw, hdr->addr2);
  480. }
  481. txi = IEEE80211_SKB_CB(skb);
  482. if (txi->control.vif)
  483. hwsim_check_magic(txi->control.vif);
  484. if (txi->control.sta)
  485. hwsim_check_sta_magic(txi->control.sta);
  486. ieee80211_tx_info_clear_status(txi);
  487. if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
  488. txi->flags |= IEEE80211_TX_STAT_ACK;
  489. ieee80211_tx_status_irqsafe(hw, skb);
  490. return NETDEV_TX_OK;
  491. }
  492. static int mac80211_hwsim_start(struct ieee80211_hw *hw)
  493. {
  494. struct mac80211_hwsim_data *data = hw->priv;
  495. wiphy_debug(hw->wiphy, "%s\n", __func__);
  496. data->started = 1;
  497. return 0;
  498. }
  499. static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
  500. {
  501. struct mac80211_hwsim_data *data = hw->priv;
  502. data->started = 0;
  503. del_timer(&data->beacon_timer);
  504. wiphy_debug(hw->wiphy, "%s\n", __func__);
  505. }
  506. static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
  507. struct ieee80211_vif *vif)
  508. {
  509. wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
  510. __func__, ieee80211_vif_type_p2p(vif),
  511. vif->addr);
  512. hwsim_set_magic(vif);
  513. return 0;
  514. }
  515. static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
  516. struct ieee80211_vif *vif,
  517. enum nl80211_iftype newtype,
  518. bool newp2p)
  519. {
  520. newtype = ieee80211_iftype_p2p(newtype, newp2p);
  521. wiphy_debug(hw->wiphy,
  522. "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
  523. __func__, ieee80211_vif_type_p2p(vif),
  524. newtype, vif->addr);
  525. hwsim_check_magic(vif);
  526. return 0;
  527. }
  528. static void mac80211_hwsim_remove_interface(
  529. struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  530. {
  531. wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
  532. __func__, ieee80211_vif_type_p2p(vif),
  533. vif->addr);
  534. hwsim_check_magic(vif);
  535. hwsim_clear_magic(vif);
  536. }
  537. static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
  538. struct ieee80211_vif *vif)
  539. {
  540. struct ieee80211_hw *hw = arg;
  541. struct sk_buff *skb;
  542. struct ieee80211_tx_info *info;
  543. hwsim_check_magic(vif);
  544. if (vif->type != NL80211_IFTYPE_AP &&
  545. vif->type != NL80211_IFTYPE_MESH_POINT &&
  546. vif->type != NL80211_IFTYPE_ADHOC)
  547. return;
  548. skb = ieee80211_beacon_get(hw, vif);
  549. if (skb == NULL)
  550. return;
  551. info = IEEE80211_SKB_CB(skb);
  552. mac80211_hwsim_monitor_rx(hw, skb);
  553. mac80211_hwsim_tx_frame(hw, skb);
  554. dev_kfree_skb(skb);
  555. }
  556. static void mac80211_hwsim_beacon(unsigned long arg)
  557. {
  558. struct ieee80211_hw *hw = (struct ieee80211_hw *) arg;
  559. struct mac80211_hwsim_data *data = hw->priv;
  560. if (!data->started)
  561. return;
  562. ieee80211_iterate_active_interfaces_atomic(
  563. hw, mac80211_hwsim_beacon_tx, hw);
  564. data->beacon_timer.expires = jiffies + data->beacon_int;
  565. add_timer(&data->beacon_timer);
  566. }
  567. static const char *hwsim_chantypes[] = {
  568. [NL80211_CHAN_NO_HT] = "noht",
  569. [NL80211_CHAN_HT20] = "ht20",
  570. [NL80211_CHAN_HT40MINUS] = "ht40-",
  571. [NL80211_CHAN_HT40PLUS] = "ht40+",
  572. };
  573. static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
  574. {
  575. struct mac80211_hwsim_data *data = hw->priv;
  576. struct ieee80211_conf *conf = &hw->conf;
  577. static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
  578. [IEEE80211_SMPS_AUTOMATIC] = "auto",
  579. [IEEE80211_SMPS_OFF] = "off",
  580. [IEEE80211_SMPS_STATIC] = "static",
  581. [IEEE80211_SMPS_DYNAMIC] = "dynamic",
  582. };
  583. wiphy_debug(hw->wiphy,
  584. "%s (freq=%d/%s idle=%d ps=%d smps=%s)\n",
  585. __func__,
  586. conf->channel->center_freq,
  587. hwsim_chantypes[conf->channel_type],
  588. !!(conf->flags & IEEE80211_CONF_IDLE),
  589. !!(conf->flags & IEEE80211_CONF_PS),
  590. smps_modes[conf->smps_mode]);
  591. data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
  592. data->channel = conf->channel;
  593. data->power_level = conf->power_level;
  594. if (!data->started || !data->beacon_int)
  595. del_timer(&data->beacon_timer);
  596. else
  597. mod_timer(&data->beacon_timer, jiffies + data->beacon_int);
  598. return 0;
  599. }
  600. static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
  601. unsigned int changed_flags,
  602. unsigned int *total_flags,u64 multicast)
  603. {
  604. struct mac80211_hwsim_data *data = hw->priv;
  605. wiphy_debug(hw->wiphy, "%s\n", __func__);
  606. data->rx_filter = 0;
  607. if (*total_flags & FIF_PROMISC_IN_BSS)
  608. data->rx_filter |= FIF_PROMISC_IN_BSS;
  609. if (*total_flags & FIF_ALLMULTI)
  610. data->rx_filter |= FIF_ALLMULTI;
  611. *total_flags = data->rx_filter;
  612. }
  613. static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
  614. struct ieee80211_vif *vif,
  615. struct ieee80211_bss_conf *info,
  616. u32 changed)
  617. {
  618. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  619. struct mac80211_hwsim_data *data = hw->priv;
  620. hwsim_check_magic(vif);
  621. wiphy_debug(hw->wiphy, "%s(changed=0x%x)\n", __func__, changed);
  622. if (changed & BSS_CHANGED_BSSID) {
  623. wiphy_debug(hw->wiphy, "%s: BSSID changed: %pM\n",
  624. __func__, info->bssid);
  625. memcpy(vp->bssid, info->bssid, ETH_ALEN);
  626. }
  627. if (changed & BSS_CHANGED_ASSOC) {
  628. wiphy_debug(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
  629. info->assoc, info->aid);
  630. vp->assoc = info->assoc;
  631. vp->aid = info->aid;
  632. }
  633. if (changed & BSS_CHANGED_BEACON_INT) {
  634. wiphy_debug(hw->wiphy, " BCNINT: %d\n", info->beacon_int);
  635. data->beacon_int = 1024 * info->beacon_int / 1000 * HZ / 1000;
  636. if (WARN_ON(!data->beacon_int))
  637. data->beacon_int = 1;
  638. if (data->started)
  639. mod_timer(&data->beacon_timer,
  640. jiffies + data->beacon_int);
  641. }
  642. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  643. wiphy_debug(hw->wiphy, " ERP_CTS_PROT: %d\n",
  644. info->use_cts_prot);
  645. }
  646. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  647. wiphy_debug(hw->wiphy, " ERP_PREAMBLE: %d\n",
  648. info->use_short_preamble);
  649. }
  650. if (changed & BSS_CHANGED_ERP_SLOT) {
  651. wiphy_debug(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
  652. }
  653. if (changed & BSS_CHANGED_HT) {
  654. wiphy_debug(hw->wiphy, " HT: op_mode=0x%x, chantype=%s\n",
  655. info->ht_operation_mode,
  656. hwsim_chantypes[info->channel_type]);
  657. }
  658. if (changed & BSS_CHANGED_BASIC_RATES) {
  659. wiphy_debug(hw->wiphy, " BASIC_RATES: 0x%llx\n",
  660. (unsigned long long) info->basic_rates);
  661. }
  662. }
  663. static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
  664. struct ieee80211_vif *vif,
  665. struct ieee80211_sta *sta)
  666. {
  667. hwsim_check_magic(vif);
  668. hwsim_set_sta_magic(sta);
  669. return 0;
  670. }
  671. static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
  672. struct ieee80211_vif *vif,
  673. struct ieee80211_sta *sta)
  674. {
  675. hwsim_check_magic(vif);
  676. hwsim_clear_sta_magic(sta);
  677. return 0;
  678. }
  679. static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
  680. struct ieee80211_vif *vif,
  681. enum sta_notify_cmd cmd,
  682. struct ieee80211_sta *sta)
  683. {
  684. hwsim_check_magic(vif);
  685. switch (cmd) {
  686. case STA_NOTIFY_SLEEP:
  687. case STA_NOTIFY_AWAKE:
  688. /* TODO: make good use of these flags */
  689. break;
  690. default:
  691. WARN(1, "Invalid sta notify: %d\n", cmd);
  692. break;
  693. }
  694. }
  695. static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
  696. struct ieee80211_sta *sta,
  697. bool set)
  698. {
  699. hwsim_check_sta_magic(sta);
  700. return 0;
  701. }
  702. static int mac80211_hwsim_conf_tx(
  703. struct ieee80211_hw *hw, u16 queue,
  704. const struct ieee80211_tx_queue_params *params)
  705. {
  706. wiphy_debug(hw->wiphy,
  707. "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
  708. __func__, queue,
  709. params->txop, params->cw_min,
  710. params->cw_max, params->aifs);
  711. return 0;
  712. }
  713. static int mac80211_hwsim_get_survey(
  714. struct ieee80211_hw *hw, int idx,
  715. struct survey_info *survey)
  716. {
  717. struct ieee80211_conf *conf = &hw->conf;
  718. wiphy_debug(hw->wiphy, "%s (idx=%d)\n", __func__, idx);
  719. if (idx != 0)
  720. return -ENOENT;
  721. /* Current channel */
  722. survey->channel = conf->channel;
  723. /*
  724. * Magically conjured noise level --- this is only ok for simulated hardware.
  725. *
  726. * A real driver which cannot determine the real channel noise MUST NOT
  727. * report any noise, especially not a magically conjured one :-)
  728. */
  729. survey->filled = SURVEY_INFO_NOISE_DBM;
  730. survey->noise = -92;
  731. return 0;
  732. }
  733. #ifdef CONFIG_NL80211_TESTMODE
  734. /*
  735. * This section contains example code for using netlink
  736. * attributes with the testmode command in nl80211.
  737. */
  738. /* These enums need to be kept in sync with userspace */
  739. enum hwsim_testmode_attr {
  740. __HWSIM_TM_ATTR_INVALID = 0,
  741. HWSIM_TM_ATTR_CMD = 1,
  742. HWSIM_TM_ATTR_PS = 2,
  743. /* keep last */
  744. __HWSIM_TM_ATTR_AFTER_LAST,
  745. HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
  746. };
  747. enum hwsim_testmode_cmd {
  748. HWSIM_TM_CMD_SET_PS = 0,
  749. HWSIM_TM_CMD_GET_PS = 1,
  750. };
  751. static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
  752. [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
  753. [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
  754. };
  755. static int hwsim_fops_ps_write(void *dat, u64 val);
  756. static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
  757. void *data, int len)
  758. {
  759. struct mac80211_hwsim_data *hwsim = hw->priv;
  760. struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
  761. struct sk_buff *skb;
  762. int err, ps;
  763. err = nla_parse(tb, HWSIM_TM_ATTR_MAX, data, len,
  764. hwsim_testmode_policy);
  765. if (err)
  766. return err;
  767. if (!tb[HWSIM_TM_ATTR_CMD])
  768. return -EINVAL;
  769. switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
  770. case HWSIM_TM_CMD_SET_PS:
  771. if (!tb[HWSIM_TM_ATTR_PS])
  772. return -EINVAL;
  773. ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
  774. return hwsim_fops_ps_write(hwsim, ps);
  775. case HWSIM_TM_CMD_GET_PS:
  776. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
  777. nla_total_size(sizeof(u32)));
  778. if (!skb)
  779. return -ENOMEM;
  780. NLA_PUT_U32(skb, HWSIM_TM_ATTR_PS, hwsim->ps);
  781. return cfg80211_testmode_reply(skb);
  782. default:
  783. return -EOPNOTSUPP;
  784. }
  785. nla_put_failure:
  786. kfree_skb(skb);
  787. return -ENOBUFS;
  788. }
  789. #endif
  790. static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
  791. struct ieee80211_vif *vif,
  792. enum ieee80211_ampdu_mlme_action action,
  793. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  794. u8 buf_size)
  795. {
  796. switch (action) {
  797. case IEEE80211_AMPDU_TX_START:
  798. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  799. break;
  800. case IEEE80211_AMPDU_TX_STOP:
  801. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  802. break;
  803. case IEEE80211_AMPDU_TX_OPERATIONAL:
  804. break;
  805. case IEEE80211_AMPDU_RX_START:
  806. case IEEE80211_AMPDU_RX_STOP:
  807. break;
  808. default:
  809. return -EOPNOTSUPP;
  810. }
  811. return 0;
  812. }
  813. static void mac80211_hwsim_flush(struct ieee80211_hw *hw, bool drop)
  814. {
  815. /*
  816. * In this special case, there's nothing we need to
  817. * do because hwsim does transmission synchronously.
  818. * In the future, when it does transmissions via
  819. * userspace, we may need to do something.
  820. */
  821. }
  822. struct hw_scan_done {
  823. struct delayed_work w;
  824. struct ieee80211_hw *hw;
  825. };
  826. static void hw_scan_done(struct work_struct *work)
  827. {
  828. struct hw_scan_done *hsd =
  829. container_of(work, struct hw_scan_done, w.work);
  830. ieee80211_scan_completed(hsd->hw, false);
  831. kfree(hsd);
  832. }
  833. static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
  834. struct ieee80211_vif *vif,
  835. struct cfg80211_scan_request *req)
  836. {
  837. struct hw_scan_done *hsd = kzalloc(sizeof(*hsd), GFP_KERNEL);
  838. int i;
  839. if (!hsd)
  840. return -ENOMEM;
  841. hsd->hw = hw;
  842. INIT_DELAYED_WORK(&hsd->w, hw_scan_done);
  843. printk(KERN_DEBUG "hwsim hw_scan request\n");
  844. for (i = 0; i < req->n_channels; i++)
  845. printk(KERN_DEBUG "hwsim hw_scan freq %d\n",
  846. req->channels[i]->center_freq);
  847. ieee80211_queue_delayed_work(hw, &hsd->w, 2 * HZ);
  848. return 0;
  849. }
  850. static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw)
  851. {
  852. struct mac80211_hwsim_data *hwsim = hw->priv;
  853. mutex_lock(&hwsim->mutex);
  854. if (hwsim->scanning) {
  855. printk(KERN_DEBUG "two hwsim sw_scans detected!\n");
  856. goto out;
  857. }
  858. printk(KERN_DEBUG "hwsim sw_scan request, prepping stuff\n");
  859. hwsim->scanning = true;
  860. out:
  861. mutex_unlock(&hwsim->mutex);
  862. }
  863. static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw)
  864. {
  865. struct mac80211_hwsim_data *hwsim = hw->priv;
  866. mutex_lock(&hwsim->mutex);
  867. printk(KERN_DEBUG "hwsim sw_scan_complete\n");
  868. hwsim->scanning = false;
  869. mutex_unlock(&hwsim->mutex);
  870. }
  871. static struct ieee80211_ops mac80211_hwsim_ops =
  872. {
  873. .tx = mac80211_hwsim_tx,
  874. .start = mac80211_hwsim_start,
  875. .stop = mac80211_hwsim_stop,
  876. .add_interface = mac80211_hwsim_add_interface,
  877. .change_interface = mac80211_hwsim_change_interface,
  878. .remove_interface = mac80211_hwsim_remove_interface,
  879. .config = mac80211_hwsim_config,
  880. .configure_filter = mac80211_hwsim_configure_filter,
  881. .bss_info_changed = mac80211_hwsim_bss_info_changed,
  882. .sta_add = mac80211_hwsim_sta_add,
  883. .sta_remove = mac80211_hwsim_sta_remove,
  884. .sta_notify = mac80211_hwsim_sta_notify,
  885. .set_tim = mac80211_hwsim_set_tim,
  886. .conf_tx = mac80211_hwsim_conf_tx,
  887. .get_survey = mac80211_hwsim_get_survey,
  888. CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd)
  889. .ampdu_action = mac80211_hwsim_ampdu_action,
  890. .sw_scan_start = mac80211_hwsim_sw_scan,
  891. .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
  892. .flush = mac80211_hwsim_flush,
  893. };
  894. static void mac80211_hwsim_free(void)
  895. {
  896. struct list_head tmplist, *i, *tmp;
  897. struct mac80211_hwsim_data *data, *tmpdata;
  898. INIT_LIST_HEAD(&tmplist);
  899. spin_lock_bh(&hwsim_radio_lock);
  900. list_for_each_safe(i, tmp, &hwsim_radios)
  901. list_move(i, &tmplist);
  902. spin_unlock_bh(&hwsim_radio_lock);
  903. list_for_each_entry_safe(data, tmpdata, &tmplist, list) {
  904. debugfs_remove(data->debugfs_group);
  905. debugfs_remove(data->debugfs_ps);
  906. debugfs_remove(data->debugfs);
  907. ieee80211_unregister_hw(data->hw);
  908. device_unregister(data->dev);
  909. ieee80211_free_hw(data->hw);
  910. }
  911. class_destroy(hwsim_class);
  912. }
  913. static struct device_driver mac80211_hwsim_driver = {
  914. .name = "mac80211_hwsim"
  915. };
  916. static const struct net_device_ops hwsim_netdev_ops = {
  917. .ndo_start_xmit = hwsim_mon_xmit,
  918. .ndo_change_mtu = eth_change_mtu,
  919. .ndo_set_mac_address = eth_mac_addr,
  920. .ndo_validate_addr = eth_validate_addr,
  921. };
  922. static void hwsim_mon_setup(struct net_device *dev)
  923. {
  924. dev->netdev_ops = &hwsim_netdev_ops;
  925. dev->destructor = free_netdev;
  926. ether_setup(dev);
  927. dev->tx_queue_len = 0;
  928. dev->type = ARPHRD_IEEE80211_RADIOTAP;
  929. memset(dev->dev_addr, 0, ETH_ALEN);
  930. dev->dev_addr[0] = 0x12;
  931. }
  932. static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
  933. {
  934. struct mac80211_hwsim_data *data = dat;
  935. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  936. struct sk_buff *skb;
  937. struct ieee80211_pspoll *pspoll;
  938. if (!vp->assoc)
  939. return;
  940. wiphy_debug(data->hw->wiphy,
  941. "%s: send PS-Poll to %pM for aid %d\n",
  942. __func__, vp->bssid, vp->aid);
  943. skb = dev_alloc_skb(sizeof(*pspoll));
  944. if (!skb)
  945. return;
  946. pspoll = (void *) skb_put(skb, sizeof(*pspoll));
  947. pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  948. IEEE80211_STYPE_PSPOLL |
  949. IEEE80211_FCTL_PM);
  950. pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
  951. memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
  952. memcpy(pspoll->ta, mac, ETH_ALEN);
  953. if (!mac80211_hwsim_tx_frame(data->hw, skb))
  954. printk(KERN_DEBUG "%s: PS-Poll frame not ack'ed\n", __func__);
  955. dev_kfree_skb(skb);
  956. }
  957. static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
  958. struct ieee80211_vif *vif, int ps)
  959. {
  960. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  961. struct sk_buff *skb;
  962. struct ieee80211_hdr *hdr;
  963. if (!vp->assoc)
  964. return;
  965. wiphy_debug(data->hw->wiphy,
  966. "%s: send data::nullfunc to %pM ps=%d\n",
  967. __func__, vp->bssid, ps);
  968. skb = dev_alloc_skb(sizeof(*hdr));
  969. if (!skb)
  970. return;
  971. hdr = (void *) skb_put(skb, sizeof(*hdr) - ETH_ALEN);
  972. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  973. IEEE80211_STYPE_NULLFUNC |
  974. (ps ? IEEE80211_FCTL_PM : 0));
  975. hdr->duration_id = cpu_to_le16(0);
  976. memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
  977. memcpy(hdr->addr2, mac, ETH_ALEN);
  978. memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
  979. if (!mac80211_hwsim_tx_frame(data->hw, skb))
  980. printk(KERN_DEBUG "%s: nullfunc frame not ack'ed\n", __func__);
  981. dev_kfree_skb(skb);
  982. }
  983. static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
  984. struct ieee80211_vif *vif)
  985. {
  986. struct mac80211_hwsim_data *data = dat;
  987. hwsim_send_nullfunc(data, mac, vif, 1);
  988. }
  989. static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
  990. struct ieee80211_vif *vif)
  991. {
  992. struct mac80211_hwsim_data *data = dat;
  993. hwsim_send_nullfunc(data, mac, vif, 0);
  994. }
  995. static int hwsim_fops_ps_read(void *dat, u64 *val)
  996. {
  997. struct mac80211_hwsim_data *data = dat;
  998. *val = data->ps;
  999. return 0;
  1000. }
  1001. static int hwsim_fops_ps_write(void *dat, u64 val)
  1002. {
  1003. struct mac80211_hwsim_data *data = dat;
  1004. enum ps_mode old_ps;
  1005. if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
  1006. val != PS_MANUAL_POLL)
  1007. return -EINVAL;
  1008. old_ps = data->ps;
  1009. data->ps = val;
  1010. if (val == PS_MANUAL_POLL) {
  1011. ieee80211_iterate_active_interfaces(data->hw,
  1012. hwsim_send_ps_poll, data);
  1013. data->ps_poll_pending = true;
  1014. } else if (old_ps == PS_DISABLED && val != PS_DISABLED) {
  1015. ieee80211_iterate_active_interfaces(data->hw,
  1016. hwsim_send_nullfunc_ps,
  1017. data);
  1018. } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
  1019. ieee80211_iterate_active_interfaces(data->hw,
  1020. hwsim_send_nullfunc_no_ps,
  1021. data);
  1022. }
  1023. return 0;
  1024. }
  1025. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
  1026. "%llu\n");
  1027. static int hwsim_fops_group_read(void *dat, u64 *val)
  1028. {
  1029. struct mac80211_hwsim_data *data = dat;
  1030. *val = data->group;
  1031. return 0;
  1032. }
  1033. static int hwsim_fops_group_write(void *dat, u64 val)
  1034. {
  1035. struct mac80211_hwsim_data *data = dat;
  1036. data->group = val;
  1037. return 0;
  1038. }
  1039. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
  1040. hwsim_fops_group_read, hwsim_fops_group_write,
  1041. "%llx\n");
  1042. static int __init init_mac80211_hwsim(void)
  1043. {
  1044. int i, err = 0;
  1045. u8 addr[ETH_ALEN];
  1046. struct mac80211_hwsim_data *data;
  1047. struct ieee80211_hw *hw;
  1048. enum ieee80211_band band;
  1049. if (radios < 1 || radios > 100)
  1050. return -EINVAL;
  1051. if (fake_hw_scan) {
  1052. mac80211_hwsim_ops.hw_scan = mac80211_hwsim_hw_scan;
  1053. mac80211_hwsim_ops.sw_scan_start = NULL;
  1054. mac80211_hwsim_ops.sw_scan_complete = NULL;
  1055. }
  1056. spin_lock_init(&hwsim_radio_lock);
  1057. INIT_LIST_HEAD(&hwsim_radios);
  1058. hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
  1059. if (IS_ERR(hwsim_class))
  1060. return PTR_ERR(hwsim_class);
  1061. memset(addr, 0, ETH_ALEN);
  1062. addr[0] = 0x02;
  1063. for (i = 0; i < radios; i++) {
  1064. printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
  1065. i);
  1066. hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
  1067. if (!hw) {
  1068. printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
  1069. "failed\n");
  1070. err = -ENOMEM;
  1071. goto failed;
  1072. }
  1073. data = hw->priv;
  1074. data->hw = hw;
  1075. data->dev = device_create(hwsim_class, NULL, 0, hw,
  1076. "hwsim%d", i);
  1077. if (IS_ERR(data->dev)) {
  1078. printk(KERN_DEBUG
  1079. "mac80211_hwsim: device_create "
  1080. "failed (%ld)\n", PTR_ERR(data->dev));
  1081. err = -ENOMEM;
  1082. goto failed_drvdata;
  1083. }
  1084. data->dev->driver = &mac80211_hwsim_driver;
  1085. SET_IEEE80211_DEV(hw, data->dev);
  1086. addr[3] = i >> 8;
  1087. addr[4] = i;
  1088. memcpy(data->addresses[0].addr, addr, ETH_ALEN);
  1089. memcpy(data->addresses[1].addr, addr, ETH_ALEN);
  1090. data->addresses[1].addr[0] |= 0x40;
  1091. hw->wiphy->n_addresses = 2;
  1092. hw->wiphy->addresses = data->addresses;
  1093. if (fake_hw_scan) {
  1094. hw->wiphy->max_scan_ssids = 255;
  1095. hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
  1096. }
  1097. hw->channel_change_time = 1;
  1098. hw->queues = 4;
  1099. hw->wiphy->interface_modes =
  1100. BIT(NL80211_IFTYPE_STATION) |
  1101. BIT(NL80211_IFTYPE_AP) |
  1102. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  1103. BIT(NL80211_IFTYPE_P2P_GO) |
  1104. BIT(NL80211_IFTYPE_ADHOC) |
  1105. BIT(NL80211_IFTYPE_MESH_POINT);
  1106. hw->flags = IEEE80211_HW_MFP_CAPABLE |
  1107. IEEE80211_HW_SIGNAL_DBM |
  1108. IEEE80211_HW_SUPPORTS_STATIC_SMPS |
  1109. IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
  1110. IEEE80211_HW_AMPDU_AGGREGATION;
  1111. /* ask mac80211 to reserve space for magic */
  1112. hw->vif_data_size = sizeof(struct hwsim_vif_priv);
  1113. hw->sta_data_size = sizeof(struct hwsim_sta_priv);
  1114. memcpy(data->channels_2ghz, hwsim_channels_2ghz,
  1115. sizeof(hwsim_channels_2ghz));
  1116. memcpy(data->channels_5ghz, hwsim_channels_5ghz,
  1117. sizeof(hwsim_channels_5ghz));
  1118. memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
  1119. for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
  1120. struct ieee80211_supported_band *sband = &data->bands[band];
  1121. switch (band) {
  1122. case IEEE80211_BAND_2GHZ:
  1123. sband->channels = data->channels_2ghz;
  1124. sband->n_channels =
  1125. ARRAY_SIZE(hwsim_channels_2ghz);
  1126. sband->bitrates = data->rates;
  1127. sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
  1128. break;
  1129. case IEEE80211_BAND_5GHZ:
  1130. sband->channels = data->channels_5ghz;
  1131. sband->n_channels =
  1132. ARRAY_SIZE(hwsim_channels_5ghz);
  1133. sband->bitrates = data->rates + 4;
  1134. sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
  1135. break;
  1136. default:
  1137. break;
  1138. }
  1139. sband->ht_cap.ht_supported = true;
  1140. sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  1141. IEEE80211_HT_CAP_GRN_FLD |
  1142. IEEE80211_HT_CAP_SGI_40 |
  1143. IEEE80211_HT_CAP_DSSSCCK40;
  1144. sband->ht_cap.ampdu_factor = 0x3;
  1145. sband->ht_cap.ampdu_density = 0x6;
  1146. memset(&sband->ht_cap.mcs, 0,
  1147. sizeof(sband->ht_cap.mcs));
  1148. sband->ht_cap.mcs.rx_mask[0] = 0xff;
  1149. sband->ht_cap.mcs.rx_mask[1] = 0xff;
  1150. sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  1151. hw->wiphy->bands[band] = sband;
  1152. }
  1153. /* By default all radios are belonging to the first group */
  1154. data->group = 1;
  1155. mutex_init(&data->mutex);
  1156. /* Work to be done prior to ieee80211_register_hw() */
  1157. switch (regtest) {
  1158. case HWSIM_REGTEST_DISABLED:
  1159. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  1160. case HWSIM_REGTEST_DRIVER_REG_ALL:
  1161. case HWSIM_REGTEST_DIFF_COUNTRY:
  1162. /*
  1163. * Nothing to be done for driver regulatory domain
  1164. * hints prior to ieee80211_register_hw()
  1165. */
  1166. break;
  1167. case HWSIM_REGTEST_WORLD_ROAM:
  1168. if (i == 0) {
  1169. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1170. wiphy_apply_custom_regulatory(hw->wiphy,
  1171. &hwsim_world_regdom_custom_01);
  1172. }
  1173. break;
  1174. case HWSIM_REGTEST_CUSTOM_WORLD:
  1175. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1176. wiphy_apply_custom_regulatory(hw->wiphy,
  1177. &hwsim_world_regdom_custom_01);
  1178. break;
  1179. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  1180. if (i == 0) {
  1181. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1182. wiphy_apply_custom_regulatory(hw->wiphy,
  1183. &hwsim_world_regdom_custom_01);
  1184. } else if (i == 1) {
  1185. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1186. wiphy_apply_custom_regulatory(hw->wiphy,
  1187. &hwsim_world_regdom_custom_02);
  1188. }
  1189. break;
  1190. case HWSIM_REGTEST_STRICT_ALL:
  1191. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1192. break;
  1193. case HWSIM_REGTEST_STRICT_FOLLOW:
  1194. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  1195. if (i == 0)
  1196. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1197. break;
  1198. case HWSIM_REGTEST_ALL:
  1199. if (i == 0) {
  1200. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1201. wiphy_apply_custom_regulatory(hw->wiphy,
  1202. &hwsim_world_regdom_custom_01);
  1203. } else if (i == 1) {
  1204. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1205. wiphy_apply_custom_regulatory(hw->wiphy,
  1206. &hwsim_world_regdom_custom_02);
  1207. } else if (i == 4)
  1208. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1209. break;
  1210. default:
  1211. break;
  1212. }
  1213. /* give the regulatory workqueue a chance to run */
  1214. if (regtest)
  1215. schedule_timeout_interruptible(1);
  1216. err = ieee80211_register_hw(hw);
  1217. if (err < 0) {
  1218. printk(KERN_DEBUG "mac80211_hwsim: "
  1219. "ieee80211_register_hw failed (%d)\n", err);
  1220. goto failed_hw;
  1221. }
  1222. /* Work to be done after to ieee80211_register_hw() */
  1223. switch (regtest) {
  1224. case HWSIM_REGTEST_WORLD_ROAM:
  1225. case HWSIM_REGTEST_DISABLED:
  1226. break;
  1227. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  1228. if (!i)
  1229. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1230. break;
  1231. case HWSIM_REGTEST_DRIVER_REG_ALL:
  1232. case HWSIM_REGTEST_STRICT_ALL:
  1233. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1234. break;
  1235. case HWSIM_REGTEST_DIFF_COUNTRY:
  1236. if (i < ARRAY_SIZE(hwsim_alpha2s))
  1237. regulatory_hint(hw->wiphy, hwsim_alpha2s[i]);
  1238. break;
  1239. case HWSIM_REGTEST_CUSTOM_WORLD:
  1240. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  1241. /*
  1242. * Nothing to be done for custom world regulatory
  1243. * domains after to ieee80211_register_hw
  1244. */
  1245. break;
  1246. case HWSIM_REGTEST_STRICT_FOLLOW:
  1247. if (i == 0)
  1248. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1249. break;
  1250. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  1251. if (i == 0)
  1252. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1253. else if (i == 1)
  1254. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  1255. break;
  1256. case HWSIM_REGTEST_ALL:
  1257. if (i == 2)
  1258. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1259. else if (i == 3)
  1260. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  1261. else if (i == 4)
  1262. regulatory_hint(hw->wiphy, hwsim_alpha2s[2]);
  1263. break;
  1264. default:
  1265. break;
  1266. }
  1267. wiphy_debug(hw->wiphy, "hwaddr %pm registered\n",
  1268. hw->wiphy->perm_addr);
  1269. data->debugfs = debugfs_create_dir("hwsim",
  1270. hw->wiphy->debugfsdir);
  1271. data->debugfs_ps = debugfs_create_file("ps", 0666,
  1272. data->debugfs, data,
  1273. &hwsim_fops_ps);
  1274. data->debugfs_group = debugfs_create_file("group", 0666,
  1275. data->debugfs, data,
  1276. &hwsim_fops_group);
  1277. setup_timer(&data->beacon_timer, mac80211_hwsim_beacon,
  1278. (unsigned long) hw);
  1279. list_add_tail(&data->list, &hwsim_radios);
  1280. }
  1281. hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
  1282. if (hwsim_mon == NULL)
  1283. goto failed;
  1284. rtnl_lock();
  1285. err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
  1286. if (err < 0)
  1287. goto failed_mon;
  1288. err = register_netdevice(hwsim_mon);
  1289. if (err < 0)
  1290. goto failed_mon;
  1291. rtnl_unlock();
  1292. return 0;
  1293. failed_mon:
  1294. rtnl_unlock();
  1295. free_netdev(hwsim_mon);
  1296. mac80211_hwsim_free();
  1297. return err;
  1298. failed_hw:
  1299. device_unregister(data->dev);
  1300. failed_drvdata:
  1301. ieee80211_free_hw(hw);
  1302. failed:
  1303. mac80211_hwsim_free();
  1304. return err;
  1305. }
  1306. static void __exit exit_mac80211_hwsim(void)
  1307. {
  1308. printk(KERN_DEBUG "mac80211_hwsim: unregister radios\n");
  1309. mac80211_hwsim_free();
  1310. unregister_netdev(hwsim_mon);
  1311. }
  1312. module_init(init_mac80211_hwsim);
  1313. module_exit(exit_mac80211_hwsim);