mac80211_hwsim.c 39 KB

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