mac80211_hwsim.c 38 KB

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