mac80211_hwsim.c 68 KB

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