mac80211_hwsim.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253
  1. /*
  2. * mac80211_hwsim - software simulator of 802.11 radio(s) for mac80211
  3. * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. /*
  10. * TODO:
  11. * - IBSS mode simulation (Beacon transmission with competition for "air time")
  12. * - RX filtering based on filter configuration (data->rx_filter)
  13. */
  14. #include <linux/list.h>
  15. #include <linux/spinlock.h>
  16. #include <net/dst.h>
  17. #include <net/xfrm.h>
  18. #include <net/mac80211.h>
  19. #include <net/ieee80211_radiotap.h>
  20. #include <linux/if_arp.h>
  21. #include <linux/rtnetlink.h>
  22. #include <linux/etherdevice.h>
  23. #include <linux/debugfs.h>
  24. MODULE_AUTHOR("Jouni Malinen");
  25. MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
  26. MODULE_LICENSE("GPL");
  27. static int radios = 2;
  28. module_param(radios, int, 0444);
  29. MODULE_PARM_DESC(radios, "Number of simulated radios");
  30. /**
  31. * enum hwsim_regtest - the type of regulatory tests we offer
  32. *
  33. * These are the different values you can use for the regtest
  34. * module parameter. This is useful to help test world roaming
  35. * and the driver regulatory_hint() call and combinations of these.
  36. * If you want to do specific alpha2 regulatory domain tests simply
  37. * use the userspace regulatory request as that will be respected as
  38. * well without the need of this module parameter. This is designed
  39. * only for testing the driver regulatory request, world roaming
  40. * and all possible combinations.
  41. *
  42. * @HWSIM_REGTEST_DISABLED: No regulatory tests are performed,
  43. * this is the default value.
  44. * @HWSIM_REGTEST_DRIVER_REG_FOLLOW: Used for testing the driver regulatory
  45. * hint, only one driver regulatory hint will be sent as such the
  46. * secondary radios are expected to follow.
  47. * @HWSIM_REGTEST_DRIVER_REG_ALL: Used for testing the driver regulatory
  48. * request with all radios reporting the same regulatory domain.
  49. * @HWSIM_REGTEST_DIFF_COUNTRY: Used for testing the drivers calling
  50. * different regulatory domains requests. Expected behaviour is for
  51. * an intersection to occur but each device will still use their
  52. * respective regulatory requested domains. Subsequent radios will
  53. * use the resulting intersection.
  54. * @HWSIM_REGTEST_WORLD_ROAM: Used for testing the world roaming. We acomplish
  55. * this by using a custom beacon-capable regulatory domain for the first
  56. * radio. All other device world roam.
  57. * @HWSIM_REGTEST_CUSTOM_WORLD: Used for testing the custom world regulatory
  58. * domain requests. All radios will adhere to this custom world regulatory
  59. * domain.
  60. * @HWSIM_REGTEST_CUSTOM_WORLD_2: Used for testing 2 custom world regulatory
  61. * domain requests. The first radio will adhere to the first custom world
  62. * regulatory domain, the second one to the second custom world regulatory
  63. * domain. All other devices will world roam.
  64. * @HWSIM_REGTEST_STRICT_FOLLOW_: Used for testing strict regulatory domain
  65. * settings, only the first radio will send a regulatory domain request
  66. * and use strict settings. The rest of the radios are expected to follow.
  67. * @HWSIM_REGTEST_STRICT_ALL: Used for testing strict regulatory domain
  68. * settings. All radios will adhere to this.
  69. * @HWSIM_REGTEST_STRICT_AND_DRIVER_REG: Used for testing strict regulatory
  70. * domain settings, combined with secondary driver regulatory domain
  71. * settings. The first radio will get a strict regulatory domain setting
  72. * using the first driver regulatory request and the second radio will use
  73. * non-strict settings using the second driver regulatory request. All
  74. * other devices should follow the intersection created between the
  75. * first two.
  76. * @HWSIM_REGTEST_ALL: Used for testing every possible mix. You will need
  77. * at least 6 radios for a complete test. We will test in this order:
  78. * 1 - driver custom world regulatory domain
  79. * 2 - second custom world regulatory domain
  80. * 3 - first driver regulatory domain request
  81. * 4 - second driver regulatory domain request
  82. * 5 - strict regulatory domain settings using the third driver regulatory
  83. * domain request
  84. * 6 and on - should follow the intersection of the 3rd, 4rth and 5th radio
  85. * regulatory requests.
  86. */
  87. enum hwsim_regtest {
  88. HWSIM_REGTEST_DISABLED = 0,
  89. HWSIM_REGTEST_DRIVER_REG_FOLLOW = 1,
  90. HWSIM_REGTEST_DRIVER_REG_ALL = 2,
  91. HWSIM_REGTEST_DIFF_COUNTRY = 3,
  92. HWSIM_REGTEST_WORLD_ROAM = 4,
  93. HWSIM_REGTEST_CUSTOM_WORLD = 5,
  94. HWSIM_REGTEST_CUSTOM_WORLD_2 = 6,
  95. HWSIM_REGTEST_STRICT_FOLLOW = 7,
  96. HWSIM_REGTEST_STRICT_ALL = 8,
  97. HWSIM_REGTEST_STRICT_AND_DRIVER_REG = 9,
  98. HWSIM_REGTEST_ALL = 10,
  99. };
  100. /* Set to one of the HWSIM_REGTEST_* values above */
  101. static int regtest = HWSIM_REGTEST_DISABLED;
  102. module_param(regtest, int, 0444);
  103. MODULE_PARM_DESC(regtest, "The type of regulatory test we want to run");
  104. static const char *hwsim_alpha2s[] = {
  105. "FI",
  106. "AL",
  107. "US",
  108. "DE",
  109. "JP",
  110. "AL",
  111. };
  112. static const struct ieee80211_regdomain hwsim_world_regdom_custom_01 = {
  113. .n_reg_rules = 4,
  114. .alpha2 = "99",
  115. .reg_rules = {
  116. REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
  117. REG_RULE(2484-10, 2484+10, 40, 0, 20, 0),
  118. REG_RULE(5150-10, 5240+10, 40, 0, 30, 0),
  119. REG_RULE(5745-10, 5825+10, 40, 0, 30, 0),
  120. }
  121. };
  122. static const struct ieee80211_regdomain hwsim_world_regdom_custom_02 = {
  123. .n_reg_rules = 2,
  124. .alpha2 = "99",
  125. .reg_rules = {
  126. REG_RULE(2412-10, 2462+10, 40, 0, 20, 0),
  127. REG_RULE(5725-10, 5850+10, 40, 0, 30,
  128. NL80211_RRF_PASSIVE_SCAN | NL80211_RRF_NO_IBSS),
  129. }
  130. };
  131. struct hwsim_vif_priv {
  132. u32 magic;
  133. u8 bssid[ETH_ALEN];
  134. bool assoc;
  135. u16 aid;
  136. };
  137. #define HWSIM_VIF_MAGIC 0x69537748
  138. static inline void hwsim_check_magic(struct ieee80211_vif *vif)
  139. {
  140. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  141. WARN_ON(vp->magic != HWSIM_VIF_MAGIC);
  142. }
  143. static inline void hwsim_set_magic(struct ieee80211_vif *vif)
  144. {
  145. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  146. vp->magic = HWSIM_VIF_MAGIC;
  147. }
  148. static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
  149. {
  150. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  151. vp->magic = 0;
  152. }
  153. struct hwsim_sta_priv {
  154. u32 magic;
  155. };
  156. #define HWSIM_STA_MAGIC 0x6d537748
  157. static inline void hwsim_check_sta_magic(struct ieee80211_sta *sta)
  158. {
  159. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  160. WARN_ON(sp->magic != HWSIM_STA_MAGIC);
  161. }
  162. static inline void hwsim_set_sta_magic(struct ieee80211_sta *sta)
  163. {
  164. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  165. sp->magic = HWSIM_STA_MAGIC;
  166. }
  167. static inline void hwsim_clear_sta_magic(struct ieee80211_sta *sta)
  168. {
  169. struct hwsim_sta_priv *sp = (void *)sta->drv_priv;
  170. sp->magic = 0;
  171. }
  172. static struct class *hwsim_class;
  173. static struct net_device *hwsim_mon; /* global monitor netdev */
  174. #define CHAN2G(_freq) { \
  175. .band = IEEE80211_BAND_2GHZ, \
  176. .center_freq = (_freq), \
  177. .hw_value = (_freq), \
  178. .max_power = 20, \
  179. }
  180. #define CHAN5G(_freq) { \
  181. .band = IEEE80211_BAND_5GHZ, \
  182. .center_freq = (_freq), \
  183. .hw_value = (_freq), \
  184. .max_power = 20, \
  185. }
  186. static const struct ieee80211_channel hwsim_channels_2ghz[] = {
  187. CHAN2G(2412), /* Channel 1 */
  188. CHAN2G(2417), /* Channel 2 */
  189. CHAN2G(2422), /* Channel 3 */
  190. CHAN2G(2427), /* Channel 4 */
  191. CHAN2G(2432), /* Channel 5 */
  192. CHAN2G(2437), /* Channel 6 */
  193. CHAN2G(2442), /* Channel 7 */
  194. CHAN2G(2447), /* Channel 8 */
  195. CHAN2G(2452), /* Channel 9 */
  196. CHAN2G(2457), /* Channel 10 */
  197. CHAN2G(2462), /* Channel 11 */
  198. CHAN2G(2467), /* Channel 12 */
  199. CHAN2G(2472), /* Channel 13 */
  200. CHAN2G(2484), /* Channel 14 */
  201. };
  202. static const struct ieee80211_channel hwsim_channels_5ghz[] = {
  203. CHAN5G(5180), /* Channel 36 */
  204. CHAN5G(5200), /* Channel 40 */
  205. CHAN5G(5220), /* Channel 44 */
  206. CHAN5G(5240), /* Channel 48 */
  207. CHAN5G(5260), /* Channel 52 */
  208. CHAN5G(5280), /* Channel 56 */
  209. CHAN5G(5300), /* Channel 60 */
  210. CHAN5G(5320), /* Channel 64 */
  211. CHAN5G(5500), /* Channel 100 */
  212. CHAN5G(5520), /* Channel 104 */
  213. CHAN5G(5540), /* Channel 108 */
  214. CHAN5G(5560), /* Channel 112 */
  215. CHAN5G(5580), /* Channel 116 */
  216. CHAN5G(5600), /* Channel 120 */
  217. CHAN5G(5620), /* Channel 124 */
  218. CHAN5G(5640), /* Channel 128 */
  219. CHAN5G(5660), /* Channel 132 */
  220. CHAN5G(5680), /* Channel 136 */
  221. CHAN5G(5700), /* Channel 140 */
  222. CHAN5G(5745), /* Channel 149 */
  223. CHAN5G(5765), /* Channel 153 */
  224. CHAN5G(5785), /* Channel 157 */
  225. CHAN5G(5805), /* Channel 161 */
  226. CHAN5G(5825), /* Channel 165 */
  227. };
  228. static const struct ieee80211_rate hwsim_rates[] = {
  229. { .bitrate = 10 },
  230. { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  231. { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  232. { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  233. { .bitrate = 60 },
  234. { .bitrate = 90 },
  235. { .bitrate = 120 },
  236. { .bitrate = 180 },
  237. { .bitrate = 240 },
  238. { .bitrate = 360 },
  239. { .bitrate = 480 },
  240. { .bitrate = 540 }
  241. };
  242. static spinlock_t hwsim_radio_lock;
  243. static struct list_head hwsim_radios;
  244. struct mac80211_hwsim_data {
  245. struct list_head list;
  246. struct ieee80211_hw *hw;
  247. struct device *dev;
  248. struct ieee80211_supported_band bands[2];
  249. struct ieee80211_channel channels_2ghz[ARRAY_SIZE(hwsim_channels_2ghz)];
  250. struct ieee80211_channel channels_5ghz[ARRAY_SIZE(hwsim_channels_5ghz)];
  251. struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
  252. struct ieee80211_channel *channel;
  253. unsigned long beacon_int; /* in jiffies unit */
  254. unsigned int rx_filter;
  255. int started;
  256. struct timer_list beacon_timer;
  257. enum ps_mode {
  258. PS_DISABLED, PS_ENABLED, PS_AUTO_POLL, PS_MANUAL_POLL
  259. } ps;
  260. bool ps_poll_pending;
  261. struct dentry *debugfs;
  262. struct dentry *debugfs_ps;
  263. /*
  264. * Only radios in the same group can communicate together (the
  265. * channel has to match too). Each bit represents a group. A
  266. * radio can be in more then one group.
  267. */
  268. u64 group;
  269. struct dentry *debugfs_group;
  270. };
  271. struct hwsim_radiotap_hdr {
  272. struct ieee80211_radiotap_header hdr;
  273. u8 rt_flags;
  274. u8 rt_rate;
  275. __le16 rt_channel;
  276. __le16 rt_chbitmask;
  277. } __attribute__ ((packed));
  278. static int hwsim_mon_xmit(struct sk_buff *skb, struct net_device *dev)
  279. {
  280. /* TODO: allow packet injection */
  281. dev_kfree_skb(skb);
  282. return NETDEV_TX_OK;
  283. }
  284. static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
  285. struct sk_buff *tx_skb)
  286. {
  287. struct mac80211_hwsim_data *data = hw->priv;
  288. struct sk_buff *skb;
  289. struct hwsim_radiotap_hdr *hdr;
  290. u16 flags;
  291. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
  292. struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
  293. if (!netif_running(hwsim_mon))
  294. return;
  295. skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
  296. if (skb == NULL)
  297. return;
  298. hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr));
  299. hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  300. hdr->hdr.it_pad = 0;
  301. hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
  302. hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  303. (1 << IEEE80211_RADIOTAP_RATE) |
  304. (1 << IEEE80211_RADIOTAP_CHANNEL));
  305. hdr->rt_flags = 0;
  306. hdr->rt_rate = txrate->bitrate / 5;
  307. hdr->rt_channel = cpu_to_le16(data->channel->center_freq);
  308. flags = IEEE80211_CHAN_2GHZ;
  309. if (txrate->flags & IEEE80211_RATE_ERP_G)
  310. flags |= IEEE80211_CHAN_OFDM;
  311. else
  312. flags |= IEEE80211_CHAN_CCK;
  313. hdr->rt_chbitmask = cpu_to_le16(flags);
  314. skb->dev = hwsim_mon;
  315. skb_set_mac_header(skb, 0);
  316. skb->ip_summed = CHECKSUM_UNNECESSARY;
  317. skb->pkt_type = PACKET_OTHERHOST;
  318. skb->protocol = htons(ETH_P_802_2);
  319. memset(skb->cb, 0, sizeof(skb->cb));
  320. netif_rx(skb);
  321. }
  322. static bool hwsim_ps_rx_ok(struct mac80211_hwsim_data *data,
  323. struct sk_buff *skb)
  324. {
  325. switch (data->ps) {
  326. case PS_DISABLED:
  327. return true;
  328. case PS_ENABLED:
  329. return false;
  330. case PS_AUTO_POLL:
  331. /* TODO: accept (some) Beacons by default and other frames only
  332. * if pending PS-Poll has been sent */
  333. return true;
  334. case PS_MANUAL_POLL:
  335. /* Allow unicast frames to own address if there is a pending
  336. * PS-Poll */
  337. if (data->ps_poll_pending &&
  338. memcmp(data->hw->wiphy->perm_addr, skb->data + 4,
  339. ETH_ALEN) == 0) {
  340. data->ps_poll_pending = false;
  341. return true;
  342. }
  343. return false;
  344. }
  345. return true;
  346. }
  347. static bool mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
  348. struct sk_buff *skb)
  349. {
  350. struct mac80211_hwsim_data *data = hw->priv, *data2;
  351. bool ack = false;
  352. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  353. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  354. struct ieee80211_rx_status rx_status;
  355. memset(&rx_status, 0, sizeof(rx_status));
  356. /* TODO: set mactime */
  357. rx_status.freq = data->channel->center_freq;
  358. rx_status.band = data->channel->band;
  359. rx_status.rate_idx = info->control.rates[0].idx;
  360. /* TODO: simulate real signal strength (and optional packet loss) */
  361. rx_status.signal = -50;
  362. if (data->ps != PS_DISABLED)
  363. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  364. /* release the skb's source info */
  365. skb_orphan(skb);
  366. skb_dst_drop(skb);
  367. skb->mark = 0;
  368. secpath_reset(skb);
  369. nf_reset(skb);
  370. /* Copy skb to all enabled radios that are on the current frequency */
  371. spin_lock(&hwsim_radio_lock);
  372. list_for_each_entry(data2, &hwsim_radios, list) {
  373. struct sk_buff *nskb;
  374. if (data == data2)
  375. continue;
  376. if (!data2->started || !hwsim_ps_rx_ok(data2, skb) ||
  377. !data->channel || !data2->channel ||
  378. data->channel->center_freq != data2->channel->center_freq ||
  379. !(data->group & data2->group))
  380. continue;
  381. nskb = skb_copy(skb, GFP_ATOMIC);
  382. if (nskb == NULL)
  383. continue;
  384. if (memcmp(hdr->addr1, data2->hw->wiphy->perm_addr,
  385. ETH_ALEN) == 0)
  386. ack = true;
  387. memcpy(IEEE80211_SKB_RXCB(nskb), &rx_status, sizeof(rx_status));
  388. ieee80211_rx_irqsafe(data2->hw, nskb);
  389. }
  390. spin_unlock(&hwsim_radio_lock);
  391. return ack;
  392. }
  393. static int mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  394. {
  395. bool ack;
  396. struct ieee80211_tx_info *txi;
  397. mac80211_hwsim_monitor_rx(hw, skb);
  398. if (skb->len < 10) {
  399. /* Should not happen; just a sanity check for addr1 use */
  400. dev_kfree_skb(skb);
  401. return NETDEV_TX_OK;
  402. }
  403. ack = mac80211_hwsim_tx_frame(hw, skb);
  404. txi = IEEE80211_SKB_CB(skb);
  405. if (txi->control.vif)
  406. hwsim_check_magic(txi->control.vif);
  407. if (txi->control.sta)
  408. hwsim_check_sta_magic(txi->control.sta);
  409. ieee80211_tx_info_clear_status(txi);
  410. if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK) && ack)
  411. txi->flags |= IEEE80211_TX_STAT_ACK;
  412. ieee80211_tx_status_irqsafe(hw, skb);
  413. return NETDEV_TX_OK;
  414. }
  415. static int mac80211_hwsim_start(struct ieee80211_hw *hw)
  416. {
  417. struct mac80211_hwsim_data *data = hw->priv;
  418. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  419. data->started = 1;
  420. return 0;
  421. }
  422. static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
  423. {
  424. struct mac80211_hwsim_data *data = hw->priv;
  425. data->started = 0;
  426. del_timer(&data->beacon_timer);
  427. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  428. }
  429. static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
  430. struct ieee80211_if_init_conf *conf)
  431. {
  432. printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%pM)\n",
  433. wiphy_name(hw->wiphy), __func__, conf->type,
  434. conf->mac_addr);
  435. hwsim_set_magic(conf->vif);
  436. return 0;
  437. }
  438. static void mac80211_hwsim_remove_interface(
  439. struct ieee80211_hw *hw, struct ieee80211_if_init_conf *conf)
  440. {
  441. printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%pM)\n",
  442. wiphy_name(hw->wiphy), __func__, conf->type,
  443. conf->mac_addr);
  444. hwsim_check_magic(conf->vif);
  445. hwsim_clear_magic(conf->vif);
  446. }
  447. static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
  448. struct ieee80211_vif *vif)
  449. {
  450. struct ieee80211_hw *hw = arg;
  451. struct sk_buff *skb;
  452. struct ieee80211_tx_info *info;
  453. hwsim_check_magic(vif);
  454. if (vif->type != NL80211_IFTYPE_AP &&
  455. vif->type != NL80211_IFTYPE_MESH_POINT)
  456. return;
  457. skb = ieee80211_beacon_get(hw, vif);
  458. if (skb == NULL)
  459. return;
  460. info = IEEE80211_SKB_CB(skb);
  461. mac80211_hwsim_monitor_rx(hw, skb);
  462. mac80211_hwsim_tx_frame(hw, skb);
  463. dev_kfree_skb(skb);
  464. }
  465. static void mac80211_hwsim_beacon(unsigned long arg)
  466. {
  467. struct ieee80211_hw *hw = (struct ieee80211_hw *) arg;
  468. struct mac80211_hwsim_data *data = hw->priv;
  469. if (!data->started)
  470. return;
  471. ieee80211_iterate_active_interfaces_atomic(
  472. hw, mac80211_hwsim_beacon_tx, hw);
  473. data->beacon_timer.expires = jiffies + data->beacon_int;
  474. add_timer(&data->beacon_timer);
  475. }
  476. static int mac80211_hwsim_config(struct ieee80211_hw *hw, u32 changed)
  477. {
  478. struct mac80211_hwsim_data *data = hw->priv;
  479. struct ieee80211_conf *conf = &hw->conf;
  480. printk(KERN_DEBUG "%s:%s (freq=%d idle=%d ps=%d)\n",
  481. wiphy_name(hw->wiphy), __func__,
  482. conf->channel->center_freq,
  483. !!(conf->flags & IEEE80211_CONF_IDLE),
  484. !!(conf->flags & IEEE80211_CONF_PS));
  485. data->channel = conf->channel;
  486. if (!data->started || !data->beacon_int)
  487. del_timer(&data->beacon_timer);
  488. else
  489. mod_timer(&data->beacon_timer, jiffies + data->beacon_int);
  490. return 0;
  491. }
  492. static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
  493. unsigned int changed_flags,
  494. unsigned int *total_flags,u64 multicast)
  495. {
  496. struct mac80211_hwsim_data *data = hw->priv;
  497. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  498. data->rx_filter = 0;
  499. if (*total_flags & FIF_PROMISC_IN_BSS)
  500. data->rx_filter |= FIF_PROMISC_IN_BSS;
  501. if (*total_flags & FIF_ALLMULTI)
  502. data->rx_filter |= FIF_ALLMULTI;
  503. *total_flags = data->rx_filter;
  504. }
  505. static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
  506. struct ieee80211_vif *vif,
  507. struct ieee80211_bss_conf *info,
  508. u32 changed)
  509. {
  510. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  511. struct mac80211_hwsim_data *data = hw->priv;
  512. hwsim_check_magic(vif);
  513. printk(KERN_DEBUG "%s:%s(changed=0x%x)\n",
  514. wiphy_name(hw->wiphy), __func__, changed);
  515. if (changed & BSS_CHANGED_BSSID) {
  516. printk(KERN_DEBUG "%s:%s: BSSID changed: %pM\n",
  517. wiphy_name(hw->wiphy), __func__,
  518. info->bssid);
  519. memcpy(vp->bssid, info->bssid, ETH_ALEN);
  520. }
  521. if (changed & BSS_CHANGED_ASSOC) {
  522. printk(KERN_DEBUG " %s: ASSOC: assoc=%d aid=%d\n",
  523. wiphy_name(hw->wiphy), info->assoc, info->aid);
  524. vp->assoc = info->assoc;
  525. vp->aid = info->aid;
  526. }
  527. if (changed & BSS_CHANGED_BEACON_INT) {
  528. printk(KERN_DEBUG " %s: BCNINT: %d\n",
  529. wiphy_name(hw->wiphy), info->beacon_int);
  530. data->beacon_int = 1024 * info->beacon_int / 1000 * HZ / 1000;
  531. if (WARN_ON(!data->beacon_int))
  532. data->beacon_int = 1;
  533. }
  534. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  535. printk(KERN_DEBUG " %s: ERP_CTS_PROT: %d\n",
  536. wiphy_name(hw->wiphy), info->use_cts_prot);
  537. }
  538. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  539. printk(KERN_DEBUG " %s: ERP_PREAMBLE: %d\n",
  540. wiphy_name(hw->wiphy), info->use_short_preamble);
  541. }
  542. if (changed & BSS_CHANGED_ERP_SLOT) {
  543. printk(KERN_DEBUG " %s: ERP_SLOT: %d\n",
  544. wiphy_name(hw->wiphy), info->use_short_slot);
  545. }
  546. if (changed & BSS_CHANGED_HT) {
  547. printk(KERN_DEBUG " %s: HT: op_mode=0x%x\n",
  548. wiphy_name(hw->wiphy),
  549. info->ht_operation_mode);
  550. }
  551. if (changed & BSS_CHANGED_BASIC_RATES) {
  552. printk(KERN_DEBUG " %s: BASIC_RATES: 0x%llx\n",
  553. wiphy_name(hw->wiphy),
  554. (unsigned long long) info->basic_rates);
  555. }
  556. }
  557. static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
  558. struct ieee80211_vif *vif,
  559. enum sta_notify_cmd cmd,
  560. struct ieee80211_sta *sta)
  561. {
  562. hwsim_check_magic(vif);
  563. switch (cmd) {
  564. case STA_NOTIFY_ADD:
  565. hwsim_set_sta_magic(sta);
  566. break;
  567. case STA_NOTIFY_REMOVE:
  568. hwsim_clear_sta_magic(sta);
  569. break;
  570. case STA_NOTIFY_SLEEP:
  571. case STA_NOTIFY_AWAKE:
  572. /* TODO: make good use of these flags */
  573. break;
  574. }
  575. }
  576. static int mac80211_hwsim_set_tim(struct ieee80211_hw *hw,
  577. struct ieee80211_sta *sta,
  578. bool set)
  579. {
  580. hwsim_check_sta_magic(sta);
  581. return 0;
  582. }
  583. static int mac80211_hwsim_conf_tx(
  584. struct ieee80211_hw *hw, u16 queue,
  585. const struct ieee80211_tx_queue_params *params)
  586. {
  587. printk(KERN_DEBUG "%s:%s (queue=%d txop=%d cw_min=%d cw_max=%d "
  588. "aifs=%d)\n",
  589. wiphy_name(hw->wiphy), __func__, queue,
  590. params->txop, params->cw_min, params->cw_max, params->aifs);
  591. return 0;
  592. }
  593. #ifdef CONFIG_NL80211_TESTMODE
  594. /*
  595. * This section contains example code for using netlink
  596. * attributes with the testmode command in nl80211.
  597. */
  598. /* These enums need to be kept in sync with userspace */
  599. enum hwsim_testmode_attr {
  600. __HWSIM_TM_ATTR_INVALID = 0,
  601. HWSIM_TM_ATTR_CMD = 1,
  602. HWSIM_TM_ATTR_PS = 2,
  603. /* keep last */
  604. __HWSIM_TM_ATTR_AFTER_LAST,
  605. HWSIM_TM_ATTR_MAX = __HWSIM_TM_ATTR_AFTER_LAST - 1
  606. };
  607. enum hwsim_testmode_cmd {
  608. HWSIM_TM_CMD_SET_PS = 0,
  609. HWSIM_TM_CMD_GET_PS = 1,
  610. };
  611. static const struct nla_policy hwsim_testmode_policy[HWSIM_TM_ATTR_MAX + 1] = {
  612. [HWSIM_TM_ATTR_CMD] = { .type = NLA_U32 },
  613. [HWSIM_TM_ATTR_PS] = { .type = NLA_U32 },
  614. };
  615. static int hwsim_fops_ps_write(void *dat, u64 val);
  616. static int mac80211_hwsim_testmode_cmd(struct ieee80211_hw *hw,
  617. void *data, int len)
  618. {
  619. struct mac80211_hwsim_data *hwsim = hw->priv;
  620. struct nlattr *tb[HWSIM_TM_ATTR_MAX + 1];
  621. struct sk_buff *skb;
  622. int err, ps;
  623. err = nla_parse(tb, HWSIM_TM_ATTR_MAX, data, len,
  624. hwsim_testmode_policy);
  625. if (err)
  626. return err;
  627. if (!tb[HWSIM_TM_ATTR_CMD])
  628. return -EINVAL;
  629. switch (nla_get_u32(tb[HWSIM_TM_ATTR_CMD])) {
  630. case HWSIM_TM_CMD_SET_PS:
  631. if (!tb[HWSIM_TM_ATTR_PS])
  632. return -EINVAL;
  633. ps = nla_get_u32(tb[HWSIM_TM_ATTR_PS]);
  634. return hwsim_fops_ps_write(hwsim, ps);
  635. case HWSIM_TM_CMD_GET_PS:
  636. skb = cfg80211_testmode_alloc_reply_skb(hw->wiphy,
  637. nla_total_size(sizeof(u32)));
  638. if (!skb)
  639. return -ENOMEM;
  640. NLA_PUT_U32(skb, HWSIM_TM_ATTR_PS, hwsim->ps);
  641. return cfg80211_testmode_reply(skb);
  642. default:
  643. return -EOPNOTSUPP;
  644. }
  645. nla_put_failure:
  646. kfree_skb(skb);
  647. return -ENOBUFS;
  648. }
  649. #endif
  650. static const struct ieee80211_ops mac80211_hwsim_ops =
  651. {
  652. .tx = mac80211_hwsim_tx,
  653. .start = mac80211_hwsim_start,
  654. .stop = mac80211_hwsim_stop,
  655. .add_interface = mac80211_hwsim_add_interface,
  656. .remove_interface = mac80211_hwsim_remove_interface,
  657. .config = mac80211_hwsim_config,
  658. .configure_filter = mac80211_hwsim_configure_filter,
  659. .bss_info_changed = mac80211_hwsim_bss_info_changed,
  660. .sta_notify = mac80211_hwsim_sta_notify,
  661. .set_tim = mac80211_hwsim_set_tim,
  662. .conf_tx = mac80211_hwsim_conf_tx,
  663. CFG80211_TESTMODE_CMD(mac80211_hwsim_testmode_cmd)
  664. };
  665. static void mac80211_hwsim_free(void)
  666. {
  667. struct list_head tmplist, *i, *tmp;
  668. struct mac80211_hwsim_data *data, *tmpdata;
  669. INIT_LIST_HEAD(&tmplist);
  670. spin_lock_bh(&hwsim_radio_lock);
  671. list_for_each_safe(i, tmp, &hwsim_radios)
  672. list_move(i, &tmplist);
  673. spin_unlock_bh(&hwsim_radio_lock);
  674. list_for_each_entry_safe(data, tmpdata, &tmplist, list) {
  675. debugfs_remove(data->debugfs_group);
  676. debugfs_remove(data->debugfs_ps);
  677. debugfs_remove(data->debugfs);
  678. ieee80211_unregister_hw(data->hw);
  679. device_unregister(data->dev);
  680. ieee80211_free_hw(data->hw);
  681. }
  682. class_destroy(hwsim_class);
  683. }
  684. static struct device_driver mac80211_hwsim_driver = {
  685. .name = "mac80211_hwsim"
  686. };
  687. static const struct net_device_ops hwsim_netdev_ops = {
  688. .ndo_start_xmit = hwsim_mon_xmit,
  689. .ndo_change_mtu = eth_change_mtu,
  690. .ndo_set_mac_address = eth_mac_addr,
  691. .ndo_validate_addr = eth_validate_addr,
  692. };
  693. static void hwsim_mon_setup(struct net_device *dev)
  694. {
  695. dev->netdev_ops = &hwsim_netdev_ops;
  696. dev->destructor = free_netdev;
  697. ether_setup(dev);
  698. dev->tx_queue_len = 0;
  699. dev->type = ARPHRD_IEEE80211_RADIOTAP;
  700. memset(dev->dev_addr, 0, ETH_ALEN);
  701. dev->dev_addr[0] = 0x12;
  702. }
  703. static void hwsim_send_ps_poll(void *dat, u8 *mac, struct ieee80211_vif *vif)
  704. {
  705. struct mac80211_hwsim_data *data = dat;
  706. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  707. struct sk_buff *skb;
  708. struct ieee80211_pspoll *pspoll;
  709. if (!vp->assoc)
  710. return;
  711. printk(KERN_DEBUG "%s:%s: send PS-Poll to %pM for aid %d\n",
  712. wiphy_name(data->hw->wiphy), __func__, vp->bssid, vp->aid);
  713. skb = dev_alloc_skb(sizeof(*pspoll));
  714. if (!skb)
  715. return;
  716. pspoll = (void *) skb_put(skb, sizeof(*pspoll));
  717. pspoll->frame_control = cpu_to_le16(IEEE80211_FTYPE_CTL |
  718. IEEE80211_STYPE_PSPOLL |
  719. IEEE80211_FCTL_PM);
  720. pspoll->aid = cpu_to_le16(0xc000 | vp->aid);
  721. memcpy(pspoll->bssid, vp->bssid, ETH_ALEN);
  722. memcpy(pspoll->ta, mac, ETH_ALEN);
  723. if (!mac80211_hwsim_tx_frame(data->hw, skb))
  724. printk(KERN_DEBUG "%s: PS-Poll frame not ack'ed\n", __func__);
  725. dev_kfree_skb(skb);
  726. }
  727. static void hwsim_send_nullfunc(struct mac80211_hwsim_data *data, u8 *mac,
  728. struct ieee80211_vif *vif, int ps)
  729. {
  730. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  731. struct sk_buff *skb;
  732. struct ieee80211_hdr *hdr;
  733. if (!vp->assoc)
  734. return;
  735. printk(KERN_DEBUG "%s:%s: send data::nullfunc to %pM ps=%d\n",
  736. wiphy_name(data->hw->wiphy), __func__, vp->bssid, ps);
  737. skb = dev_alloc_skb(sizeof(*hdr));
  738. if (!skb)
  739. return;
  740. hdr = (void *) skb_put(skb, sizeof(*hdr) - ETH_ALEN);
  741. hdr->frame_control = cpu_to_le16(IEEE80211_FTYPE_DATA |
  742. IEEE80211_STYPE_NULLFUNC |
  743. (ps ? IEEE80211_FCTL_PM : 0));
  744. hdr->duration_id = cpu_to_le16(0);
  745. memcpy(hdr->addr1, vp->bssid, ETH_ALEN);
  746. memcpy(hdr->addr2, mac, ETH_ALEN);
  747. memcpy(hdr->addr3, vp->bssid, ETH_ALEN);
  748. if (!mac80211_hwsim_tx_frame(data->hw, skb))
  749. printk(KERN_DEBUG "%s: nullfunc frame not ack'ed\n", __func__);
  750. dev_kfree_skb(skb);
  751. }
  752. static void hwsim_send_nullfunc_ps(void *dat, u8 *mac,
  753. struct ieee80211_vif *vif)
  754. {
  755. struct mac80211_hwsim_data *data = dat;
  756. hwsim_send_nullfunc(data, mac, vif, 1);
  757. }
  758. static void hwsim_send_nullfunc_no_ps(void *dat, u8 *mac,
  759. struct ieee80211_vif *vif)
  760. {
  761. struct mac80211_hwsim_data *data = dat;
  762. hwsim_send_nullfunc(data, mac, vif, 0);
  763. }
  764. static int hwsim_fops_ps_read(void *dat, u64 *val)
  765. {
  766. struct mac80211_hwsim_data *data = dat;
  767. *val = data->ps;
  768. return 0;
  769. }
  770. static int hwsim_fops_ps_write(void *dat, u64 val)
  771. {
  772. struct mac80211_hwsim_data *data = dat;
  773. enum ps_mode old_ps;
  774. if (val != PS_DISABLED && val != PS_ENABLED && val != PS_AUTO_POLL &&
  775. val != PS_MANUAL_POLL)
  776. return -EINVAL;
  777. old_ps = data->ps;
  778. data->ps = val;
  779. if (val == PS_MANUAL_POLL) {
  780. ieee80211_iterate_active_interfaces(data->hw,
  781. hwsim_send_ps_poll, data);
  782. data->ps_poll_pending = true;
  783. } else if (old_ps == PS_DISABLED && val != PS_DISABLED) {
  784. ieee80211_iterate_active_interfaces(data->hw,
  785. hwsim_send_nullfunc_ps,
  786. data);
  787. } else if (old_ps != PS_DISABLED && val == PS_DISABLED) {
  788. ieee80211_iterate_active_interfaces(data->hw,
  789. hwsim_send_nullfunc_no_ps,
  790. data);
  791. }
  792. return 0;
  793. }
  794. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_ps, hwsim_fops_ps_read, hwsim_fops_ps_write,
  795. "%llu\n");
  796. static int hwsim_fops_group_read(void *dat, u64 *val)
  797. {
  798. struct mac80211_hwsim_data *data = dat;
  799. *val = data->group;
  800. return 0;
  801. }
  802. static int hwsim_fops_group_write(void *dat, u64 val)
  803. {
  804. struct mac80211_hwsim_data *data = dat;
  805. data->group = val;
  806. return 0;
  807. }
  808. DEFINE_SIMPLE_ATTRIBUTE(hwsim_fops_group,
  809. hwsim_fops_group_read, hwsim_fops_group_write,
  810. "%llx\n");
  811. static int __init init_mac80211_hwsim(void)
  812. {
  813. int i, err = 0;
  814. u8 addr[ETH_ALEN];
  815. struct mac80211_hwsim_data *data;
  816. struct ieee80211_hw *hw;
  817. enum ieee80211_band band;
  818. if (radios < 1 || radios > 100)
  819. return -EINVAL;
  820. spin_lock_init(&hwsim_radio_lock);
  821. INIT_LIST_HEAD(&hwsim_radios);
  822. hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
  823. if (IS_ERR(hwsim_class))
  824. return PTR_ERR(hwsim_class);
  825. memset(addr, 0, ETH_ALEN);
  826. addr[0] = 0x02;
  827. for (i = 0; i < radios; i++) {
  828. printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
  829. i);
  830. hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
  831. if (!hw) {
  832. printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
  833. "failed\n");
  834. err = -ENOMEM;
  835. goto failed;
  836. }
  837. data = hw->priv;
  838. data->hw = hw;
  839. data->dev = device_create(hwsim_class, NULL, 0, hw,
  840. "hwsim%d", i);
  841. if (IS_ERR(data->dev)) {
  842. printk(KERN_DEBUG
  843. "mac80211_hwsim: device_create "
  844. "failed (%ld)\n", PTR_ERR(data->dev));
  845. err = -ENOMEM;
  846. goto failed_drvdata;
  847. }
  848. data->dev->driver = &mac80211_hwsim_driver;
  849. SET_IEEE80211_DEV(hw, data->dev);
  850. addr[3] = i >> 8;
  851. addr[4] = i;
  852. SET_IEEE80211_PERM_ADDR(hw, addr);
  853. hw->channel_change_time = 1;
  854. hw->queues = 4;
  855. hw->wiphy->interface_modes =
  856. BIT(NL80211_IFTYPE_STATION) |
  857. BIT(NL80211_IFTYPE_AP) |
  858. BIT(NL80211_IFTYPE_MESH_POINT);
  859. hw->flags = IEEE80211_HW_MFP_CAPABLE |
  860. IEEE80211_HW_SIGNAL_DBM;
  861. /* ask mac80211 to reserve space for magic */
  862. hw->vif_data_size = sizeof(struct hwsim_vif_priv);
  863. hw->sta_data_size = sizeof(struct hwsim_sta_priv);
  864. memcpy(data->channels_2ghz, hwsim_channels_2ghz,
  865. sizeof(hwsim_channels_2ghz));
  866. memcpy(data->channels_5ghz, hwsim_channels_5ghz,
  867. sizeof(hwsim_channels_5ghz));
  868. memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
  869. for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
  870. struct ieee80211_supported_band *sband = &data->bands[band];
  871. switch (band) {
  872. case IEEE80211_BAND_2GHZ:
  873. sband->channels = data->channels_2ghz;
  874. sband->n_channels =
  875. ARRAY_SIZE(hwsim_channels_2ghz);
  876. break;
  877. case IEEE80211_BAND_5GHZ:
  878. sband->channels = data->channels_5ghz;
  879. sband->n_channels =
  880. ARRAY_SIZE(hwsim_channels_5ghz);
  881. break;
  882. default:
  883. break;
  884. }
  885. sband->bitrates = data->rates;
  886. sband->n_bitrates = ARRAY_SIZE(hwsim_rates);
  887. sband->ht_cap.ht_supported = true;
  888. sband->ht_cap.cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  889. IEEE80211_HT_CAP_GRN_FLD |
  890. IEEE80211_HT_CAP_SGI_40 |
  891. IEEE80211_HT_CAP_DSSSCCK40;
  892. sband->ht_cap.ampdu_factor = 0x3;
  893. sband->ht_cap.ampdu_density = 0x6;
  894. memset(&sband->ht_cap.mcs, 0,
  895. sizeof(sband->ht_cap.mcs));
  896. sband->ht_cap.mcs.rx_mask[0] = 0xff;
  897. sband->ht_cap.mcs.rx_mask[1] = 0xff;
  898. sband->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  899. hw->wiphy->bands[band] = sband;
  900. }
  901. /* By default all radios are belonging to the first group */
  902. data->group = 1;
  903. /* Work to be done prior to ieee80211_register_hw() */
  904. switch (regtest) {
  905. case HWSIM_REGTEST_DISABLED:
  906. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  907. case HWSIM_REGTEST_DRIVER_REG_ALL:
  908. case HWSIM_REGTEST_DIFF_COUNTRY:
  909. /*
  910. * Nothing to be done for driver regulatory domain
  911. * hints prior to ieee80211_register_hw()
  912. */
  913. break;
  914. case HWSIM_REGTEST_WORLD_ROAM:
  915. if (i == 0) {
  916. hw->wiphy->custom_regulatory = true;
  917. wiphy_apply_custom_regulatory(hw->wiphy,
  918. &hwsim_world_regdom_custom_01);
  919. }
  920. break;
  921. case HWSIM_REGTEST_CUSTOM_WORLD:
  922. hw->wiphy->custom_regulatory = true;
  923. wiphy_apply_custom_regulatory(hw->wiphy,
  924. &hwsim_world_regdom_custom_01);
  925. break;
  926. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  927. if (i == 0) {
  928. hw->wiphy->custom_regulatory = true;
  929. wiphy_apply_custom_regulatory(hw->wiphy,
  930. &hwsim_world_regdom_custom_01);
  931. } else if (i == 1) {
  932. hw->wiphy->custom_regulatory = true;
  933. wiphy_apply_custom_regulatory(hw->wiphy,
  934. &hwsim_world_regdom_custom_02);
  935. }
  936. break;
  937. case HWSIM_REGTEST_STRICT_ALL:
  938. hw->wiphy->strict_regulatory = true;
  939. break;
  940. case HWSIM_REGTEST_STRICT_FOLLOW:
  941. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  942. if (i == 0)
  943. hw->wiphy->strict_regulatory = true;
  944. break;
  945. case HWSIM_REGTEST_ALL:
  946. if (i == 0) {
  947. hw->wiphy->custom_regulatory = true;
  948. wiphy_apply_custom_regulatory(hw->wiphy,
  949. &hwsim_world_regdom_custom_01);
  950. } else if (i == 1) {
  951. hw->wiphy->custom_regulatory = true;
  952. wiphy_apply_custom_regulatory(hw->wiphy,
  953. &hwsim_world_regdom_custom_02);
  954. } else if (i == 4)
  955. hw->wiphy->strict_regulatory = true;
  956. break;
  957. default:
  958. break;
  959. }
  960. /* give the regulatory workqueue a chance to run */
  961. if (regtest)
  962. schedule_timeout_interruptible(1);
  963. err = ieee80211_register_hw(hw);
  964. if (err < 0) {
  965. printk(KERN_DEBUG "mac80211_hwsim: "
  966. "ieee80211_register_hw failed (%d)\n", err);
  967. goto failed_hw;
  968. }
  969. /* Work to be done after to ieee80211_register_hw() */
  970. switch (regtest) {
  971. case HWSIM_REGTEST_WORLD_ROAM:
  972. case HWSIM_REGTEST_DISABLED:
  973. break;
  974. case HWSIM_REGTEST_DRIVER_REG_FOLLOW:
  975. if (!i)
  976. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  977. break;
  978. case HWSIM_REGTEST_DRIVER_REG_ALL:
  979. case HWSIM_REGTEST_STRICT_ALL:
  980. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  981. break;
  982. case HWSIM_REGTEST_DIFF_COUNTRY:
  983. if (i < ARRAY_SIZE(hwsim_alpha2s))
  984. regulatory_hint(hw->wiphy, hwsim_alpha2s[i]);
  985. break;
  986. case HWSIM_REGTEST_CUSTOM_WORLD:
  987. case HWSIM_REGTEST_CUSTOM_WORLD_2:
  988. /*
  989. * Nothing to be done for custom world regulatory
  990. * domains after to ieee80211_register_hw
  991. */
  992. break;
  993. case HWSIM_REGTEST_STRICT_FOLLOW:
  994. if (i == 0)
  995. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  996. break;
  997. case HWSIM_REGTEST_STRICT_AND_DRIVER_REG:
  998. if (i == 0)
  999. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1000. else if (i == 1)
  1001. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  1002. break;
  1003. case HWSIM_REGTEST_ALL:
  1004. if (i == 2)
  1005. regulatory_hint(hw->wiphy, hwsim_alpha2s[0]);
  1006. else if (i == 3)
  1007. regulatory_hint(hw->wiphy, hwsim_alpha2s[1]);
  1008. else if (i == 4)
  1009. regulatory_hint(hw->wiphy, hwsim_alpha2s[2]);
  1010. break;
  1011. default:
  1012. break;
  1013. }
  1014. printk(KERN_DEBUG "%s: hwaddr %pM registered\n",
  1015. wiphy_name(hw->wiphy),
  1016. hw->wiphy->perm_addr);
  1017. data->debugfs = debugfs_create_dir("hwsim",
  1018. hw->wiphy->debugfsdir);
  1019. data->debugfs_ps = debugfs_create_file("ps", 0666,
  1020. data->debugfs, data,
  1021. &hwsim_fops_ps);
  1022. data->debugfs_group = debugfs_create_file("group", 0666,
  1023. data->debugfs, data,
  1024. &hwsim_fops_group);
  1025. setup_timer(&data->beacon_timer, mac80211_hwsim_beacon,
  1026. (unsigned long) hw);
  1027. list_add_tail(&data->list, &hwsim_radios);
  1028. }
  1029. hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
  1030. if (hwsim_mon == NULL)
  1031. goto failed;
  1032. rtnl_lock();
  1033. err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
  1034. if (err < 0)
  1035. goto failed_mon;
  1036. err = register_netdevice(hwsim_mon);
  1037. if (err < 0)
  1038. goto failed_mon;
  1039. rtnl_unlock();
  1040. return 0;
  1041. failed_mon:
  1042. rtnl_unlock();
  1043. free_netdev(hwsim_mon);
  1044. mac80211_hwsim_free();
  1045. return err;
  1046. failed_hw:
  1047. device_unregister(data->dev);
  1048. failed_drvdata:
  1049. ieee80211_free_hw(hw);
  1050. failed:
  1051. mac80211_hwsim_free();
  1052. return err;
  1053. }
  1054. static void __exit exit_mac80211_hwsim(void)
  1055. {
  1056. printk(KERN_DEBUG "mac80211_hwsim: unregister radios\n");
  1057. mac80211_hwsim_free();
  1058. unregister_netdev(hwsim_mon);
  1059. }
  1060. module_init(init_mac80211_hwsim);
  1061. module_exit(exit_mac80211_hwsim);