mac80211_hwsim.c 34 KB

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