mac80211_hwsim.c 38 KB

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