mac80211_hwsim.c 36 KB

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