mac80211_hwsim.c 41 KB

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