mac80211_hwsim.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368
  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)->channel))
  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_MPDU;
  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. nskb = skb_copy(skb, GFP_ATOMIC);
  644. if (nskb == NULL)
  645. continue;
  646. if (mac80211_hwsim_addr_match(data2, hdr->addr1))
  647. ack = true;
  648. /* set bcn timestamp relative to receiver mactime */
  649. rx_status.mactime =
  650. le64_to_cpu(__mac80211_hwsim_get_tsf(data2));
  651. mgmt = (struct ieee80211_mgmt *) nskb->data;
  652. if (ieee80211_is_beacon(mgmt->frame_control) ||
  653. ieee80211_is_probe_resp(mgmt->frame_control))
  654. mgmt->u.beacon.timestamp = cpu_to_le64(
  655. rx_status.mactime +
  656. (data->tsf_offset - data2->tsf_offset) +
  657. 24 * 8 * 10 / txrate->bitrate);
  658. memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
  659. ieee80211_rx_irqsafe(data2->hw, nskb);
  660. }
  661. spin_unlock(&hwsim_radio_lock);
  662. return ack;
  663. }
  664. static void mac80211_hwsim_tx(struct ieee80211_hw *hw,
  665. struct ieee80211_tx_control *control,
  666. struct sk_buff *skb)
  667. {
  668. struct mac80211_hwsim_data *data = hw->priv;
  669. struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb);
  670. struct ieee80211_chanctx_conf *chanctx_conf;
  671. struct ieee80211_channel *channel;
  672. bool ack;
  673. u32 _portid;
  674. if (WARN_ON(skb->len < 10)) {
  675. /* Should not happen; just a sanity check for addr1 use */
  676. dev_kfree_skb(skb);
  677. return;
  678. }
  679. if (channels == 1) {
  680. channel = data->channel;
  681. } else if (txi->hw_queue == 4) {
  682. channel = data->tmp_chan;
  683. } else {
  684. chanctx_conf = rcu_dereference(txi->control.vif->chanctx_conf);
  685. if (chanctx_conf)
  686. channel = chanctx_conf->channel;
  687. else
  688. channel = NULL;
  689. }
  690. if (WARN(!channel, "TX w/o channel - queue = %d\n", txi->hw_queue)) {
  691. dev_kfree_skb(skb);
  692. return;
  693. }
  694. if (data->idle && !data->tmp_chan) {
  695. wiphy_debug(hw->wiphy, "Trying to TX when idle - reject\n");
  696. dev_kfree_skb(skb);
  697. return;
  698. }
  699. if (txi->control.vif)
  700. hwsim_check_magic(txi->control.vif);
  701. if (control->sta)
  702. hwsim_check_sta_magic(control->sta);
  703. txi->rate_driver_data[0] = channel;
  704. mac80211_hwsim_monitor_rx(hw, skb, channel);
  705. /* wmediumd mode check */
  706. _portid = ACCESS_ONCE(wmediumd_portid);
  707. if (_portid)
  708. return mac80211_hwsim_tx_frame_nl(hw, skb, _portid);
  709. /* NO wmediumd detected, perfect medium simulation */
  710. ack = mac80211_hwsim_tx_frame_no_nl(hw, skb, channel);
  711. if (ack && skb->len >= 16) {
  712. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  713. mac80211_hwsim_monitor_ack(channel, hdr->addr2);
  714. }
  715. ieee80211_tx_info_clear_status(txi);
  716. /* frame was transmitted at most favorable rate at first attempt */
  717. txi->control.rates[0].count = 1;
  718. txi->control.rates[1].idx = -1;
  719. if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
  720. txi->flags |= IEEE80211_TX_STAT_ACK;
  721. ieee80211_tx_status_irqsafe(hw, skb);
  722. }
  723. static int mac80211_hwsim_start(struct ieee80211_hw *hw)
  724. {
  725. struct mac80211_hwsim_data *data = hw->priv;
  726. wiphy_debug(hw->wiphy, "%s\n", __func__);
  727. data->started = true;
  728. return 0;
  729. }
  730. static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
  731. {
  732. struct mac80211_hwsim_data *data = hw->priv;
  733. data->started = false;
  734. del_timer(&data->beacon_timer);
  735. wiphy_debug(hw->wiphy, "%s\n", __func__);
  736. }
  737. static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
  738. struct ieee80211_vif *vif)
  739. {
  740. wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
  741. __func__, ieee80211_vif_type_p2p(vif),
  742. vif->addr);
  743. hwsim_set_magic(vif);
  744. vif->cab_queue = 0;
  745. vif->hw_queue[IEEE80211_AC_VO] = 0;
  746. vif->hw_queue[IEEE80211_AC_VI] = 1;
  747. vif->hw_queue[IEEE80211_AC_BE] = 2;
  748. vif->hw_queue[IEEE80211_AC_BK] = 3;
  749. return 0;
  750. }
  751. static int mac80211_hwsim_change_interface(struct ieee80211_hw *hw,
  752. struct ieee80211_vif *vif,
  753. enum nl80211_iftype newtype,
  754. bool newp2p)
  755. {
  756. newtype = ieee80211_iftype_p2p(newtype, newp2p);
  757. wiphy_debug(hw->wiphy,
  758. "%s (old type=%d, new type=%d, mac_addr=%pM)\n",
  759. __func__, ieee80211_vif_type_p2p(vif),
  760. newtype, vif->addr);
  761. hwsim_check_magic(vif);
  762. return 0;
  763. }
  764. static void mac80211_hwsim_remove_interface(
  765. struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  766. {
  767. wiphy_debug(hw->wiphy, "%s (type=%d mac_addr=%pM)\n",
  768. __func__, ieee80211_vif_type_p2p(vif),
  769. vif->addr);
  770. hwsim_check_magic(vif);
  771. hwsim_clear_magic(vif);
  772. }
  773. static void mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
  774. struct sk_buff *skb,
  775. struct ieee80211_channel *chan)
  776. {
  777. u32 _pid = ACCESS_ONCE(wmediumd_portid);
  778. mac80211_hwsim_monitor_rx(hw, skb, chan);
  779. if (_pid)
  780. return mac80211_hwsim_tx_frame_nl(hw, skb, _pid);
  781. mac80211_hwsim_tx_frame_no_nl(hw, skb, chan);
  782. dev_kfree_skb(skb);
  783. }
  784. static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
  785. struct ieee80211_vif *vif)
  786. {
  787. struct ieee80211_hw *hw = arg;
  788. struct sk_buff *skb;
  789. hwsim_check_magic(vif);
  790. if (vif->type != NL80211_IFTYPE_AP &&
  791. vif->type != NL80211_IFTYPE_MESH_POINT &&
  792. vif->type != NL80211_IFTYPE_ADHOC)
  793. return;
  794. skb = ieee80211_beacon_get(hw, vif);
  795. if (skb == NULL)
  796. return;
  797. mac80211_hwsim_tx_frame(hw, skb,
  798. rcu_dereference(vif->chanctx_conf)->channel);
  799. }
  800. static void mac80211_hwsim_beacon(unsigned long arg)
  801. {
  802. struct ieee80211_hw *hw = (struct ieee80211_hw *) arg;
  803. struct mac80211_hwsim_data *data = hw->priv;
  804. if (!data->started)
  805. return;
  806. ieee80211_iterate_active_interfaces_atomic(
  807. hw, IEEE80211_IFACE_ITER_NORMAL,
  808. mac80211_hwsim_beacon_tx, hw);
  809. data->beacon_timer.expires = jiffies + data->beacon_int;
  810. add_timer(&data->beacon_timer);
  811. }
  812. static const char *hwsim_chantypes[] = {
  813. [NL80211_CHAN_NO_HT] = "noht",
  814. [NL80211_CHAN_HT20] = "ht20",
  815. [NL80211_CHAN_HT40MINUS] = "ht40-",
  816. [NL80211_CHAN_HT40PLUS] = "ht40+",
  817. };
  818. static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
  819. {
  820. struct mac80211_hwsim_data *data = hw->priv;
  821. struct ieee80211_conf *conf = &hw->conf;
  822. static const char *smps_modes[IEEE80211_SMPS_NUM_MODES] = {
  823. [IEEE80211_SMPS_AUTOMATIC] = "auto",
  824. [IEEE80211_SMPS_OFF] = "off",
  825. [IEEE80211_SMPS_STATIC] = "static",
  826. [IEEE80211_SMPS_DYNAMIC] = "dynamic",
  827. };
  828. wiphy_debug(hw->wiphy,
  829. "%s (freq=%d/%s idle=%d ps=%d smps=%s)\n",
  830. __func__,
  831. conf->channel ? conf->channel->center_freq : 0,
  832. hwsim_chantypes[conf->channel_type],
  833. !!(conf->flags & IEEE80211_CONF_IDLE),
  834. !!(conf->flags & IEEE80211_CONF_PS),
  835. smps_modes[conf->smps_mode]);
  836. data->idle = !!(conf->flags & IEEE80211_CONF_IDLE);
  837. data->channel = conf->channel;
  838. WARN_ON(data->channel && channels > 1);
  839. data->power_level = conf->power_level;
  840. if (!data->started || !data->beacon_int)
  841. del_timer(&data->beacon_timer);
  842. else
  843. mod_timer(&data->beacon_timer, jiffies + data->beacon_int);
  844. return 0;
  845. }
  846. static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
  847. unsigned int changed_flags,
  848. unsigned int *total_flags,u64 multicast)
  849. {
  850. struct mac80211_hwsim_data *data = hw->priv;
  851. wiphy_debug(hw->wiphy, "%s\n", __func__);
  852. data->rx_filter = 0;
  853. if (*total_flags & FIF_PROMISC_IN_BSS)
  854. data->rx_filter |= FIF_PROMISC_IN_BSS;
  855. if (*total_flags & FIF_ALLMULTI)
  856. data->rx_filter |= FIF_ALLMULTI;
  857. *total_flags = data->rx_filter;
  858. }
  859. static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
  860. struct ieee80211_vif *vif,
  861. struct ieee80211_bss_conf *info,
  862. u32 changed)
  863. {
  864. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  865. struct mac80211_hwsim_data *data = hw->priv;
  866. hwsim_check_magic(vif);
  867. wiphy_debug(hw->wiphy, "%s(changed=0x%x)\n", __func__, changed);
  868. if (changed & BSS_CHANGED_BSSID) {
  869. wiphy_debug(hw->wiphy, "%s: BSSID changed: %pM\n",
  870. __func__, info->bssid);
  871. memcpy(vp->bssid, info->bssid, ETH_ALEN);
  872. }
  873. if (changed & BSS_CHANGED_ASSOC) {
  874. wiphy_debug(hw->wiphy, " ASSOC: assoc=%d aid=%d\n",
  875. info->assoc, info->aid);
  876. vp->assoc = info->assoc;
  877. vp->aid = info->aid;
  878. }
  879. if (changed & BSS_CHANGED_BEACON_INT) {
  880. wiphy_debug(hw->wiphy, " BCNINT: %d\n", info->beacon_int);
  881. data->beacon_int = 1024 * info->beacon_int / 1000 * HZ / 1000;
  882. if (WARN_ON(!data->beacon_int))
  883. data->beacon_int = 1;
  884. if (data->started)
  885. mod_timer(&data->beacon_timer,
  886. jiffies + data->beacon_int);
  887. }
  888. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  889. wiphy_debug(hw->wiphy, " ERP_CTS_PROT: %d\n",
  890. info->use_cts_prot);
  891. }
  892. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  893. wiphy_debug(hw->wiphy, " ERP_PREAMBLE: %d\n",
  894. info->use_short_preamble);
  895. }
  896. if (changed & BSS_CHANGED_ERP_SLOT) {
  897. wiphy_debug(hw->wiphy, " ERP_SLOT: %d\n", info->use_short_slot);
  898. }
  899. if (changed & BSS_CHANGED_HT) {
  900. wiphy_debug(hw->wiphy, " HT: op_mode=0x%x, chantype=%s\n",
  901. info->ht_operation_mode,
  902. hwsim_chantypes[info->channel_type]);
  903. }
  904. if (changed & BSS_CHANGED_BASIC_RATES) {
  905. wiphy_debug(hw->wiphy, " BASIC_RATES: 0x%llx\n",
  906. (unsigned long long) info->basic_rates);
  907. }
  908. if (changed & BSS_CHANGED_TXPOWER)
  909. wiphy_debug(hw->wiphy, " TX Power: %d dBm\n", info->txpower);
  910. }
  911. static int mac80211_hwsim_sta_add(struct ieee80211_hw *hw,
  912. struct ieee80211_vif *vif,
  913. struct ieee80211_sta *sta)
  914. {
  915. hwsim_check_magic(vif);
  916. hwsim_set_sta_magic(sta);
  917. return 0;
  918. }
  919. static int mac80211_hwsim_sta_remove(struct ieee80211_hw *hw,
  920. struct ieee80211_vif *vif,
  921. struct ieee80211_sta *sta)
  922. {
  923. hwsim_check_magic(vif);
  924. hwsim_clear_sta_magic(sta);
  925. return 0;
  926. }
  927. static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
  928. struct ieee80211_vif *vif,
  929. enum sta_notify_cmd cmd,
  930. struct ieee80211_sta *sta)
  931. {
  932. hwsim_check_magic(vif);
  933. switch (cmd) {
  934. case STA_NOTIFY_SLEEP:
  935. case STA_NOTIFY_AWAKE:
  936. /* TODO: make good use of these flags */
  937. break;
  938. default:
  939. WARN(1, "Invalid sta notify: %d\n", cmd);
  940. break;
  941. }
  942. }
  943. static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
  944. struct ieee80211_sta *sta,
  945. bool set)
  946. {
  947. hwsim_check_sta_magic(sta);
  948. return 0;
  949. }
  950. static int mac80211_hwsim_conf_tx(
  951. struct ieee80211_hw *hw,
  952. struct ieee80211_vif *vif, u16 queue,
  953. const struct ieee80211_tx_queue_params *params)
  954. {
  955. wiphy_debug(hw->wiphy,
  956. "%s (queue=%d txop=%d cw_min=%d cw_max=%d aifs=%d)\n",
  957. __func__, queue,
  958. params->txop, params->cw_min,
  959. params->cw_max, params->aifs);
  960. return 0;
  961. }
  962. static int mac80211_hwsim_get_survey(
  963. struct ieee80211_hw *hw, int idx,
  964. struct survey_info *survey)
  965. {
  966. struct ieee80211_conf *conf = &hw->conf;
  967. wiphy_debug(hw->wiphy, "%s (idx=%d)\n", __func__, idx);
  968. if (idx != 0)
  969. return -ENOENT;
  970. /* Current channel */
  971. survey->channel = conf->channel;
  972. /*
  973. * Magically conjured noise level --- this is only ok for simulated hardware.
  974. *
  975. * A real driver which cannot determine the real channel noise MUST NOT
  976. * report any noise, especially not a magically conjured one :-)
  977. */
  978. survey->filled = SURVEY_INFO_NOISE_DBM;
  979. survey->noise = -92;
  980. return 0;
  981. }
  982. #ifdef CONFIG_NL80211_TESTMODE
  983. /*
  984. * This section contains example code for using netlink
  985. * attributes with the testmode command in nl80211.
  986. */
  987. /* These enums need to be kept in sync with userspace */
  988. enum hwsim_testmode_attr {
  989. __HWSIM_TM_ATTR_INVALID = 0,
  990. HWSIM_TM_ATTR_CMD = 1,
  991. HWSIM_TM_ATTR_PS = 2,
  992. /* keep last */
  993. __HWSIM_TM_ATTR_AFTER_LAST,
  994. HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
  995. };
  996. enum hwsim_testmode_cmd {
  997. HWSIM_TM_CMD_SET_PS = 0,
  998. HWSIM_TM_CMD_GET_PS = 1,
  999. HWSIM_TM_CMD_STOP_QUEUES = 2,
  1000. HWSIM_TM_CMD_WAKE_QUEUES = 3,
  1001. };
  1002. static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
  1003. [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
  1004. [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
  1005. };
  1006. static int hwsim_fops_ps_write(void *dat, u64 val);
  1007. static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
  1008. void *data, int len)
  1009. {
  1010. struct mac80211_hwsim_data *hwsim = hw->priv;
  1011. struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
  1012. struct sk_buff *skb;
  1013. int err, ps;
  1014. err = nla_parse(tb, HWSIM_TM_ATTR_MAX, data, len,
  1015. hwsim_testmode_policy);
  1016. if (err)
  1017. return err;
  1018. if (!tb[HWSIM_TM_ATTR_CMD])
  1019. return -EINVAL;
  1020. switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
  1021. case HWSIM_TM_CMD_SET_PS:
  1022. if (!tb[HWSIM_TM_ATTR_PS])
  1023. return -EINVAL;
  1024. ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
  1025. return hwsim_fops_ps_write(hwsim, ps);
  1026. case HWSIM_TM_CMD_GET_PS:
  1027. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
  1028. nla_total_size(sizeof(u32)));
  1029. if (!skb)
  1030. return -ENOMEM;
  1031. if (nla_put_u32(skb, HWSIM_TM_ATTR_PS, hwsim->ps))
  1032. goto nla_put_failure;
  1033. return cfg80211_testmode_reply(skb);
  1034. case HWSIM_TM_CMD_STOP_QUEUES:
  1035. ieee80211_stop_queues(hw);
  1036. return 0;
  1037. case HWSIM_TM_CMD_WAKE_QUEUES:
  1038. ieee80211_wake_queues(hw);
  1039. return 0;
  1040. default:
  1041. return -EOPNOTSUPP;
  1042. }
  1043. nla_put_failure:
  1044. kfree_skb(skb);
  1045. return -ENOBUFS;
  1046. }
  1047. #endif
  1048. static int mac80211_hwsim_ampdu_action(struct ieee80211_hw *hw,
  1049. struct ieee80211_vif *vif,
  1050. enum ieee80211_ampdu_mlme_action action,
  1051. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  1052. u8 buf_size)
  1053. {
  1054. switch (action) {
  1055. case IEEE80211_AMPDU_TX_START:
  1056. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1057. break;
  1058. case IEEE80211_AMPDU_TX_STOP:
  1059. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1060. break;
  1061. case IEEE80211_AMPDU_TX_OPERATIONAL:
  1062. break;
  1063. case IEEE80211_AMPDU_RX_START:
  1064. case IEEE80211_AMPDU_RX_STOP:
  1065. break;
  1066. default:
  1067. return -EOPNOTSUPP;
  1068. }
  1069. return 0;
  1070. }
  1071. static void mac80211_hwsim_flush(struct ieee80211_hw *hw, bool drop)
  1072. {
  1073. /* Not implemented, queues only on kernel side */
  1074. }
  1075. static void hw_scan_work(struct work_struct *work)
  1076. {
  1077. struct mac80211_hwsim_data *hwsim =
  1078. container_of(work, struct mac80211_hwsim_data, hw_scan.work);
  1079. struct cfg80211_scan_request *req = hwsim->hw_scan_request;
  1080. int dwell, i;
  1081. mutex_lock(&hwsim->mutex);
  1082. if (hwsim->scan_chan_idx >= req->n_channels) {
  1083. wiphy_debug(hwsim->hw->wiphy, "hw scan complete\n");
  1084. ieee80211_scan_completed(hwsim->hw, false);
  1085. hwsim->hw_scan_request = NULL;
  1086. hwsim->hw_scan_vif = NULL;
  1087. hwsim->tmp_chan = NULL;
  1088. mutex_unlock(&hwsim->mutex);
  1089. return;
  1090. }
  1091. wiphy_debug(hwsim->hw->wiphy, "hw scan %d MHz\n",
  1092. req->channels[hwsim->scan_chan_idx]->center_freq);
  1093. hwsim->tmp_chan = req->channels[hwsim->scan_chan_idx];
  1094. if (hwsim->tmp_chan->flags & IEEE80211_CHAN_PASSIVE_SCAN ||
  1095. !req->n_ssids) {
  1096. dwell = 120;
  1097. } else {
  1098. dwell = 30;
  1099. /* send probes */
  1100. for (i = 0; i < req->n_ssids; i++) {
  1101. struct sk_buff *probe;
  1102. probe = ieee80211_probereq_get(hwsim->hw,
  1103. hwsim->hw_scan_vif,
  1104. req->ssids[i].ssid,
  1105. req->ssids[i].ssid_len,
  1106. req->ie, req->ie_len);
  1107. if (!probe)
  1108. continue;
  1109. local_bh_disable();
  1110. mac80211_hwsim_tx_frame(hwsim->hw, probe,
  1111. hwsim->tmp_chan);
  1112. local_bh_enable();
  1113. }
  1114. }
  1115. ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan,
  1116. msecs_to_jiffies(dwell));
  1117. hwsim->scan_chan_idx++;
  1118. mutex_unlock(&hwsim->mutex);
  1119. }
  1120. static int mac80211_hwsim_hw_scan(struct ieee80211_hw *hw,
  1121. struct ieee80211_vif *vif,
  1122. struct cfg80211_scan_request *req)
  1123. {
  1124. struct mac80211_hwsim_data *hwsim = hw->priv;
  1125. int i;
  1126. mutex_lock(&hwsim->mutex);
  1127. if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
  1128. mutex_unlock(&hwsim->mutex);
  1129. return -EBUSY;
  1130. }
  1131. hwsim->hw_scan_request = req;
  1132. hwsim->hw_scan_vif = vif;
  1133. hwsim->scan_chan_idx = 0;
  1134. mutex_unlock(&hwsim->mutex);
  1135. wiphy_debug(hw->wiphy, "hwsim hw_scan request\n");
  1136. for (i = 0; i < req->n_channels; i++)
  1137. printk(KERN_DEBUG "hwsim hw_scan freq %d\n",
  1138. req->channels[i]->center_freq);
  1139. print_hex_dump(KERN_DEBUG, "scan IEs: ", DUMP_PREFIX_OFFSET,
  1140. 16, 1, req->ie, req->ie_len, 1);
  1141. ieee80211_queue_delayed_work(hwsim->hw, &hwsim->hw_scan, 0);
  1142. return 0;
  1143. }
  1144. static void mac80211_hwsim_cancel_hw_scan(struct ieee80211_hw *hw,
  1145. struct ieee80211_vif *vif)
  1146. {
  1147. struct mac80211_hwsim_data *hwsim = hw->priv;
  1148. wiphy_debug(hw->wiphy, "hwsim cancel_hw_scan\n");
  1149. cancel_delayed_work_sync(&hwsim->hw_scan);
  1150. mutex_lock(&hwsim->mutex);
  1151. ieee80211_scan_completed(hwsim->hw, true);
  1152. hwsim->tmp_chan = NULL;
  1153. hwsim->hw_scan_request = NULL;
  1154. hwsim->hw_scan_vif = NULL;
  1155. mutex_unlock(&hwsim->mutex);
  1156. }
  1157. static void mac80211_hwsim_sw_scan(struct ieee80211_hw *hw)
  1158. {
  1159. struct mac80211_hwsim_data *hwsim = hw->priv;
  1160. mutex_lock(&hwsim->mutex);
  1161. if (hwsim->scanning) {
  1162. printk(KERN_DEBUG "two hwsim sw_scans detected!\n");
  1163. goto out;
  1164. }
  1165. printk(KERN_DEBUG "hwsim sw_scan request, prepping stuff\n");
  1166. hwsim->scanning = true;
  1167. out:
  1168. mutex_unlock(&hwsim->mutex);
  1169. }
  1170. static void mac80211_hwsim_sw_scan_complete(struct ieee80211_hw *hw)
  1171. {
  1172. struct mac80211_hwsim_data *hwsim = hw->priv;
  1173. mutex_lock(&hwsim->mutex);
  1174. printk(KERN_DEBUG "hwsim sw_scan_complete\n");
  1175. hwsim->scanning = false;
  1176. mutex_unlock(&hwsim->mutex);
  1177. }
  1178. static void hw_roc_done(struct work_struct *work)
  1179. {
  1180. struct mac80211_hwsim_data *hwsim =
  1181. container_of(work, struct mac80211_hwsim_data, roc_done.work);
  1182. mutex_lock(&hwsim->mutex);
  1183. ieee80211_remain_on_channel_expired(hwsim->hw);
  1184. hwsim->tmp_chan = NULL;
  1185. mutex_unlock(&hwsim->mutex);
  1186. wiphy_debug(hwsim->hw->wiphy, "hwsim ROC expired\n");
  1187. }
  1188. static int mac80211_hwsim_roc(struct ieee80211_hw *hw,
  1189. struct ieee80211_channel *chan,
  1190. enum nl80211_channel_type channel_type,
  1191. int duration)
  1192. {
  1193. struct mac80211_hwsim_data *hwsim = hw->priv;
  1194. mutex_lock(&hwsim->mutex);
  1195. if (WARN_ON(hwsim->tmp_chan || hwsim->hw_scan_request)) {
  1196. mutex_unlock(&hwsim->mutex);
  1197. return -EBUSY;
  1198. }
  1199. hwsim->tmp_chan = chan;
  1200. mutex_unlock(&hwsim->mutex);
  1201. wiphy_debug(hw->wiphy, "hwsim ROC (%d MHz, %d ms)\n",
  1202. chan->center_freq, duration);
  1203. ieee80211_ready_on_channel(hw);
  1204. ieee80211_queue_delayed_work(hw, &hwsim->roc_done,
  1205. msecs_to_jiffies(duration));
  1206. return 0;
  1207. }
  1208. static int mac80211_hwsim_croc(struct ieee80211_hw *hw)
  1209. {
  1210. struct mac80211_hwsim_data *hwsim = hw->priv;
  1211. cancel_delayed_work_sync(&hwsim->roc_done);
  1212. mutex_lock(&hwsim->mutex);
  1213. hwsim->tmp_chan = NULL;
  1214. mutex_unlock(&hwsim->mutex);
  1215. wiphy_debug(hw->wiphy, "hwsim ROC canceled\n");
  1216. return 0;
  1217. }
  1218. static int mac80211_hwsim_add_chanctx(struct ieee80211_hw *hw,
  1219. struct ieee80211_chanctx_conf *ctx)
  1220. {
  1221. hwsim_set_chanctx_magic(ctx);
  1222. wiphy_debug(hw->wiphy, "add channel context %d MHz/%d\n",
  1223. ctx->channel->center_freq, ctx->channel_type);
  1224. return 0;
  1225. }
  1226. static void mac80211_hwsim_remove_chanctx(struct ieee80211_hw *hw,
  1227. struct ieee80211_chanctx_conf *ctx)
  1228. {
  1229. wiphy_debug(hw->wiphy, "remove channel context %d MHz/%d\n",
  1230. ctx->channel->center_freq, ctx->channel_type);
  1231. hwsim_check_chanctx_magic(ctx);
  1232. hwsim_clear_chanctx_magic(ctx);
  1233. }
  1234. static void mac80211_hwsim_change_chanctx(struct ieee80211_hw *hw,
  1235. struct ieee80211_chanctx_conf *ctx,
  1236. u32 changed)
  1237. {
  1238. hwsim_check_chanctx_magic(ctx);
  1239. wiphy_debug(hw->wiphy, "change channel context %#x (%d MHz/%d)\n",
  1240. changed, ctx->channel->center_freq, ctx->channel_type);
  1241. }
  1242. static int mac80211_hwsim_assign_vif_chanctx(struct ieee80211_hw *hw,
  1243. struct ieee80211_vif *vif,
  1244. struct ieee80211_chanctx_conf *ctx)
  1245. {
  1246. hwsim_check_magic(vif);
  1247. hwsim_check_chanctx_magic(ctx);
  1248. return 0;
  1249. }
  1250. static void mac80211_hwsim_unassign_vif_chanctx(struct ieee80211_hw *hw,
  1251. struct ieee80211_vif *vif,
  1252. struct ieee80211_chanctx_conf *ctx)
  1253. {
  1254. hwsim_check_magic(vif);
  1255. hwsim_check_chanctx_magic(ctx);
  1256. }
  1257. static struct ieee80211_ops mac80211_hwsim_ops =
  1258. {
  1259. .tx = mac80211_hwsim_tx,
  1260. .start = mac80211_hwsim_start,
  1261. .stop = mac80211_hwsim_stop,
  1262. .add_interface = mac80211_hwsim_add_interface,
  1263. .change_interface = mac80211_hwsim_change_interface,
  1264. .remove_interface = mac80211_hwsim_remove_interface,
  1265. .config = mac80211_hwsim_config,
  1266. .configure_filter = mac80211_hwsim_configure_filter,
  1267. .bss_info_changed = mac80211_hwsim_bss_info_changed,
  1268. .sta_add = mac80211_hwsim_sta_add,
  1269. .sta_remove = mac80211_hwsim_sta_remove,
  1270. .sta_notify = mac80211_hwsim_sta_notify,
  1271. .set_tim = mac80211_hwsim_set_tim,
  1272. .conf_tx = mac80211_hwsim_conf_tx,
  1273. .get_survey = mac80211_hwsim_get_survey,
  1274. CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd)
  1275. .ampdu_action = mac80211_hwsim_ampdu_action,
  1276. .sw_scan_start = mac80211_hwsim_sw_scan,
  1277. .sw_scan_complete = mac80211_hwsim_sw_scan_complete,
  1278. .flush = mac80211_hwsim_flush,
  1279. .get_tsf = mac80211_hwsim_get_tsf,
  1280. .set_tsf = mac80211_hwsim_set_tsf,
  1281. };
  1282. static void mac80211_hwsim_free(void)
  1283. {
  1284. struct list_head tmplist, *i, *tmp;
  1285. struct mac80211_hwsim_data *data, *tmpdata;
  1286. INIT_LIST_HEAD(&tmplist);
  1287. spin_lock_bh(&hwsim_radio_lock);
  1288. list_for_each_safe(i, tmp, &hwsim_radios)
  1289. list_move(i, &tmplist);
  1290. spin_unlock_bh(&hwsim_radio_lock);
  1291. list_for_each_entry_safe(data, tmpdata, &tmplist, list) {
  1292. debugfs_remove(data->debugfs_group);
  1293. debugfs_remove(data->debugfs_ps);
  1294. debugfs_remove(data->debugfs);
  1295. ieee80211_unregister_hw(data->hw);
  1296. device_unregister(data->dev);
  1297. ieee80211_free_hw(data->hw);
  1298. }
  1299. class_destroy(hwsim_class);
  1300. }
  1301. static struct device_driver mac80211_hwsim_driver = {
  1302. .name = "mac80211_hwsim"
  1303. };
  1304. static const struct net_device_ops hwsim_netdev_ops = {
  1305. .ndo_start_xmit = hwsim_mon_xmit,
  1306. .ndo_change_mtu = eth_change_mtu,
  1307. .ndo_set_mac_address = eth_mac_addr,
  1308. .ndo_validate_addr = eth_validate_addr,
  1309. };
  1310. static void hwsim_mon_setup(struct net_device *dev)
  1311. {
  1312. dev->netdev_ops = &hwsim_netdev_ops;
  1313. dev->destructor = free_netdev;
  1314. ether_setup(dev);
  1315. dev->tx_queue_len = 0;
  1316. dev->type = ARPHRD_IEEE80211_RADIOTAP;
  1317. memset(dev->dev_addr, 0, ETH_ALEN);
  1318. dev->dev_addr[0] = 0x12;
  1319. }
  1320. static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
  1321. {
  1322. struct mac80211_hwsim_data *data = dat;
  1323. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  1324. struct sk_buff *skb;
  1325. struct ieee80211_pspoll *pspoll;
  1326. if (!vp->assoc)
  1327. return;
  1328. wiphy_debug(data->hw->wiphy,
  1329. "%s: send PS-Poll to %pM for aid %d\n",
  1330. __func__, vp->bssid, vp->aid);
  1331. skb = dev_alloc_skb(sizeof(*pspoll));
  1332. if (!skb)
  1333. return;
  1334. pspoll = (void *) skb_put(skb, sizeof(*pspoll));
  1335. pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  1336. IEEE80211_STYPE_PSPOLL |
  1337. IEEE80211_FCTL_PM);
  1338. pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
  1339. memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
  1340. memcpy(pspoll->ta, mac, ETH_ALEN);
  1341. rcu_read_lock();
  1342. mac80211_hwsim_tx_frame(data->hw, skb,
  1343. rcu_dereference(vif->chanctx_conf)->channel);
  1344. rcu_read_unlock();
  1345. }
  1346. static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
  1347. struct ieee80211_vif *vif, int ps)
  1348. {
  1349. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  1350. struct sk_buff *skb;
  1351. struct ieee80211_hdr *hdr;
  1352. if (!vp->assoc)
  1353. return;
  1354. wiphy_debug(data->hw->wiphy,
  1355. "%s: send data::nullfunc to %pM ps=%d\n",
  1356. __func__, vp->bssid, ps);
  1357. skb = dev_alloc_skb(sizeof(*hdr));
  1358. if (!skb)
  1359. return;
  1360. hdr = (void *) skb_put(skb, sizeof(*hdr) - ETH_ALEN);
  1361. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  1362. IEEE80211_STYPE_NULLFUNC |
  1363. (ps ? IEEE80211_FCTL_PM : 0));
  1364. hdr->duration_id = cpu_to_le16(0);
  1365. memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
  1366. memcpy(hdr->addr2, mac, ETH_ALEN);
  1367. memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
  1368. rcu_read_lock();
  1369. mac80211_hwsim_tx_frame(data->hw, skb,
  1370. rcu_dereference(vif->chanctx_conf)->channel);
  1371. rcu_read_unlock();
  1372. }
  1373. static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
  1374. struct ieee80211_vif *vif)
  1375. {
  1376. struct mac80211_hwsim_data *data = dat;
  1377. hwsim_send_nullfunc(data, mac, vif, 1);
  1378. }
  1379. static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
  1380. struct ieee80211_vif *vif)
  1381. {
  1382. struct mac80211_hwsim_data *data = dat;
  1383. hwsim_send_nullfunc(data, mac, vif, 0);
  1384. }
  1385. static int hwsim_fops_ps_read(void *dat, u64 *val)
  1386. {
  1387. struct mac80211_hwsim_data *data = dat;
  1388. *val = data->ps;
  1389. return 0;
  1390. }
  1391. static int hwsim_fops_ps_write(void *dat, u64 val)
  1392. {
  1393. struct mac80211_hwsim_data *data = dat;
  1394. enum ps_mode old_ps;
  1395. if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
  1396. val != PS_MANUAL_POLL)
  1397. return -EINVAL;
  1398. old_ps = data->ps;
  1399. data->ps = val;
  1400. if (val == PS_MANUAL_POLL) {
  1401. ieee80211_iterate_active_interfaces(data->hw,
  1402. IEEE80211_IFACE_ITER_NORMAL,
  1403. hwsim_send_ps_poll, data);
  1404. data->ps_poll_pending = true;
  1405. } else if (old_ps == PS_DISABLED && val != PS_DISABLED) {
  1406. ieee80211_iterate_active_interfaces(data->hw,
  1407. IEEE80211_IFACE_ITER_NORMAL,
  1408. hwsim_send_nullfunc_ps,
  1409. data);
  1410. } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
  1411. ieee80211_iterate_active_interfaces(data->hw,
  1412. IEEE80211_IFACE_ITER_NORMAL,
  1413. hwsim_send_nullfunc_no_ps,
  1414. data);
  1415. }
  1416. return 0;
  1417. }
  1418. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
  1419. "%llu\n");
  1420. static int hwsim_fops_group_read(void *dat, u64 *val)
  1421. {
  1422. struct mac80211_hwsim_data *data = dat;
  1423. *val = data->group;
  1424. return 0;
  1425. }
  1426. static int hwsim_fops_group_write(void *dat, u64 val)
  1427. {
  1428. struct mac80211_hwsim_data *data = dat;
  1429. data->group = val;
  1430. return 0;
  1431. }
  1432. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
  1433. hwsim_fops_group_read, hwsim_fops_group_write,
  1434. "%llx\n");
  1435. static struct mac80211_hwsim_data *get_hwsim_data_ref_from_addr(
  1436. struct mac_address *addr)
  1437. {
  1438. struct mac80211_hwsim_data *data;
  1439. bool _found = false;
  1440. spin_lock_bh(&hwsim_radio_lock);
  1441. list_for_each_entry(data, &hwsim_radios, list) {
  1442. if (memcmp(data->addresses[1].addr, addr,
  1443. sizeof(struct mac_address)) == 0) {
  1444. _found = true;
  1445. break;
  1446. }
  1447. }
  1448. spin_unlock_bh(&hwsim_radio_lock);
  1449. if (!_found)
  1450. return NULL;
  1451. return data;
  1452. }
  1453. static int hwsim_tx_info_frame_received_nl(struct sk_buff *skb_2,
  1454. struct genl_info *info)
  1455. {
  1456. struct ieee80211_hdr *hdr;
  1457. struct mac80211_hwsim_data *data2;
  1458. struct ieee80211_tx_info *txi;
  1459. struct hwsim_tx_rate *tx_attempts;
  1460. unsigned long ret_skb_ptr;
  1461. struct sk_buff *skb, *tmp;
  1462. struct mac_address *src;
  1463. unsigned int hwsim_flags;
  1464. int i;
  1465. bool found = false;
  1466. if (!info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER] ||
  1467. !info->attrs[HWSIM_ATTR_FLAGS] ||
  1468. !info->attrs[HWSIM_ATTR_COOKIE] ||
  1469. !info->attrs[HWSIM_ATTR_TX_INFO])
  1470. goto out;
  1471. src = (struct mac_address *)nla_data(
  1472. info->attrs[HWSIM_ATTR_ADDR_TRANSMITTER]);
  1473. hwsim_flags = nla_get_u32(info->attrs[HWSIM_ATTR_FLAGS]);
  1474. ret_skb_ptr = nla_get_u64(info->attrs[HWSIM_ATTR_COOKIE]);
  1475. data2 = get_hwsim_data_ref_from_addr(src);
  1476. if (data2 == NULL)
  1477. goto out;
  1478. /* look for the skb matching the cookie passed back from user */
  1479. skb_queue_walk_safe(&data2->pending, skb, tmp) {
  1480. if ((unsigned long)skb == ret_skb_ptr) {
  1481. skb_unlink(skb, &data2->pending);
  1482. found = true;
  1483. break;
  1484. }
  1485. }
  1486. /* not found */
  1487. if (!found)
  1488. goto out;
  1489. /* Tx info received because the frame was broadcasted on user space,
  1490. so we get all the necessary info: tx attempts and skb control buff */
  1491. tx_attempts = (struct hwsim_tx_rate *)nla_data(
  1492. info->attrs[HWSIM_ATTR_TX_INFO]);
  1493. /* now send back TX status */
  1494. txi = IEEE80211_SKB_CB(skb);
  1495. ieee80211_tx_info_clear_status(txi);
  1496. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  1497. txi->status.rates[i].idx = tx_attempts[i].idx;
  1498. txi->status.rates[i].count = tx_attempts[i].count;
  1499. /*txi->status.rates[i].flags = 0;*/
  1500. }
  1501. txi->status.ack_signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
  1502. if (!(hwsim_flags & HWSIM_TX_CTL_NO_ACK) &&
  1503. (hwsim_flags & HWSIM_TX_STAT_ACK)) {
  1504. if (skb->len >= 16) {
  1505. hdr = (struct ieee80211_hdr *) skb->data;
  1506. mac80211_hwsim_monitor_ack(txi->rate_driver_data[0],
  1507. hdr->addr2);
  1508. }
  1509. txi->flags |= IEEE80211_TX_STAT_ACK;
  1510. }
  1511. ieee80211_tx_status_irqsafe(data2->hw, skb);
  1512. return 0;
  1513. out:
  1514. return -EINVAL;
  1515. }
  1516. static int hwsim_cloned_frame_received_nl(struct sk_buff *skb_2,
  1517. struct genl_info *info)
  1518. {
  1519. struct mac80211_hwsim_data *data2;
  1520. struct ieee80211_rx_status rx_status;
  1521. struct mac_address *dst;
  1522. int frame_data_len;
  1523. char *frame_data;
  1524. struct sk_buff *skb = NULL;
  1525. if (!info->attrs[HWSIM_ATTR_ADDR_RECEIVER] ||
  1526. !info->attrs[HWSIM_ATTR_FRAME] ||
  1527. !info->attrs[HWSIM_ATTR_RX_RATE] ||
  1528. !info->attrs[HWSIM_ATTR_SIGNAL])
  1529. goto out;
  1530. dst = (struct mac_address *)nla_data(
  1531. info->attrs[HWSIM_ATTR_ADDR_RECEIVER]);
  1532. frame_data_len = nla_len(info->attrs[HWSIM_ATTR_FRAME]);
  1533. frame_data = (char *)nla_data(info->attrs[HWSIM_ATTR_FRAME]);
  1534. /* Allocate new skb here */
  1535. skb = alloc_skb(frame_data_len, GFP_KERNEL);
  1536. if (skb == NULL)
  1537. goto err;
  1538. if (frame_data_len <= IEEE80211_MAX_DATA_LEN) {
  1539. /* Copy the data */
  1540. memcpy(skb_put(skb, frame_data_len), frame_data,
  1541. frame_data_len);
  1542. } else
  1543. goto err;
  1544. data2 = get_hwsim_data_ref_from_addr(dst);
  1545. if (data2 == NULL)
  1546. goto out;
  1547. /* check if radio is configured properly */
  1548. if (data2->idle || !data2->started)
  1549. goto out;
  1550. /*A frame is received from user space*/
  1551. memset(&rx_status, 0, sizeof(rx_status));
  1552. rx_status.freq = data2->channel->center_freq;
  1553. rx_status.band = data2->channel->band;
  1554. rx_status.rate_idx = nla_get_u32(info->attrs[HWSIM_ATTR_RX_RATE]);
  1555. rx_status.signal = nla_get_u32(info->attrs[HWSIM_ATTR_SIGNAL]);
  1556. memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
  1557. ieee80211_rx_irqsafe(data2->hw, skb);
  1558. return 0;
  1559. err:
  1560. printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
  1561. goto out;
  1562. out:
  1563. dev_kfree_skb(skb);
  1564. return -EINVAL;
  1565. }
  1566. static int hwsim_register_received_nl(struct sk_buff *skb_2,
  1567. struct genl_info *info)
  1568. {
  1569. if (info == NULL)
  1570. goto out;
  1571. wmediumd_portid = info->snd_portid;
  1572. printk(KERN_DEBUG "mac80211_hwsim: received a REGISTER, "
  1573. "switching to wmediumd mode with pid %d\n", info->snd_portid);
  1574. return 0;
  1575. out:
  1576. printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
  1577. return -EINVAL;
  1578. }
  1579. /* Generic Netlink operations array */
  1580. static struct genl_ops hwsim_ops[] = {
  1581. {
  1582. .cmd = HWSIM_CMD_REGISTER,
  1583. .policy = hwsim_genl_policy,
  1584. .doit = hwsim_register_received_nl,
  1585. .flags = GENL_ADMIN_PERM,
  1586. },
  1587. {
  1588. .cmd = HWSIM_CMD_FRAME,
  1589. .policy = hwsim_genl_policy,
  1590. .doit = hwsim_cloned_frame_received_nl,
  1591. },
  1592. {
  1593. .cmd = HWSIM_CMD_TX_INFO_FRAME,
  1594. .policy = hwsim_genl_policy,
  1595. .doit = hwsim_tx_info_frame_received_nl,
  1596. },
  1597. };
  1598. static int mac80211_hwsim_netlink_notify(struct notifier_block *nb,
  1599. unsigned long state,
  1600. void *_notify)
  1601. {
  1602. struct netlink_notify *notify = _notify;
  1603. if (state != NETLINK_URELEASE)
  1604. return NOTIFY_DONE;
  1605. if (notify->portid == wmediumd_portid) {
  1606. printk(KERN_INFO "mac80211_hwsim: wmediumd released netlink"
  1607. " socket, switching to perfect channel medium\n");
  1608. wmediumd_portid = 0;
  1609. }
  1610. return NOTIFY_DONE;
  1611. }
  1612. static struct notifier_block hwsim_netlink_notifier = {
  1613. .notifier_call = mac80211_hwsim_netlink_notify,
  1614. };
  1615. static int hwsim_init_netlink(void)
  1616. {
  1617. int rc;
  1618. /* userspace test API hasn't been adjusted for multi-channel */
  1619. if (channels > 1)
  1620. return 0;
  1621. printk(KERN_INFO "mac80211_hwsim: initializing netlink\n");
  1622. rc = genl_register_family_with_ops(&hwsim_genl_family,
  1623. hwsim_ops, ARRAY_SIZE(hwsim_ops));
  1624. if (rc)
  1625. goto failure;
  1626. rc = netlink_register_notifier(&hwsim_netlink_notifier);
  1627. if (rc)
  1628. goto failure;
  1629. return 0;
  1630. failure:
  1631. printk(KERN_DEBUG "mac80211_hwsim: error occurred in %s\n", __func__);
  1632. return -EINVAL;
  1633. }
  1634. static void hwsim_exit_netlink(void)
  1635. {
  1636. int ret;
  1637. /* userspace test API hasn't been adjusted for multi-channel */
  1638. if (channels > 1)
  1639. return;
  1640. printk(KERN_INFO "mac80211_hwsim: closing netlink\n");
  1641. /* unregister the notifier */
  1642. netlink_unregister_notifier(&hwsim_netlink_notifier);
  1643. /* unregister the family */
  1644. ret = genl_unregister_family(&hwsim_genl_family);
  1645. if (ret)
  1646. printk(KERN_DEBUG "mac80211_hwsim: "
  1647. "unregister family %i\n", ret);
  1648. }
  1649. static const struct ieee80211_iface_limit hwsim_if_limits[] = {
  1650. { .max = 1, .types = BIT(NL80211_IFTYPE_ADHOC) },
  1651. { .max = 2048, .types = BIT(NL80211_IFTYPE_STATION) |
  1652. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  1653. #ifdef CONFIG_MAC80211_MESH
  1654. BIT(NL80211_IFTYPE_MESH_POINT) |
  1655. #endif
  1656. BIT(NL80211_IFTYPE_AP) |
  1657. BIT(NL80211_IFTYPE_P2P_GO) },
  1658. { .max = 1, .types = BIT(NL80211_IFTYPE_P2P_DEVICE) },
  1659. };
  1660. static struct ieee80211_iface_combination hwsim_if_comb = {
  1661. .limits = hwsim_if_limits,
  1662. .n_limits = ARRAY_SIZE(hwsim_if_limits),
  1663. .max_interfaces = 2048,
  1664. .num_different_channels = 1,
  1665. };
  1666. static int __init init_mac80211_hwsim(void)
  1667. {
  1668. int i, err = 0;
  1669. u8 addr[ETH_ALEN];
  1670. struct mac80211_hwsim_data *data;
  1671. struct ieee80211_hw *hw;
  1672. enum ieee80211_band band;
  1673. if (radios < 1 || radios > 100)
  1674. return -EINVAL;
  1675. if (channels < 1)
  1676. return -EINVAL;
  1677. if (channels > 1) {
  1678. hwsim_if_comb.num_different_channels = channels;
  1679. mac80211_hwsim_ops.hw_scan = mac80211_hwsim_hw_scan;
  1680. mac80211_hwsim_ops.cancel_hw_scan =
  1681. mac80211_hwsim_cancel_hw_scan;
  1682. mac80211_hwsim_ops.sw_scan_start = NULL;
  1683. mac80211_hwsim_ops.sw_scan_complete = NULL;
  1684. mac80211_hwsim_ops.remain_on_channel =
  1685. mac80211_hwsim_roc;
  1686. mac80211_hwsim_ops.cancel_remain_on_channel =
  1687. mac80211_hwsim_croc;
  1688. mac80211_hwsim_ops.add_chanctx =
  1689. mac80211_hwsim_add_chanctx;
  1690. mac80211_hwsim_ops.remove_chanctx =
  1691. mac80211_hwsim_remove_chanctx;
  1692. mac80211_hwsim_ops.change_chanctx =
  1693. mac80211_hwsim_change_chanctx;
  1694. mac80211_hwsim_ops.assign_vif_chanctx =
  1695. mac80211_hwsim_assign_vif_chanctx;
  1696. mac80211_hwsim_ops.unassign_vif_chanctx =
  1697. mac80211_hwsim_unassign_vif_chanctx;
  1698. }
  1699. spin_lock_init(&hwsim_radio_lock);
  1700. INIT_LIST_HEAD(&hwsim_radios);
  1701. hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
  1702. if (IS_ERR(hwsim_class))
  1703. return PTR_ERR(hwsim_class);
  1704. memset(addr, 0, ETH_ALEN);
  1705. addr[0] = 0x02;
  1706. for (i = 0; i < radios; i++) {
  1707. printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
  1708. i);
  1709. hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
  1710. if (!hw) {
  1711. printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
  1712. "failed\n");
  1713. err = -ENOMEM;
  1714. goto failed;
  1715. }
  1716. data = hw->priv;
  1717. data->hw = hw;
  1718. data->dev = device_create(hwsim_class, NULL, 0, hw,
  1719. "hwsim%d", i);
  1720. if (IS_ERR(data->dev)) {
  1721. printk(KERN_DEBUG
  1722. "mac80211_hwsim: device_create "
  1723. "failed (%ld)\n", PTR_ERR(data->dev));
  1724. err = -ENOMEM;
  1725. goto failed_drvdata;
  1726. }
  1727. data->dev->driver = &mac80211_hwsim_driver;
  1728. skb_queue_head_init(&data->pending);
  1729. SET_IEEE80211_DEV(hw, data->dev);
  1730. addr[3] = i >> 8;
  1731. addr[4] = i;
  1732. memcpy(data->addresses[0].addr, addr, ETH_ALEN);
  1733. memcpy(data->addresses[1].addr, addr, ETH_ALEN);
  1734. data->addresses[1].addr[0] |= 0x40;
  1735. hw->wiphy->n_addresses = 2;
  1736. hw->wiphy->addresses = data->addresses;
  1737. hw->wiphy->iface_combinations = &hwsim_if_comb;
  1738. hw->wiphy->n_iface_combinations = 1;
  1739. if (channels > 1) {
  1740. hw->wiphy->max_scan_ssids = 255;
  1741. hw->wiphy->max_scan_ie_len = IEEE80211_MAX_DATA_LEN;
  1742. hw->wiphy->max_remain_on_channel_duration = 1000;
  1743. }
  1744. INIT_DELAYED_WORK(&data->roc_done, hw_roc_done);
  1745. INIT_DELAYED_WORK(&data->hw_scan, hw_scan_work);
  1746. hw->channel_change_time = 1;
  1747. hw->queues = 5;
  1748. hw->offchannel_tx_hw_queue = 4;
  1749. hw->wiphy->interface_modes =
  1750. BIT(NL80211_IFTYPE_STATION) |
  1751. BIT(NL80211_IFTYPE_AP) |
  1752. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  1753. BIT(NL80211_IFTYPE_P2P_GO) |
  1754. BIT(NL80211_IFTYPE_ADHOC) |
  1755. BIT(NL80211_IFTYPE_MESH_POINT) |
  1756. BIT(NL80211_IFTYPE_P2P_DEVICE);
  1757. hw->flags = IEEE80211_HW_MFP_CAPABLE |
  1758. IEEE80211_HW_SIGNAL_DBM |
  1759. IEEE80211_HW_SUPPORTS_STATIC_SMPS |
  1760. IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
  1761. IEEE80211_HW_AMPDU_AGGREGATION |
  1762. IEEE80211_HW_WANT_MONITOR_VIF |
  1763. IEEE80211_HW_QUEUE_CONTROL;
  1764. hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS |
  1765. WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
  1766. /* ask mac80211 to reserve space for magic */
  1767. hw->vif_data_size = sizeof(struct hwsim_vif_priv);
  1768. hw->sta_data_size = sizeof(struct hwsim_sta_priv);
  1769. memcpy(data->channels_2ghz, hwsim_channels_2ghz,
  1770. sizeof(hwsim_channels_2ghz));
  1771. memcpy(data->channels_5ghz, hwsim_channels_5ghz,
  1772. sizeof(hwsim_channels_5ghz));
  1773. memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
  1774. for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
  1775. struct ieee80211_supported_band *sband = &data->bands[band];
  1776. switch (band) {
  1777. case IEEE80211_BAND_2GHZ:
  1778. sband->channels = data->channels_2ghz;
  1779. sband->n_channels =
  1780. ARRAY_SIZE(hwsim_channels_2ghz);
  1781. sband->bitrates = data->rates;
  1782. sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
  1783. break;
  1784. case IEEE80211_BAND_5GHZ:
  1785. sband->channels = data->channels_5ghz;
  1786. sband->n_channels =
  1787. ARRAY_SIZE(hwsim_channels_5ghz);
  1788. sband->bitrates = data->rates + 4;
  1789. sband->n_bitrates = ARRAY_SIZE(hwsim_rates) - 4;
  1790. break;
  1791. default:
  1792. continue;
  1793. }
  1794. sband->ht_cap.ht_supported = true;
  1795. sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  1796. IEEE80211_HT_CAP_GRN_FLD |
  1797. IEEE80211_HT_CAP_SGI_40 |
  1798. IEEE80211_HT_CAP_DSSSCCK40;
  1799. sband->ht_cap.ampdu_factor = 0x3;
  1800. sband->ht_cap.ampdu_density = 0x6;
  1801. memset(&sband->ht_cap.mcs, 0,
  1802. sizeof(sband->ht_cap.mcs));
  1803. sband->ht_cap.mcs.rx_mask[0] = 0xff;
  1804. sband->ht_cap.mcs.rx_mask[1] = 0xff;
  1805. sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  1806. hw->wiphy->bands[band] = sband;
  1807. }
  1808. /* By default all radios are belonging to the first group */
  1809. data->group = 1;
  1810. mutex_init(&data->mutex);
  1811. /* Enable frame retransmissions for lossy channels */
  1812. hw->max_rates = 4;
  1813. hw->max_rate_tries = 11;
  1814. /* Work to be done prior to ieee80211_register_hw() */
  1815. switch (regtest) {
  1816. case HWSIM_REGTEST_DISABLED:
  1817. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  1818. case HWSIM_REGTEST_DRIVER_REG_ALL:
  1819. case HWSIM_REGTEST_DIFF_COUNTRY:
  1820. /*
  1821. * Nothing to be done for driver regulatory domain
  1822. * hints prior to ieee80211_register_hw()
  1823. */
  1824. break;
  1825. case HWSIM_REGTEST_WORLD_ROAM:
  1826. if (i == 0) {
  1827. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1828. wiphy_apply_custom_regulatory(hw->wiphy,
  1829. &hwsim_world_regdom_custom_01);
  1830. }
  1831. break;
  1832. case HWSIM_REGTEST_CUSTOM_WORLD:
  1833. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1834. wiphy_apply_custom_regulatory(hw->wiphy,
  1835. &hwsim_world_regdom_custom_01);
  1836. break;
  1837. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  1838. if (i == 0) {
  1839. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1840. wiphy_apply_custom_regulatory(hw->wiphy,
  1841. &hwsim_world_regdom_custom_01);
  1842. } else if (i == 1) {
  1843. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1844. wiphy_apply_custom_regulatory(hw->wiphy,
  1845. &hwsim_world_regdom_custom_02);
  1846. }
  1847. break;
  1848. case HWSIM_REGTEST_STRICT_ALL:
  1849. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1850. break;
  1851. case HWSIM_REGTEST_STRICT_FOLLOW:
  1852. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  1853. if (i == 0)
  1854. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1855. break;
  1856. case HWSIM_REGTEST_ALL:
  1857. if (i == 0) {
  1858. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1859. wiphy_apply_custom_regulatory(hw->wiphy,
  1860. &hwsim_world_regdom_custom_01);
  1861. } else if (i == 1) {
  1862. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY;
  1863. wiphy_apply_custom_regulatory(hw->wiphy,
  1864. &hwsim_world_regdom_custom_02);
  1865. } else if (i == 4)
  1866. hw->wiphy->flags |= WIPHY_FLAG_STRICT_REGULATORY;
  1867. break;
  1868. default:
  1869. break;
  1870. }
  1871. /* give the regulatory workqueue a chance to run */
  1872. if (regtest)
  1873. schedule_timeout_interruptible(1);
  1874. err = ieee80211_register_hw(hw);
  1875. if (err < 0) {
  1876. printk(KERN_DEBUG "mac80211_hwsim: "
  1877. "ieee80211_register_hw failed (%d)\n", err);
  1878. goto failed_hw;
  1879. }
  1880. /* Work to be done after to ieee80211_register_hw() */
  1881. switch (regtest) {
  1882. case HWSIM_REGTEST_WORLD_ROAM:
  1883. case HWSIM_REGTEST_DISABLED:
  1884. break;
  1885. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  1886. if (!i)
  1887. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1888. break;
  1889. case HWSIM_REGTEST_DRIVER_REG_ALL:
  1890. case HWSIM_REGTEST_STRICT_ALL:
  1891. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1892. break;
  1893. case HWSIM_REGTEST_DIFF_COUNTRY:
  1894. if (i < ARRAY_SIZE(hwsim_alpha2s))
  1895. regulatory_hint(hw->wiphy, hwsim_alpha2s[i]);
  1896. break;
  1897. case HWSIM_REGTEST_CUSTOM_WORLD:
  1898. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  1899. /*
  1900. * Nothing to be done for custom world regulatory
  1901. * domains after to ieee80211_register_hw
  1902. */
  1903. break;
  1904. case HWSIM_REGTEST_STRICT_FOLLOW:
  1905. if (i == 0)
  1906. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1907. break;
  1908. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  1909. if (i == 0)
  1910. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1911. else if (i == 1)
  1912. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  1913. break;
  1914. case HWSIM_REGTEST_ALL:
  1915. if (i == 2)
  1916. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1917. else if (i == 3)
  1918. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  1919. else if (i == 4)
  1920. regulatory_hint(hw->wiphy, hwsim_alpha2s[2]);
  1921. break;
  1922. default:
  1923. break;
  1924. }
  1925. wiphy_debug(hw->wiphy, "hwaddr %pm registered\n",
  1926. hw->wiphy->perm_addr);
  1927. data->debugfs = debugfs_create_dir("hwsim",
  1928. hw->wiphy->debugfsdir);
  1929. data->debugfs_ps = debugfs_create_file("ps", 0666,
  1930. data->debugfs, data,
  1931. &hwsim_fops_ps);
  1932. data->debugfs_group = debugfs_create_file("group", 0666,
  1933. data->debugfs, data,
  1934. &hwsim_fops_group);
  1935. setup_timer(&data->beacon_timer, mac80211_hwsim_beacon,
  1936. (unsigned long) hw);
  1937. list_add_tail(&data->list, &hwsim_radios);
  1938. }
  1939. hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
  1940. if (hwsim_mon == NULL)
  1941. goto failed;
  1942. rtnl_lock();
  1943. err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
  1944. if (err < 0)
  1945. goto failed_mon;
  1946. err = register_netdevice(hwsim_mon);
  1947. if (err < 0)
  1948. goto failed_mon;
  1949. rtnl_unlock();
  1950. err = hwsim_init_netlink();
  1951. if (err < 0)
  1952. goto failed_nl;
  1953. return 0;
  1954. failed_nl:
  1955. printk(KERN_DEBUG "mac_80211_hwsim: failed initializing netlink\n");
  1956. return err;
  1957. failed_mon:
  1958. rtnl_unlock();
  1959. free_netdev(hwsim_mon);
  1960. mac80211_hwsim_free();
  1961. return err;
  1962. failed_hw:
  1963. device_unregister(data->dev);
  1964. failed_drvdata:
  1965. ieee80211_free_hw(hw);
  1966. failed:
  1967. mac80211_hwsim_free();
  1968. return err;
  1969. }
  1970. module_init(init_mac80211_hwsim);
  1971. static void __exit exit_mac80211_hwsim(void)
  1972. {
  1973. printk(KERN_DEBUG "mac80211_hwsim: unregister radios\n");
  1974. hwsim_exit_netlink();
  1975. mac80211_hwsim_free();
  1976. unregister_netdev(hwsim_mon);
  1977. }
  1978. module_exit(exit_mac80211_hwsim);