mac80211_hwsim.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431
  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_portid;
  37. static int radios = 2;
  38. module_param(radios, int, 0444);
  39. MODULE_PARM_DESC(radios, "Number of simulated radios");
  40. static int channels = 1;
  41. module_param(channels, int, 0444);
  42. MODULE_PARM_DESC(channels, "Number of concurrent channels");
  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(vp->magic != HWSIM_VIF_MAGIC,
  155. "Invalid VIF (%p) magic %#x, %pM, %d/%d\n",
  156. vif, vp->magic, vif->addr, vif->type, vif->p2p);
  157. }
  158. static inline void hwsim_set_magic(struct ieee80211_vif *vif)
  159. {
  160. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  161. vp->magic = HWSIM_VIF_MAGIC;
  162. }
  163. static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
  164. {
  165. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  166. vp->magic = 0;
  167. }
  168. struct hwsim_sta_priv {
  169. u32 magic;
  170. };
  171. #define HWSIM_STA_MAGIC 0x6d537749
  172. static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
  173. {
  174. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  175. WARN_ON(sp->magic != HWSIM_STA_MAGIC);
  176. }
  177. static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
  178. {
  179. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  180. sp->magic = HWSIM_STA_MAGIC;
  181. }
  182. static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
  183. {
  184. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  185. sp->magic = 0;
  186. }
  187. struct hwsim_chanctx_priv {
  188. u32 magic;
  189. };
  190. #define HWSIM_CHANCTX_MAGIC 0x6d53774a
  191. static inline void hwsim_check_chanctx_magic(struct ieee80211_chanctx_conf *c)
  192. {
  193. struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
  194. WARN_ON(cp->magic != HWSIM_CHANCTX_MAGIC);
  195. }
  196. static inline void hwsim_set_chanctx_magic(struct ieee80211_chanctx_conf *c)
  197. {
  198. struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
  199. cp->magic = HWSIM_CHANCTX_MAGIC;
  200. }
  201. static inline void hwsim_clear_chanctx_magic(struct ieee80211_chanctx_conf *c)
  202. {
  203. struct hwsim_chanctx_priv *cp = (void *)c->drv_priv;
  204. cp->magic = 0;
  205. }
  206. static struct class *hwsim_class;
  207. static struct net_device *hwsim_mon; /* global monitor netdev */
  208. #define CHAN2G(_freq) { \
  209. .band = IEEE80211_BAND_2GHZ, \
  210. .center_freq = (_freq), \
  211. .hw_value = (_freq), \
  212. .max_power = 20, \
  213. }
  214. #define CHAN5G(_freq) { \
  215. .band = IEEE80211_BAND_5GHZ, \
  216. .center_freq = (_freq), \
  217. .hw_value = (_freq), \
  218. .max_power = 20, \
  219. }
  220. static const struct ieee80211_channel hwsim_channels_2ghz[] = {
  221. CHAN2G(2412), /* Channel 1 */
  222. CHAN2G(2417), /* Channel 2 */
  223. CHAN2G(2422), /* Channel 3 */
  224. CHAN2G(2427), /* Channel 4 */
  225. CHAN2G(2432), /* Channel 5 */
  226. CHAN2G(2437), /* Channel 6 */
  227. CHAN2G(2442), /* Channel 7 */
  228. CHAN2G(2447), /* Channel 8 */
  229. CHAN2G(2452), /* Channel 9 */
  230. CHAN2G(2457), /* Channel 10 */
  231. CHAN2G(2462), /* Channel 11 */
  232. CHAN2G(2467), /* Channel 12 */
  233. CHAN2G(2472), /* Channel 13 */
  234. CHAN2G(2484), /* Channel 14 */
  235. };
  236. static const struct ieee80211_channel hwsim_channels_5ghz[] = {
  237. CHAN5G(5180), /* Channel 36 */
  238. CHAN5G(5200), /* Channel 40 */
  239. CHAN5G(5220), /* Channel 44 */
  240. CHAN5G(5240), /* Channel 48 */
  241. CHAN5G(5260), /* Channel 52 */
  242. CHAN5G(5280), /* Channel 56 */
  243. CHAN5G(5300), /* Channel 60 */
  244. CHAN5G(5320), /* Channel 64 */
  245. CHAN5G(5500), /* Channel 100 */
  246. CHAN5G(5520), /* Channel 104 */
  247. CHAN5G(5540), /* Channel 108 */
  248. CHAN5G(5560), /* Channel 112 */
  249. CHAN5G(5580), /* Channel 116 */
  250. CHAN5G(5600), /* Channel 120 */
  251. CHAN5G(5620), /* Channel 124 */
  252. CHAN5G(5640), /* Channel 128 */
  253. CHAN5G(5660), /* Channel 132 */
  254. CHAN5G(5680), /* Channel 136 */
  255. CHAN5G(5700), /* Channel 140 */
  256. CHAN5G(5745), /* Channel 149 */
  257. CHAN5G(5765), /* Channel 153 */
  258. CHAN5G(5785), /* Channel 157 */
  259. CHAN5G(5805), /* Channel 161 */
  260. CHAN5G(5825), /* Channel 165 */
  261. };
  262. static const struct ieee80211_rate hwsim_rates[] = {
  263. { .bitrate = 10 },
  264. { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  265. { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  266. { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  267. { .bitrate = 60 },
  268. { .bitrate = 90 },
  269. { .bitrate = 120 },
  270. { .bitrate = 180 },
  271. { .bitrate = 240 },
  272. { .bitrate = 360 },
  273. { .bitrate = 480 },
  274. { .bitrate = 540 }
  275. };
  276. static spinlock_t hwsim_radio_lock;
  277. static struct list_head hwsim_radios;
  278. struct mac80211_hwsim_data {
  279. struct list_head list;
  280. struct ieee80211_hw *hw;
  281. struct device *dev;
  282. struct ieee80211_supported_band bands[IEEE80211_NUM_BANDS];
  283. struct ieee80211_channel channels_2ghz[ARRAY_SIZE(hwsim_channels_2ghz)];
  284. struct ieee80211_channel channels_5ghz[ARRAY_SIZE(hwsim_channels_5ghz)];
  285. struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
  286. struct mac_address addresses[2];
  287. struct ieee80211_channel *tmp_chan;
  288. struct delayed_work roc_done;
  289. struct delayed_work hw_scan;
  290. struct cfg80211_scan_request *hw_scan_request;
  291. struct ieee80211_vif *hw_scan_vif;
  292. int scan_chan_idx;
  293. struct ieee80211_channel *channel;
  294. unsigned long beacon_int; /* in jiffies unit */
  295. unsigned int rx_filter;
  296. bool started, idle, scanning;
  297. struct mutex mutex;
  298. struct timer_list beacon_timer;
  299. enum ps_mode {
  300. PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
  301. } ps;
  302. bool ps_poll_pending;
  303. struct dentry *debugfs;
  304. struct dentry *debugfs_ps;
  305. struct sk_buff_head pending; /* packets pending */
  306. /*
  307. * Only radios in the same group can communicate together (the
  308. * channel has to match too). Each bit represents a group. A
  309. * radio can be in more then one group.
  310. */
  311. u64 group;
  312. struct dentry *debugfs_group;
  313. int power_level;
  314. /* difference between this hw's clock and the real clock, in usecs */
  315. u64 tsf_offset;
  316. };
  317. struct hwsim_radiotap_hdr {
  318. struct ieee80211_radiotap_header hdr;
  319. __le64 rt_tsft;
  320. u8 rt_flags;
  321. u8 rt_rate;
  322. __le16 rt_channel;
  323. __le16 rt_chbitmask;
  324. } __packed;
  325. /* MAC80211_HWSIM netlinf family */
  326. static struct genl_family hwsim_genl_family = {
  327. .id = GENL_ID_GENERATE,
  328. .hdrsize = 0,
  329. .name = "MAC80211_HWSIM",
  330. .version = 1,
  331. .maxattr = HWSIM_ATTR_MAX,
  332. };
  333. /* MAC80211_HWSIM netlink policy */
  334. static struct nla_policy hwsim_genl_policy[HWSIM_ATTR_MAX + 1] = {
  335. [HWSIM_ATTR_ADDR_RECEIVER] = { .type = NLA_UNSPEC,
  336. .len = 6*sizeof(u8) },
  337. [HWSIM_ATTR_ADDR_TRANSMITTER] = { .type = NLA_UNSPEC,
  338. .len = 6*sizeof(u8) },
  339. [HWSIM_ATTR_FRAME] = { .type = NLA_BINARY,
  340. .len = IEEE80211_MAX_DATA_LEN },
  341. [HWSIM_ATTR_FLAGS] = { .type = NLA_U32 },
  342. [HWSIM_ATTR_RX_RATE] = { .type = NLA_U32 },
  343. [HWSIM_ATTR_SIGNAL] = { .type = NLA_U32 },
  344. [HWSIM_ATTR_TX_INFO] = { .type = NLA_UNSPEC,
  345. .len = IEEE80211_TX_MAX_RATES*sizeof(
  346. struct hwsim_tx_rate)},
  347. [HWSIM_ATTR_COOKIE] = { .type = NLA_U64 },
  348. };
  349. static netdev_tx_t hwsim_mon_xmit(struct sk_buff *skb,
  350. struct net_device *dev)
  351. {
  352. /* TODO: allow packet injection */
  353. dev_kfree_skb(skb);
  354. return NETDEV_TX_OK;
  355. }
  356. static __le64 __mac80211_hwsim_get_tsf(struct mac80211_hwsim_data *data)
  357. {
  358. struct timeval tv = ktime_to_timeval(ktime_get_real());
  359. u64 now = tv.tv_sec * USEC_PER_SEC + tv.tv_usec;
  360. return cpu_to_le64(now + data->tsf_offset);
  361. }
  362. static u64 mac80211_hwsim_get_tsf(struct ieee80211_hw *hw,
  363. struct ieee80211_vif *vif)
  364. {
  365. struct mac80211_hwsim_data *data = hw->priv;
  366. return le64_to_cpu(__mac80211_hwsim_get_tsf(data));
  367. }
  368. static void mac80211_hwsim_set_tsf(struct ieee80211_hw *hw,
  369. struct ieee80211_vif *vif, u64 tsf)
  370. {
  371. struct mac80211_hwsim_data *data = hw->priv;
  372. struct timeval tv = ktime_to_timeval(ktime_get_real());
  373. u64 now = tv.tv_sec * USEC_PER_SEC + tv.tv_usec;
  374. data->tsf_offset = tsf - now;
  375. }
  376. static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
  377. struct sk_buff *tx_skb,
  378. struct ieee80211_channel *chan)
  379. {
  380. struct mac80211_hwsim_data *data = hw->priv;
  381. struct sk_buff *skb;
  382. struct hwsim_radiotap_hdr *hdr;
  383. u16 flags;
  384. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
  385. struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
  386. if (!netif_running(hwsim_mon))
  387. return;
  388. skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
  389. if (skb == NULL)
  390. return;
  391. hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr));
  392. hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  393. hdr->hdr.it_pad = 0;
  394. hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
  395. hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  396. (1 << IEEE80211_RADIOTAP_RATE) |
  397. (1 << IEEE80211_RADIOTAP_TSFT) |
  398. (1 << IEEE80211_RADIOTAP_CHANNEL));
  399. hdr->rt_tsft = __mac80211_hwsim_get_tsf(data);
  400. hdr->rt_flags = 0;
  401. hdr->rt_rate = txrate->bitrate / 5;
  402. hdr->rt_channel = cpu_to_le16(chan->center_freq);
  403. flags = IEEE80211_CHAN_2GHZ;
  404. if (txrate->flags & IEEE80211_RATE_ERP_G)
  405. flags |= IEEE80211_CHAN_OFDM;
  406. else
  407. flags |= IEEE80211_CHAN_CCK;
  408. hdr->rt_chbitmask = cpu_to_le16(flags);
  409. skb->dev = hwsim_mon;
  410. skb_set_mac_header(skb, 0);
  411. skb->ip_summed = CHECKSUM_UNNECESSARY;
  412. skb->pkt_type = PACKET_OTHERHOST;
  413. skb->protocol = htons(ETH_P_802_2);
  414. memset(skb->cb, 0, sizeof(skb->cb));
  415. netif_rx(skb);
  416. }
  417. static void mac80211_hwsim_monitor_ack(struct ieee80211_channel *chan,
  418. const u8 *addr)
  419. {
  420. struct sk_buff *skb;
  421. struct hwsim_radiotap_hdr *hdr;
  422. u16 flags;
  423. struct ieee80211_hdr *hdr11;
  424. if (!netif_running(hwsim_mon))
  425. return;
  426. skb = dev_alloc_skb(100);
  427. if (skb == NULL)
  428. return;
  429. hdr = (struct hwsim_radiotap_hdr *) skb_put(skb, sizeof(*hdr));
  430. hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  431. hdr->hdr.it_pad = 0;
  432. hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
  433. hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  434. (1 << IEEE80211_RADIOTAP_CHANNEL));
  435. hdr->rt_flags = 0;
  436. hdr->rt_rate = 0;
  437. hdr->rt_channel = cpu_to_le16(chan->center_freq);
  438. flags = IEEE80211_CHAN_2GHZ;
  439. hdr->rt_chbitmask = cpu_to_le16(flags);
  440. hdr11 = (struct ieee80211_hdr *) skb_put(skb, 10);
  441. hdr11->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  442. IEEE80211_STYPE_ACK);
  443. hdr11->duration_id = cpu_to_le16(0);
  444. memcpy(hdr11->addr1, addr, ETH_ALEN);
  445. skb->dev = hwsim_mon;
  446. skb_set_mac_header(skb, 0);
  447. skb->ip_summed = CHECKSUM_UNNECESSARY;
  448. skb->pkt_type = PACKET_OTHERHOST;
  449. skb->protocol = htons(ETH_P_802_2);
  450. memset(skb->cb, 0, sizeof(skb->cb));
  451. netif_rx(skb);
  452. }
  453. static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
  454. struct sk_buff *skb)
  455. {
  456. switch (data->ps) {
  457. case PS_DISABLED:
  458. return true;
  459. case PS_ENABLED:
  460. return false;
  461. case PS_AUTO_POLL:
  462. /* TODO: accept (some) Beacons by default and other frames only
  463. * if pending PS-Poll has been sent */
  464. return true;
  465. case PS_MANUAL_POLL:
  466. /* Allow unicast frames to own address if there is a pending
  467. * PS-Poll */
  468. if (data->ps_poll_pending &&
  469. memcmp(data->hw->wiphy->perm_addr, skb->data + 4,
  470. ETH_ALEN) == 0) {
  471. data->ps_poll_pending = false;
  472. return true;
  473. }
  474. return false;
  475. }
  476. return true;
  477. }
  478. struct mac80211_hwsim_addr_match_data {
  479. bool ret;
  480. const u8 *addr;
  481. };
  482. static void mac80211_hwsim_addr_iter(void *data, u8 *mac,
  483. struct ieee80211_vif *vif)
  484. {
  485. struct mac80211_hwsim_addr_match_data *md = data;
  486. if (memcmp(mac, md->addr, ETH_ALEN) == 0)
  487. md->ret = true;
  488. }
  489. static bool mac80211_hwsim_addr_match(struct mac80211_hwsim_data *data,
  490. const u8 *addr)
  491. {
  492. struct mac80211_hwsim_addr_match_data md;
  493. if (memcmp(addr, data->hw->wiphy->perm_addr, ETH_ALEN) == 0)
  494. return true;
  495. md.ret = false;
  496. md.addr = addr;
  497. ieee80211_iterate_active_interfaces_atomic(data->hw,
  498. IEEE80211_IFACE_ITER_NORMAL,
  499. mac80211_hwsim_addr_iter,
  500. &md);
  501. return md.ret;
  502. }
  503. static void mac80211_hwsim_tx_frame_nl(struct ieee80211_hw *hw,
  504. struct sk_buff *my_skb,
  505. int dst_portid)
  506. {
  507. struct sk_buff *skb;
  508. struct mac80211_hwsim_data *data = hw->priv;
  509. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) my_skb->data;
  510. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(my_skb);
  511. void *msg_head;
  512. unsigned int hwsim_flags = 0;
  513. int i;
  514. struct hwsim_tx_rate tx_attempts[IEEE80211_TX_MAX_RATES];
  515. if (data->ps != PS_DISABLED)
  516. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  517. /* If the queue contains MAX_QUEUE skb's drop some */
  518. if (skb_queue_len(&data->pending) >= MAX_QUEUE) {
  519. /* Droping until WARN_QUEUE level */
  520. while (skb_queue_len(&data->pending) >= WARN_QUEUE)
  521. skb_dequeue(&data->pending);
  522. }
  523. skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  524. if (skb == NULL)
  525. goto nla_put_failure;
  526. msg_head = genlmsg_put(skb, 0, 0, &hwsim_genl_family, 0,
  527. HWSIM_CMD_FRAME);
  528. if (msg_head == NULL) {
  529. printk(KERN_DEBUG "mac80211_hwsim: problem with msg_head\n");
  530. goto nla_put_failure;
  531. }
  532. if (nla_put(skb, HWSIM_ATTR_ADDR_TRANSMITTER,
  533. sizeof(struct mac_address), data->addresses[1].addr))
  534. goto nla_put_failure;
  535. /* We get the skb->data */
  536. if (nla_put(skb, HWSIM_ATTR_FRAME, my_skb->len, my_skb->data))
  537. goto nla_put_failure;
  538. /* We get the flags for this transmission, and we translate them to
  539. wmediumd flags */
  540. if (info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS)
  541. hwsim_flags |= HWSIM_TX_CTL_REQ_TX_STATUS;
  542. if (info->flags & IEEE80211_TX_CTL_NO_ACK)
  543. hwsim_flags |= HWSIM_TX_CTL_NO_ACK;
  544. if (nla_put_u32(skb, HWSIM_ATTR_FLAGS, hwsim_flags))
  545. goto nla_put_failure;
  546. /* We get the tx control (rate and retries) info*/
  547. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  548. tx_attempts[i].idx = info->status.rates[i].idx;
  549. tx_attempts[i].count = info->status.rates[i].count;
  550. }
  551. if (nla_put(skb, HWSIM_ATTR_TX_INFO,
  552. sizeof(struct hwsim_tx_rate)*IEEE80211_TX_MAX_RATES,
  553. tx_attempts))
  554. goto nla_put_failure;
  555. /* We create a cookie to identify this skb */
  556. if (nla_put_u64(skb, HWSIM_ATTR_COOKIE, (unsigned long) my_skb))
  557. goto nla_put_failure;
  558. genlmsg_end(skb, msg_head);
  559. genlmsg_unicast(&init_net, skb, dst_portid);
  560. /* Enqueue the packet */
  561. skb_queue_tail(&data->pending, my_skb);
  562. return;
  563. nla_put_failure:
  564. printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
  565. }
  566. static bool hwsim_chans_compat(struct ieee80211_channel *c1,
  567. struct ieee80211_channel *c2)
  568. {
  569. if (!c1 || !c2)
  570. return false;
  571. return c1->center_freq == c2->center_freq;
  572. }
  573. struct tx_iter_data {
  574. struct ieee80211_channel *channel;
  575. bool receive;
  576. };
  577. static void mac80211_hwsim_tx_iter(void *_data, u8 *addr,
  578. struct ieee80211_vif *vif)
  579. {
  580. struct tx_iter_data *data = _data;
  581. if (!vif->chanctx_conf)
  582. return;
  583. if (!hwsim_chans_compat(data->channel,
  584. rcu_dereference(vif->chanctx_conf)->def.chan))
  585. return;
  586. data->receive = true;
  587. }
  588. static bool mac80211_hwsim_tx_frame_no_nl(struct ieee80211_hw *hw,
  589. struct sk_buff *skb,
  590. struct ieee80211_channel *chan)
  591. {
  592. struct mac80211_hwsim_data *data = hw->priv, *data2;
  593. bool ack = false;
  594. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  595. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  596. struct ieee80211_rx_status rx_status;
  597. struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
  598. memset(&rx_status, 0, sizeof(rx_status));
  599. rx_status.flag |= RX_FLAG_MACTIME_START;
  600. rx_status.freq = chan->center_freq;
  601. rx_status.band = chan->band;
  602. rx_status.rate_idx = info->control.rates[0].idx;
  603. if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
  604. rx_status.flag |= RX_FLAG_HT;
  605. if (info->control.rates[0].flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  606. rx_status.flag |= RX_FLAG_40MHZ;
  607. if (info->control.rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
  608. rx_status.flag |= RX_FLAG_SHORT_GI;
  609. /* TODO: simulate real signal strength (and optional packet loss) */
  610. rx_status.signal = data->power_level - 50;
  611. if (data->ps != PS_DISABLED)
  612. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  613. /* release the skb's source info */
  614. skb_orphan(skb);
  615. skb_dst_drop(skb);
  616. skb->mark = 0;
  617. secpath_reset(skb);
  618. nf_reset(skb);
  619. /* Copy skb to all enabled radios that are on the current frequency */
  620. spin_lock(&hwsim_radio_lock);
  621. list_for_each_entry(data2, &hwsim_radios, list) {
  622. struct sk_buff *nskb;
  623. struct ieee80211_mgmt *mgmt;
  624. struct tx_iter_data tx_iter_data = {
  625. .receive = false,
  626. .channel = chan,
  627. };
  628. if (data == data2)
  629. continue;
  630. if (!data2->started || (data2->idle && !data2->tmp_chan) ||
  631. !hwsim_ps_rx_ok(data2, skb))
  632. continue;
  633. if (!(data->group & data2->group))
  634. continue;
  635. if (!hwsim_chans_compat(chan, data2->tmp_chan) &&
  636. !hwsim_chans_compat(chan, data2->channel)) {
  637. ieee80211_iterate_active_interfaces_atomic(
  638. data2->hw, IEEE80211_IFACE_ITER_NORMAL,
  639. mac80211_hwsim_tx_iter, &tx_iter_data);
  640. if (!tx_iter_data.receive)
  641. continue;
  642. }
  643. /*
  644. * reserve some space for our vendor and the normal
  645. * radiotap header, since we're copying anyway
  646. */
  647. nskb = skb_copy_expand(skb, 64, 0, GFP_ATOMIC);
  648. if (nskb == NULL)
  649. continue;
  650. if (mac80211_hwsim_addr_match(data2, hdr->addr1))
  651. ack = true;
  652. /* set bcn timestamp relative to receiver mactime */
  653. rx_status.mactime =
  654. le64_to_cpu(__mac80211_hwsim_get_tsf(data2));
  655. mgmt = (struct ieee80211_mgmt *) nskb->data;
  656. if (ieee80211_is_beacon(mgmt->frame_control) ||
  657. ieee80211_is_probe_resp(mgmt->frame_control))
  658. mgmt->u.beacon.timestamp = cpu_to_le64(
  659. rx_status.mactime +
  660. (data->tsf_offset - data2->tsf_offset) +
  661. 24 * 8 * 10 / txrate->bitrate);
  662. #if 0
  663. /*
  664. * Don't enable this code by default as the OUI 00:00:00
  665. * is registered to Xerox so we shouldn't use it here, it
  666. * might find its way into pcap files.
  667. * Note that this code requires the headroom in the SKB
  668. * that was allocated earlier.
  669. */
  670. rx_status.vendor_radiotap_oui[0] = 0x00;
  671. rx_status.vendor_radiotap_oui[1] = 0x00;
  672. rx_status.vendor_radiotap_oui[2] = 0x00;
  673. rx_status.vendor_radiotap_subns = 127;
  674. /*
  675. * Radiotap vendor namespaces can (and should) also be
  676. * split into fields by using the standard radiotap
  677. * presence bitmap mechanism. Use just BIT(0) here for
  678. * the presence bitmap.
  679. */
  680. rx_status.vendor_radiotap_bitmap = BIT(0);
  681. /* We have 8 bytes of (dummy) data */
  682. rx_status.vendor_radiotap_len = 8;
  683. /* For testing, also require it to be aligned */
  684. rx_status.vendor_radiotap_align = 8;
  685. /* push the data */
  686. memcpy(skb_push(nskb, 8), "ABCDEFGH", 8);
  687. #endif
  688. memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
  689. ieee80211_rx_irqsafe(data2->hw, nskb);
  690. }
  691. spin_unlock(&hwsim_radio_lock);
  692. return ack;
  693. }
  694. static void mac80211_hwsim_tx(struct ieee80211_hw *hw,
  695. struct ieee80211_tx_control *control,
  696. struct sk_buff *skb)
  697. {
  698. struct mac80211_hwsim_data *data = hw->priv;
  699. struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
  700. struct ieee80211_chanctx_conf *chanctx_conf;
  701. struct ieee80211_channel *channel;
  702. bool ack;
  703. u32 _portid;
  704. if (WARN_ON(skb->len < 10)) {
  705. /* Should not happen; just a sanity check for addr1 use */
  706. dev_kfree_skb(skb);
  707. return;
  708. }
  709. if (channels == 1) {
  710. channel = data->channel;
  711. } else if (txi->hw_queue == 4) {
  712. channel = data->tmp_chan;
  713. } else {
  714. chanctx_conf = rcu_dereference(txi->control.vif->chanctx_conf);
  715. if (chanctx_conf)
  716. channel = chanctx_conf->def.chan;
  717. else
  718. channel = NULL;
  719. }
  720. if (WARN(!channel, "TX w/o channel - queue = %d\n", txi->hw_queue)) {
  721. dev_kfree_skb(skb);
  722. return;
  723. }
  724. if (data->idle && !data->tmp_chan) {
  725. wiphy_debug(hw->wiphy, "Trying to TX when idle - reject\n");
  726. dev_kfree_skb(skb);
  727. return;
  728. }
  729. if (txi->control.vif)
  730. hwsim_check_magic(txi->control.vif);
  731. if (control->sta)
  732. hwsim_check_sta_magic(control->sta);
  733. txi->rate_driver_data[0] = channel;
  734. mac80211_hwsim_monitor_rx(hw, skb, channel);
  735. /* wmediumd mode check */
  736. _portid = ACCESS_ONCE(wmediumd_portid);
  737. if (_portid)
  738. return mac80211_hwsim_tx_frame_nl(hw, skb, _portid);
  739. /* NO wmediumd detected, perfect medium simulation */
  740. ack = mac80211_hwsim_tx_frame_no_nl(hw, skb, channel);
  741. if (ack && skb->len >= 16) {
  742. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  743. mac80211_hwsim_monitor_ack(channel, hdr->addr2);
  744. }
  745. ieee80211_tx_info_clear_status(txi);
  746. /* frame was transmitted at most favorable rate at first attempt */
  747. txi->control.rates[0].count = 1;
  748. txi->control.rates[1].idx = -1;
  749. if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
  750. txi->flags |= IEEE80211_TX_STAT_ACK;
  751. ieee80211_tx_status_irqsafe(hw, skb);
  752. }
  753. static int mac80211_hwsim_start(struct ieee80211_hw *hw)
  754. {
  755. struct mac80211_hwsim_data *data = hw->priv;
  756. wiphy_debug(hw->wiphy, "%s\n", __func__);
  757. data->started = true;
  758. return 0;
  759. }
  760. static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
  761. {
  762. struct mac80211_hwsim_data *data = hw->priv;
  763. data->started = false;
  764. del_timer(&data->beacon_timer);
  765. wiphy_debug(hw->wiphy, "%s\n", __func__);
  766. }
  767. static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
  768. struct ieee80211_vif *vif)
  769. {
  770. wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
  771. __func__, ieee80211_vif_type_p2p(vif),
  772. vif->addr);
  773. hwsim_set_magic(vif);
  774. vif->cab_queue = 0;
  775. vif->hw_queue[IEEE80211_AC_VO] = 0;
  776. vif->hw_queue[IEEE80211_AC_VI] = 1;
  777. vif->hw_queue[IEEE80211_AC_BE] = 2;
  778. vif->hw_queue[IEEE80211_AC_BK] = 3;
  779. return 0;
  780. }
  781. static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
  782. struct ieee80211_vif *vif,
  783. enum nl80211_iftype newtype,
  784. bool newp2p)
  785. {
  786. newtype = ieee80211_iftype_p2p(newtype, newp2p);
  787. wiphy_debug(hw->wiphy,
  788. "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
  789. __func__, ieee80211_vif_type_p2p(vif),
  790. newtype, vif->addr);
  791. hwsim_check_magic(vif);
  792. return 0;
  793. }
  794. static void mac80211_hwsim_remove_interface(
  795. struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  796. {
  797. wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
  798. __func__, ieee80211_vif_type_p2p(vif),
  799. vif->addr);
  800. hwsim_check_magic(vif);
  801. hwsim_clear_magic(vif);
  802. }
  803. static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
  804. struct sk_buff *skb,
  805. struct ieee80211_channel *chan)
  806. {
  807. u32 _pid = ACCESS_ONCE(wmediumd_portid);
  808. mac80211_hwsim_monitor_rx(hw, skb, chan);
  809. if (_pid)
  810. return mac80211_hwsim_tx_frame_nl(hw, skb, _pid);
  811. mac80211_hwsim_tx_frame_no_nl(hw, skb, chan);
  812. dev_kfree_skb(skb);
  813. }
  814. static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
  815. struct ieee80211_vif *vif)
  816. {
  817. struct ieee80211_hw *hw = arg;
  818. struct sk_buff *skb;
  819. hwsim_check_magic(vif);
  820. if (vif->type != NL80211_IFTYPE_AP &&
  821. vif->type != NL80211_IFTYPE_MESH_POINT &&
  822. vif->type != NL80211_IFTYPE_ADHOC)
  823. return;
  824. skb = ieee80211_beacon_get(hw, vif);
  825. if (skb == NULL)
  826. return;
  827. mac80211_hwsim_tx_frame(hw, skb,
  828. rcu_dereference(vif->chanctx_conf)->def.chan);
  829. }
  830. static void mac80211_hwsim_beacon(unsigned long arg)
  831. {
  832. struct ieee80211_hw *hw = (struct ieee80211_hw *) arg;
  833. struct mac80211_hwsim_data *data = hw->priv;
  834. if (!data->started)
  835. return;
  836. ieee80211_iterate_active_interfaces_atomic(
  837. hw, IEEE80211_IFACE_ITER_NORMAL,
  838. mac80211_hwsim_beacon_tx, hw);
  839. data->beacon_timer.expires = jiffies + data->beacon_int;
  840. add_timer(&data->beacon_timer);
  841. }
  842. static const char *hwsim_chantypes[] = {
  843. [NL80211_CHAN_NO_HT] = "noht",
  844. [NL80211_CHAN_HT20] = "ht20",
  845. [NL80211_CHAN_HT40MINUS] = "ht40-",
  846. [NL80211_CHAN_HT40PLUS] = "ht40+",
  847. };
  848. static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
  849. {
  850. struct mac80211_hwsim_data *data = hw->priv;
  851. struct ieee80211_conf *conf = &hw->conf;
  852. static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
  853. [IEEE80211_SMPS_AUTOMATIC] = "auto",
  854. [IEEE80211_SMPS_OFF] = "off",
  855. [IEEE80211_SMPS_STATIC] = "static",
  856. [IEEE80211_SMPS_DYNAMIC] = "dynamic",
  857. };
  858. wiphy_debug(hw->wiphy,
  859. "%s (freq=%d/%s idle=%d ps=%d smps=%s)\n",
  860. __func__,
  861. conf->channel ? conf->channel->center_freq : 0,
  862. hwsim_chantypes[conf->channel_type],
  863. !!(conf->flags & IEEE80211_CONF_IDLE),
  864. !!(conf->flags & IEEE80211_CONF_PS),
  865. smps_modes[conf->smps_mode]);
  866. data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
  867. data->channel = conf->channel;
  868. WARN_ON(data->channel && channels > 1);
  869. data->power_level = conf->power_level;
  870. if (!data->started || !data->beacon_int)
  871. del_timer(&data->beacon_timer);
  872. else
  873. mod_timer(&data->beacon_timer, jiffies + data->beacon_int);
  874. return 0;
  875. }
  876. static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
  877. unsigned int changed_flags,
  878. unsigned int *total_flags,u64 multicast)
  879. {
  880. struct mac80211_hwsim_data *data = hw->priv;
  881. wiphy_debug(hw->wiphy, "%s\n", __func__);
  882. data->rx_filter = 0;
  883. if (*total_flags & FIF_PROMISC_IN_BSS)
  884. data->rx_filter |= FIF_PROMISC_IN_BSS;
  885. if (*total_flags & FIF_ALLMULTI)
  886. data->rx_filter |= FIF_ALLMULTI;
  887. *total_flags = data->rx_filter;
  888. }
  889. static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
  890. struct ieee80211_vif *vif,
  891. struct ieee80211_bss_conf *info,
  892. u32 changed)
  893. {
  894. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  895. struct mac80211_hwsim_data *data = hw->priv;
  896. hwsim_check_magic(vif);
  897. wiphy_debug(hw->wiphy, "%s(changed=0x%x)\n", __func__, changed);
  898. if (changed & BSS_CHANGED_BSSID) {
  899. wiphy_debug(hw->wiphy, "%s: BSSID changed: %pM\n",
  900. __func__, info->bssid);
  901. memcpy(vp->bssid, info->bssid, ETH_ALEN);
  902. }
  903. if (changed & BSS_CHANGED_ASSOC) {
  904. wiphy_debug(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
  905. info->assoc, info->aid);
  906. vp->assoc = info->assoc;
  907. vp->aid = info->aid;
  908. }
  909. if (changed & BSS_CHANGED_BEACON_INT) {
  910. wiphy_debug(hw->wiphy, " BCNINT: %d\n", info->beacon_int);
  911. data->beacon_int = 1024 * info->beacon_int / 1000 * HZ / 1000;
  912. if (WARN_ON(!data->beacon_int))
  913. data->beacon_int = 1;
  914. if (data->started)
  915. mod_timer(&data->beacon_timer,
  916. jiffies + data->beacon_int);
  917. }
  918. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  919. wiphy_debug(hw->wiphy, " ERP_CTS_PROT: %d\n",
  920. info->use_cts_prot);
  921. }
  922. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  923. wiphy_debug(hw->wiphy, " ERP_PREAMBLE: %d\n",
  924. info->use_short_preamble);
  925. }
  926. if (changed & BSS_CHANGED_ERP_SLOT) {
  927. wiphy_debug(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
  928. }
  929. if (changed & BSS_CHANGED_HT) {
  930. wiphy_debug(hw->wiphy, " HT: op_mode=0x%x\n",
  931. info->ht_operation_mode);
  932. }
  933. if (changed & BSS_CHANGED_BASIC_RATES) {
  934. wiphy_debug(hw->wiphy, " BASIC_RATES: 0x%llx\n",
  935. (unsigned long long) info->basic_rates);
  936. }
  937. if (changed & BSS_CHANGED_TXPOWER)
  938. wiphy_debug(hw->wiphy, " TX Power: %d dBm\n", info->txpower);
  939. }
  940. static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
  941. struct ieee80211_vif *vif,
  942. struct ieee80211_sta *sta)
  943. {
  944. hwsim_check_magic(vif);
  945. hwsim_set_sta_magic(sta);
  946. return 0;
  947. }
  948. static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
  949. struct ieee80211_vif *vif,
  950. struct ieee80211_sta *sta)
  951. {
  952. hwsim_check_magic(vif);
  953. hwsim_clear_sta_magic(sta);
  954. return 0;
  955. }
  956. static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
  957. struct ieee80211_vif *vif,
  958. enum sta_notify_cmd cmd,
  959. struct ieee80211_sta *sta)
  960. {
  961. hwsim_check_magic(vif);
  962. switch (cmd) {
  963. case STA_NOTIFY_SLEEP:
  964. case STA_NOTIFY_AWAKE:
  965. /* TODO: make good use of these flags */
  966. break;
  967. default:
  968. WARN(1, "Invalid sta notify: %d\n", cmd);
  969. break;
  970. }
  971. }
  972. static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
  973. struct ieee80211_sta *sta,
  974. bool set)
  975. {
  976. hwsim_check_sta_magic(sta);
  977. return 0;
  978. }
  979. static int mac80211_hwsim_conf_tx(
  980. struct ieee80211_hw *hw,
  981. struct ieee80211_vif *vif, u16 queue,
  982. const struct ieee80211_tx_queue_params *params)
  983. {
  984. wiphy_debug(hw->wiphy,
  985. "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
  986. __func__, queue,
  987. params->txop, params->cw_min,
  988. params->cw_max, params->aifs);
  989. return 0;
  990. }
  991. static int mac80211_hwsim_get_survey(
  992. struct ieee80211_hw *hw, int idx,
  993. struct survey_info *survey)
  994. {
  995. struct ieee80211_conf *conf = &hw->conf;
  996. wiphy_debug(hw->wiphy, "%s (idx=%d)\n", __func__, idx);
  997. if (idx != 0)
  998. return -ENOENT;
  999. /* Current channel */
  1000. survey->channel = conf->channel;
  1001. /*
  1002. * Magically conjured noise level --- this is only ok for simulated hardware.
  1003. *
  1004. * A real driver which cannot determine the real channel noise MUST NOT
  1005. * report any noise, especially not a magically conjured one :-)
  1006. */
  1007. survey->filled = SURVEY_INFO_NOISE_DBM;
  1008. survey->noise = -92;
  1009. return 0;
  1010. }
  1011. #ifdef CONFIG_NL80211_TESTMODE
  1012. /*
  1013. * This section contains example code for using netlink
  1014. * attributes with the testmode command in nl80211.
  1015. */
  1016. /* These enums need to be kept in sync with userspace */
  1017. enum hwsim_testmode_attr {
  1018. __HWSIM_TM_ATTR_INVALID = 0,
  1019. HWSIM_TM_ATTR_CMD = 1,
  1020. HWSIM_TM_ATTR_PS = 2,
  1021. /* keep last */
  1022. __HWSIM_TM_ATTR_AFTER_LAST,
  1023. HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
  1024. };
  1025. enum hwsim_testmode_cmd {
  1026. HWSIM_TM_CMD_SET_PS = 0,
  1027. HWSIM_TM_CMD_GET_PS = 1,
  1028. HWSIM_TM_CMD_STOP_QUEUES = 2,
  1029. HWSIM_TM_CMD_WAKE_QUEUES = 3,
  1030. };
  1031. static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
  1032. [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
  1033. [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
  1034. };
  1035. static int hwsim_fops_ps_write(void *dat, u64 val);
  1036. static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
  1037. void *data, int len)
  1038. {
  1039. struct mac80211_hwsim_data *hwsim = hw->priv;
  1040. struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
  1041. struct sk_buff *skb;
  1042. int err, ps;
  1043. err = nla_parse(tb, HWSIM_TM_ATTR_MAX, data, len,
  1044. hwsim_testmode_policy);
  1045. if (err)
  1046. return err;
  1047. if (!tb[HWSIM_TM_ATTR_CMD])
  1048. return -EINVAL;
  1049. switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
  1050. case HWSIM_TM_CMD_SET_PS:
  1051. if (!tb[HWSIM_TM_ATTR_PS])
  1052. return -EINVAL;
  1053. ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
  1054. return hwsim_fops_ps_write(hwsim, ps);
  1055. case HWSIM_TM_CMD_GET_PS:
  1056. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
  1057. nla_total_size(sizeof(u32)));
  1058. if (!skb)
  1059. return -ENOMEM;
  1060. if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
  1061. goto nla_put_failure;
  1062. return cfg80211_testmode_reply(skb);
  1063. case HWSIM_TM_CMD_STOP_QUEUES:
  1064. ieee80211_stop_queues(hw);
  1065. return 0;
  1066. case HWSIM_TM_CMD_WAKE_QUEUES:
  1067. ieee80211_wake_queues(hw);
  1068. return 0;
  1069. default:
  1070. return -EOPNOTSUPP;
  1071. }
  1072. nla_put_failure:
  1073. kfree_skb(skb);
  1074. return -ENOBUFS;
  1075. }
  1076. #endif
  1077. static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
  1078. struct ieee80211_vif *vif,
  1079. enum ieee80211_ampdu_mlme_action action,
  1080. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  1081. u8 buf_size)
  1082. {
  1083. switch (action) {
  1084. case IEEE80211_AMPDU_TX_START:
  1085. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1086. break;
  1087. case IEEE80211_AMPDU_TX_STOP:
  1088. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1089. break;
  1090. case IEEE80211_AMPDU_TX_OPERATIONAL:
  1091. break;
  1092. case IEEE80211_AMPDU_RX_START:
  1093. case IEEE80211_AMPDU_RX_STOP:
  1094. break;
  1095. default:
  1096. return -EOPNOTSUPP;
  1097. }
  1098. return 0;
  1099. }
  1100. static void mac80211_hwsim_flush(struct ieee80211_hw *hw, bool drop)
  1101. {
  1102. /* Not implemented, queues only on kernel side */
  1103. }
  1104. static void hw_scan_work(struct work_struct *work)
  1105. {
  1106. struct mac80211_hwsim_data *hwsim =
  1107. container_of(work, struct mac80211_hwsim_data, hw_scan.work);
  1108. struct cfg80211_scan_request *req = hwsim->hw_scan_request;
  1109. int dwell, i;
  1110. mutex_lock(&hwsim->mutex);
  1111. if (hwsim->scan_chan_idx >= req->n_channels) {
  1112. wiphy_debug(hwsim->hw->wiphy, "hw scan complete\n");
  1113. ieee80211_scan_completed(hwsim->hw, false);
  1114. hwsim->hw_scan_request = NULL;
  1115. hwsim->hw_scan_vif = NULL;
  1116. hwsim->tmp_chan = NULL;
  1117. mutex_unlock(&hwsim->mutex);
  1118. return;
  1119. }
  1120. wiphy_debug(hwsim->hw->wiphy, "hw scan %d MHz\n",
  1121. req->channels[hwsim->scan_chan_idx]->center_freq);
  1122. hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx];
  1123. if (hwsim->tmp_chan->flags & IEEE80211_CHAN_PASSIVE_SCAN ||
  1124. !req->n_ssids) {
  1125. dwell = 120;
  1126. } else {
  1127. dwell = 30;
  1128. /* send probes */
  1129. for (i = 0; i < req->n_ssids; i++) {
  1130. struct sk_buff *probe;
  1131. probe = ieee80211_probereq_get(hwsim->hw,
  1132. hwsim->hw_scan_vif,
  1133. req->ssids[i].ssid,
  1134. req->ssids[i].ssid_len,
  1135. req->ie_len);
  1136. if (!probe)
  1137. continue;
  1138. if (req->ie_len)
  1139. memcpy(skb_put(probe, req->ie_len), req->ie,
  1140. req->ie_len);
  1141. local_bh_disable();
  1142. mac80211_hwsim_tx_frame(hwsim->hw, probe,
  1143. hwsim->tmp_chan);
  1144. local_bh_enable();
  1145. }
  1146. }
  1147. ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
  1148. msecs_to_jiffies(dwell));
  1149. hwsim->scan_chan_idx++;
  1150. mutex_unlock(&hwsim->mutex);
  1151. }
  1152. static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
  1153. struct ieee80211_vif *vif,
  1154. struct cfg80211_scan_request *req)
  1155. {
  1156. struct mac80211_hwsim_data *hwsim = hw->priv;
  1157. mutex_lock(&hwsim->mutex);
  1158. if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
  1159. mutex_unlock(&hwsim->mutex);
  1160. return -EBUSY;
  1161. }
  1162. hwsim->hw_scan_request = req;
  1163. hwsim->hw_scan_vif = vif;
  1164. hwsim->scan_chan_idx = 0;
  1165. mutex_unlock(&hwsim->mutex);
  1166. wiphy_debug(hw->wiphy, "hwsim hw_scan request\n");
  1167. ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
  1168. return 0;
  1169. }
  1170. static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
  1171. struct ieee80211_vif *vif)
  1172. {
  1173. struct mac80211_hwsim_data *hwsim = hw->priv;
  1174. wiphy_debug(hw->wiphy, "hwsim cancel_hw_scan\n");
  1175. cancel_delayed_work_sync(&hwsim->hw_scan);
  1176. mutex_lock(&hwsim->mutex);
  1177. ieee80211_scan_completed(hwsim->hw, true);
  1178. hwsim->tmp_chan = NULL;
  1179. hwsim->hw_scan_request = NULL;
  1180. hwsim->hw_scan_vif = NULL;
  1181. mutex_unlock(&hwsim->mutex);
  1182. }
  1183. static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw)
  1184. {
  1185. struct mac80211_hwsim_data *hwsim = hw->priv;
  1186. mutex_lock(&hwsim->mutex);
  1187. if (hwsim->scanning) {
  1188. printk(KERN_DEBUG "two hwsim sw_scans detected!\n");
  1189. goto out;
  1190. }
  1191. printk(KERN_DEBUG "hwsim sw_scan request, prepping stuff\n");
  1192. hwsim->scanning = true;
  1193. out:
  1194. mutex_unlock(&hwsim->mutex);
  1195. }
  1196. static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw)
  1197. {
  1198. struct mac80211_hwsim_data *hwsim = hw->priv;
  1199. mutex_lock(&hwsim->mutex);
  1200. printk(KERN_DEBUG "hwsim sw_scan_complete\n");
  1201. hwsim->scanning = false;
  1202. mutex_unlock(&hwsim->mutex);
  1203. }
  1204. static void hw_roc_done(struct work_struct *work)
  1205. {
  1206. struct mac80211_hwsim_data *hwsim =
  1207. container_of(work, struct mac80211_hwsim_data, roc_done.work);
  1208. mutex_lock(&hwsim->mutex);
  1209. ieee80211_remain_on_channel_expired(hwsim->hw);
  1210. hwsim->tmp_chan = NULL;
  1211. mutex_unlock(&hwsim->mutex);
  1212. wiphy_debug(hwsim->hw->wiphy, "hwsim ROC expired\n");
  1213. }
  1214. static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
  1215. struct ieee80211_vif *vif,
  1216. struct ieee80211_channel *chan,
  1217. int duration)
  1218. {
  1219. struct mac80211_hwsim_data *hwsim = hw->priv;
  1220. mutex_lock(&hwsim->mutex);
  1221. if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
  1222. mutex_unlock(&hwsim->mutex);
  1223. return -EBUSY;
  1224. }
  1225. hwsim->tmp_chan = chan;
  1226. mutex_unlock(&hwsim->mutex);
  1227. wiphy_debug(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
  1228. chan->center_freq, duration);
  1229. ieee80211_ready_on_channel(hw);
  1230. ieee80211_queue_delayed_work(hw, &hwsim->roc_done,
  1231. msecs_to_jiffies(duration));
  1232. return 0;
  1233. }
  1234. static int mac80211_hwsim_croc(struct ieee80211_hw *hw)
  1235. {
  1236. struct mac80211_hwsim_data *hwsim = hw->priv;
  1237. cancel_delayed_work_sync(&hwsim->roc_done);
  1238. mutex_lock(&hwsim->mutex);
  1239. hwsim->tmp_chan = NULL;
  1240. mutex_unlock(&hwsim->mutex);
  1241. wiphy_debug(hw->wiphy, "hwsim ROC canceled\n");
  1242. return 0;
  1243. }
  1244. static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
  1245. struct ieee80211_chanctx_conf *ctx)
  1246. {
  1247. hwsim_set_chanctx_magic(ctx);
  1248. wiphy_debug(hw->wiphy,
  1249. "add channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
  1250. ctx->def.chan->center_freq, ctx->def.width,
  1251. ctx->def.center_freq1, ctx->def.center_freq2);
  1252. return 0;
  1253. }
  1254. static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
  1255. struct ieee80211_chanctx_conf *ctx)
  1256. {
  1257. wiphy_debug(hw->wiphy,
  1258. "remove channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
  1259. ctx->def.chan->center_freq, ctx->def.width,
  1260. ctx->def.center_freq1, ctx->def.center_freq2);
  1261. hwsim_check_chanctx_magic(ctx);
  1262. hwsim_clear_chanctx_magic(ctx);
  1263. }
  1264. static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
  1265. struct ieee80211_chanctx_conf *ctx,
  1266. u32 changed)
  1267. {
  1268. hwsim_check_chanctx_magic(ctx);
  1269. wiphy_debug(hw->wiphy,
  1270. "change channel context control: %d MHz/width: %d/cfreqs:%d/%d MHz\n",
  1271. ctx->def.chan->center_freq, ctx->def.width,
  1272. ctx->def.center_freq1, ctx->def.center_freq2);
  1273. }
  1274. static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
  1275. struct ieee80211_vif *vif,
  1276. struct ieee80211_chanctx_conf *ctx)
  1277. {
  1278. hwsim_check_magic(vif);
  1279. hwsim_check_chanctx_magic(ctx);
  1280. return 0;
  1281. }
  1282. static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
  1283. struct ieee80211_vif *vif,
  1284. struct ieee80211_chanctx_conf *ctx)
  1285. {
  1286. hwsim_check_magic(vif);
  1287. hwsim_check_chanctx_magic(ctx);
  1288. }
  1289. static struct ieee80211_ops mac80211_hwsim_ops =
  1290. {
  1291. .tx = mac80211_hwsim_tx,
  1292. .start = mac80211_hwsim_start,
  1293. .stop = mac80211_hwsim_stop,
  1294. .add_interface = mac80211_hwsim_add_interface,
  1295. .change_interface = mac80211_hwsim_change_interface,
  1296. .remove_interface = mac80211_hwsim_remove_interface,
  1297. .config = mac80211_hwsim_config,
  1298. .configure_filter = mac80211_hwsim_configure_filter,
  1299. .bss_info_changed = mac80211_hwsim_bss_info_changed,
  1300. .sta_add = mac80211_hwsim_sta_add,
  1301. .sta_remove = mac80211_hwsim_sta_remove,
  1302. .sta_notify = mac80211_hwsim_sta_notify,
  1303. .set_tim = mac80211_hwsim_set_tim,
  1304. .conf_tx = mac80211_hwsim_conf_tx,
  1305. .get_survey = mac80211_hwsim_get_survey,
  1306. CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd)
  1307. .ampdu_action = mac80211_hwsim_ampdu_action,
  1308. .sw_scan_start = mac80211_hwsim_sw_scan,
  1309. .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
  1310. .flush = mac80211_hwsim_flush,
  1311. .get_tsf = mac80211_hwsim_get_tsf,
  1312. .set_tsf = mac80211_hwsim_set_tsf,
  1313. };
  1314. static void mac80211_hwsim_free(void)
  1315. {
  1316. struct list_head tmplist, *i, *tmp;
  1317. struct mac80211_hwsim_data *data, *tmpdata;
  1318. INIT_LIST_HEAD(&tmplist);
  1319. spin_lock_bh(&hwsim_radio_lock);
  1320. list_for_each_safe(i, tmp, &hwsim_radios)
  1321. list_move(i, &tmplist);
  1322. spin_unlock_bh(&hwsim_radio_lock);
  1323. list_for_each_entry_safe(data, tmpdata, &tmplist, list) {
  1324. debugfs_remove(data->debugfs_group);
  1325. debugfs_remove(data->debugfs_ps);
  1326. debugfs_remove(data->debugfs);
  1327. ieee80211_unregister_hw(data->hw);
  1328. device_unregister(data->dev);
  1329. ieee80211_free_hw(data->hw);
  1330. }
  1331. class_destroy(hwsim_class);
  1332. }
  1333. static struct device_driver mac80211_hwsim_driver = {
  1334. .name = "mac80211_hwsim"
  1335. };
  1336. static const struct net_device_ops hwsim_netdev_ops = {
  1337. .ndo_start_xmit = hwsim_mon_xmit,
  1338. .ndo_change_mtu = eth_change_mtu,
  1339. .ndo_set_mac_address = eth_mac_addr,
  1340. .ndo_validate_addr = eth_validate_addr,
  1341. };
  1342. static void hwsim_mon_setup(struct net_device *dev)
  1343. {
  1344. dev->netdev_ops = &hwsim_netdev_ops;
  1345. dev->destructor = free_netdev;
  1346. ether_setup(dev);
  1347. dev->tx_queue_len = 0;
  1348. dev->type = ARPHRD_IEEE80211_RADIOTAP;
  1349. memset(dev->dev_addr, 0, ETH_ALEN);
  1350. dev->dev_addr[0] = 0x12;
  1351. }
  1352. static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
  1353. {
  1354. struct mac80211_hwsim_data *data = dat;
  1355. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  1356. struct sk_buff *skb;
  1357. struct ieee80211_pspoll *pspoll;
  1358. if (!vp->assoc)
  1359. return;
  1360. wiphy_debug(data->hw->wiphy,
  1361. "%s: send PS-Poll to %pM for aid %d\n",
  1362. __func__, vp->bssid, vp->aid);
  1363. skb = dev_alloc_skb(sizeof(*pspoll));
  1364. if (!skb)
  1365. return;
  1366. pspoll = (void *) skb_put(skb, sizeof(*pspoll));
  1367. pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  1368. IEEE80211_STYPE_PSPOLL |
  1369. IEEE80211_FCTL_PM);
  1370. pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
  1371. memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
  1372. memcpy(pspoll->ta, mac, ETH_ALEN);
  1373. rcu_read_lock();
  1374. mac80211_hwsim_tx_frame(data->hw, skb,
  1375. rcu_dereference(vif->chanctx_conf)->def.chan);
  1376. rcu_read_unlock();
  1377. }
  1378. static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
  1379. struct ieee80211_vif *vif, int ps)
  1380. {
  1381. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  1382. struct sk_buff *skb;
  1383. struct ieee80211_hdr *hdr;
  1384. if (!vp->assoc)
  1385. return;
  1386. wiphy_debug(data->hw->wiphy,
  1387. "%s: send data::nullfunc to %pM ps=%d\n",
  1388. __func__, vp->bssid, ps);
  1389. skb = dev_alloc_skb(sizeof(*hdr));
  1390. if (!skb)
  1391. return;
  1392. hdr = (void *) skb_put(skb, sizeof(*hdr) - ETH_ALEN);
  1393. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  1394. IEEE80211_STYPE_NULLFUNC |
  1395. (ps ? IEEE80211_FCTL_PM : 0));
  1396. hdr->duration_id = cpu_to_le16(0);
  1397. memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
  1398. memcpy(hdr->addr2, mac, ETH_ALEN);
  1399. memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
  1400. rcu_read_lock();
  1401. mac80211_hwsim_tx_frame(data->hw, skb,
  1402. rcu_dereference(vif->chanctx_conf)->def.chan);
  1403. rcu_read_unlock();
  1404. }
  1405. static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
  1406. struct ieee80211_vif *vif)
  1407. {
  1408. struct mac80211_hwsim_data *data = dat;
  1409. hwsim_send_nullfunc(data, mac, vif, 1);
  1410. }
  1411. static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
  1412. struct ieee80211_vif *vif)
  1413. {
  1414. struct mac80211_hwsim_data *data = dat;
  1415. hwsim_send_nullfunc(data, mac, vif, 0);
  1416. }
  1417. static int hwsim_fops_ps_read(void *dat, u64 *val)
  1418. {
  1419. struct mac80211_hwsim_data *data = dat;
  1420. *val = data->ps;
  1421. return 0;
  1422. }
  1423. static int hwsim_fops_ps_write(void *dat, u64 val)
  1424. {
  1425. struct mac80211_hwsim_data *data = dat;
  1426. enum ps_mode old_ps;
  1427. if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
  1428. val != PS_MANUAL_POLL)
  1429. return -EINVAL;
  1430. old_ps = data->ps;
  1431. data->ps = val;
  1432. if (val == PS_MANUAL_POLL) {
  1433. ieee80211_iterate_active_interfaces(data->hw,
  1434. IEEE80211_IFACE_ITER_NORMAL,
  1435. hwsim_send_ps_poll, data);
  1436. data->ps_poll_pending = true;
  1437. } else if (old_ps == PS_DISABLED && val != PS_DISABLED) {
  1438. ieee80211_iterate_active_interfaces(data->hw,
  1439. IEEE80211_IFACE_ITER_NORMAL,
  1440. hwsim_send_nullfunc_ps,
  1441. data);
  1442. } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
  1443. ieee80211_iterate_active_interfaces(data->hw,
  1444. IEEE80211_IFACE_ITER_NORMAL,
  1445. hwsim_send_nullfunc_no_ps,
  1446. data);
  1447. }
  1448. return 0;
  1449. }
  1450. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
  1451. "%llu\n");
  1452. static int hwsim_fops_group_read(void *dat, u64 *val)
  1453. {
  1454. struct mac80211_hwsim_data *data = dat;
  1455. *val = data->group;
  1456. return 0;
  1457. }
  1458. static int hwsim_fops_group_write(void *dat, u64 val)
  1459. {
  1460. struct mac80211_hwsim_data *data = dat;
  1461. data->group = val;
  1462. return 0;
  1463. }
  1464. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
  1465. hwsim_fops_group_read, hwsim_fops_group_write,
  1466. "%llx\n");
  1467. static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(
  1468. struct mac_address *addr)
  1469. {
  1470. struct mac80211_hwsim_data *data;
  1471. bool _found = false;
  1472. spin_lock_bh(&hwsim_radio_lock);
  1473. list_for_each_entry(data, &hwsim_radios, list) {
  1474. if (memcmp(data->addresses[1].addr, addr,
  1475. sizeof(struct mac_address)) == 0) {
  1476. _found = true;
  1477. break;
  1478. }
  1479. }
  1480. spin_unlock_bh(&hwsim_radio_lock);
  1481. if (!_found)
  1482. return NULL;
  1483. return data;
  1484. }
  1485. static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
  1486. struct genl_info *info)
  1487. {
  1488. struct ieee80211_hdr *hdr;
  1489. struct mac80211_hwsim_data *data2;
  1490. struct ieee80211_tx_info *txi;
  1491. struct hwsim_tx_rate *tx_attempts;
  1492. unsigned long ret_skb_ptr;
  1493. struct sk_buff *skb, *tmp;
  1494. struct mac_address *src;
  1495. unsigned int hwsim_flags;
  1496. int i;
  1497. bool found = false;
  1498. if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
  1499. !info->attrs[HWSIM_ATTR_FLAGS] ||
  1500. !info->attrs[HWSIM_ATTR_COOKIE] ||
  1501. !info->attrs[HWSIM_ATTR_TX_INFO])
  1502. goto out;
  1503. src = (struct mac_address *)nla_data(
  1504. info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
  1505. hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
  1506. ret_skb_ptr = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
  1507. data2 = get_hwsim_data_ref_from_addr(src);
  1508. if (data2 == NULL)
  1509. goto out;
  1510. /* look for the skb matching the cookie passed back from user */
  1511. skb_queue_walk_safe(&data2->pending, skb, tmp) {
  1512. if ((unsigned long)skb == ret_skb_ptr) {
  1513. skb_unlink(skb, &data2->pending);
  1514. found = true;
  1515. break;
  1516. }
  1517. }
  1518. /* not found */
  1519. if (!found)
  1520. goto out;
  1521. /* Tx info received because the frame was broadcasted on user space,
  1522. so we get all the necessary info: tx attempts and skb control buff */
  1523. tx_attempts = (struct hwsim_tx_rate *)nla_data(
  1524. info->attrs[HWSIM_ATTR_TX_INFO]);
  1525. /* now send back TX status */
  1526. txi = IEEE80211_SKB_CB(skb);
  1527. ieee80211_tx_info_clear_status(txi);
  1528. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  1529. txi->status.rates[i].idx = tx_attempts[i].idx;
  1530. txi->status.rates[i].count = tx_attempts[i].count;
  1531. /*txi->status.rates[i].flags = 0;*/
  1532. }
  1533. txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
  1534. if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
  1535. (hwsim_flags & HWSIM_TX_STAT_ACK)) {
  1536. if (skb->len >= 16) {
  1537. hdr = (struct ieee80211_hdr *) skb->data;
  1538. mac80211_hwsim_monitor_ack(txi->rate_driver_data[0],
  1539. hdr->addr2);
  1540. }
  1541. txi->flags |= IEEE80211_TX_STAT_ACK;
  1542. }
  1543. ieee80211_tx_status_irqsafe(data2->hw, skb);
  1544. return 0;
  1545. out:
  1546. return -EINVAL;
  1547. }
  1548. static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
  1549. struct genl_info *info)
  1550. {
  1551. struct mac80211_hwsim_data *data2;
  1552. struct ieee80211_rx_status rx_status;
  1553. struct mac_address *dst;
  1554. int frame_data_len;
  1555. char *frame_data;
  1556. struct sk_buff *skb = NULL;
  1557. if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
  1558. !info->attrs[HWSIM_ATTR_FRAME] ||
  1559. !info->attrs[HWSIM_ATTR_RX_RATE] ||
  1560. !info->attrs[HWSIM_ATTR_SIGNAL])
  1561. goto out;
  1562. dst = (struct mac_address *)nla_data(
  1563. info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
  1564. frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
  1565. frame_data = (char *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
  1566. /* Allocate new skb here */
  1567. skb = alloc_skb(frame_data_len, GFP_KERNEL);
  1568. if (skb == NULL)
  1569. goto err;
  1570. if (frame_data_len <= IEEE80211_MAX_DATA_LEN) {
  1571. /* Copy the data */
  1572. memcpy(skb_put(skb, frame_data_len), frame_data,
  1573. frame_data_len);
  1574. } else
  1575. goto err;
  1576. data2 = get_hwsim_data_ref_from_addr(dst);
  1577. if (data2 == NULL)
  1578. goto out;
  1579. /* check if radio is configured properly */
  1580. if (data2->idle || !data2->started)
  1581. goto out;
  1582. /*A frame is received from user space*/
  1583. memset(&rx_status, 0, sizeof(rx_status));
  1584. rx_status.freq = data2->channel->center_freq;
  1585. rx_status.band = data2->channel->band;
  1586. rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
  1587. rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
  1588. memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
  1589. ieee80211_rx_irqsafe(data2->hw, skb);
  1590. return 0;
  1591. err:
  1592. printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
  1593. goto out;
  1594. out:
  1595. dev_kfree_skb(skb);
  1596. return -EINVAL;
  1597. }
  1598. static int hwsim_register_received_nl(struct sk_buff *skb_2,
  1599. struct genl_info *info)
  1600. {
  1601. if (info == NULL)
  1602. goto out;
  1603. wmediumd_portid = info->snd_portid;
  1604. printk(KERN_DEBUG "mac80211_hwsim: received a REGISTER, "
  1605. "switching to wmediumd mode with pid %d\n", info->snd_portid);
  1606. return 0;
  1607. out:
  1608. printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
  1609. return -EINVAL;
  1610. }
  1611. /* Generic Netlink operations array */
  1612. static struct genl_ops hwsim_ops[] = {
  1613. {
  1614. .cmd = HWSIM_CMD_REGISTER,
  1615. .policy = hwsim_genl_policy,
  1616. .doit = hwsim_register_received_nl,
  1617. .flags = GENL_ADMIN_PERM,
  1618. },
  1619. {
  1620. .cmd = HWSIM_CMD_FRAME,
  1621. .policy = hwsim_genl_policy,
  1622. .doit = hwsim_cloned_frame_received_nl,
  1623. },
  1624. {
  1625. .cmd = HWSIM_CMD_TX_INFO_FRAME,
  1626. .policy = hwsim_genl_policy,
  1627. .doit = hwsim_tx_info_frame_received_nl,
  1628. },
  1629. };
  1630. static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
  1631. unsigned long state,
  1632. void *_notify)
  1633. {
  1634. struct netlink_notify *notify = _notify;
  1635. if (state != NETLINK_URELEASE)
  1636. return NOTIFY_DONE;
  1637. if (notify->portid == wmediumd_portid) {
  1638. printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
  1639. " socket, switching to perfect channel medium\n");
  1640. wmediumd_portid = 0;
  1641. }
  1642. return NOTIFY_DONE;
  1643. }
  1644. static struct notifier_block hwsim_netlink_notifier = {
  1645. .notifier_call = mac80211_hwsim_netlink_notify,
  1646. };
  1647. static int hwsim_init_netlink(void)
  1648. {
  1649. int rc;
  1650. /* userspace test API hasn't been adjusted for multi-channel */
  1651. if (channels > 1)
  1652. return 0;
  1653. printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
  1654. rc = genl_register_family_with_ops(&hwsim_genl_family,
  1655. hwsim_ops, ARRAY_SIZE(hwsim_ops));
  1656. if (rc)
  1657. goto failure;
  1658. rc = netlink_register_notifier(&hwsim_netlink_notifier);
  1659. if (rc)
  1660. goto failure;
  1661. return 0;
  1662. failure:
  1663. printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
  1664. return -EINVAL;
  1665. }
  1666. static void hwsim_exit_netlink(void)
  1667. {
  1668. int ret;
  1669. /* userspace test API hasn't been adjusted for multi-channel */
  1670. if (channels > 1)
  1671. return;
  1672. printk(KERN_INFO "mac80211_hwsim: closing netlink\n");
  1673. /* unregister the notifier */
  1674. netlink_unregister_notifier(&hwsim_netlink_notifier);
  1675. /* unregister the family */
  1676. ret = genl_unregister_family(&hwsim_genl_family);
  1677. if (ret)
  1678. printk(KERN_DEBUG "mac80211_hwsim: "
  1679. "unregister family %i\n", ret);
  1680. }
  1681. static const struct ieee80211_iface_limit hwsim_if_limits[] = {
  1682. { .max = 1, .types = BIT(NL80211_IFTYPE_ADHOC) },
  1683. { .max = 2048, .types = BIT(NL80211_IFTYPE_STATION) |
  1684. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  1685. #ifdef CONFIG_MAC80211_MESH
  1686. BIT(NL80211_IFTYPE_MESH_POINT) |
  1687. #endif
  1688. BIT(NL80211_IFTYPE_AP) |
  1689. BIT(NL80211_IFTYPE_P2P_GO) },
  1690. { .max = 1, .types = BIT(NL80211_IFTYPE_P2P_DEVICE) },
  1691. };
  1692. static struct ieee80211_iface_combination hwsim_if_comb = {
  1693. .limits = hwsim_if_limits,
  1694. .n_limits = ARRAY_SIZE(hwsim_if_limits),
  1695. .max_interfaces = 2048,
  1696. .num_different_channels = 1,
  1697. };
  1698. static int __init init_mac80211_hwsim(void)
  1699. {
  1700. int i, err = 0;
  1701. u8 addr[ETH_ALEN];
  1702. struct mac80211_hwsim_data *data;
  1703. struct ieee80211_hw *hw;
  1704. enum ieee80211_band band;
  1705. if (radios < 1 || radios > 100)
  1706. return -EINVAL;
  1707. if (channels < 1)
  1708. return -EINVAL;
  1709. if (channels > 1) {
  1710. hwsim_if_comb.num_different_channels = channels;
  1711. mac80211_hwsim_ops.hw_scan = mac80211_hwsim_hw_scan;
  1712. mac80211_hwsim_ops.cancel_hw_scan =
  1713. mac80211_hwsim_cancel_hw_scan;
  1714. mac80211_hwsim_ops.sw_scan_start = NULL;
  1715. mac80211_hwsim_ops.sw_scan_complete = NULL;
  1716. mac80211_hwsim_ops.remain_on_channel =
  1717. mac80211_hwsim_roc;
  1718. mac80211_hwsim_ops.cancel_remain_on_channel =
  1719. mac80211_hwsim_croc;
  1720. mac80211_hwsim_ops.add_chanctx =
  1721. mac80211_hwsim_add_chanctx;
  1722. mac80211_hwsim_ops.remove_chanctx =
  1723. mac80211_hwsim_remove_chanctx;
  1724. mac80211_hwsim_ops.change_chanctx =
  1725. mac80211_hwsim_change_chanctx;
  1726. mac80211_hwsim_ops.assign_vif_chanctx =
  1727. mac80211_hwsim_assign_vif_chanctx;
  1728. mac80211_hwsim_ops.unassign_vif_chanctx =
  1729. mac80211_hwsim_unassign_vif_chanctx;
  1730. }
  1731. spin_lock_init(&hwsim_radio_lock);
  1732. INIT_LIST_HEAD(&hwsim_radios);
  1733. hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
  1734. if (IS_ERR(hwsim_class))
  1735. return PTR_ERR(hwsim_class);
  1736. memset(addr, 0, ETH_ALEN);
  1737. addr[0] = 0x02;
  1738. for (i = 0; i < radios; i++) {
  1739. printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
  1740. i);
  1741. hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
  1742. if (!hw) {
  1743. printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
  1744. "failed\n");
  1745. err = -ENOMEM;
  1746. goto failed;
  1747. }
  1748. data = hw->priv;
  1749. data->hw = hw;
  1750. data->dev = device_create(hwsim_class, NULL, 0, hw,
  1751. "hwsim%d", i);
  1752. if (IS_ERR(data->dev)) {
  1753. printk(KERN_DEBUG
  1754. "mac80211_hwsim: device_create "
  1755. "failed (%ld)\n", PTR_ERR(data->dev));
  1756. err = -ENOMEM;
  1757. goto failed_drvdata;
  1758. }
  1759. data->dev->driver = &mac80211_hwsim_driver;
  1760. skb_queue_head_init(&data->pending);
  1761. SET_IEEE80211_DEV(hw, data->dev);
  1762. addr[3] = i >> 8;
  1763. addr[4] = i;
  1764. memcpy(data->addresses[0].addr, addr, ETH_ALEN);
  1765. memcpy(data->addresses[1].addr, addr, ETH_ALEN);
  1766. data->addresses[1].addr[0] |= 0x40;
  1767. hw->wiphy->n_addresses = 2;
  1768. hw->wiphy->addresses = data->addresses;
  1769. hw->wiphy->iface_combinations = &hwsim_if_comb;
  1770. hw->wiphy->n_iface_combinations = 1;
  1771. if (channels > 1) {
  1772. hw->wiphy->max_scan_ssids = 255;
  1773. hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
  1774. hw->wiphy->max_remain_on_channel_duration = 1000;
  1775. }
  1776. INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
  1777. INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
  1778. hw->channel_change_time = 1;
  1779. hw->queues = 5;
  1780. hw->offchannel_tx_hw_queue = 4;
  1781. hw->wiphy->interface_modes =
  1782. BIT(NL80211_IFTYPE_STATION) |
  1783. BIT(NL80211_IFTYPE_AP) |
  1784. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  1785. BIT(NL80211_IFTYPE_P2P_GO) |
  1786. BIT(NL80211_IFTYPE_ADHOC) |
  1787. BIT(NL80211_IFTYPE_MESH_POINT) |
  1788. BIT(NL80211_IFTYPE_P2P_DEVICE);
  1789. hw->flags = IEEE80211_HW_MFP_CAPABLE |
  1790. IEEE80211_HW_SIGNAL_DBM |
  1791. IEEE80211_HW_SUPPORTS_STATIC_SMPS |
  1792. IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
  1793. IEEE80211_HW_AMPDU_AGGREGATION |
  1794. IEEE80211_HW_WANT_MONITOR_VIF |
  1795. IEEE80211_HW_QUEUE_CONTROL;
  1796. hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
  1797. WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
  1798. /* ask mac80211 to reserve space for magic */
  1799. hw->vif_data_size = sizeof(struct hwsim_vif_priv);
  1800. hw->sta_data_size = sizeof(struct hwsim_sta_priv);
  1801. memcpy(data->channels_2ghz, hwsim_channels_2ghz,
  1802. sizeof(hwsim_channels_2ghz));
  1803. memcpy(data->channels_5ghz, hwsim_channels_5ghz,
  1804. sizeof(hwsim_channels_5ghz));
  1805. memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
  1806. for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
  1807. struct ieee80211_supported_band *sband = &data->bands[band];
  1808. switch (band) {
  1809. case IEEE80211_BAND_2GHZ:
  1810. sband->channels = data->channels_2ghz;
  1811. sband->n_channels =
  1812. ARRAY_SIZE(hwsim_channels_2ghz);
  1813. sband->bitrates = data->rates;
  1814. sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
  1815. break;
  1816. case IEEE80211_BAND_5GHZ:
  1817. sband->channels = data->channels_5ghz;
  1818. sband->n_channels =
  1819. ARRAY_SIZE(hwsim_channels_5ghz);
  1820. sband->bitrates = data->rates + 4;
  1821. sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
  1822. break;
  1823. default:
  1824. continue;
  1825. }
  1826. sband->ht_cap.ht_supported = true;
  1827. sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  1828. IEEE80211_HT_CAP_GRN_FLD |
  1829. IEEE80211_HT_CAP_SGI_40 |
  1830. IEEE80211_HT_CAP_DSSSCCK40;
  1831. sband->ht_cap.ampdu_factor = 0x3;
  1832. sband->ht_cap.ampdu_density = 0x6;
  1833. memset(&sband->ht_cap.mcs, 0,
  1834. sizeof(sband->ht_cap.mcs));
  1835. sband->ht_cap.mcs.rx_mask[0] = 0xff;
  1836. sband->ht_cap.mcs.rx_mask[1] = 0xff;
  1837. sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  1838. hw->wiphy->bands[band] = sband;
  1839. if (channels == 1)
  1840. continue;
  1841. sband->vht_cap.vht_supported = true;
  1842. sband->vht_cap.cap =
  1843. IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454 |
  1844. IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ |
  1845. IEEE80211_VHT_CAP_RXLDPC |
  1846. IEEE80211_VHT_CAP_SHORT_GI_80 |
  1847. IEEE80211_VHT_CAP_SHORT_GI_160 |
  1848. IEEE80211_VHT_CAP_TXSTBC |
  1849. IEEE80211_VHT_CAP_RXSTBC_1 |
  1850. IEEE80211_VHT_CAP_RXSTBC_2 |
  1851. IEEE80211_VHT_CAP_RXSTBC_3 |
  1852. IEEE80211_VHT_CAP_RXSTBC_4 |
  1853. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
  1854. sband->vht_cap.vht_mcs.rx_mcs_map =
  1855. cpu_to_le16(IEEE80211_VHT_MCS_SUPPORT_0_8 << 0 |
  1856. IEEE80211_VHT_MCS_SUPPORT_0_8 << 2 |
  1857. IEEE80211_VHT_MCS_SUPPORT_0_9 << 4 |
  1858. IEEE80211_VHT_MCS_SUPPORT_0_8 << 6 |
  1859. IEEE80211_VHT_MCS_SUPPORT_0_8 << 8 |
  1860. IEEE80211_VHT_MCS_SUPPORT_0_9 << 10 |
  1861. IEEE80211_VHT_MCS_SUPPORT_0_9 << 12 |
  1862. IEEE80211_VHT_MCS_SUPPORT_0_8 << 14);
  1863. sband->vht_cap.vht_mcs.tx_mcs_map =
  1864. sband->vht_cap.vht_mcs.rx_mcs_map;
  1865. }
  1866. /* By default all radios are belonging to the first group */
  1867. data->group = 1;
  1868. mutex_init(&data->mutex);
  1869. /* Enable frame retransmissions for lossy channels */
  1870. hw->max_rates = 4;
  1871. hw->max_rate_tries = 11;
  1872. /* Work to be done prior to ieee80211_register_hw() */
  1873. switch (regtest) {
  1874. case HWSIM_REGTEST_DISABLED:
  1875. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  1876. case HWSIM_REGTEST_DRIVER_REG_ALL:
  1877. case HWSIM_REGTEST_DIFF_COUNTRY:
  1878. /*
  1879. * Nothing to be done for driver regulatory domain
  1880. * hints prior to ieee80211_register_hw()
  1881. */
  1882. break;
  1883. case HWSIM_REGTEST_WORLD_ROAM:
  1884. if (i == 0) {
  1885. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1886. wiphy_apply_custom_regulatory(hw->wiphy,
  1887. &hwsim_world_regdom_custom_01);
  1888. }
  1889. break;
  1890. case HWSIM_REGTEST_CUSTOM_WORLD:
  1891. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1892. wiphy_apply_custom_regulatory(hw->wiphy,
  1893. &hwsim_world_regdom_custom_01);
  1894. break;
  1895. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  1896. if (i == 0) {
  1897. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1898. wiphy_apply_custom_regulatory(hw->wiphy,
  1899. &hwsim_world_regdom_custom_01);
  1900. } else if (i == 1) {
  1901. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1902. wiphy_apply_custom_regulatory(hw->wiphy,
  1903. &hwsim_world_regdom_custom_02);
  1904. }
  1905. break;
  1906. case HWSIM_REGTEST_STRICT_ALL:
  1907. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1908. break;
  1909. case HWSIM_REGTEST_STRICT_FOLLOW:
  1910. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  1911. if (i == 0)
  1912. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1913. break;
  1914. case HWSIM_REGTEST_ALL:
  1915. if (i == 0) {
  1916. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1917. wiphy_apply_custom_regulatory(hw->wiphy,
  1918. &hwsim_world_regdom_custom_01);
  1919. } else if (i == 1) {
  1920. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1921. wiphy_apply_custom_regulatory(hw->wiphy,
  1922. &hwsim_world_regdom_custom_02);
  1923. } else if (i == 4)
  1924. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1925. break;
  1926. default:
  1927. break;
  1928. }
  1929. /* give the regulatory workqueue a chance to run */
  1930. if (regtest)
  1931. schedule_timeout_interruptible(1);
  1932. err = ieee80211_register_hw(hw);
  1933. if (err < 0) {
  1934. printk(KERN_DEBUG "mac80211_hwsim: "
  1935. "ieee80211_register_hw failed (%d)\n", err);
  1936. goto failed_hw;
  1937. }
  1938. /* Work to be done after to ieee80211_register_hw() */
  1939. switch (regtest) {
  1940. case HWSIM_REGTEST_WORLD_ROAM:
  1941. case HWSIM_REGTEST_DISABLED:
  1942. break;
  1943. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  1944. if (!i)
  1945. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1946. break;
  1947. case HWSIM_REGTEST_DRIVER_REG_ALL:
  1948. case HWSIM_REGTEST_STRICT_ALL:
  1949. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1950. break;
  1951. case HWSIM_REGTEST_DIFF_COUNTRY:
  1952. if (i < ARRAY_SIZE(hwsim_alpha2s))
  1953. regulatory_hint(hw->wiphy, hwsim_alpha2s[i]);
  1954. break;
  1955. case HWSIM_REGTEST_CUSTOM_WORLD:
  1956. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  1957. /*
  1958. * Nothing to be done for custom world regulatory
  1959. * domains after to ieee80211_register_hw
  1960. */
  1961. break;
  1962. case HWSIM_REGTEST_STRICT_FOLLOW:
  1963. if (i == 0)
  1964. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1965. break;
  1966. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  1967. if (i == 0)
  1968. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1969. else if (i == 1)
  1970. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  1971. break;
  1972. case HWSIM_REGTEST_ALL:
  1973. if (i == 2)
  1974. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1975. else if (i == 3)
  1976. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  1977. else if (i == 4)
  1978. regulatory_hint(hw->wiphy, hwsim_alpha2s[2]);
  1979. break;
  1980. default:
  1981. break;
  1982. }
  1983. wiphy_debug(hw->wiphy, "hwaddr %pm registered\n",
  1984. hw->wiphy->perm_addr);
  1985. data->debugfs = debugfs_create_dir("hwsim",
  1986. hw->wiphy->debugfsdir);
  1987. data->debugfs_ps = debugfs_create_file("ps", 0666,
  1988. data->debugfs, data,
  1989. &hwsim_fops_ps);
  1990. data->debugfs_group = debugfs_create_file("group", 0666,
  1991. data->debugfs, data,
  1992. &hwsim_fops_group);
  1993. setup_timer(&data->beacon_timer, mac80211_hwsim_beacon,
  1994. (unsigned long) hw);
  1995. list_add_tail(&data->list, &hwsim_radios);
  1996. }
  1997. hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
  1998. if (hwsim_mon == NULL)
  1999. goto failed;
  2000. rtnl_lock();
  2001. err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
  2002. if (err < 0)
  2003. goto failed_mon;
  2004. err = register_netdevice(hwsim_mon);
  2005. if (err < 0)
  2006. goto failed_mon;
  2007. rtnl_unlock();
  2008. err = hwsim_init_netlink();
  2009. if (err < 0)
  2010. goto failed_nl;
  2011. return 0;
  2012. failed_nl:
  2013. printk(KERN_DEBUG "mac_80211_hwsim: failed initializing netlink\n");
  2014. return err;
  2015. failed_mon:
  2016. rtnl_unlock();
  2017. free_netdev(hwsim_mon);
  2018. mac80211_hwsim_free();
  2019. return err;
  2020. failed_hw:
  2021. device_unregister(data->dev);
  2022. failed_drvdata:
  2023. ieee80211_free_hw(hw);
  2024. failed:
  2025. mac80211_hwsim_free();
  2026. return err;
  2027. }
  2028. module_init(init_mac80211_hwsim);
  2029. static void __exit exit_mac80211_hwsim(void)
  2030. {
  2031. printk(KERN_DEBUG "mac80211_hwsim: unregister radios\n");
  2032. hwsim_exit_netlink();
  2033. mac80211_hwsim_free();
  2034. unregister_netdev(hwsim_mon);
  2035. }
  2036. module_exit(exit_mac80211_hwsim);