mac80211_hwsim.c 41 KB

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