mac80211_hwsim.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540
  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. } __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 const char *hwsim_chantypes[] = {
  553. [NL80211_CHAN_NO_HT] = "noht",
  554. [NL80211_CHAN_HT20] = "ht20",
  555. [NL80211_CHAN_HT40MINUS] = "ht40-",
  556. [NL80211_CHAN_HT40PLUS] = "ht40+",
  557. };
  558. static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
  559. {
  560. struct mac80211_hwsim_data *data = hw->priv;
  561. struct ieee80211_conf *conf = &hw->conf;
  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. hwsim_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, chantype=%s\n",
  642. wiphy_name(hw->wiphy),
  643. info->ht_operation_mode,
  644. hwsim_chantypes[info->channel_type]);
  645. }
  646. if (changed & BSS_CHANGED_BASIC_RATES) {
  647. printk(KERN_DEBUG " %s: BASIC_RATES: 0x%llx\n",
  648. wiphy_name(hw->wiphy),
  649. (unsigned long long) info->basic_rates);
  650. }
  651. }
  652. static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
  653. struct ieee80211_vif *vif,
  654. struct ieee80211_sta *sta)
  655. {
  656. hwsim_check_magic(vif);
  657. hwsim_set_sta_magic(sta);
  658. return 0;
  659. }
  660. static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
  661. struct ieee80211_vif *vif,
  662. struct ieee80211_sta *sta)
  663. {
  664. hwsim_check_magic(vif);
  665. hwsim_clear_sta_magic(sta);
  666. return 0;
  667. }
  668. static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
  669. struct ieee80211_vif *vif,
  670. enum sta_notify_cmd cmd,
  671. struct ieee80211_sta *sta)
  672. {
  673. hwsim_check_magic(vif);
  674. switch (cmd) {
  675. case STA_NOTIFY_SLEEP:
  676. case STA_NOTIFY_AWAKE:
  677. /* TODO: make good use of these flags */
  678. break;
  679. default:
  680. WARN(1, "Invalid sta notify: %d\n", cmd);
  681. break;
  682. }
  683. }
  684. static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
  685. struct ieee80211_sta *sta,
  686. bool set)
  687. {
  688. hwsim_check_sta_magic(sta);
  689. return 0;
  690. }
  691. static int mac80211_hwsim_conf_tx(
  692. struct ieee80211_hw *hw, u16 queue,
  693. const struct ieee80211_tx_queue_params *params)
  694. {
  695. printk(KERN_DEBUG "%s:%s (queue=%d txop=%d cw_min=%d cw_max=%d "
  696. "aifs=%d)\n",
  697. wiphy_name(hw->wiphy), __func__, queue,
  698. params->txop, params->cw_min, params->cw_max, params->aifs);
  699. return 0;
  700. }
  701. static int mac80211_hwsim_get_survey(
  702. struct ieee80211_hw *hw, int idx,
  703. struct survey_info *survey)
  704. {
  705. struct ieee80211_conf *conf = &hw->conf;
  706. printk(KERN_DEBUG "%s:%s (idx=%d)\n",
  707. wiphy_name(hw->wiphy), __func__, idx);
  708. if (idx != 0)
  709. return -ENOENT;
  710. /* Current channel */
  711. survey->channel = conf->channel;
  712. /*
  713. * Magically conjured noise level --- this is only ok for simulated hardware.
  714. *
  715. * A real driver which cannot determine the real channel noise MUST NOT
  716. * report any noise, especially not a magically conjured one :-)
  717. */
  718. survey->filled = SURVEY_INFO_NOISE_DBM;
  719. survey->noise = -92;
  720. return 0;
  721. }
  722. #ifdef CONFIG_NL80211_TESTMODE
  723. /*
  724. * This section contains example code for using netlink
  725. * attributes with the testmode command in nl80211.
  726. */
  727. /* These enums need to be kept in sync with userspace */
  728. enum hwsim_testmode_attr {
  729. __HWSIM_TM_ATTR_INVALID = 0,
  730. HWSIM_TM_ATTR_CMD = 1,
  731. HWSIM_TM_ATTR_PS = 2,
  732. /* keep last */
  733. __HWSIM_TM_ATTR_AFTER_LAST,
  734. HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
  735. };
  736. enum hwsim_testmode_cmd {
  737. HWSIM_TM_CMD_SET_PS = 0,
  738. HWSIM_TM_CMD_GET_PS = 1,
  739. };
  740. static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
  741. [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
  742. [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
  743. };
  744. static int hwsim_fops_ps_write(void *dat, u64 val);
  745. static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
  746. void *data, int len)
  747. {
  748. struct mac80211_hwsim_data *hwsim = hw->priv;
  749. struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
  750. struct sk_buff *skb;
  751. int err, ps;
  752. err = nla_parse(tb, HWSIM_TM_ATTR_MAX, data, len,
  753. hwsim_testmode_policy);
  754. if (err)
  755. return err;
  756. if (!tb[HWSIM_TM_ATTR_CMD])
  757. return -EINVAL;
  758. switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
  759. case HWSIM_TM_CMD_SET_PS:
  760. if (!tb[HWSIM_TM_ATTR_PS])
  761. return -EINVAL;
  762. ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
  763. return hwsim_fops_ps_write(hwsim, ps);
  764. case HWSIM_TM_CMD_GET_PS:
  765. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
  766. nla_total_size(sizeof(u32)));
  767. if (!skb)
  768. return -ENOMEM;
  769. NLA_PUT_U32(skb, HWSIM_TM_ATTR_PS, hwsim->ps);
  770. return cfg80211_testmode_reply(skb);
  771. default:
  772. return -EOPNOTSUPP;
  773. }
  774. nla_put_failure:
  775. kfree_skb(skb);
  776. return -ENOBUFS;
  777. }
  778. #endif
  779. static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
  780. struct ieee80211_vif *vif,
  781. enum ieee80211_ampdu_mlme_action action,
  782. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  783. {
  784. switch (action) {
  785. case IEEE80211_AMPDU_TX_START:
  786. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  787. break;
  788. case IEEE80211_AMPDU_TX_STOP:
  789. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  790. break;
  791. case IEEE80211_AMPDU_TX_OPERATIONAL:
  792. break;
  793. case IEEE80211_AMPDU_RX_START:
  794. case IEEE80211_AMPDU_RX_STOP:
  795. break;
  796. default:
  797. return -EOPNOTSUPP;
  798. }
  799. return 0;
  800. }
  801. static void mac80211_hwsim_flush(struct ieee80211_hw *hw, bool drop)
  802. {
  803. /*
  804. * In this special case, there's nothing we need to
  805. * do because hwsim does transmission synchronously.
  806. * In the future, when it does transmissions via
  807. * userspace, we may need to do something.
  808. */
  809. }
  810. struct hw_scan_done {
  811. struct delayed_work w;
  812. struct ieee80211_hw *hw;
  813. };
  814. static void hw_scan_done(struct work_struct *work)
  815. {
  816. struct hw_scan_done *hsd =
  817. container_of(work, struct hw_scan_done, w.work);
  818. ieee80211_scan_completed(hsd->hw, false);
  819. kfree(hsd);
  820. }
  821. static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
  822. struct ieee80211_vif *vif,
  823. struct cfg80211_scan_request *req)
  824. {
  825. struct hw_scan_done *hsd = kzalloc(sizeof(*hsd), GFP_KERNEL);
  826. int i;
  827. if (!hsd)
  828. return -ENOMEM;
  829. hsd->hw = hw;
  830. INIT_DELAYED_WORK(&hsd->w, hw_scan_done);
  831. printk(KERN_DEBUG "hwsim hw_scan request\n");
  832. for (i = 0; i < req->n_channels; i++)
  833. printk(KERN_DEBUG "hwsim hw_scan freq %d\n",
  834. req->channels[i]->center_freq);
  835. ieee80211_queue_delayed_work(hw, &hsd->w, 2 * HZ);
  836. return 0;
  837. }
  838. static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw)
  839. {
  840. struct mac80211_hwsim_data *hwsim = hw->priv;
  841. mutex_lock(&hwsim->mutex);
  842. if (hwsim->scanning) {
  843. printk(KERN_DEBUG "two hwsim sw_scans detected!\n");
  844. goto out;
  845. }
  846. printk(KERN_DEBUG "hwsim sw_scan request, prepping stuff\n");
  847. hwsim->scanning = true;
  848. out:
  849. mutex_unlock(&hwsim->mutex);
  850. }
  851. static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw)
  852. {
  853. struct mac80211_hwsim_data *hwsim = hw->priv;
  854. mutex_lock(&hwsim->mutex);
  855. printk(KERN_DEBUG "hwsim sw_scan_complete\n");
  856. hwsim->scanning = false;
  857. mutex_unlock(&hwsim->mutex);
  858. }
  859. static struct ieee80211_ops mac80211_hwsim_ops =
  860. {
  861. .tx = mac80211_hwsim_tx,
  862. .start = mac80211_hwsim_start,
  863. .stop = mac80211_hwsim_stop,
  864. .add_interface = mac80211_hwsim_add_interface,
  865. .remove_interface = mac80211_hwsim_remove_interface,
  866. .config = mac80211_hwsim_config,
  867. .configure_filter = mac80211_hwsim_configure_filter,
  868. .bss_info_changed = mac80211_hwsim_bss_info_changed,
  869. .sta_add = mac80211_hwsim_sta_add,
  870. .sta_remove = mac80211_hwsim_sta_remove,
  871. .sta_notify = mac80211_hwsim_sta_notify,
  872. .set_tim = mac80211_hwsim_set_tim,
  873. .conf_tx = mac80211_hwsim_conf_tx,
  874. .get_survey = mac80211_hwsim_get_survey,
  875. CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd)
  876. .ampdu_action = mac80211_hwsim_ampdu_action,
  877. .sw_scan_start = mac80211_hwsim_sw_scan,
  878. .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
  879. .flush = mac80211_hwsim_flush,
  880. };
  881. static void mac80211_hwsim_free(void)
  882. {
  883. struct list_head tmplist, *i, *tmp;
  884. struct mac80211_hwsim_data *data, *tmpdata;
  885. INIT_LIST_HEAD(&tmplist);
  886. spin_lock_bh(&hwsim_radio_lock);
  887. list_for_each_safe(i, tmp, &hwsim_radios)
  888. list_move(i, &tmplist);
  889. spin_unlock_bh(&hwsim_radio_lock);
  890. list_for_each_entry_safe(data, tmpdata, &tmplist, list) {
  891. debugfs_remove(data->debugfs_group);
  892. debugfs_remove(data->debugfs_ps);
  893. debugfs_remove(data->debugfs);
  894. ieee80211_unregister_hw(data->hw);
  895. device_unregister(data->dev);
  896. ieee80211_free_hw(data->hw);
  897. }
  898. class_destroy(hwsim_class);
  899. }
  900. static struct device_driver mac80211_hwsim_driver = {
  901. .name = "mac80211_hwsim"
  902. };
  903. static const struct net_device_ops hwsim_netdev_ops = {
  904. .ndo_start_xmit = hwsim_mon_xmit,
  905. .ndo_change_mtu = eth_change_mtu,
  906. .ndo_set_mac_address = eth_mac_addr,
  907. .ndo_validate_addr = eth_validate_addr,
  908. };
  909. static void hwsim_mon_setup(struct net_device *dev)
  910. {
  911. dev->netdev_ops = &hwsim_netdev_ops;
  912. dev->destructor = free_netdev;
  913. ether_setup(dev);
  914. dev->tx_queue_len = 0;
  915. dev->type = ARPHRD_IEEE80211_RADIOTAP;
  916. memset(dev->dev_addr, 0, ETH_ALEN);
  917. dev->dev_addr[0] = 0x12;
  918. }
  919. static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
  920. {
  921. struct mac80211_hwsim_data *data = dat;
  922. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  923. struct sk_buff *skb;
  924. struct ieee80211_pspoll *pspoll;
  925. if (!vp->assoc)
  926. return;
  927. printk(KERN_DEBUG "%s:%s: send PS-Poll to %pM for aid %d\n",
  928. wiphy_name(data->hw->wiphy), __func__, vp->bssid, vp->aid);
  929. skb = dev_alloc_skb(sizeof(*pspoll));
  930. if (!skb)
  931. return;
  932. pspoll = (void *) skb_put(skb, sizeof(*pspoll));
  933. pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  934. IEEE80211_STYPE_PSPOLL |
  935. IEEE80211_FCTL_PM);
  936. pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
  937. memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
  938. memcpy(pspoll->ta, mac, ETH_ALEN);
  939. if (!mac80211_hwsim_tx_frame(data->hw, skb))
  940. printk(KERN_DEBUG "%s: PS-Poll frame not ack'ed\n", __func__);
  941. dev_kfree_skb(skb);
  942. }
  943. static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
  944. struct ieee80211_vif *vif, int ps)
  945. {
  946. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  947. struct sk_buff *skb;
  948. struct ieee80211_hdr *hdr;
  949. if (!vp->assoc)
  950. return;
  951. printk(KERN_DEBUG "%s:%s: send data::nullfunc to %pM ps=%d\n",
  952. wiphy_name(data->hw->wiphy), __func__, vp->bssid, ps);
  953. skb = dev_alloc_skb(sizeof(*hdr));
  954. if (!skb)
  955. return;
  956. hdr = (void *) skb_put(skb, sizeof(*hdr) - ETH_ALEN);
  957. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  958. IEEE80211_STYPE_NULLFUNC |
  959. (ps ? IEEE80211_FCTL_PM : 0));
  960. hdr->duration_id = cpu_to_le16(0);
  961. memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
  962. memcpy(hdr->addr2, mac, ETH_ALEN);
  963. memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
  964. if (!mac80211_hwsim_tx_frame(data->hw, skb))
  965. printk(KERN_DEBUG "%s: nullfunc frame not ack'ed\n", __func__);
  966. dev_kfree_skb(skb);
  967. }
  968. static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
  969. struct ieee80211_vif *vif)
  970. {
  971. struct mac80211_hwsim_data *data = dat;
  972. hwsim_send_nullfunc(data, mac, vif, 1);
  973. }
  974. static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
  975. struct ieee80211_vif *vif)
  976. {
  977. struct mac80211_hwsim_data *data = dat;
  978. hwsim_send_nullfunc(data, mac, vif, 0);
  979. }
  980. static int hwsim_fops_ps_read(void *dat, u64 *val)
  981. {
  982. struct mac80211_hwsim_data *data = dat;
  983. *val = data->ps;
  984. return 0;
  985. }
  986. static int hwsim_fops_ps_write(void *dat, u64 val)
  987. {
  988. struct mac80211_hwsim_data *data = dat;
  989. enum ps_mode old_ps;
  990. if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
  991. val != PS_MANUAL_POLL)
  992. return -EINVAL;
  993. old_ps = data->ps;
  994. data->ps = val;
  995. if (val == PS_MANUAL_POLL) {
  996. ieee80211_iterate_active_interfaces(data->hw,
  997. hwsim_send_ps_poll, data);
  998. data->ps_poll_pending = true;
  999. } else if (old_ps == PS_DISABLED && val != PS_DISABLED) {
  1000. ieee80211_iterate_active_interfaces(data->hw,
  1001. hwsim_send_nullfunc_ps,
  1002. data);
  1003. } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
  1004. ieee80211_iterate_active_interfaces(data->hw,
  1005. hwsim_send_nullfunc_no_ps,
  1006. data);
  1007. }
  1008. return 0;
  1009. }
  1010. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
  1011. "%llu\n");
  1012. static int hwsim_fops_group_read(void *dat, u64 *val)
  1013. {
  1014. struct mac80211_hwsim_data *data = dat;
  1015. *val = data->group;
  1016. return 0;
  1017. }
  1018. static int hwsim_fops_group_write(void *dat, u64 val)
  1019. {
  1020. struct mac80211_hwsim_data *data = dat;
  1021. data->group = val;
  1022. return 0;
  1023. }
  1024. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
  1025. hwsim_fops_group_read, hwsim_fops_group_write,
  1026. "%llx\n");
  1027. static int __init init_mac80211_hwsim(void)
  1028. {
  1029. int i, err = 0;
  1030. u8 addr[ETH_ALEN];
  1031. struct mac80211_hwsim_data *data;
  1032. struct ieee80211_hw *hw;
  1033. enum ieee80211_band band;
  1034. if (radios < 1 || radios > 100)
  1035. return -EINVAL;
  1036. if (fake_hw_scan) {
  1037. mac80211_hwsim_ops.hw_scan = mac80211_hwsim_hw_scan;
  1038. mac80211_hwsim_ops.sw_scan_start = NULL;
  1039. mac80211_hwsim_ops.sw_scan_complete = NULL;
  1040. }
  1041. spin_lock_init(&hwsim_radio_lock);
  1042. INIT_LIST_HEAD(&hwsim_radios);
  1043. hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
  1044. if (IS_ERR(hwsim_class))
  1045. return PTR_ERR(hwsim_class);
  1046. memset(addr, 0, ETH_ALEN);
  1047. addr[0] = 0x02;
  1048. for (i = 0; i < radios; i++) {
  1049. printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
  1050. i);
  1051. hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
  1052. if (!hw) {
  1053. printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
  1054. "failed\n");
  1055. err = -ENOMEM;
  1056. goto failed;
  1057. }
  1058. data = hw->priv;
  1059. data->hw = hw;
  1060. data->dev = device_create(hwsim_class, NULL, 0, hw,
  1061. "hwsim%d", i);
  1062. if (IS_ERR(data->dev)) {
  1063. printk(KERN_DEBUG
  1064. "mac80211_hwsim: device_create "
  1065. "failed (%ld)\n", PTR_ERR(data->dev));
  1066. err = -ENOMEM;
  1067. goto failed_drvdata;
  1068. }
  1069. data->dev->driver = &mac80211_hwsim_driver;
  1070. SET_IEEE80211_DEV(hw, data->dev);
  1071. addr[3] = i >> 8;
  1072. addr[4] = i;
  1073. memcpy(data->addresses[0].addr, addr, ETH_ALEN);
  1074. memcpy(data->addresses[1].addr, addr, ETH_ALEN);
  1075. data->addresses[1].addr[0] |= 0x40;
  1076. hw->wiphy->n_addresses = 2;
  1077. hw->wiphy->addresses = data->addresses;
  1078. if (fake_hw_scan) {
  1079. hw->wiphy->max_scan_ssids = 255;
  1080. hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
  1081. }
  1082. hw->channel_change_time = 1;
  1083. hw->queues = 4;
  1084. hw->wiphy->interface_modes =
  1085. BIT(NL80211_IFTYPE_STATION) |
  1086. BIT(NL80211_IFTYPE_AP) |
  1087. BIT(NL80211_IFTYPE_MESH_POINT);
  1088. hw->flags = IEEE80211_HW_MFP_CAPABLE |
  1089. IEEE80211_HW_SIGNAL_DBM |
  1090. IEEE80211_HW_SUPPORTS_STATIC_SMPS |
  1091. IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
  1092. IEEE80211_HW_AMPDU_AGGREGATION;
  1093. /* ask mac80211 to reserve space for magic */
  1094. hw->vif_data_size = sizeof(struct hwsim_vif_priv);
  1095. hw->sta_data_size = sizeof(struct hwsim_sta_priv);
  1096. memcpy(data->channels_2ghz, hwsim_channels_2ghz,
  1097. sizeof(hwsim_channels_2ghz));
  1098. memcpy(data->channels_5ghz, hwsim_channels_5ghz,
  1099. sizeof(hwsim_channels_5ghz));
  1100. memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
  1101. for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
  1102. struct ieee80211_supported_band *sband = &data->bands[band];
  1103. switch (band) {
  1104. case IEEE80211_BAND_2GHZ:
  1105. sband->channels = data->channels_2ghz;
  1106. sband->n_channels =
  1107. ARRAY_SIZE(hwsim_channels_2ghz);
  1108. sband->bitrates = data->rates;
  1109. sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
  1110. break;
  1111. case IEEE80211_BAND_5GHZ:
  1112. sband->channels = data->channels_5ghz;
  1113. sband->n_channels =
  1114. ARRAY_SIZE(hwsim_channels_5ghz);
  1115. sband->bitrates = data->rates + 4;
  1116. sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
  1117. break;
  1118. default:
  1119. break;
  1120. }
  1121. sband->ht_cap.ht_supported = true;
  1122. sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  1123. IEEE80211_HT_CAP_GRN_FLD |
  1124. IEEE80211_HT_CAP_SGI_40 |
  1125. IEEE80211_HT_CAP_DSSSCCK40;
  1126. sband->ht_cap.ampdu_factor = 0x3;
  1127. sband->ht_cap.ampdu_density = 0x6;
  1128. memset(&sband->ht_cap.mcs, 0,
  1129. sizeof(sband->ht_cap.mcs));
  1130. sband->ht_cap.mcs.rx_mask[0] = 0xff;
  1131. sband->ht_cap.mcs.rx_mask[1] = 0xff;
  1132. sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  1133. hw->wiphy->bands[band] = sband;
  1134. }
  1135. /* By default all radios are belonging to the first group */
  1136. data->group = 1;
  1137. mutex_init(&data->mutex);
  1138. /* Work to be done prior to ieee80211_register_hw() */
  1139. switch (regtest) {
  1140. case HWSIM_REGTEST_DISABLED:
  1141. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  1142. case HWSIM_REGTEST_DRIVER_REG_ALL:
  1143. case HWSIM_REGTEST_DIFF_COUNTRY:
  1144. /*
  1145. * Nothing to be done for driver regulatory domain
  1146. * hints prior to ieee80211_register_hw()
  1147. */
  1148. break;
  1149. case HWSIM_REGTEST_WORLD_ROAM:
  1150. if (i == 0) {
  1151. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1152. wiphy_apply_custom_regulatory(hw->wiphy,
  1153. &hwsim_world_regdom_custom_01);
  1154. }
  1155. break;
  1156. case HWSIM_REGTEST_CUSTOM_WORLD:
  1157. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1158. wiphy_apply_custom_regulatory(hw->wiphy,
  1159. &hwsim_world_regdom_custom_01);
  1160. break;
  1161. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  1162. if (i == 0) {
  1163. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1164. wiphy_apply_custom_regulatory(hw->wiphy,
  1165. &hwsim_world_regdom_custom_01);
  1166. } else if (i == 1) {
  1167. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1168. wiphy_apply_custom_regulatory(hw->wiphy,
  1169. &hwsim_world_regdom_custom_02);
  1170. }
  1171. break;
  1172. case HWSIM_REGTEST_STRICT_ALL:
  1173. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1174. break;
  1175. case HWSIM_REGTEST_STRICT_FOLLOW:
  1176. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  1177. if (i == 0)
  1178. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1179. break;
  1180. case HWSIM_REGTEST_ALL:
  1181. if (i == 0) {
  1182. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1183. wiphy_apply_custom_regulatory(hw->wiphy,
  1184. &hwsim_world_regdom_custom_01);
  1185. } else if (i == 1) {
  1186. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1187. wiphy_apply_custom_regulatory(hw->wiphy,
  1188. &hwsim_world_regdom_custom_02);
  1189. } else if (i == 4)
  1190. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1191. break;
  1192. default:
  1193. break;
  1194. }
  1195. /* give the regulatory workqueue a chance to run */
  1196. if (regtest)
  1197. schedule_timeout_interruptible(1);
  1198. err = ieee80211_register_hw(hw);
  1199. if (err < 0) {
  1200. printk(KERN_DEBUG "mac80211_hwsim: "
  1201. "ieee80211_register_hw failed (%d)\n", err);
  1202. goto failed_hw;
  1203. }
  1204. /* Work to be done after to ieee80211_register_hw() */
  1205. switch (regtest) {
  1206. case HWSIM_REGTEST_WORLD_ROAM:
  1207. case HWSIM_REGTEST_DISABLED:
  1208. break;
  1209. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  1210. if (!i)
  1211. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1212. break;
  1213. case HWSIM_REGTEST_DRIVER_REG_ALL:
  1214. case HWSIM_REGTEST_STRICT_ALL:
  1215. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1216. break;
  1217. case HWSIM_REGTEST_DIFF_COUNTRY:
  1218. if (i < ARRAY_SIZE(hwsim_alpha2s))
  1219. regulatory_hint(hw->wiphy, hwsim_alpha2s[i]);
  1220. break;
  1221. case HWSIM_REGTEST_CUSTOM_WORLD:
  1222. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  1223. /*
  1224. * Nothing to be done for custom world regulatory
  1225. * domains after to ieee80211_register_hw
  1226. */
  1227. break;
  1228. case HWSIM_REGTEST_STRICT_FOLLOW:
  1229. if (i == 0)
  1230. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1231. break;
  1232. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  1233. if (i == 0)
  1234. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1235. else if (i == 1)
  1236. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  1237. break;
  1238. case HWSIM_REGTEST_ALL:
  1239. if (i == 2)
  1240. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1241. else if (i == 3)
  1242. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  1243. else if (i == 4)
  1244. regulatory_hint(hw->wiphy, hwsim_alpha2s[2]);
  1245. break;
  1246. default:
  1247. break;
  1248. }
  1249. printk(KERN_DEBUG "%s: hwaddr %pM registered\n",
  1250. wiphy_name(hw->wiphy),
  1251. hw->wiphy->perm_addr);
  1252. data->debugfs = debugfs_create_dir("hwsim",
  1253. hw->wiphy->debugfsdir);
  1254. data->debugfs_ps = debugfs_create_file("ps", 0666,
  1255. data->debugfs, data,
  1256. &hwsim_fops_ps);
  1257. data->debugfs_group = debugfs_create_file("group", 0666,
  1258. data->debugfs, data,
  1259. &hwsim_fops_group);
  1260. setup_timer(&data->beacon_timer, mac80211_hwsim_beacon,
  1261. (unsigned long) hw);
  1262. list_add_tail(&data->list, &hwsim_radios);
  1263. }
  1264. hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
  1265. if (hwsim_mon == NULL)
  1266. goto failed;
  1267. rtnl_lock();
  1268. err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
  1269. if (err < 0)
  1270. goto failed_mon;
  1271. err = register_netdevice(hwsim_mon);
  1272. if (err < 0)
  1273. goto failed_mon;
  1274. rtnl_unlock();
  1275. return 0;
  1276. failed_mon:
  1277. rtnl_unlock();
  1278. free_netdev(hwsim_mon);
  1279. mac80211_hwsim_free();
  1280. return err;
  1281. failed_hw:
  1282. device_unregister(data->dev);
  1283. failed_drvdata:
  1284. ieee80211_free_hw(hw);
  1285. failed:
  1286. mac80211_hwsim_free();
  1287. return err;
  1288. }
  1289. static void __exit exit_mac80211_hwsim(void)
  1290. {
  1291. printk(KERN_DEBUG "mac80211_hwsim: unregister radios\n");
  1292. mac80211_hwsim_free();
  1293. unregister_netdev(hwsim_mon);
  1294. }
  1295. module_init(init_mac80211_hwsim);
  1296. module_exit(exit_mac80211_hwsim);