mac80211_hwsim.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  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. static bool mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
  385. struct sk_buff *skb)
  386. {
  387. struct mac80211_hwsim_data *data = hw->priv, *data2;
  388. bool ack = false;
  389. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  390. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  391. struct ieee80211_rx_status rx_status;
  392. if (data->idle) {
  393. printk(KERN_DEBUG "%s: Trying to TX when idle - reject\n",
  394. wiphy_name(hw->wiphy));
  395. return false;
  396. }
  397. memset(&rx_status, 0, sizeof(rx_status));
  398. /* TODO: set mactime */
  399. rx_status.freq = data->channel->center_freq;
  400. rx_status.band = data->channel->band;
  401. rx_status.rate_idx = info->control.rates[0].idx;
  402. /* TODO: simulate real signal strength (and optional packet loss) */
  403. rx_status.signal = -50;
  404. if (data->ps != PS_DISABLED)
  405. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  406. /* release the skb's source info */
  407. skb_orphan(skb);
  408. skb_dst_drop(skb);
  409. skb->mark = 0;
  410. secpath_reset(skb);
  411. nf_reset(skb);
  412. /* Copy skb to all enabled radios that are on the current frequency */
  413. spin_lock(&hwsim_radio_lock);
  414. list_for_each_entry(data2, &hwsim_radios, list) {
  415. struct sk_buff *nskb;
  416. if (data == data2)
  417. continue;
  418. if (data2->idle || !data2->started ||
  419. !hwsim_ps_rx_ok(data2, skb) ||
  420. !data->channel || !data2->channel ||
  421. data->channel->center_freq != data2->channel->center_freq ||
  422. !(data->group & data2->group))
  423. continue;
  424. nskb = skb_copy(skb, GFP_ATOMIC);
  425. if (nskb == NULL)
  426. continue;
  427. if (memcmp(hdr->addr1, data2->hw->wiphy->perm_addr,
  428. ETH_ALEN) == 0)
  429. ack = true;
  430. memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
  431. ieee80211_rx_irqsafe(data2->hw, nskb);
  432. }
  433. spin_unlock(&hwsim_radio_lock);
  434. return ack;
  435. }
  436. static int mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  437. {
  438. bool ack;
  439. struct ieee80211_tx_info *txi;
  440. mac80211_hwsim_monitor_rx(hw, skb);
  441. if (skb->len < 10) {
  442. /* Should not happen; just a sanity check for addr1 use */
  443. dev_kfree_skb(skb);
  444. return NETDEV_TX_OK;
  445. }
  446. ack = mac80211_hwsim_tx_frame(hw, skb);
  447. if (ack && skb->len >= 16) {
  448. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  449. mac80211_hwsim_monitor_ack(hw, hdr->addr2);
  450. }
  451. txi = IEEE80211_SKB_CB(skb);
  452. if (txi->control.vif)
  453. hwsim_check_magic(txi->control.vif);
  454. if (txi->control.sta)
  455. hwsim_check_sta_magic(txi->control.sta);
  456. ieee80211_tx_info_clear_status(txi);
  457. if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
  458. txi->flags |= IEEE80211_TX_STAT_ACK;
  459. ieee80211_tx_status_irqsafe(hw, skb);
  460. return NETDEV_TX_OK;
  461. }
  462. static int mac80211_hwsim_start(struct ieee80211_hw *hw)
  463. {
  464. struct mac80211_hwsim_data *data = hw->priv;
  465. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  466. data->started = 1;
  467. return 0;
  468. }
  469. static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
  470. {
  471. struct mac80211_hwsim_data *data = hw->priv;
  472. data->started = 0;
  473. del_timer(&data->beacon_timer);
  474. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  475. }
  476. static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
  477. struct ieee80211_if_init_conf *conf)
  478. {
  479. printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%pM)\n",
  480. wiphy_name(hw->wiphy), __func__, conf->type,
  481. conf->mac_addr);
  482. hwsim_set_magic(conf->vif);
  483. return 0;
  484. }
  485. static void mac80211_hwsim_remove_interface(
  486. struct ieee80211_hw *hw, struct ieee80211_if_init_conf *conf)
  487. {
  488. printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%pM)\n",
  489. wiphy_name(hw->wiphy), __func__, conf->type,
  490. conf->mac_addr);
  491. hwsim_check_magic(conf->vif);
  492. hwsim_clear_magic(conf->vif);
  493. }
  494. static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
  495. struct ieee80211_vif *vif)
  496. {
  497. struct ieee80211_hw *hw = arg;
  498. struct sk_buff *skb;
  499. struct ieee80211_tx_info *info;
  500. hwsim_check_magic(vif);
  501. if (vif->type != NL80211_IFTYPE_AP &&
  502. vif->type != NL80211_IFTYPE_MESH_POINT)
  503. return;
  504. skb = ieee80211_beacon_get(hw, vif);
  505. if (skb == NULL)
  506. return;
  507. info = IEEE80211_SKB_CB(skb);
  508. mac80211_hwsim_monitor_rx(hw, skb);
  509. mac80211_hwsim_tx_frame(hw, skb);
  510. dev_kfree_skb(skb);
  511. }
  512. static void mac80211_hwsim_beacon(unsigned long arg)
  513. {
  514. struct ieee80211_hw *hw = (struct ieee80211_hw *) arg;
  515. struct mac80211_hwsim_data *data = hw->priv;
  516. if (!data->started)
  517. return;
  518. ieee80211_iterate_active_interfaces_atomic(
  519. hw, mac80211_hwsim_beacon_tx, hw);
  520. data->beacon_timer.expires = jiffies + data->beacon_int;
  521. add_timer(&data->beacon_timer);
  522. }
  523. static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
  524. {
  525. struct mac80211_hwsim_data *data = hw->priv;
  526. struct ieee80211_conf *conf = &hw->conf;
  527. printk(KERN_DEBUG "%s:%s (freq=%d idle=%d ps=%d)\n",
  528. wiphy_name(hw->wiphy), __func__,
  529. conf->channel->center_freq,
  530. !!(conf->flags & IEEE80211_CONF_IDLE),
  531. !!(conf->flags & IEEE80211_CONF_PS));
  532. data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
  533. data->channel = conf->channel;
  534. if (!data->started || !data->beacon_int)
  535. del_timer(&data->beacon_timer);
  536. else
  537. mod_timer(&data->beacon_timer, jiffies + data->beacon_int);
  538. return 0;
  539. }
  540. static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
  541. unsigned int changed_flags,
  542. unsigned int *total_flags,u64 multicast)
  543. {
  544. struct mac80211_hwsim_data *data = hw->priv;
  545. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  546. data->rx_filter = 0;
  547. if (*total_flags & FIF_PROMISC_IN_BSS)
  548. data->rx_filter |= FIF_PROMISC_IN_BSS;
  549. if (*total_flags & FIF_ALLMULTI)
  550. data->rx_filter |= FIF_ALLMULTI;
  551. *total_flags = data->rx_filter;
  552. }
  553. static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
  554. struct ieee80211_vif *vif,
  555. struct ieee80211_bss_conf *info,
  556. u32 changed)
  557. {
  558. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  559. struct mac80211_hwsim_data *data = hw->priv;
  560. hwsim_check_magic(vif);
  561. printk(KERN_DEBUG "%s:%s(changed=0x%x)\n",
  562. wiphy_name(hw->wiphy), __func__, changed);
  563. if (changed & BSS_CHANGED_BSSID) {
  564. printk(KERN_DEBUG "%s:%s: BSSID changed: %pM\n",
  565. wiphy_name(hw->wiphy), __func__,
  566. info->bssid);
  567. memcpy(vp->bssid, info->bssid, ETH_ALEN);
  568. }
  569. if (changed & BSS_CHANGED_ASSOC) {
  570. printk(KERN_DEBUG " %s: ASSOC: assoc=%d aid=%d\n",
  571. wiphy_name(hw->wiphy), info->assoc, info->aid);
  572. vp->assoc = info->assoc;
  573. vp->aid = info->aid;
  574. }
  575. if (changed & BSS_CHANGED_BEACON_INT) {
  576. printk(KERN_DEBUG " %s: BCNINT: %d\n",
  577. wiphy_name(hw->wiphy), info->beacon_int);
  578. data->beacon_int = 1024 * info->beacon_int / 1000 * HZ / 1000;
  579. if (WARN_ON(!data->beacon_int))
  580. data->beacon_int = 1;
  581. if (data->started)
  582. mod_timer(&data->beacon_timer,
  583. jiffies + data->beacon_int);
  584. }
  585. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  586. printk(KERN_DEBUG " %s: ERP_CTS_PROT: %d\n",
  587. wiphy_name(hw->wiphy), info->use_cts_prot);
  588. }
  589. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  590. printk(KERN_DEBUG " %s: ERP_PREAMBLE: %d\n",
  591. wiphy_name(hw->wiphy), info->use_short_preamble);
  592. }
  593. if (changed & BSS_CHANGED_ERP_SLOT) {
  594. printk(KERN_DEBUG " %s: ERP_SLOT: %d\n",
  595. wiphy_name(hw->wiphy), info->use_short_slot);
  596. }
  597. if (changed & BSS_CHANGED_HT) {
  598. printk(KERN_DEBUG " %s: HT: op_mode=0x%x\n",
  599. wiphy_name(hw->wiphy),
  600. info->ht_operation_mode);
  601. }
  602. if (changed & BSS_CHANGED_BASIC_RATES) {
  603. printk(KERN_DEBUG " %s: BASIC_RATES: 0x%llx\n",
  604. wiphy_name(hw->wiphy),
  605. (unsigned long long) info->basic_rates);
  606. }
  607. }
  608. static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
  609. struct ieee80211_vif *vif,
  610. enum sta_notify_cmd cmd,
  611. struct ieee80211_sta *sta)
  612. {
  613. hwsim_check_magic(vif);
  614. switch (cmd) {
  615. case STA_NOTIFY_ADD:
  616. hwsim_set_sta_magic(sta);
  617. break;
  618. case STA_NOTIFY_REMOVE:
  619. hwsim_clear_sta_magic(sta);
  620. break;
  621. case STA_NOTIFY_SLEEP:
  622. case STA_NOTIFY_AWAKE:
  623. /* TODO: make good use of these flags */
  624. break;
  625. }
  626. }
  627. static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
  628. struct ieee80211_sta *sta,
  629. bool set)
  630. {
  631. hwsim_check_sta_magic(sta);
  632. return 0;
  633. }
  634. static int mac80211_hwsim_conf_tx(
  635. struct ieee80211_hw *hw, u16 queue,
  636. const struct ieee80211_tx_queue_params *params)
  637. {
  638. printk(KERN_DEBUG "%s:%s (queue=%d txop=%d cw_min=%d cw_max=%d "
  639. "aifs=%d)\n",
  640. wiphy_name(hw->wiphy), __func__, queue,
  641. params->txop, params->cw_min, params->cw_max, params->aifs);
  642. return 0;
  643. }
  644. #ifdef CONFIG_NL80211_TESTMODE
  645. /*
  646. * This section contains example code for using netlink
  647. * attributes with the testmode command in nl80211.
  648. */
  649. /* These enums need to be kept in sync with userspace */
  650. enum hwsim_testmode_attr {
  651. __HWSIM_TM_ATTR_INVALID = 0,
  652. HWSIM_TM_ATTR_CMD = 1,
  653. HWSIM_TM_ATTR_PS = 2,
  654. /* keep last */
  655. __HWSIM_TM_ATTR_AFTER_LAST,
  656. HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
  657. };
  658. enum hwsim_testmode_cmd {
  659. HWSIM_TM_CMD_SET_PS = 0,
  660. HWSIM_TM_CMD_GET_PS = 1,
  661. };
  662. static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
  663. [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
  664. [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
  665. };
  666. static int hwsim_fops_ps_write(void *dat, u64 val);
  667. static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
  668. void *data, int len)
  669. {
  670. struct mac80211_hwsim_data *hwsim = hw->priv;
  671. struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
  672. struct sk_buff *skb;
  673. int err, ps;
  674. err = nla_parse(tb, HWSIM_TM_ATTR_MAX, data, len,
  675. hwsim_testmode_policy);
  676. if (err)
  677. return err;
  678. if (!tb[HWSIM_TM_ATTR_CMD])
  679. return -EINVAL;
  680. switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
  681. case HWSIM_TM_CMD_SET_PS:
  682. if (!tb[HWSIM_TM_ATTR_PS])
  683. return -EINVAL;
  684. ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
  685. return hwsim_fops_ps_write(hwsim, ps);
  686. case HWSIM_TM_CMD_GET_PS:
  687. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
  688. nla_total_size(sizeof(u32)));
  689. if (!skb)
  690. return -ENOMEM;
  691. NLA_PUT_U32(skb, HWSIM_TM_ATTR_PS, hwsim->ps);
  692. return cfg80211_testmode_reply(skb);
  693. default:
  694. return -EOPNOTSUPP;
  695. }
  696. nla_put_failure:
  697. kfree_skb(skb);
  698. return -ENOBUFS;
  699. }
  700. #endif
  701. static const struct ieee80211_ops mac80211_hwsim_ops =
  702. {
  703. .tx = mac80211_hwsim_tx,
  704. .start = mac80211_hwsim_start,
  705. .stop = mac80211_hwsim_stop,
  706. .add_interface = mac80211_hwsim_add_interface,
  707. .remove_interface = mac80211_hwsim_remove_interface,
  708. .config = mac80211_hwsim_config,
  709. .configure_filter = mac80211_hwsim_configure_filter,
  710. .bss_info_changed = mac80211_hwsim_bss_info_changed,
  711. .sta_notify = mac80211_hwsim_sta_notify,
  712. .set_tim = mac80211_hwsim_set_tim,
  713. .conf_tx = mac80211_hwsim_conf_tx,
  714. CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd)
  715. };
  716. static void mac80211_hwsim_free(void)
  717. {
  718. struct list_head tmplist, *i, *tmp;
  719. struct mac80211_hwsim_data *data, *tmpdata;
  720. INIT_LIST_HEAD(&tmplist);
  721. spin_lock_bh(&hwsim_radio_lock);
  722. list_for_each_safe(i, tmp, &hwsim_radios)
  723. list_move(i, &tmplist);
  724. spin_unlock_bh(&hwsim_radio_lock);
  725. list_for_each_entry_safe(data, tmpdata, &tmplist, list) {
  726. debugfs_remove(data->debugfs_group);
  727. debugfs_remove(data->debugfs_ps);
  728. debugfs_remove(data->debugfs);
  729. ieee80211_unregister_hw(data->hw);
  730. device_unregister(data->dev);
  731. ieee80211_free_hw(data->hw);
  732. }
  733. class_destroy(hwsim_class);
  734. }
  735. static struct device_driver mac80211_hwsim_driver = {
  736. .name = "mac80211_hwsim"
  737. };
  738. static const struct net_device_ops hwsim_netdev_ops = {
  739. .ndo_start_xmit = hwsim_mon_xmit,
  740. .ndo_change_mtu = eth_change_mtu,
  741. .ndo_set_mac_address = eth_mac_addr,
  742. .ndo_validate_addr = eth_validate_addr,
  743. };
  744. static void hwsim_mon_setup(struct net_device *dev)
  745. {
  746. dev->netdev_ops = &hwsim_netdev_ops;
  747. dev->destructor = free_netdev;
  748. ether_setup(dev);
  749. dev->tx_queue_len = 0;
  750. dev->type = ARPHRD_IEEE80211_RADIOTAP;
  751. memset(dev->dev_addr, 0, ETH_ALEN);
  752. dev->dev_addr[0] = 0x12;
  753. }
  754. static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
  755. {
  756. struct mac80211_hwsim_data *data = dat;
  757. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  758. struct sk_buff *skb;
  759. struct ieee80211_pspoll *pspoll;
  760. if (!vp->assoc)
  761. return;
  762. printk(KERN_DEBUG "%s:%s: send PS-Poll to %pM for aid %d\n",
  763. wiphy_name(data->hw->wiphy), __func__, vp->bssid, vp->aid);
  764. skb = dev_alloc_skb(sizeof(*pspoll));
  765. if (!skb)
  766. return;
  767. pspoll = (void *) skb_put(skb, sizeof(*pspoll));
  768. pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  769. IEEE80211_STYPE_PSPOLL |
  770. IEEE80211_FCTL_PM);
  771. pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
  772. memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
  773. memcpy(pspoll->ta, mac, ETH_ALEN);
  774. if (!mac80211_hwsim_tx_frame(data->hw, skb))
  775. printk(KERN_DEBUG "%s: PS-Poll frame not ack'ed\n", __func__);
  776. dev_kfree_skb(skb);
  777. }
  778. static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
  779. struct ieee80211_vif *vif, int ps)
  780. {
  781. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  782. struct sk_buff *skb;
  783. struct ieee80211_hdr *hdr;
  784. if (!vp->assoc)
  785. return;
  786. printk(KERN_DEBUG "%s:%s: send data::nullfunc to %pM ps=%d\n",
  787. wiphy_name(data->hw->wiphy), __func__, vp->bssid, ps);
  788. skb = dev_alloc_skb(sizeof(*hdr));
  789. if (!skb)
  790. return;
  791. hdr = (void *) skb_put(skb, sizeof(*hdr) - ETH_ALEN);
  792. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  793. IEEE80211_STYPE_NULLFUNC |
  794. (ps ? IEEE80211_FCTL_PM : 0));
  795. hdr->duration_id = cpu_to_le16(0);
  796. memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
  797. memcpy(hdr->addr2, mac, ETH_ALEN);
  798. memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
  799. if (!mac80211_hwsim_tx_frame(data->hw, skb))
  800. printk(KERN_DEBUG "%s: nullfunc frame not ack'ed\n", __func__);
  801. dev_kfree_skb(skb);
  802. }
  803. static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
  804. struct ieee80211_vif *vif)
  805. {
  806. struct mac80211_hwsim_data *data = dat;
  807. hwsim_send_nullfunc(data, mac, vif, 1);
  808. }
  809. static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
  810. struct ieee80211_vif *vif)
  811. {
  812. struct mac80211_hwsim_data *data = dat;
  813. hwsim_send_nullfunc(data, mac, vif, 0);
  814. }
  815. static int hwsim_fops_ps_read(void *dat, u64 *val)
  816. {
  817. struct mac80211_hwsim_data *data = dat;
  818. *val = data->ps;
  819. return 0;
  820. }
  821. static int hwsim_fops_ps_write(void *dat, u64 val)
  822. {
  823. struct mac80211_hwsim_data *data = dat;
  824. enum ps_mode old_ps;
  825. if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
  826. val != PS_MANUAL_POLL)
  827. return -EINVAL;
  828. old_ps = data->ps;
  829. data->ps = val;
  830. if (val == PS_MANUAL_POLL) {
  831. ieee80211_iterate_active_interfaces(data->hw,
  832. hwsim_send_ps_poll, data);
  833. data->ps_poll_pending = true;
  834. } else if (old_ps == PS_DISABLED && val != PS_DISABLED) {
  835. ieee80211_iterate_active_interfaces(data->hw,
  836. hwsim_send_nullfunc_ps,
  837. data);
  838. } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
  839. ieee80211_iterate_active_interfaces(data->hw,
  840. hwsim_send_nullfunc_no_ps,
  841. data);
  842. }
  843. return 0;
  844. }
  845. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
  846. "%llu\n");
  847. static int hwsim_fops_group_read(void *dat, u64 *val)
  848. {
  849. struct mac80211_hwsim_data *data = dat;
  850. *val = data->group;
  851. return 0;
  852. }
  853. static int hwsim_fops_group_write(void *dat, u64 val)
  854. {
  855. struct mac80211_hwsim_data *data = dat;
  856. data->group = val;
  857. return 0;
  858. }
  859. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
  860. hwsim_fops_group_read, hwsim_fops_group_write,
  861. "%llx\n");
  862. static int __init init_mac80211_hwsim(void)
  863. {
  864. int i, err = 0;
  865. u8 addr[ETH_ALEN];
  866. struct mac80211_hwsim_data *data;
  867. struct ieee80211_hw *hw;
  868. enum ieee80211_band band;
  869. if (radios < 1 || radios > 100)
  870. return -EINVAL;
  871. spin_lock_init(&hwsim_radio_lock);
  872. INIT_LIST_HEAD(&hwsim_radios);
  873. hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
  874. if (IS_ERR(hwsim_class))
  875. return PTR_ERR(hwsim_class);
  876. memset(addr, 0, ETH_ALEN);
  877. addr[0] = 0x02;
  878. for (i = 0; i < radios; i++) {
  879. printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
  880. i);
  881. hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
  882. if (!hw) {
  883. printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
  884. "failed\n");
  885. err = -ENOMEM;
  886. goto failed;
  887. }
  888. data = hw->priv;
  889. data->hw = hw;
  890. data->dev = device_create(hwsim_class, NULL, 0, hw,
  891. "hwsim%d", i);
  892. if (IS_ERR(data->dev)) {
  893. printk(KERN_DEBUG
  894. "mac80211_hwsim: device_create "
  895. "failed (%ld)\n", PTR_ERR(data->dev));
  896. err = -ENOMEM;
  897. goto failed_drvdata;
  898. }
  899. data->dev->driver = &mac80211_hwsim_driver;
  900. SET_IEEE80211_DEV(hw, data->dev);
  901. addr[3] = i >> 8;
  902. addr[4] = i;
  903. SET_IEEE80211_PERM_ADDR(hw, addr);
  904. hw->channel_change_time = 1;
  905. hw->queues = 4;
  906. hw->wiphy->interface_modes =
  907. BIT(NL80211_IFTYPE_STATION) |
  908. BIT(NL80211_IFTYPE_AP) |
  909. BIT(NL80211_IFTYPE_MESH_POINT);
  910. hw->flags = IEEE80211_HW_MFP_CAPABLE |
  911. IEEE80211_HW_SIGNAL_DBM;
  912. /* ask mac80211 to reserve space for magic */
  913. hw->vif_data_size = sizeof(struct hwsim_vif_priv);
  914. hw->sta_data_size = sizeof(struct hwsim_sta_priv);
  915. memcpy(data->channels_2ghz, hwsim_channels_2ghz,
  916. sizeof(hwsim_channels_2ghz));
  917. memcpy(data->channels_5ghz, hwsim_channels_5ghz,
  918. sizeof(hwsim_channels_5ghz));
  919. memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
  920. for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
  921. struct ieee80211_supported_band *sband = &data->bands[band];
  922. switch (band) {
  923. case IEEE80211_BAND_2GHZ:
  924. sband->channels = data->channels_2ghz;
  925. sband->n_channels =
  926. ARRAY_SIZE(hwsim_channels_2ghz);
  927. sband->bitrates = data->rates;
  928. sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
  929. break;
  930. case IEEE80211_BAND_5GHZ:
  931. sband->channels = data->channels_5ghz;
  932. sband->n_channels =
  933. ARRAY_SIZE(hwsim_channels_5ghz);
  934. sband->bitrates = data->rates + 4;
  935. sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
  936. break;
  937. default:
  938. break;
  939. }
  940. sband->ht_cap.ht_supported = true;
  941. sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  942. IEEE80211_HT_CAP_GRN_FLD |
  943. IEEE80211_HT_CAP_SGI_40 |
  944. IEEE80211_HT_CAP_DSSSCCK40;
  945. sband->ht_cap.ampdu_factor = 0x3;
  946. sband->ht_cap.ampdu_density = 0x6;
  947. memset(&sband->ht_cap.mcs, 0,
  948. sizeof(sband->ht_cap.mcs));
  949. sband->ht_cap.mcs.rx_mask[0] = 0xff;
  950. sband->ht_cap.mcs.rx_mask[1] = 0xff;
  951. sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  952. hw->wiphy->bands[band] = sband;
  953. }
  954. /* By default all radios are belonging to the first group */
  955. data->group = 1;
  956. /* Work to be done prior to ieee80211_register_hw() */
  957. switch (regtest) {
  958. case HWSIM_REGTEST_DISABLED:
  959. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  960. case HWSIM_REGTEST_DRIVER_REG_ALL:
  961. case HWSIM_REGTEST_DIFF_COUNTRY:
  962. /*
  963. * Nothing to be done for driver regulatory domain
  964. * hints prior to ieee80211_register_hw()
  965. */
  966. break;
  967. case HWSIM_REGTEST_WORLD_ROAM:
  968. if (i == 0) {
  969. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  970. wiphy_apply_custom_regulatory(hw->wiphy,
  971. &hwsim_world_regdom_custom_01);
  972. }
  973. break;
  974. case HWSIM_REGTEST_CUSTOM_WORLD:
  975. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  976. wiphy_apply_custom_regulatory(hw->wiphy,
  977. &hwsim_world_regdom_custom_01);
  978. break;
  979. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  980. if (i == 0) {
  981. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  982. wiphy_apply_custom_regulatory(hw->wiphy,
  983. &hwsim_world_regdom_custom_01);
  984. } else if (i == 1) {
  985. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  986. wiphy_apply_custom_regulatory(hw->wiphy,
  987. &hwsim_world_regdom_custom_02);
  988. }
  989. break;
  990. case HWSIM_REGTEST_STRICT_ALL:
  991. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  992. break;
  993. case HWSIM_REGTEST_STRICT_FOLLOW:
  994. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  995. if (i == 0)
  996. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  997. break;
  998. case HWSIM_REGTEST_ALL:
  999. if (i == 0) {
  1000. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1001. wiphy_apply_custom_regulatory(hw->wiphy,
  1002. &hwsim_world_regdom_custom_01);
  1003. } else if (i == 1) {
  1004. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1005. wiphy_apply_custom_regulatory(hw->wiphy,
  1006. &hwsim_world_regdom_custom_02);
  1007. } else if (i == 4)
  1008. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1009. break;
  1010. default:
  1011. break;
  1012. }
  1013. /* give the regulatory workqueue a chance to run */
  1014. if (regtest)
  1015. schedule_timeout_interruptible(1);
  1016. err = ieee80211_register_hw(hw);
  1017. if (err < 0) {
  1018. printk(KERN_DEBUG "mac80211_hwsim: "
  1019. "ieee80211_register_hw failed (%d)\n", err);
  1020. goto failed_hw;
  1021. }
  1022. /* Work to be done after to ieee80211_register_hw() */
  1023. switch (regtest) {
  1024. case HWSIM_REGTEST_WORLD_ROAM:
  1025. case HWSIM_REGTEST_DISABLED:
  1026. break;
  1027. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  1028. if (!i)
  1029. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1030. break;
  1031. case HWSIM_REGTEST_DRIVER_REG_ALL:
  1032. case HWSIM_REGTEST_STRICT_ALL:
  1033. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1034. break;
  1035. case HWSIM_REGTEST_DIFF_COUNTRY:
  1036. if (i < ARRAY_SIZE(hwsim_alpha2s))
  1037. regulatory_hint(hw->wiphy, hwsim_alpha2s[i]);
  1038. break;
  1039. case HWSIM_REGTEST_CUSTOM_WORLD:
  1040. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  1041. /*
  1042. * Nothing to be done for custom world regulatory
  1043. * domains after to ieee80211_register_hw
  1044. */
  1045. break;
  1046. case HWSIM_REGTEST_STRICT_FOLLOW:
  1047. if (i == 0)
  1048. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1049. break;
  1050. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  1051. if (i == 0)
  1052. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1053. else if (i == 1)
  1054. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  1055. break;
  1056. case HWSIM_REGTEST_ALL:
  1057. if (i == 2)
  1058. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1059. else if (i == 3)
  1060. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  1061. else if (i == 4)
  1062. regulatory_hint(hw->wiphy, hwsim_alpha2s[2]);
  1063. break;
  1064. default:
  1065. break;
  1066. }
  1067. printk(KERN_DEBUG "%s: hwaddr %pM registered\n",
  1068. wiphy_name(hw->wiphy),
  1069. hw->wiphy->perm_addr);
  1070. data->debugfs = debugfs_create_dir("hwsim",
  1071. hw->wiphy->debugfsdir);
  1072. data->debugfs_ps = debugfs_create_file("ps", 0666,
  1073. data->debugfs, data,
  1074. &hwsim_fops_ps);
  1075. data->debugfs_group = debugfs_create_file("group", 0666,
  1076. data->debugfs, data,
  1077. &hwsim_fops_group);
  1078. setup_timer(&data->beacon_timer, mac80211_hwsim_beacon,
  1079. (unsigned long) hw);
  1080. list_add_tail(&data->list, &hwsim_radios);
  1081. }
  1082. hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
  1083. if (hwsim_mon == NULL)
  1084. goto failed;
  1085. rtnl_lock();
  1086. err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
  1087. if (err < 0)
  1088. goto failed_mon;
  1089. err = register_netdevice(hwsim_mon);
  1090. if (err < 0)
  1091. goto failed_mon;
  1092. rtnl_unlock();
  1093. return 0;
  1094. failed_mon:
  1095. rtnl_unlock();
  1096. free_netdev(hwsim_mon);
  1097. mac80211_hwsim_free();
  1098. return err;
  1099. failed_hw:
  1100. device_unregister(data->dev);
  1101. failed_drvdata:
  1102. ieee80211_free_hw(hw);
  1103. failed:
  1104. mac80211_hwsim_free();
  1105. return err;
  1106. }
  1107. static void __exit exit_mac80211_hwsim(void)
  1108. {
  1109. printk(KERN_DEBUG "mac80211_hwsim: unregister radios\n");
  1110. mac80211_hwsim_free();
  1111. unregister_netdev(hwsim_mon);
  1112. }
  1113. module_init(init_mac80211_hwsim);
  1114. module_exit(exit_mac80211_hwsim);