mac80211_hwsim.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071
  1. /*
  2. * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211
  3. * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
  4. * Copyright (c) 2011, Javier Lopez <jlopex@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. /*
  11. * TODO:
  12. * - Add TSF sync and fix IBSS beacon transmission by adding
  13. * competition for "air time" at TBTT
  14. * - RX filtering based on filter configuration (data->rx_filter)
  15. */
  16. #include <linux/list.h>
  17. #include <linux/slab.h>
  18. #include <linux/spinlock.h>
  19. #include <net/dst.h>
  20. #include <net/xfrm.h>
  21. #include <net/mac80211.h>
  22. #include <net/ieee80211_radiotap.h>
  23. #include <linux/if_arp.h>
  24. #include <linux/rtnetlink.h>
  25. #include <linux/etherdevice.h>
  26. #include <linux/debugfs.h>
  27. #include <linux/module.h>
  28. #include <linux/ktime.h>
  29. #include <net/genetlink.h>
  30. #include "mac80211_hwsim.h"
  31. #define WARN_QUEUE 100
  32. #define MAX_QUEUE 200
  33. MODULE_AUTHOR("Jouni Malinen");
  34. MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
  35. MODULE_LICENSE("GPL");
  36. static u32 wmediumd_pid;
  37. static int radios = 2;
  38. module_param(radios, int, 0444);
  39. MODULE_PARM_DESC(radios, "Number of simulated radios");
  40. static bool fake_hw_scan;
  41. module_param(fake_hw_scan, bool, 0444);
  42. MODULE_PARM_DESC(fake_hw_scan, "Install fake (no-op) hw-scan handler");
  43. /**
  44. * enum hwsim_regtest - the type of regulatory tests we offer
  45. *
  46. * These are the different values you can use for the regtest
  47. * module parameter. This is useful to help test world roaming
  48. * and the driver regulatory_hint() call and combinations of these.
  49. * If you want to do specific alpha2 regulatory domain tests simply
  50. * use the userspace regulatory request as that will be respected as
  51. * well without the need of this module parameter. This is designed
  52. * only for testing the driver regulatory request, world roaming
  53. * and all possible combinations.
  54. *
  55. * @HWSIM_REGTEST_DISABLED: No regulatory tests are performed,
  56. * this is the default value.
  57. * @HWSIM_REGTEST_DRIVER_REG_FOLLOW: Used for testing the driver regulatory
  58. * hint, only one driver regulatory hint will be sent as such the
  59. * secondary radios are expected to follow.
  60. * @HWSIM_REGTEST_DRIVER_REG_ALL: Used for testing the driver regulatory
  61. * request with all radios reporting the same regulatory domain.
  62. * @HWSIM_REGTEST_DIFF_COUNTRY: Used for testing the drivers calling
  63. * different regulatory domains requests. Expected behaviour is for
  64. * an intersection to occur but each device will still use their
  65. * respective regulatory requested domains. Subsequent radios will
  66. * use the resulting intersection.
  67. * @HWSIM_REGTEST_WORLD_ROAM: Used for testing the world roaming. We accomplish
  68. * this by using a custom beacon-capable regulatory domain for the first
  69. * radio. All other device world roam.
  70. * @HWSIM_REGTEST_CUSTOM_WORLD: Used for testing the custom world regulatory
  71. * domain requests. All radios will adhere to this custom world regulatory
  72. * domain.
  73. * @HWSIM_REGTEST_CUSTOM_WORLD_2: Used for testing 2 custom world regulatory
  74. * domain requests. The first radio will adhere to the first custom world
  75. * regulatory domain, the second one to the second custom world regulatory
  76. * domain. All other devices will world roam.
  77. * @HWSIM_REGTEST_STRICT_FOLLOW_: Used for testing strict regulatory domain
  78. * settings, only the first radio will send a regulatory domain request
  79. * and use strict settings. The rest of the radios are expected to follow.
  80. * @HWSIM_REGTEST_STRICT_ALL: Used for testing strict regulatory domain
  81. * settings. All radios will adhere to this.
  82. * @HWSIM_REGTEST_STRICT_AND_DRIVER_REG: Used for testing strict regulatory
  83. * domain settings, combined with secondary driver regulatory domain
  84. * settings. The first radio will get a strict regulatory domain setting
  85. * using the first driver regulatory request and the second radio will use
  86. * non-strict settings using the second driver regulatory request. All
  87. * other devices should follow the intersection created between the
  88. * first two.
  89. * @HWSIM_REGTEST_ALL: Used for testing every possible mix. You will need
  90. * at least 6 radios for a complete test. We will test in this order:
  91. * 1 - driver custom world regulatory domain
  92. * 2 - second custom world regulatory domain
  93. * 3 - first driver regulatory domain request
  94. * 4 - second driver regulatory domain request
  95. * 5 - strict regulatory domain settings using the third driver regulatory
  96. * domain request
  97. * 6 and on - should follow the intersection of the 3rd, 4rth and 5th radio
  98. * regulatory requests.
  99. */
  100. enum hwsim_regtest {
  101. HWSIM_REGTEST_DISABLED = 0,
  102. HWSIM_REGTEST_DRIVER_REG_FOLLOW = 1,
  103. HWSIM_REGTEST_DRIVER_REG_ALL = 2,
  104. HWSIM_REGTEST_DIFF_COUNTRY = 3,
  105. HWSIM_REGTEST_WORLD_ROAM = 4,
  106. HWSIM_REGTEST_CUSTOM_WORLD = 5,
  107. HWSIM_REGTEST_CUSTOM_WORLD_2 = 6,
  108. HWSIM_REGTEST_STRICT_FOLLOW = 7,
  109. HWSIM_REGTEST_STRICT_ALL = 8,
  110. HWSIM_REGTEST_STRICT_AND_DRIVER_REG = 9,
  111. HWSIM_REGTEST_ALL = 10,
  112. };
  113. /* Set to one of the HWSIM_REGTEST_* values above */
  114. static int regtest = HWSIM_REGTEST_DISABLED;
  115. module_param(regtest, int, 0444);
  116. MODULE_PARM_DESC(regtest, "The type of regulatory test we want to run");
  117. static const char *hwsim_alpha2s[] = {
  118. "FI",
  119. "AL",
  120. "US",
  121. "DE",
  122. "JP",
  123. "AL",
  124. };
  125. static const struct ieee80211_regdomain hwsim_world_regdom_custom_01 = {
  126. .n_reg_rules = 4,
  127. .alpha2 = "99",
  128. .reg_rules = {
  129. REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
  130. REG_RULE(2484-10, 2484+10, 40, 0, 20, 0),
  131. REG_RULE(5150-10, 5240+10, 40, 0, 30, 0),
  132. REG_RULE(5745-10, 5825+10, 40, 0, 30, 0),
  133. }
  134. };
  135. static const struct ieee80211_regdomain hwsim_world_regdom_custom_02 = {
  136. .n_reg_rules = 2,
  137. .alpha2 = "99",
  138. .reg_rules = {
  139. REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
  140. REG_RULE(5725-10, 5850+10, 40, 0, 30,
  141. NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_IBSS),
  142. }
  143. };
  144. struct hwsim_vif_priv {
  145. u32 magic;
  146. u8 bssid[ETH_ALEN];
  147. bool assoc;
  148. u16 aid;
  149. };
  150. #define HWSIM_VIF_MAGIC 0x69537748
  151. static inline void hwsim_check_magic(struct ieee80211_vif *vif)
  152. {
  153. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  154. WARN_ON(vp->magic != HWSIM_VIF_MAGIC);
  155. }
  156. static inline void hwsim_set_magic(struct ieee80211_vif *vif)
  157. {
  158. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  159. vp->magic = HWSIM_VIF_MAGIC;
  160. }
  161. static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
  162. {
  163. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  164. vp->magic = 0;
  165. }
  166. struct hwsim_sta_priv {
  167. u32 magic;
  168. };
  169. #define HWSIM_STA_MAGIC 0x6d537748
  170. static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
  171. {
  172. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  173. WARN_ON(sp->magic != HWSIM_STA_MAGIC);
  174. }
  175. static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
  176. {
  177. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  178. sp->magic = HWSIM_STA_MAGIC;
  179. }
  180. static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
  181. {
  182. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  183. sp->magic = 0;
  184. }
  185. static struct class *hwsim_class;
  186. static struct net_device *hwsim_mon; /* global monitor netdev */
  187. #define CHAN2G(_freq) { \
  188. .band = IEEE80211_BAND_2GHZ, \
  189. .center_freq = (_freq), \
  190. .hw_value = (_freq), \
  191. .max_power = 20, \
  192. }
  193. #define CHAN5G(_freq) { \
  194. .band = IEEE80211_BAND_5GHZ, \
  195. .center_freq = (_freq), \
  196. .hw_value = (_freq), \
  197. .max_power = 20, \
  198. }
  199. static const struct ieee80211_channel hwsim_channels_2ghz[] = {
  200. CHAN2G(2412), /* Channel 1 */
  201. CHAN2G(2417), /* Channel 2 */
  202. CHAN2G(2422), /* Channel 3 */
  203. CHAN2G(2427), /* Channel 4 */
  204. CHAN2G(2432), /* Channel 5 */
  205. CHAN2G(2437), /* Channel 6 */
  206. CHAN2G(2442), /* Channel 7 */
  207. CHAN2G(2447), /* Channel 8 */
  208. CHAN2G(2452), /* Channel 9 */
  209. CHAN2G(2457), /* Channel 10 */
  210. CHAN2G(2462), /* Channel 11 */
  211. CHAN2G(2467), /* Channel 12 */
  212. CHAN2G(2472), /* Channel 13 */
  213. CHAN2G(2484), /* Channel 14 */
  214. };
  215. static const struct ieee80211_channel hwsim_channels_5ghz[] = {
  216. CHAN5G(5180), /* Channel 36 */
  217. CHAN5G(5200), /* Channel 40 */
  218. CHAN5G(5220), /* Channel 44 */
  219. CHAN5G(5240), /* Channel 48 */
  220. CHAN5G(5260), /* Channel 52 */
  221. CHAN5G(5280), /* Channel 56 */
  222. CHAN5G(5300), /* Channel 60 */
  223. CHAN5G(5320), /* Channel 64 */
  224. CHAN5G(5500), /* Channel 100 */
  225. CHAN5G(5520), /* Channel 104 */
  226. CHAN5G(5540), /* Channel 108 */
  227. CHAN5G(5560), /* Channel 112 */
  228. CHAN5G(5580), /* Channel 116 */
  229. CHAN5G(5600), /* Channel 120 */
  230. CHAN5G(5620), /* Channel 124 */
  231. CHAN5G(5640), /* Channel 128 */
  232. CHAN5G(5660), /* Channel 132 */
  233. CHAN5G(5680), /* Channel 136 */
  234. CHAN5G(5700), /* Channel 140 */
  235. CHAN5G(5745), /* Channel 149 */
  236. CHAN5G(5765), /* Channel 153 */
  237. CHAN5G(5785), /* Channel 157 */
  238. CHAN5G(5805), /* Channel 161 */
  239. CHAN5G(5825), /* Channel 165 */
  240. };
  241. static const struct ieee80211_rate hwsim_rates[] = {
  242. { .bitrate = 10 },
  243. { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  244. { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  245. { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  246. { .bitrate = 60 },
  247. { .bitrate = 90 },
  248. { .bitrate = 120 },
  249. { .bitrate = 180 },
  250. { .bitrate = 240 },
  251. { .bitrate = 360 },
  252. { .bitrate = 480 },
  253. { .bitrate = 540 }
  254. };
  255. static spinlock_t hwsim_radio_lock;
  256. static struct list_head hwsim_radios;
  257. struct mac80211_hwsim_data {
  258. struct list_head list;
  259. struct ieee80211_hw *hw;
  260. struct device *dev;
  261. struct ieee80211_supported_band bands[2];
  262. struct ieee80211_channel channels_2ghz[ARRAY_SIZE(hwsim_channels_2ghz)];
  263. struct ieee80211_channel channels_5ghz[ARRAY_SIZE(hwsim_channels_5ghz)];
  264. struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
  265. struct mac_address addresses[2];
  266. struct ieee80211_channel *channel;
  267. unsigned long beacon_int; /* in jiffies unit */
  268. unsigned int rx_filter;
  269. bool started, idle, scanning;
  270. struct mutex mutex;
  271. struct timer_list beacon_timer;
  272. enum ps_mode {
  273. PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
  274. } ps;
  275. bool ps_poll_pending;
  276. struct dentry *debugfs;
  277. struct dentry *debugfs_ps;
  278. struct sk_buff_head pending; /* packets pending */
  279. /*
  280. * Only radios in the same group can communicate together (the
  281. * channel has to match too). Each bit represents a group. A
  282. * radio can be in more then one group.
  283. */
  284. u64 group;
  285. struct dentry *debugfs_group;
  286. int power_level;
  287. /* difference between this hw's clock and the real clock, in usecs */
  288. u64 tsf_offset;
  289. };
  290. struct hwsim_radiotap_hdr {
  291. struct ieee80211_radiotap_header hdr;
  292. __le64 rt_tsft;
  293. u8 rt_flags;
  294. u8 rt_rate;
  295. __le16 rt_channel;
  296. __le16 rt_chbitmask;
  297. } __packed;
  298. /* MAC80211_HWSIM netlinf family */
  299. static struct genl_family hwsim_genl_family = {
  300. .id = GENL_ID_GENERATE,
  301. .hdrsize = 0,
  302. .name = "MAC80211_HWSIM",
  303. .version = 1,
  304. .maxattr = HWSIM_ATTR_MAX,
  305. };
  306. /* MAC80211_HWSIM netlink policy */
  307. static struct nla_policy hwsim_genl_policy[HWSIM_ATTR_MAX + 1] = {
  308. [HWSIM_ATTR_ADDR_RECEIVER] = { .type = NLA_UNSPEC,
  309. .len = 6*sizeof(u8) },
  310. [HWSIM_ATTR_ADDR_TRANSMITTER] = { .type = NLA_UNSPEC,
  311. .len = 6*sizeof(u8) },
  312. [HWSIM_ATTR_FRAME] = { .type = NLA_BINARY,
  313. .len = IEEE80211_MAX_DATA_LEN },
  314. [HWSIM_ATTR_FLAGS] = { .type = NLA_U32 },
  315. [HWSIM_ATTR_RX_RATE] = { .type = NLA_U32 },
  316. [HWSIM_ATTR_SIGNAL] = { .type = NLA_U32 },
  317. [HWSIM_ATTR_TX_INFO] = { .type = NLA_UNSPEC,
  318. .len = IEEE80211_TX_MAX_RATES*sizeof(
  319. struct hwsim_tx_rate)},
  320. [HWSIM_ATTR_COOKIE] = { .type = NLA_U64 },
  321. };
  322. static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
  323. struct net_device *dev)
  324. {
  325. /* TODO: allow packet injection */
  326. dev_kfree_skb(skb);
  327. return NETDEV_TX_OK;
  328. }
  329. static __le64 __mac80211_hwsim_get_tsf(struct mac80211_hwsim_data *data)
  330. {
  331. struct timeval tv = ktime_to_timeval(ktime_get_real());
  332. u64 now = tv.tv_sec * USEC_PER_SEC + tv.tv_usec;
  333. return cpu_to_le64(now + data->tsf_offset);
  334. }
  335. static u64 mac80211_hwsim_get_tsf(struct ieee80211_hw *hw,
  336. struct ieee80211_vif *vif)
  337. {
  338. struct mac80211_hwsim_data *data = hw->priv;
  339. return le64_to_cpu(__mac80211_hwsim_get_tsf(data));
  340. }
  341. static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw,
  342. struct ieee80211_vif *vif, u64 tsf)
  343. {
  344. struct mac80211_hwsim_data *data = hw->priv;
  345. struct timeval tv = ktime_to_timeval(ktime_get_real());
  346. u64 now = tv.tv_sec * USEC_PER_SEC + tv.tv_usec;
  347. data->tsf_offset = tsf - now;
  348. }
  349. static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
  350. struct sk_buff *tx_skb)
  351. {
  352. struct mac80211_hwsim_data *data = hw->priv;
  353. struct sk_buff *skb;
  354. struct hwsim_radiotap_hdr *hdr;
  355. u16 flags;
  356. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
  357. struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
  358. if (!netif_running(hwsim_mon))
  359. return;
  360. skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
  361. if (skb == NULL)
  362. return;
  363. hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr));
  364. hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  365. hdr->hdr.it_pad = 0;
  366. hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
  367. hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  368. (1 << IEEE80211_RADIOTAP_RATE) |
  369. (1 << IEEE80211_RADIOTAP_TSFT) |
  370. (1 << IEEE80211_RADIOTAP_CHANNEL));
  371. hdr->rt_tsft = __mac80211_hwsim_get_tsf(data);
  372. hdr->rt_flags = 0;
  373. hdr->rt_rate = txrate->bitrate / 5;
  374. hdr->rt_channel = cpu_to_le16(data->channel->center_freq);
  375. flags = IEEE80211_CHAN_2GHZ;
  376. if (txrate->flags & IEEE80211_RATE_ERP_G)
  377. flags |= IEEE80211_CHAN_OFDM;
  378. else
  379. flags |= IEEE80211_CHAN_CCK;
  380. hdr->rt_chbitmask = cpu_to_le16(flags);
  381. skb->dev = hwsim_mon;
  382. skb_set_mac_header(skb, 0);
  383. skb->ip_summed = CHECKSUM_UNNECESSARY;
  384. skb->pkt_type = PACKET_OTHERHOST;
  385. skb->protocol = htons(ETH_P_802_2);
  386. memset(skb->cb, 0, sizeof(skb->cb));
  387. netif_rx(skb);
  388. }
  389. static void mac80211_hwsim_monitor_ack(struct ieee80211_hw *hw, const u8 *addr)
  390. {
  391. struct mac80211_hwsim_data *data = hw->priv;
  392. struct sk_buff *skb;
  393. struct hwsim_radiotap_hdr *hdr;
  394. u16 flags;
  395. struct ieee80211_hdr *hdr11;
  396. if (!netif_running(hwsim_mon))
  397. return;
  398. skb = dev_alloc_skb(100);
  399. if (skb == NULL)
  400. return;
  401. hdr = (struct hwsim_radiotap_hdr *) skb_put(skb, sizeof(*hdr));
  402. hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  403. hdr->hdr.it_pad = 0;
  404. hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
  405. hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  406. (1 << IEEE80211_RADIOTAP_CHANNEL));
  407. hdr->rt_flags = 0;
  408. hdr->rt_rate = 0;
  409. hdr->rt_channel = cpu_to_le16(data->channel->center_freq);
  410. flags = IEEE80211_CHAN_2GHZ;
  411. hdr->rt_chbitmask = cpu_to_le16(flags);
  412. hdr11 = (struct ieee80211_hdr *) skb_put(skb, 10);
  413. hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  414. IEEE80211_STYPE_ACK);
  415. hdr11->duration_id = cpu_to_le16(0);
  416. memcpy(hdr11->addr1, addr, ETH_ALEN);
  417. skb->dev = hwsim_mon;
  418. skb_set_mac_header(skb, 0);
  419. skb->ip_summed = CHECKSUM_UNNECESSARY;
  420. skb->pkt_type = PACKET_OTHERHOST;
  421. skb->protocol = htons(ETH_P_802_2);
  422. memset(skb->cb, 0, sizeof(skb->cb));
  423. netif_rx(skb);
  424. }
  425. static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
  426. struct sk_buff *skb)
  427. {
  428. switch (data->ps) {
  429. case PS_DISABLED:
  430. return true;
  431. case PS_ENABLED:
  432. return false;
  433. case PS_AUTO_POLL:
  434. /* TODO: accept (some) Beacons by default and other frames only
  435. * if pending PS-Poll has been sent */
  436. return true;
  437. case PS_MANUAL_POLL:
  438. /* Allow unicast frames to own address if there is a pending
  439. * PS-Poll */
  440. if (data->ps_poll_pending &&
  441. memcmp(data->hw->wiphy->perm_addr, skb->data + 4,
  442. ETH_ALEN) == 0) {
  443. data->ps_poll_pending = false;
  444. return true;
  445. }
  446. return false;
  447. }
  448. return true;
  449. }
  450. struct mac80211_hwsim_addr_match_data {
  451. bool ret;
  452. const u8 *addr;
  453. };
  454. static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
  455. struct ieee80211_vif *vif)
  456. {
  457. struct mac80211_hwsim_addr_match_data *md = data;
  458. if (memcmp(mac, md->addr, ETH_ALEN) == 0)
  459. md->ret = true;
  460. }
  461. static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
  462. const u8 *addr)
  463. {
  464. struct mac80211_hwsim_addr_match_data md;
  465. if (memcmp(addr, data->hw->wiphy->perm_addr, ETH_ALEN) == 0)
  466. return true;
  467. md.ret = false;
  468. md.addr = addr;
  469. ieee80211_iterate_active_interfaces_atomic(data->hw,
  470. mac80211_hwsim_addr_iter,
  471. &md);
  472. return md.ret;
  473. }
  474. static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
  475. struct sk_buff *my_skb,
  476. int dst_pid)
  477. {
  478. struct sk_buff *skb;
  479. struct mac80211_hwsim_data *data = hw->priv;
  480. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data;
  481. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb);
  482. void *msg_head;
  483. unsigned int hwsim_flags = 0;
  484. int i;
  485. struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES];
  486. if (data->idle) {
  487. wiphy_debug(hw->wiphy, "Trying to TX when idle - reject\n");
  488. dev_kfree_skb(my_skb);
  489. return;
  490. }
  491. if (data->ps != PS_DISABLED)
  492. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  493. /* If the queue contains MAX_QUEUE skb's drop some */
  494. if (skb_queue_len(&data->pending) >= MAX_QUEUE) {
  495. /* Droping until WARN_QUEUE level */
  496. while (skb_queue_len(&data->pending) >= WARN_QUEUE)
  497. skb_dequeue(&data->pending);
  498. }
  499. skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  500. if (skb == NULL)
  501. goto nla_put_failure;
  502. msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
  503. HWSIM_CMD_FRAME);
  504. if (msg_head == NULL) {
  505. printk(KERN_DEBUG "mac80211_hwsim: problem with msg_head\n");
  506. goto nla_put_failure;
  507. }
  508. if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
  509. sizeof(struct mac_address), data->addresses[1].addr))
  510. goto nla_put_failure;
  511. /* We get the skb->data */
  512. if (nla_put(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data))
  513. goto nla_put_failure;
  514. /* We get the flags for this transmission, and we translate them to
  515. wmediumd flags */
  516. if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
  517. hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS;
  518. if (info->flags & IEEE80211_TX_CTL_NO_ACK)
  519. hwsim_flags |= HWSIM_TX_CTL_NO_ACK;
  520. if (nla_put_u32(skb, HWSIM_ATTR_FLAGS, hwsim_flags))
  521. goto nla_put_failure;
  522. /* We get the tx control (rate and retries) info*/
  523. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  524. tx_attempts[i].idx = info->status.rates[i].idx;
  525. tx_attempts[i].count = info->status.rates[i].count;
  526. }
  527. if (nla_put(skb, HWSIM_ATTR_TX_INFO,
  528. sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES,
  529. tx_attempts))
  530. goto nla_put_failure;
  531. /* We create a cookie to identify this skb */
  532. if (nla_put_u64(skb, HWSIM_ATTR_COOKIE, (unsigned long) my_skb))
  533. goto nla_put_failure;
  534. genlmsg_end(skb, msg_head);
  535. genlmsg_unicast(&init_net, skb, dst_pid);
  536. /* Enqueue the packet */
  537. skb_queue_tail(&data->pending, my_skb);
  538. return;
  539. nla_put_failure:
  540. printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
  541. }
  542. static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw,
  543. struct sk_buff *skb)
  544. {
  545. struct mac80211_hwsim_data *data = hw->priv, *data2;
  546. bool ack = false;
  547. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  548. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  549. struct ieee80211_rx_status rx_status;
  550. struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
  551. if (data->idle) {
  552. wiphy_debug(hw->wiphy, "Trying to TX when idle - reject\n");
  553. return false;
  554. }
  555. memset(&rx_status, 0, sizeof(rx_status));
  556. rx_status.flag |= RX_FLAG_MACTIME_MPDU;
  557. rx_status.freq = data->channel->center_freq;
  558. rx_status.band = data->channel->band;
  559. rx_status.rate_idx = info->control.rates[0].idx;
  560. if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
  561. rx_status.flag |= RX_FLAG_HT;
  562. if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  563. rx_status.flag |= RX_FLAG_40MHZ;
  564. if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
  565. rx_status.flag |= RX_FLAG_SHORT_GI;
  566. /* TODO: simulate real signal strength (and optional packet loss) */
  567. rx_status.signal = data->power_level - 50;
  568. if (data->ps != PS_DISABLED)
  569. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  570. /* release the skb's source info */
  571. skb_orphan(skb);
  572. skb_dst_drop(skb);
  573. skb->mark = 0;
  574. secpath_reset(skb);
  575. nf_reset(skb);
  576. /* Copy skb to all enabled radios that are on the current frequency */
  577. spin_lock(&hwsim_radio_lock);
  578. list_for_each_entry(data2, &hwsim_radios, list) {
  579. struct sk_buff *nskb;
  580. struct ieee80211_mgmt *mgmt;
  581. if (data == data2)
  582. continue;
  583. if (data2->idle || !data2->started ||
  584. !hwsim_ps_rx_ok(data2, skb) || !data2->channel ||
  585. data->channel->center_freq != data2->channel->center_freq ||
  586. !(data->group & data2->group))
  587. continue;
  588. nskb = skb_copy(skb, GFP_ATOMIC);
  589. if (nskb == NULL)
  590. continue;
  591. if (mac80211_hwsim_addr_match(data2, hdr->addr1))
  592. ack = true;
  593. /* set bcn timestamp relative to receiver mactime */
  594. rx_status.mactime =
  595. le64_to_cpu(__mac80211_hwsim_get_tsf(data2));
  596. mgmt = (struct ieee80211_mgmt *) nskb->data;
  597. if (ieee80211_is_beacon(mgmt->frame_control) ||
  598. ieee80211_is_probe_resp(mgmt->frame_control))
  599. mgmt->u.beacon.timestamp = cpu_to_le64(
  600. rx_status.mactime +
  601. (data->tsf_offset - data2->tsf_offset) +
  602. 24 * 8 * 10 / txrate->bitrate);
  603. memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
  604. ieee80211_rx_irqsafe(data2->hw, nskb);
  605. }
  606. spin_unlock(&hwsim_radio_lock);
  607. return ack;
  608. }
  609. static void mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  610. {
  611. bool ack;
  612. struct ieee80211_tx_info *txi;
  613. u32 _pid;
  614. mac80211_hwsim_monitor_rx(hw, skb);
  615. if (skb->len < 10) {
  616. /* Should not happen; just a sanity check for addr1 use */
  617. dev_kfree_skb(skb);
  618. return;
  619. }
  620. /* wmediumd mode check */
  621. _pid = ACCESS_ONCE(wmediumd_pid);
  622. if (_pid)
  623. return mac80211_hwsim_tx_frame_nl(hw, skb, _pid);
  624. /* NO wmediumd detected, perfect medium simulation */
  625. ack = mac80211_hwsim_tx_frame_no_nl(hw, skb);
  626. if (ack && skb->len >= 16) {
  627. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  628. mac80211_hwsim_monitor_ack(hw, hdr->addr2);
  629. }
  630. txi = IEEE80211_SKB_CB(skb);
  631. if (txi->control.vif)
  632. hwsim_check_magic(txi->control.vif);
  633. if (txi->control.sta)
  634. hwsim_check_sta_magic(txi->control.sta);
  635. ieee80211_tx_info_clear_status(txi);
  636. /* frame was transmitted at most favorable rate at first attempt */
  637. txi->control.rates[0].count = 1;
  638. txi->control.rates[1].idx = -1;
  639. if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
  640. txi->flags |= IEEE80211_TX_STAT_ACK;
  641. ieee80211_tx_status_irqsafe(hw, skb);
  642. }
  643. static int mac80211_hwsim_start(struct ieee80211_hw *hw)
  644. {
  645. struct mac80211_hwsim_data *data = hw->priv;
  646. wiphy_debug(hw->wiphy, "%s\n", __func__);
  647. data->started = true;
  648. return 0;
  649. }
  650. static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
  651. {
  652. struct mac80211_hwsim_data *data = hw->priv;
  653. data->started = false;
  654. del_timer(&data->beacon_timer);
  655. wiphy_debug(hw->wiphy, "%s\n", __func__);
  656. }
  657. static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
  658. struct ieee80211_vif *vif)
  659. {
  660. wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
  661. __func__, ieee80211_vif_type_p2p(vif),
  662. vif->addr);
  663. hwsim_set_magic(vif);
  664. return 0;
  665. }
  666. static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
  667. struct ieee80211_vif *vif,
  668. enum nl80211_iftype newtype,
  669. bool newp2p)
  670. {
  671. newtype = ieee80211_iftype_p2p(newtype, newp2p);
  672. wiphy_debug(hw->wiphy,
  673. "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
  674. __func__, ieee80211_vif_type_p2p(vif),
  675. newtype, vif->addr);
  676. hwsim_check_magic(vif);
  677. return 0;
  678. }
  679. static void mac80211_hwsim_remove_interface(
  680. struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  681. {
  682. wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
  683. __func__, ieee80211_vif_type_p2p(vif),
  684. vif->addr);
  685. hwsim_check_magic(vif);
  686. hwsim_clear_magic(vif);
  687. }
  688. static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
  689. struct ieee80211_vif *vif)
  690. {
  691. struct ieee80211_hw *hw = arg;
  692. struct sk_buff *skb;
  693. struct ieee80211_tx_info *info;
  694. u32 _pid;
  695. hwsim_check_magic(vif);
  696. if (vif->type != NL80211_IFTYPE_AP &&
  697. vif->type != NL80211_IFTYPE_MESH_POINT &&
  698. vif->type != NL80211_IFTYPE_ADHOC)
  699. return;
  700. skb = ieee80211_beacon_get(hw, vif);
  701. if (skb == NULL)
  702. return;
  703. info = IEEE80211_SKB_CB(skb);
  704. mac80211_hwsim_monitor_rx(hw, skb);
  705. /* wmediumd mode check */
  706. _pid = ACCESS_ONCE(wmediumd_pid);
  707. if (_pid)
  708. return mac80211_hwsim_tx_frame_nl(hw, skb, _pid);
  709. mac80211_hwsim_tx_frame_no_nl(hw, skb);
  710. dev_kfree_skb(skb);
  711. }
  712. static void mac80211_hwsim_beacon(unsigned long arg)
  713. {
  714. struct ieee80211_hw *hw = (struct ieee80211_hw *) arg;
  715. struct mac80211_hwsim_data *data = hw->priv;
  716. if (!data->started)
  717. return;
  718. ieee80211_iterate_active_interfaces_atomic(
  719. hw, mac80211_hwsim_beacon_tx, hw);
  720. data->beacon_timer.expires = jiffies + data->beacon_int;
  721. add_timer(&data->beacon_timer);
  722. }
  723. static const char *hwsim_chantypes[] = {
  724. [NL80211_CHAN_NO_HT] = "noht",
  725. [NL80211_CHAN_HT20] = "ht20",
  726. [NL80211_CHAN_HT40MINUS] = "ht40-",
  727. [NL80211_CHAN_HT40PLUS] = "ht40+",
  728. };
  729. static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
  730. {
  731. struct mac80211_hwsim_data *data = hw->priv;
  732. struct ieee80211_conf *conf = &hw->conf;
  733. static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
  734. [IEEE80211_SMPS_AUTOMATIC] = "auto",
  735. [IEEE80211_SMPS_OFF] = "off",
  736. [IEEE80211_SMPS_STATIC] = "static",
  737. [IEEE80211_SMPS_DYNAMIC] = "dynamic",
  738. };
  739. wiphy_debug(hw->wiphy,
  740. "%s (freq=%d/%s idle=%d ps=%d smps=%s)\n",
  741. __func__,
  742. conf->channel->center_freq,
  743. hwsim_chantypes[conf->channel_type],
  744. !!(conf->flags & IEEE80211_CONF_IDLE),
  745. !!(conf->flags & IEEE80211_CONF_PS),
  746. smps_modes[conf->smps_mode]);
  747. data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
  748. data->channel = conf->channel;
  749. data->power_level = conf->power_level;
  750. if (!data->started || !data->beacon_int)
  751. del_timer(&data->beacon_timer);
  752. else
  753. mod_timer(&data->beacon_timer, jiffies + data->beacon_int);
  754. return 0;
  755. }
  756. static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
  757. unsigned int changed_flags,
  758. unsigned int *total_flags,u64 multicast)
  759. {
  760. struct mac80211_hwsim_data *data = hw->priv;
  761. wiphy_debug(hw->wiphy, "%s\n", __func__);
  762. data->rx_filter = 0;
  763. if (*total_flags & FIF_PROMISC_IN_BSS)
  764. data->rx_filter |= FIF_PROMISC_IN_BSS;
  765. if (*total_flags & FIF_ALLMULTI)
  766. data->rx_filter |= FIF_ALLMULTI;
  767. *total_flags = data->rx_filter;
  768. }
  769. static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
  770. struct ieee80211_vif *vif,
  771. struct ieee80211_bss_conf *info,
  772. u32 changed)
  773. {
  774. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  775. struct mac80211_hwsim_data *data = hw->priv;
  776. hwsim_check_magic(vif);
  777. wiphy_debug(hw->wiphy, "%s(changed=0x%x)\n", __func__, changed);
  778. if (changed & BSS_CHANGED_BSSID) {
  779. wiphy_debug(hw->wiphy, "%s: BSSID changed: %pM\n",
  780. __func__, info->bssid);
  781. memcpy(vp->bssid, info->bssid, ETH_ALEN);
  782. }
  783. if (changed & BSS_CHANGED_ASSOC) {
  784. wiphy_debug(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
  785. info->assoc, info->aid);
  786. vp->assoc = info->assoc;
  787. vp->aid = info->aid;
  788. }
  789. if (changed & BSS_CHANGED_BEACON_INT) {
  790. wiphy_debug(hw->wiphy, " BCNINT: %d\n", info->beacon_int);
  791. data->beacon_int = 1024 * info->beacon_int / 1000 * HZ / 1000;
  792. if (WARN_ON(!data->beacon_int))
  793. data->beacon_int = 1;
  794. if (data->started)
  795. mod_timer(&data->beacon_timer,
  796. jiffies + data->beacon_int);
  797. }
  798. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  799. wiphy_debug(hw->wiphy, " ERP_CTS_PROT: %d\n",
  800. info->use_cts_prot);
  801. }
  802. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  803. wiphy_debug(hw->wiphy, " ERP_PREAMBLE: %d\n",
  804. info->use_short_preamble);
  805. }
  806. if (changed & BSS_CHANGED_ERP_SLOT) {
  807. wiphy_debug(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
  808. }
  809. if (changed & BSS_CHANGED_HT) {
  810. wiphy_debug(hw->wiphy, " HT: op_mode=0x%x, chantype=%s\n",
  811. info->ht_operation_mode,
  812. hwsim_chantypes[info->channel_type]);
  813. }
  814. if (changed & BSS_CHANGED_BASIC_RATES) {
  815. wiphy_debug(hw->wiphy, " BASIC_RATES: 0x%llx\n",
  816. (unsigned long long) info->basic_rates);
  817. }
  818. }
  819. static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
  820. struct ieee80211_vif *vif,
  821. struct ieee80211_sta *sta)
  822. {
  823. hwsim_check_magic(vif);
  824. hwsim_set_sta_magic(sta);
  825. return 0;
  826. }
  827. static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
  828. struct ieee80211_vif *vif,
  829. struct ieee80211_sta *sta)
  830. {
  831. hwsim_check_magic(vif);
  832. hwsim_clear_sta_magic(sta);
  833. return 0;
  834. }
  835. static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
  836. struct ieee80211_vif *vif,
  837. enum sta_notify_cmd cmd,
  838. struct ieee80211_sta *sta)
  839. {
  840. hwsim_check_magic(vif);
  841. switch (cmd) {
  842. case STA_NOTIFY_SLEEP:
  843. case STA_NOTIFY_AWAKE:
  844. /* TODO: make good use of these flags */
  845. break;
  846. default:
  847. WARN(1, "Invalid sta notify: %d\n", cmd);
  848. break;
  849. }
  850. }
  851. static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
  852. struct ieee80211_sta *sta,
  853. bool set)
  854. {
  855. hwsim_check_sta_magic(sta);
  856. return 0;
  857. }
  858. static int mac80211_hwsim_conf_tx(
  859. struct ieee80211_hw *hw,
  860. struct ieee80211_vif *vif, u16 queue,
  861. const struct ieee80211_tx_queue_params *params)
  862. {
  863. wiphy_debug(hw->wiphy,
  864. "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
  865. __func__, queue,
  866. params->txop, params->cw_min,
  867. params->cw_max, params->aifs);
  868. return 0;
  869. }
  870. static int mac80211_hwsim_get_survey(
  871. struct ieee80211_hw *hw, int idx,
  872. struct survey_info *survey)
  873. {
  874. struct ieee80211_conf *conf = &hw->conf;
  875. wiphy_debug(hw->wiphy, "%s (idx=%d)\n", __func__, idx);
  876. if (idx != 0)
  877. return -ENOENT;
  878. /* Current channel */
  879. survey->channel = conf->channel;
  880. /*
  881. * Magically conjured noise level --- this is only ok for simulated hardware.
  882. *
  883. * A real driver which cannot determine the real channel noise MUST NOT
  884. * report any noise, especially not a magically conjured one :-)
  885. */
  886. survey->filled = SURVEY_INFO_NOISE_DBM;
  887. survey->noise = -92;
  888. return 0;
  889. }
  890. #ifdef CONFIG_NL80211_TESTMODE
  891. /*
  892. * This section contains example code for using netlink
  893. * attributes with the testmode command in nl80211.
  894. */
  895. /* These enums need to be kept in sync with userspace */
  896. enum hwsim_testmode_attr {
  897. __HWSIM_TM_ATTR_INVALID = 0,
  898. HWSIM_TM_ATTR_CMD = 1,
  899. HWSIM_TM_ATTR_PS = 2,
  900. /* keep last */
  901. __HWSIM_TM_ATTR_AFTER_LAST,
  902. HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
  903. };
  904. enum hwsim_testmode_cmd {
  905. HWSIM_TM_CMD_SET_PS = 0,
  906. HWSIM_TM_CMD_GET_PS = 1,
  907. };
  908. static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
  909. [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
  910. [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
  911. };
  912. static int hwsim_fops_ps_write(void *dat, u64 val);
  913. static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
  914. void *data, int len)
  915. {
  916. struct mac80211_hwsim_data *hwsim = hw->priv;
  917. struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
  918. struct sk_buff *skb;
  919. int err, ps;
  920. err = nla_parse(tb, HWSIM_TM_ATTR_MAX, data, len,
  921. hwsim_testmode_policy);
  922. if (err)
  923. return err;
  924. if (!tb[HWSIM_TM_ATTR_CMD])
  925. return -EINVAL;
  926. switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
  927. case HWSIM_TM_CMD_SET_PS:
  928. if (!tb[HWSIM_TM_ATTR_PS])
  929. return -EINVAL;
  930. ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
  931. return hwsim_fops_ps_write(hwsim, ps);
  932. case HWSIM_TM_CMD_GET_PS:
  933. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
  934. nla_total_size(sizeof(u32)));
  935. if (!skb)
  936. return -ENOMEM;
  937. if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
  938. goto nla_put_failure;
  939. return cfg80211_testmode_reply(skb);
  940. default:
  941. return -EOPNOTSUPP;
  942. }
  943. nla_put_failure:
  944. kfree_skb(skb);
  945. return -ENOBUFS;
  946. }
  947. #endif
  948. static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
  949. struct ieee80211_vif *vif,
  950. enum ieee80211_ampdu_mlme_action action,
  951. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  952. u8 buf_size)
  953. {
  954. switch (action) {
  955. case IEEE80211_AMPDU_TX_START:
  956. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  957. break;
  958. case IEEE80211_AMPDU_TX_STOP:
  959. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  960. break;
  961. case IEEE80211_AMPDU_TX_OPERATIONAL:
  962. break;
  963. case IEEE80211_AMPDU_RX_START:
  964. case IEEE80211_AMPDU_RX_STOP:
  965. break;
  966. default:
  967. return -EOPNOTSUPP;
  968. }
  969. return 0;
  970. }
  971. static void mac80211_hwsim_flush(struct ieee80211_hw *hw, bool drop)
  972. {
  973. /* Not implemented, queues only on kernel side */
  974. }
  975. struct hw_scan_done {
  976. struct delayed_work w;
  977. struct ieee80211_hw *hw;
  978. };
  979. static void hw_scan_done(struct work_struct *work)
  980. {
  981. struct hw_scan_done *hsd =
  982. container_of(work, struct hw_scan_done, w.work);
  983. ieee80211_scan_completed(hsd->hw, false);
  984. kfree(hsd);
  985. }
  986. static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
  987. struct ieee80211_vif *vif,
  988. struct cfg80211_scan_request *req)
  989. {
  990. struct hw_scan_done *hsd = kzalloc(sizeof(*hsd), GFP_KERNEL);
  991. int i;
  992. if (!hsd)
  993. return -ENOMEM;
  994. hsd->hw = hw;
  995. INIT_DELAYED_WORK(&hsd->w, hw_scan_done);
  996. printk(KERN_DEBUG "hwsim hw_scan request\n");
  997. for (i = 0; i < req->n_channels; i++)
  998. printk(KERN_DEBUG "hwsim hw_scan freq %d\n",
  999. req->channels[i]->center_freq);
  1000. print_hex_dump(KERN_DEBUG, "scan IEs: ", DUMP_PREFIX_OFFSET,
  1001. 16, 1, req->ie, req->ie_len, 1);
  1002. ieee80211_queue_delayed_work(hw, &hsd->w, 2 * HZ);
  1003. return 0;
  1004. }
  1005. static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw)
  1006. {
  1007. struct mac80211_hwsim_data *hwsim = hw->priv;
  1008. mutex_lock(&hwsim->mutex);
  1009. if (hwsim->scanning) {
  1010. printk(KERN_DEBUG "two hwsim sw_scans detected!\n");
  1011. goto out;
  1012. }
  1013. printk(KERN_DEBUG "hwsim sw_scan request, prepping stuff\n");
  1014. hwsim->scanning = true;
  1015. out:
  1016. mutex_unlock(&hwsim->mutex);
  1017. }
  1018. static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw)
  1019. {
  1020. struct mac80211_hwsim_data *hwsim = hw->priv;
  1021. mutex_lock(&hwsim->mutex);
  1022. printk(KERN_DEBUG "hwsim sw_scan_complete\n");
  1023. hwsim->scanning = false;
  1024. mutex_unlock(&hwsim->mutex);
  1025. }
  1026. static struct ieee80211_ops mac80211_hwsim_ops =
  1027. {
  1028. .tx = mac80211_hwsim_tx,
  1029. .start = mac80211_hwsim_start,
  1030. .stop = mac80211_hwsim_stop,
  1031. .add_interface = mac80211_hwsim_add_interface,
  1032. .change_interface = mac80211_hwsim_change_interface,
  1033. .remove_interface = mac80211_hwsim_remove_interface,
  1034. .config = mac80211_hwsim_config,
  1035. .configure_filter = mac80211_hwsim_configure_filter,
  1036. .bss_info_changed = mac80211_hwsim_bss_info_changed,
  1037. .sta_add = mac80211_hwsim_sta_add,
  1038. .sta_remove = mac80211_hwsim_sta_remove,
  1039. .sta_notify = mac80211_hwsim_sta_notify,
  1040. .set_tim = mac80211_hwsim_set_tim,
  1041. .conf_tx = mac80211_hwsim_conf_tx,
  1042. .get_survey = mac80211_hwsim_get_survey,
  1043. CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd)
  1044. .ampdu_action = mac80211_hwsim_ampdu_action,
  1045. .sw_scan_start = mac80211_hwsim_sw_scan,
  1046. .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
  1047. .flush = mac80211_hwsim_flush,
  1048. .get_tsf = mac80211_hwsim_get_tsf,
  1049. .set_tsf = mac80211_hwsim_set_tsf,
  1050. };
  1051. static void mac80211_hwsim_free(void)
  1052. {
  1053. struct list_head tmplist, *i, *tmp;
  1054. struct mac80211_hwsim_data *data, *tmpdata;
  1055. INIT_LIST_HEAD(&tmplist);
  1056. spin_lock_bh(&hwsim_radio_lock);
  1057. list_for_each_safe(i, tmp, &hwsim_radios)
  1058. list_move(i, &tmplist);
  1059. spin_unlock_bh(&hwsim_radio_lock);
  1060. list_for_each_entry_safe(data, tmpdata, &tmplist, list) {
  1061. debugfs_remove(data->debugfs_group);
  1062. debugfs_remove(data->debugfs_ps);
  1063. debugfs_remove(data->debugfs);
  1064. ieee80211_unregister_hw(data->hw);
  1065. device_unregister(data->dev);
  1066. ieee80211_free_hw(data->hw);
  1067. }
  1068. class_destroy(hwsim_class);
  1069. }
  1070. static struct device_driver mac80211_hwsim_driver = {
  1071. .name = "mac80211_hwsim"
  1072. };
  1073. static const struct net_device_ops hwsim_netdev_ops = {
  1074. .ndo_start_xmit = hwsim_mon_xmit,
  1075. .ndo_change_mtu = eth_change_mtu,
  1076. .ndo_set_mac_address = eth_mac_addr,
  1077. .ndo_validate_addr = eth_validate_addr,
  1078. };
  1079. static void hwsim_mon_setup(struct net_device *dev)
  1080. {
  1081. dev->netdev_ops = &hwsim_netdev_ops;
  1082. dev->destructor = free_netdev;
  1083. ether_setup(dev);
  1084. dev->tx_queue_len = 0;
  1085. dev->type = ARPHRD_IEEE80211_RADIOTAP;
  1086. memset(dev->dev_addr, 0, ETH_ALEN);
  1087. dev->dev_addr[0] = 0x12;
  1088. }
  1089. static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
  1090. {
  1091. struct mac80211_hwsim_data *data = dat;
  1092. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  1093. struct sk_buff *skb;
  1094. struct ieee80211_pspoll *pspoll;
  1095. u32 _pid;
  1096. if (!vp->assoc)
  1097. return;
  1098. wiphy_debug(data->hw->wiphy,
  1099. "%s: send PS-Poll to %pM for aid %d\n",
  1100. __func__, vp->bssid, vp->aid);
  1101. skb = dev_alloc_skb(sizeof(*pspoll));
  1102. if (!skb)
  1103. return;
  1104. pspoll = (void *) skb_put(skb, sizeof(*pspoll));
  1105. pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  1106. IEEE80211_STYPE_PSPOLL |
  1107. IEEE80211_FCTL_PM);
  1108. pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
  1109. memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
  1110. memcpy(pspoll->ta, mac, ETH_ALEN);
  1111. /* wmediumd mode check */
  1112. _pid = ACCESS_ONCE(wmediumd_pid);
  1113. if (_pid)
  1114. return mac80211_hwsim_tx_frame_nl(data->hw, skb, _pid);
  1115. if (!mac80211_hwsim_tx_frame_no_nl(data->hw, skb))
  1116. printk(KERN_DEBUG "%s: PS-poll frame not ack'ed\n", __func__);
  1117. dev_kfree_skb(skb);
  1118. }
  1119. static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
  1120. struct ieee80211_vif *vif, int ps)
  1121. {
  1122. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  1123. struct sk_buff *skb;
  1124. struct ieee80211_hdr *hdr;
  1125. u32 _pid;
  1126. if (!vp->assoc)
  1127. return;
  1128. wiphy_debug(data->hw->wiphy,
  1129. "%s: send data::nullfunc to %pM ps=%d\n",
  1130. __func__, vp->bssid, ps);
  1131. skb = dev_alloc_skb(sizeof(*hdr));
  1132. if (!skb)
  1133. return;
  1134. hdr = (void *) skb_put(skb, sizeof(*hdr) - ETH_ALEN);
  1135. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  1136. IEEE80211_STYPE_NULLFUNC |
  1137. (ps ? IEEE80211_FCTL_PM : 0));
  1138. hdr->duration_id = cpu_to_le16(0);
  1139. memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
  1140. memcpy(hdr->addr2, mac, ETH_ALEN);
  1141. memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
  1142. /* wmediumd mode check */
  1143. _pid = ACCESS_ONCE(wmediumd_pid);
  1144. if (_pid)
  1145. return mac80211_hwsim_tx_frame_nl(data->hw, skb, _pid);
  1146. if (!mac80211_hwsim_tx_frame_no_nl(data->hw, skb))
  1147. printk(KERN_DEBUG "%s: nullfunc frame not ack'ed\n", __func__);
  1148. dev_kfree_skb(skb);
  1149. }
  1150. static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
  1151. struct ieee80211_vif *vif)
  1152. {
  1153. struct mac80211_hwsim_data *data = dat;
  1154. hwsim_send_nullfunc(data, mac, vif, 1);
  1155. }
  1156. static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
  1157. struct ieee80211_vif *vif)
  1158. {
  1159. struct mac80211_hwsim_data *data = dat;
  1160. hwsim_send_nullfunc(data, mac, vif, 0);
  1161. }
  1162. static int hwsim_fops_ps_read(void *dat, u64 *val)
  1163. {
  1164. struct mac80211_hwsim_data *data = dat;
  1165. *val = data->ps;
  1166. return 0;
  1167. }
  1168. static int hwsim_fops_ps_write(void *dat, u64 val)
  1169. {
  1170. struct mac80211_hwsim_data *data = dat;
  1171. enum ps_mode old_ps;
  1172. if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
  1173. val != PS_MANUAL_POLL)
  1174. return -EINVAL;
  1175. old_ps = data->ps;
  1176. data->ps = val;
  1177. if (val == PS_MANUAL_POLL) {
  1178. ieee80211_iterate_active_interfaces(data->hw,
  1179. hwsim_send_ps_poll, data);
  1180. data->ps_poll_pending = true;
  1181. } else if (old_ps == PS_DISABLED && val != PS_DISABLED) {
  1182. ieee80211_iterate_active_interfaces(data->hw,
  1183. hwsim_send_nullfunc_ps,
  1184. data);
  1185. } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
  1186. ieee80211_iterate_active_interfaces(data->hw,
  1187. hwsim_send_nullfunc_no_ps,
  1188. data);
  1189. }
  1190. return 0;
  1191. }
  1192. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
  1193. "%llu\n");
  1194. static int hwsim_fops_group_read(void *dat, u64 *val)
  1195. {
  1196. struct mac80211_hwsim_data *data = dat;
  1197. *val = data->group;
  1198. return 0;
  1199. }
  1200. static int hwsim_fops_group_write(void *dat, u64 val)
  1201. {
  1202. struct mac80211_hwsim_data *data = dat;
  1203. data->group = val;
  1204. return 0;
  1205. }
  1206. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
  1207. hwsim_fops_group_read, hwsim_fops_group_write,
  1208. "%llx\n");
  1209. static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(
  1210. struct mac_address *addr)
  1211. {
  1212. struct mac80211_hwsim_data *data;
  1213. bool _found = false;
  1214. spin_lock_bh(&hwsim_radio_lock);
  1215. list_for_each_entry(data, &hwsim_radios, list) {
  1216. if (memcmp(data->addresses[1].addr, addr,
  1217. sizeof(struct mac_address)) == 0) {
  1218. _found = true;
  1219. break;
  1220. }
  1221. }
  1222. spin_unlock_bh(&hwsim_radio_lock);
  1223. if (!_found)
  1224. return NULL;
  1225. return data;
  1226. }
  1227. static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
  1228. struct genl_info *info)
  1229. {
  1230. struct ieee80211_hdr *hdr;
  1231. struct mac80211_hwsim_data *data2;
  1232. struct ieee80211_tx_info *txi;
  1233. struct hwsim_tx_rate *tx_attempts;
  1234. unsigned long ret_skb_ptr;
  1235. struct sk_buff *skb, *tmp;
  1236. struct mac_address *src;
  1237. unsigned int hwsim_flags;
  1238. int i;
  1239. bool found = false;
  1240. if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
  1241. !info->attrs[HWSIM_ATTR_FLAGS] ||
  1242. !info->attrs[HWSIM_ATTR_COOKIE] ||
  1243. !info->attrs[HWSIM_ATTR_TX_INFO])
  1244. goto out;
  1245. src = (struct mac_address *)nla_data(
  1246. info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
  1247. hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
  1248. ret_skb_ptr = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
  1249. data2 = get_hwsim_data_ref_from_addr(src);
  1250. if (data2 == NULL)
  1251. goto out;
  1252. /* look for the skb matching the cookie passed back from user */
  1253. skb_queue_walk_safe(&data2->pending, skb, tmp) {
  1254. if ((unsigned long)skb == ret_skb_ptr) {
  1255. skb_unlink(skb, &data2->pending);
  1256. found = true;
  1257. break;
  1258. }
  1259. }
  1260. /* not found */
  1261. if (!found)
  1262. goto out;
  1263. /* Tx info received because the frame was broadcasted on user space,
  1264. so we get all the necessary info: tx attempts and skb control buff */
  1265. tx_attempts = (struct hwsim_tx_rate *)nla_data(
  1266. info->attrs[HWSIM_ATTR_TX_INFO]);
  1267. /* now send back TX status */
  1268. txi = IEEE80211_SKB_CB(skb);
  1269. if (txi->control.vif)
  1270. hwsim_check_magic(txi->control.vif);
  1271. if (txi->control.sta)
  1272. hwsim_check_sta_magic(txi->control.sta);
  1273. ieee80211_tx_info_clear_status(txi);
  1274. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  1275. txi->status.rates[i].idx = tx_attempts[i].idx;
  1276. txi->status.rates[i].count = tx_attempts[i].count;
  1277. /*txi->status.rates[i].flags = 0;*/
  1278. }
  1279. txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
  1280. if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
  1281. (hwsim_flags & HWSIM_TX_STAT_ACK)) {
  1282. if (skb->len >= 16) {
  1283. hdr = (struct ieee80211_hdr *) skb->data;
  1284. mac80211_hwsim_monitor_ack(data2->hw, hdr->addr2);
  1285. }
  1286. txi->flags |= IEEE80211_TX_STAT_ACK;
  1287. }
  1288. ieee80211_tx_status_irqsafe(data2->hw, skb);
  1289. return 0;
  1290. out:
  1291. return -EINVAL;
  1292. }
  1293. static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
  1294. struct genl_info *info)
  1295. {
  1296. struct mac80211_hwsim_data *data2;
  1297. struct ieee80211_rx_status rx_status;
  1298. struct mac_address *dst;
  1299. int frame_data_len;
  1300. char *frame_data;
  1301. struct sk_buff *skb = NULL;
  1302. if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
  1303. !info->attrs[HWSIM_ATTR_FRAME] ||
  1304. !info->attrs[HWSIM_ATTR_RX_RATE] ||
  1305. !info->attrs[HWSIM_ATTR_SIGNAL])
  1306. goto out;
  1307. dst = (struct mac_address *)nla_data(
  1308. info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
  1309. frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
  1310. frame_data = (char *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
  1311. /* Allocate new skb here */
  1312. skb = alloc_skb(frame_data_len, GFP_KERNEL);
  1313. if (skb == NULL)
  1314. goto err;
  1315. if (frame_data_len <= IEEE80211_MAX_DATA_LEN) {
  1316. /* Copy the data */
  1317. memcpy(skb_put(skb, frame_data_len), frame_data,
  1318. frame_data_len);
  1319. } else
  1320. goto err;
  1321. data2 = get_hwsim_data_ref_from_addr(dst);
  1322. if (data2 == NULL)
  1323. goto out;
  1324. /* check if radio is configured properly */
  1325. if (data2->idle || !data2->started || !data2->channel)
  1326. goto out;
  1327. /*A frame is received from user space*/
  1328. memset(&rx_status, 0, sizeof(rx_status));
  1329. rx_status.freq = data2->channel->center_freq;
  1330. rx_status.band = data2->channel->band;
  1331. rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
  1332. rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
  1333. memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
  1334. ieee80211_rx_irqsafe(data2->hw, skb);
  1335. return 0;
  1336. err:
  1337. printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
  1338. goto out;
  1339. out:
  1340. dev_kfree_skb(skb);
  1341. return -EINVAL;
  1342. }
  1343. static int hwsim_register_received_nl(struct sk_buff *skb_2,
  1344. struct genl_info *info)
  1345. {
  1346. if (info == NULL)
  1347. goto out;
  1348. wmediumd_pid = info->snd_pid;
  1349. printk(KERN_DEBUG "mac80211_hwsim: received a REGISTER, "
  1350. "switching to wmediumd mode with pid %d\n", info->snd_pid);
  1351. return 0;
  1352. out:
  1353. printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
  1354. return -EINVAL;
  1355. }
  1356. /* Generic Netlink operations array */
  1357. static struct genl_ops hwsim_ops[] = {
  1358. {
  1359. .cmd = HWSIM_CMD_REGISTER,
  1360. .policy = hwsim_genl_policy,
  1361. .doit = hwsim_register_received_nl,
  1362. .flags = GENL_ADMIN_PERM,
  1363. },
  1364. {
  1365. .cmd = HWSIM_CMD_FRAME,
  1366. .policy = hwsim_genl_policy,
  1367. .doit = hwsim_cloned_frame_received_nl,
  1368. },
  1369. {
  1370. .cmd = HWSIM_CMD_TX_INFO_FRAME,
  1371. .policy = hwsim_genl_policy,
  1372. .doit = hwsim_tx_info_frame_received_nl,
  1373. },
  1374. };
  1375. static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
  1376. unsigned long state,
  1377. void *_notify)
  1378. {
  1379. struct netlink_notify *notify = _notify;
  1380. if (state != NETLINK_URELEASE)
  1381. return NOTIFY_DONE;
  1382. if (notify->pid == wmediumd_pid) {
  1383. printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
  1384. " socket, switching to perfect channel medium\n");
  1385. wmediumd_pid = 0;
  1386. }
  1387. return NOTIFY_DONE;
  1388. }
  1389. static struct notifier_block hwsim_netlink_notifier = {
  1390. .notifier_call = mac80211_hwsim_netlink_notify,
  1391. };
  1392. static int hwsim_init_netlink(void)
  1393. {
  1394. int rc;
  1395. printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
  1396. rc = genl_register_family_with_ops(&hwsim_genl_family,
  1397. hwsim_ops, ARRAY_SIZE(hwsim_ops));
  1398. if (rc)
  1399. goto failure;
  1400. rc = netlink_register_notifier(&hwsim_netlink_notifier);
  1401. if (rc)
  1402. goto failure;
  1403. return 0;
  1404. failure:
  1405. printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
  1406. return -EINVAL;
  1407. }
  1408. static void hwsim_exit_netlink(void)
  1409. {
  1410. int ret;
  1411. printk(KERN_INFO "mac80211_hwsim: closing netlink\n");
  1412. /* unregister the notifier */
  1413. netlink_unregister_notifier(&hwsim_netlink_notifier);
  1414. /* unregister the family */
  1415. ret = genl_unregister_family(&hwsim_genl_family);
  1416. if (ret)
  1417. printk(KERN_DEBUG "mac80211_hwsim: "
  1418. "unregister family %i\n", ret);
  1419. }
  1420. static const struct ieee80211_iface_limit hwsim_if_limits[] = {
  1421. { .max = 1, .types = BIT(NL80211_IFTYPE_ADHOC) },
  1422. { .max = 2048, .types = BIT(NL80211_IFTYPE_STATION) |
  1423. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  1424. #ifdef CONFIG_MAC80211_MESH
  1425. BIT(NL80211_IFTYPE_MESH_POINT) |
  1426. #endif
  1427. BIT(NL80211_IFTYPE_AP) |
  1428. BIT(NL80211_IFTYPE_P2P_GO) },
  1429. };
  1430. static const struct ieee80211_iface_combination hwsim_if_comb = {
  1431. .limits = hwsim_if_limits,
  1432. .n_limits = ARRAY_SIZE(hwsim_if_limits),
  1433. .max_interfaces = 2048,
  1434. .num_different_channels = 1,
  1435. };
  1436. static int __init init_mac80211_hwsim(void)
  1437. {
  1438. int i, err = 0;
  1439. u8 addr[ETH_ALEN];
  1440. struct mac80211_hwsim_data *data;
  1441. struct ieee80211_hw *hw;
  1442. enum ieee80211_band band;
  1443. if (radios < 1 || radios > 100)
  1444. return -EINVAL;
  1445. if (fake_hw_scan) {
  1446. mac80211_hwsim_ops.hw_scan = mac80211_hwsim_hw_scan;
  1447. mac80211_hwsim_ops.sw_scan_start = NULL;
  1448. mac80211_hwsim_ops.sw_scan_complete = NULL;
  1449. }
  1450. spin_lock_init(&hwsim_radio_lock);
  1451. INIT_LIST_HEAD(&hwsim_radios);
  1452. hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
  1453. if (IS_ERR(hwsim_class))
  1454. return PTR_ERR(hwsim_class);
  1455. memset(addr, 0, ETH_ALEN);
  1456. addr[0] = 0x02;
  1457. for (i = 0; i < radios; i++) {
  1458. printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
  1459. i);
  1460. hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
  1461. if (!hw) {
  1462. printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
  1463. "failed\n");
  1464. err = -ENOMEM;
  1465. goto failed;
  1466. }
  1467. data = hw->priv;
  1468. data->hw = hw;
  1469. data->dev = device_create(hwsim_class, NULL, 0, hw,
  1470. "hwsim%d", i);
  1471. if (IS_ERR(data->dev)) {
  1472. printk(KERN_DEBUG
  1473. "mac80211_hwsim: device_create "
  1474. "failed (%ld)\n", PTR_ERR(data->dev));
  1475. err = -ENOMEM;
  1476. goto failed_drvdata;
  1477. }
  1478. data->dev->driver = &mac80211_hwsim_driver;
  1479. skb_queue_head_init(&data->pending);
  1480. SET_IEEE80211_DEV(hw, data->dev);
  1481. addr[3] = i >> 8;
  1482. addr[4] = i;
  1483. memcpy(data->addresses[0].addr, addr, ETH_ALEN);
  1484. memcpy(data->addresses[1].addr, addr, ETH_ALEN);
  1485. data->addresses[1].addr[0] |= 0x40;
  1486. hw->wiphy->n_addresses = 2;
  1487. hw->wiphy->addresses = data->addresses;
  1488. hw->wiphy->iface_combinations = &hwsim_if_comb;
  1489. hw->wiphy->n_iface_combinations = 1;
  1490. if (fake_hw_scan) {
  1491. hw->wiphy->max_scan_ssids = 255;
  1492. hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
  1493. }
  1494. hw->channel_change_time = 1;
  1495. hw->queues = 4;
  1496. hw->wiphy->interface_modes =
  1497. BIT(NL80211_IFTYPE_STATION) |
  1498. BIT(NL80211_IFTYPE_AP) |
  1499. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  1500. BIT(NL80211_IFTYPE_P2P_GO) |
  1501. BIT(NL80211_IFTYPE_ADHOC) |
  1502. BIT(NL80211_IFTYPE_MESH_POINT);
  1503. hw->flags = IEEE80211_HW_MFP_CAPABLE |
  1504. IEEE80211_HW_SIGNAL_DBM |
  1505. IEEE80211_HW_SUPPORTS_STATIC_SMPS |
  1506. IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
  1507. IEEE80211_HW_AMPDU_AGGREGATION |
  1508. IEEE80211_HW_WANT_MONITOR_VIF;
  1509. hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
  1510. WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
  1511. /* ask mac80211 to reserve space for magic */
  1512. hw->vif_data_size = sizeof(struct hwsim_vif_priv);
  1513. hw->sta_data_size = sizeof(struct hwsim_sta_priv);
  1514. memcpy(data->channels_2ghz, hwsim_channels_2ghz,
  1515. sizeof(hwsim_channels_2ghz));
  1516. memcpy(data->channels_5ghz, hwsim_channels_5ghz,
  1517. sizeof(hwsim_channels_5ghz));
  1518. memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
  1519. for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
  1520. struct ieee80211_supported_band *sband = &data->bands[band];
  1521. switch (band) {
  1522. case IEEE80211_BAND_2GHZ:
  1523. sband->channels = data->channels_2ghz;
  1524. sband->n_channels =
  1525. ARRAY_SIZE(hwsim_channels_2ghz);
  1526. sband->bitrates = data->rates;
  1527. sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
  1528. break;
  1529. case IEEE80211_BAND_5GHZ:
  1530. sband->channels = data->channels_5ghz;
  1531. sband->n_channels =
  1532. ARRAY_SIZE(hwsim_channels_5ghz);
  1533. sband->bitrates = data->rates + 4;
  1534. sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
  1535. break;
  1536. default:
  1537. break;
  1538. }
  1539. sband->ht_cap.ht_supported = true;
  1540. sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  1541. IEEE80211_HT_CAP_GRN_FLD |
  1542. IEEE80211_HT_CAP_SGI_40 |
  1543. IEEE80211_HT_CAP_DSSSCCK40;
  1544. sband->ht_cap.ampdu_factor = 0x3;
  1545. sband->ht_cap.ampdu_density = 0x6;
  1546. memset(&sband->ht_cap.mcs, 0,
  1547. sizeof(sband->ht_cap.mcs));
  1548. sband->ht_cap.mcs.rx_mask[0] = 0xff;
  1549. sband->ht_cap.mcs.rx_mask[1] = 0xff;
  1550. sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  1551. hw->wiphy->bands[band] = sband;
  1552. }
  1553. /* By default all radios are belonging to the first group */
  1554. data->group = 1;
  1555. mutex_init(&data->mutex);
  1556. /* Enable frame retransmissions for lossy channels */
  1557. hw->max_rates = 4;
  1558. hw->max_rate_tries = 11;
  1559. /* Work to be done prior to ieee80211_register_hw() */
  1560. switch (regtest) {
  1561. case HWSIM_REGTEST_DISABLED:
  1562. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  1563. case HWSIM_REGTEST_DRIVER_REG_ALL:
  1564. case HWSIM_REGTEST_DIFF_COUNTRY:
  1565. /*
  1566. * Nothing to be done for driver regulatory domain
  1567. * hints prior to ieee80211_register_hw()
  1568. */
  1569. break;
  1570. case HWSIM_REGTEST_WORLD_ROAM:
  1571. if (i == 0) {
  1572. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1573. wiphy_apply_custom_regulatory(hw->wiphy,
  1574. &hwsim_world_regdom_custom_01);
  1575. }
  1576. break;
  1577. case HWSIM_REGTEST_CUSTOM_WORLD:
  1578. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1579. wiphy_apply_custom_regulatory(hw->wiphy,
  1580. &hwsim_world_regdom_custom_01);
  1581. break;
  1582. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  1583. if (i == 0) {
  1584. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1585. wiphy_apply_custom_regulatory(hw->wiphy,
  1586. &hwsim_world_regdom_custom_01);
  1587. } else if (i == 1) {
  1588. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1589. wiphy_apply_custom_regulatory(hw->wiphy,
  1590. &hwsim_world_regdom_custom_02);
  1591. }
  1592. break;
  1593. case HWSIM_REGTEST_STRICT_ALL:
  1594. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1595. break;
  1596. case HWSIM_REGTEST_STRICT_FOLLOW:
  1597. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  1598. if (i == 0)
  1599. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1600. break;
  1601. case HWSIM_REGTEST_ALL:
  1602. if (i == 0) {
  1603. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1604. wiphy_apply_custom_regulatory(hw->wiphy,
  1605. &hwsim_world_regdom_custom_01);
  1606. } else if (i == 1) {
  1607. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1608. wiphy_apply_custom_regulatory(hw->wiphy,
  1609. &hwsim_world_regdom_custom_02);
  1610. } else if (i == 4)
  1611. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1612. break;
  1613. default:
  1614. break;
  1615. }
  1616. /* give the regulatory workqueue a chance to run */
  1617. if (regtest)
  1618. schedule_timeout_interruptible(1);
  1619. err = ieee80211_register_hw(hw);
  1620. if (err < 0) {
  1621. printk(KERN_DEBUG "mac80211_hwsim: "
  1622. "ieee80211_register_hw failed (%d)\n", err);
  1623. goto failed_hw;
  1624. }
  1625. /* Work to be done after to ieee80211_register_hw() */
  1626. switch (regtest) {
  1627. case HWSIM_REGTEST_WORLD_ROAM:
  1628. case HWSIM_REGTEST_DISABLED:
  1629. break;
  1630. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  1631. if (!i)
  1632. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1633. break;
  1634. case HWSIM_REGTEST_DRIVER_REG_ALL:
  1635. case HWSIM_REGTEST_STRICT_ALL:
  1636. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1637. break;
  1638. case HWSIM_REGTEST_DIFF_COUNTRY:
  1639. if (i < ARRAY_SIZE(hwsim_alpha2s))
  1640. regulatory_hint(hw->wiphy, hwsim_alpha2s[i]);
  1641. break;
  1642. case HWSIM_REGTEST_CUSTOM_WORLD:
  1643. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  1644. /*
  1645. * Nothing to be done for custom world regulatory
  1646. * domains after to ieee80211_register_hw
  1647. */
  1648. break;
  1649. case HWSIM_REGTEST_STRICT_FOLLOW:
  1650. if (i == 0)
  1651. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1652. break;
  1653. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  1654. if (i == 0)
  1655. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1656. else if (i == 1)
  1657. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  1658. break;
  1659. case HWSIM_REGTEST_ALL:
  1660. if (i == 2)
  1661. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1662. else if (i == 3)
  1663. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  1664. else if (i == 4)
  1665. regulatory_hint(hw->wiphy, hwsim_alpha2s[2]);
  1666. break;
  1667. default:
  1668. break;
  1669. }
  1670. wiphy_debug(hw->wiphy, "hwaddr %pm registered\n",
  1671. hw->wiphy->perm_addr);
  1672. data->debugfs = debugfs_create_dir("hwsim",
  1673. hw->wiphy->debugfsdir);
  1674. data->debugfs_ps = debugfs_create_file("ps", 0666,
  1675. data->debugfs, data,
  1676. &hwsim_fops_ps);
  1677. data->debugfs_group = debugfs_create_file("group", 0666,
  1678. data->debugfs, data,
  1679. &hwsim_fops_group);
  1680. setup_timer(&data->beacon_timer, mac80211_hwsim_beacon,
  1681. (unsigned long) hw);
  1682. list_add_tail(&data->list, &hwsim_radios);
  1683. }
  1684. hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
  1685. if (hwsim_mon == NULL)
  1686. goto failed;
  1687. rtnl_lock();
  1688. err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
  1689. if (err < 0)
  1690. goto failed_mon;
  1691. err = register_netdevice(hwsim_mon);
  1692. if (err < 0)
  1693. goto failed_mon;
  1694. rtnl_unlock();
  1695. err = hwsim_init_netlink();
  1696. if (err < 0)
  1697. goto failed_nl;
  1698. return 0;
  1699. failed_nl:
  1700. printk(KERN_DEBUG "mac_80211_hwsim: failed initializing netlink\n");
  1701. return err;
  1702. failed_mon:
  1703. rtnl_unlock();
  1704. free_netdev(hwsim_mon);
  1705. mac80211_hwsim_free();
  1706. return err;
  1707. failed_hw:
  1708. device_unregister(data->dev);
  1709. failed_drvdata:
  1710. ieee80211_free_hw(hw);
  1711. failed:
  1712. mac80211_hwsim_free();
  1713. return err;
  1714. }
  1715. static void __exit exit_mac80211_hwsim(void)
  1716. {
  1717. printk(KERN_DEBUG "mac80211_hwsim: unregister radios\n");
  1718. hwsim_exit_netlink();
  1719. mac80211_hwsim_free();
  1720. unregister_netdev(hwsim_mon);
  1721. }
  1722. module_init(init_mac80211_hwsim);
  1723. module_exit(exit_mac80211_hwsim);