mac80211_hwsim.c 39 KB

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