mac80211_hwsim.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. * - IEEE 802.11a and 802.11n modes
  13. * - RX filtering based on filter configuration (data->rx_filter)
  14. */
  15. #include <net/mac80211.h>
  16. #include <net/ieee80211_radiotap.h>
  17. #include <linux/if_arp.h>
  18. #include <linux/rtnetlink.h>
  19. #include <linux/etherdevice.h>
  20. MODULE_AUTHOR("Jouni Malinen");
  21. MODULE_DESCRIPTION("Software simulator of 802.11 radio(s) for mac80211");
  22. MODULE_LICENSE("GPL");
  23. static int radios = 2;
  24. module_param(radios, int, 0444);
  25. MODULE_PARM_DESC(radios, "Number of simulated radios");
  26. struct hwsim_vif_priv {
  27. u32 magic;
  28. };
  29. #define HWSIM_VIF_MAGIC 0x69537748
  30. static inline void hwsim_check_magic(struct ieee80211_vif *vif)
  31. {
  32. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  33. WARN_ON(vp->magic != HWSIM_VIF_MAGIC);
  34. }
  35. static inline void hwsim_set_magic(struct ieee80211_vif *vif)
  36. {
  37. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  38. vp->magic = HWSIM_VIF_MAGIC;
  39. }
  40. static inline void hwsim_clear_magic(struct ieee80211_vif *vif)
  41. {
  42. struct hwsim_vif_priv *vp = (void *)vif->drv_priv;
  43. vp->magic = 0;
  44. }
  45. static struct class *hwsim_class;
  46. static struct ieee80211_hw **hwsim_radios;
  47. static int hwsim_radio_count;
  48. static struct net_device *hwsim_mon; /* global monitor netdev */
  49. static const struct ieee80211_channel hwsim_channels[] = {
  50. { .center_freq = 2412 },
  51. { .center_freq = 2417 },
  52. { .center_freq = 2422 },
  53. { .center_freq = 2427 },
  54. { .center_freq = 2432 },
  55. { .center_freq = 2437 },
  56. { .center_freq = 2442 },
  57. { .center_freq = 2447 },
  58. { .center_freq = 2452 },
  59. { .center_freq = 2457 },
  60. { .center_freq = 2462 },
  61. { .center_freq = 2467 },
  62. { .center_freq = 2472 },
  63. { .center_freq = 2484 },
  64. };
  65. static const struct ieee80211_rate hwsim_rates[] = {
  66. { .bitrate = 10 },
  67. { .bitrate = 20, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  68. { .bitrate = 55, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  69. { .bitrate = 110, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
  70. { .bitrate = 60 },
  71. { .bitrate = 90 },
  72. { .bitrate = 120 },
  73. { .bitrate = 180 },
  74. { .bitrate = 240 },
  75. { .bitrate = 360 },
  76. { .bitrate = 480 },
  77. { .bitrate = 540 }
  78. };
  79. struct mac80211_hwsim_data {
  80. struct device *dev;
  81. struct ieee80211_supported_band band;
  82. struct ieee80211_channel channels[ARRAY_SIZE(hwsim_channels)];
  83. struct ieee80211_rate rates[ARRAY_SIZE(hwsim_rates)];
  84. struct ieee80211_channel *channel;
  85. int radio_enabled;
  86. unsigned long beacon_int; /* in jiffies unit */
  87. unsigned int rx_filter;
  88. int started;
  89. struct timer_list beacon_timer;
  90. };
  91. struct hwsim_radiotap_hdr {
  92. struct ieee80211_radiotap_header hdr;
  93. u8 rt_flags;
  94. u8 rt_rate;
  95. __le16 rt_channel;
  96. __le16 rt_chbitmask;
  97. } __attribute__ ((packed));
  98. static int hwsim_mon_xmit(struct sk_buff *skb, struct net_device *dev)
  99. {
  100. /* TODO: allow packet injection */
  101. dev_kfree_skb(skb);
  102. return 0;
  103. }
  104. static void mac80211_hwsim_monitor_rx(struct ieee80211_hw *hw,
  105. struct sk_buff *tx_skb)
  106. {
  107. struct mac80211_hwsim_data *data = hw->priv;
  108. struct sk_buff *skb;
  109. struct hwsim_radiotap_hdr *hdr;
  110. u16 flags;
  111. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_skb);
  112. struct ieee80211_rate *txrate = ieee80211_get_tx_rate(hw, info);
  113. if (!netif_running(hwsim_mon))
  114. return;
  115. skb = skb_copy_expand(tx_skb, sizeof(*hdr), 0, GFP_ATOMIC);
  116. if (skb == NULL)
  117. return;
  118. hdr = (struct hwsim_radiotap_hdr *) skb_push(skb, sizeof(*hdr));
  119. hdr->hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  120. hdr->hdr.it_pad = 0;
  121. hdr->hdr.it_len = cpu_to_le16(sizeof(*hdr));
  122. hdr->hdr.it_present = cpu_to_le32((1 << IEEE80211_RADIOTAP_FLAGS) |
  123. (1 << IEEE80211_RADIOTAP_RATE) |
  124. (1 << IEEE80211_RADIOTAP_CHANNEL));
  125. hdr->rt_flags = 0;
  126. hdr->rt_rate = txrate->bitrate / 5;
  127. hdr->rt_channel = cpu_to_le16(data->channel->center_freq);
  128. flags = IEEE80211_CHAN_2GHZ;
  129. if (txrate->flags & IEEE80211_RATE_ERP_G)
  130. flags |= IEEE80211_CHAN_OFDM;
  131. else
  132. flags |= IEEE80211_CHAN_CCK;
  133. hdr->rt_chbitmask = cpu_to_le16(flags);
  134. skb->dev = hwsim_mon;
  135. skb_set_mac_header(skb, 0);
  136. skb->ip_summed = CHECKSUM_UNNECESSARY;
  137. skb->pkt_type = PACKET_OTHERHOST;
  138. skb->protocol = htons(ETH_P_802_2);
  139. memset(skb->cb, 0, sizeof(skb->cb));
  140. netif_rx(skb);
  141. }
  142. static int mac80211_hwsim_tx_frame(struct ieee80211_hw *hw,
  143. struct sk_buff *skb)
  144. {
  145. struct mac80211_hwsim_data *data = hw->priv;
  146. int i, ack = 0;
  147. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  148. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  149. struct ieee80211_rx_status rx_status;
  150. memset(&rx_status, 0, sizeof(rx_status));
  151. /* TODO: set mactime */
  152. rx_status.freq = data->channel->center_freq;
  153. rx_status.band = data->channel->band;
  154. rx_status.rate_idx = info->tx_rate_idx;
  155. /* TODO: simulate signal strength (and optional packet drop) */
  156. /* Copy skb to all enabled radios that are on the current frequency */
  157. for (i = 0; i < hwsim_radio_count; i++) {
  158. struct mac80211_hwsim_data *data2;
  159. struct sk_buff *nskb;
  160. if (hwsim_radios[i] == NULL || hwsim_radios[i] == hw)
  161. continue;
  162. data2 = hwsim_radios[i]->priv;
  163. if (!data2->started || !data2->radio_enabled ||
  164. data->channel->center_freq != data2->channel->center_freq)
  165. continue;
  166. nskb = skb_copy(skb, GFP_ATOMIC);
  167. if (nskb == NULL)
  168. continue;
  169. if (memcmp(hdr->addr1, hwsim_radios[i]->wiphy->perm_addr,
  170. ETH_ALEN) == 0)
  171. ack = 1;
  172. ieee80211_rx_irqsafe(hwsim_radios[i], nskb, &rx_status);
  173. }
  174. return ack;
  175. }
  176. static int mac80211_hwsim_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  177. {
  178. struct mac80211_hwsim_data *data = hw->priv;
  179. int ack;
  180. struct ieee80211_tx_info *txi;
  181. mac80211_hwsim_monitor_rx(hw, skb);
  182. if (skb->len < 10) {
  183. /* Should not happen; just a sanity check for addr1 use */
  184. dev_kfree_skb(skb);
  185. return NETDEV_TX_OK;
  186. }
  187. if (!data->radio_enabled) {
  188. printk(KERN_DEBUG "%s: dropped TX frame since radio "
  189. "disabled\n", wiphy_name(hw->wiphy));
  190. dev_kfree_skb(skb);
  191. return NETDEV_TX_OK;
  192. }
  193. ack = mac80211_hwsim_tx_frame(hw, skb);
  194. txi = IEEE80211_SKB_CB(skb);
  195. hwsim_check_magic(txi->control.vif);
  196. memset(&txi->status, 0, sizeof(txi->status));
  197. if (!(txi->flags & IEEE80211_TX_CTL_NO_ACK)) {
  198. if (ack)
  199. txi->flags |= IEEE80211_TX_STAT_ACK;
  200. else
  201. txi->status.excessive_retries = 1;
  202. }
  203. ieee80211_tx_status_irqsafe(hw, skb);
  204. return NETDEV_TX_OK;
  205. }
  206. static int mac80211_hwsim_start(struct ieee80211_hw *hw)
  207. {
  208. struct mac80211_hwsim_data *data = hw->priv;
  209. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  210. data->started = 1;
  211. return 0;
  212. }
  213. static void mac80211_hwsim_stop(struct ieee80211_hw *hw)
  214. {
  215. struct mac80211_hwsim_data *data = hw->priv;
  216. data->started = 0;
  217. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  218. }
  219. static int mac80211_hwsim_add_interface(struct ieee80211_hw *hw,
  220. struct ieee80211_if_init_conf *conf)
  221. {
  222. DECLARE_MAC_BUF(mac);
  223. printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%s)\n",
  224. wiphy_name(hw->wiphy), __func__, conf->type,
  225. print_mac(mac, conf->mac_addr));
  226. hwsim_set_magic(conf->vif);
  227. return 0;
  228. }
  229. static void mac80211_hwsim_remove_interface(
  230. struct ieee80211_hw *hw, struct ieee80211_if_init_conf *conf)
  231. {
  232. DECLARE_MAC_BUF(mac);
  233. printk(KERN_DEBUG "%s:%s (type=%d mac_addr=%s)\n",
  234. wiphy_name(hw->wiphy), __func__, conf->type,
  235. print_mac(mac, conf->mac_addr));
  236. hwsim_check_magic(conf->vif);
  237. hwsim_clear_magic(conf->vif);
  238. }
  239. static void mac80211_hwsim_beacon_tx(void *arg, u8 *mac,
  240. struct ieee80211_vif *vif)
  241. {
  242. struct ieee80211_hw *hw = arg;
  243. struct sk_buff *skb;
  244. struct ieee80211_tx_info *info;
  245. hwsim_check_magic(vif);
  246. if (vif->type != NL80211_IFTYPE_AP)
  247. return;
  248. skb = ieee80211_beacon_get(hw, vif);
  249. if (skb == NULL)
  250. return;
  251. info = IEEE80211_SKB_CB(skb);
  252. mac80211_hwsim_monitor_rx(hw, skb);
  253. mac80211_hwsim_tx_frame(hw, skb);
  254. dev_kfree_skb(skb);
  255. }
  256. static void mac80211_hwsim_beacon(unsigned long arg)
  257. {
  258. struct ieee80211_hw *hw = (struct ieee80211_hw *) arg;
  259. struct mac80211_hwsim_data *data = hw->priv;
  260. if (!data->started || !data->radio_enabled)
  261. return;
  262. ieee80211_iterate_active_interfaces_atomic(
  263. hw, mac80211_hwsim_beacon_tx, hw);
  264. data->beacon_timer.expires = jiffies + data->beacon_int;
  265. add_timer(&data->beacon_timer);
  266. }
  267. static int mac80211_hwsim_config(struct ieee80211_hw *hw,
  268. struct ieee80211_conf *conf)
  269. {
  270. struct mac80211_hwsim_data *data = hw->priv;
  271. printk(KERN_DEBUG "%s:%s (freq=%d radio_enabled=%d beacon_int=%d)\n",
  272. wiphy_name(hw->wiphy), __func__,
  273. conf->channel->center_freq, conf->radio_enabled,
  274. conf->beacon_int);
  275. data->channel = conf->channel;
  276. data->radio_enabled = conf->radio_enabled;
  277. data->beacon_int = 1024 * conf->beacon_int / 1000 * HZ / 1000;
  278. if (data->beacon_int < 1)
  279. data->beacon_int = 1;
  280. if (!data->started || !data->radio_enabled)
  281. del_timer(&data->beacon_timer);
  282. else
  283. mod_timer(&data->beacon_timer, jiffies + data->beacon_int);
  284. return 0;
  285. }
  286. static void mac80211_hwsim_configure_filter(struct ieee80211_hw *hw,
  287. unsigned int changed_flags,
  288. unsigned int *total_flags,
  289. int mc_count,
  290. struct dev_addr_list *mc_list)
  291. {
  292. struct mac80211_hwsim_data *data = hw->priv;
  293. printk(KERN_DEBUG "%s:%s\n", wiphy_name(hw->wiphy), __func__);
  294. data->rx_filter = 0;
  295. if (*total_flags & FIF_PROMISC_IN_BSS)
  296. data->rx_filter |= FIF_PROMISC_IN_BSS;
  297. if (*total_flags & FIF_ALLMULTI)
  298. data->rx_filter |= FIF_ALLMULTI;
  299. *total_flags = data->rx_filter;
  300. }
  301. static int mac80211_hwsim_config_interface(struct ieee80211_hw *hw,
  302. struct ieee80211_vif *vif,
  303. struct ieee80211_if_conf *conf)
  304. {
  305. hwsim_check_magic(vif);
  306. return 0;
  307. }
  308. static void mac80211_hwsim_bss_info_changed(struct ieee80211_hw *hw,
  309. struct ieee80211_vif *vif,
  310. struct ieee80211_bss_conf *info,
  311. u32 changed)
  312. {
  313. hwsim_check_magic(vif);
  314. }
  315. static void mac80211_hwsim_sta_notify(struct ieee80211_hw *hw,
  316. struct ieee80211_vif *vif,
  317. enum sta_notify_cmd cmd, const u8 *addr)
  318. {
  319. hwsim_check_magic(vif);
  320. }
  321. static const struct ieee80211_ops mac80211_hwsim_ops =
  322. {
  323. .tx = mac80211_hwsim_tx,
  324. .start = mac80211_hwsim_start,
  325. .stop = mac80211_hwsim_stop,
  326. .add_interface = mac80211_hwsim_add_interface,
  327. .remove_interface = mac80211_hwsim_remove_interface,
  328. .config = mac80211_hwsim_config,
  329. .configure_filter = mac80211_hwsim_configure_filter,
  330. .config_interface = mac80211_hwsim_config_interface,
  331. .bss_info_changed = mac80211_hwsim_bss_info_changed,
  332. .sta_notify = mac80211_hwsim_sta_notify,
  333. };
  334. static void mac80211_hwsim_free(void)
  335. {
  336. int i;
  337. for (i = 0; i < hwsim_radio_count; i++) {
  338. if (hwsim_radios[i]) {
  339. struct mac80211_hwsim_data *data;
  340. data = hwsim_radios[i]->priv;
  341. ieee80211_unregister_hw(hwsim_radios[i]);
  342. device_unregister(data->dev);
  343. ieee80211_free_hw(hwsim_radios[i]);
  344. }
  345. }
  346. kfree(hwsim_radios);
  347. class_destroy(hwsim_class);
  348. }
  349. static struct device_driver mac80211_hwsim_driver = {
  350. .name = "mac80211_hwsim"
  351. };
  352. static void hwsim_mon_setup(struct net_device *dev)
  353. {
  354. dev->hard_start_xmit = hwsim_mon_xmit;
  355. dev->destructor = free_netdev;
  356. ether_setup(dev);
  357. dev->tx_queue_len = 0;
  358. dev->type = ARPHRD_IEEE80211_RADIOTAP;
  359. memset(dev->dev_addr, 0, ETH_ALEN);
  360. dev->dev_addr[0] = 0x12;
  361. }
  362. static int __init init_mac80211_hwsim(void)
  363. {
  364. int i, err = 0;
  365. u8 addr[ETH_ALEN];
  366. struct mac80211_hwsim_data *data;
  367. struct ieee80211_hw *hw;
  368. DECLARE_MAC_BUF(mac);
  369. if (radios < 1 || radios > 65535)
  370. return -EINVAL;
  371. hwsim_radio_count = radios;
  372. hwsim_radios = kcalloc(hwsim_radio_count,
  373. sizeof(struct ieee80211_hw *), GFP_KERNEL);
  374. if (hwsim_radios == NULL)
  375. return -ENOMEM;
  376. hwsim_class = class_create(THIS_MODULE, "mac80211_hwsim");
  377. if (IS_ERR(hwsim_class)) {
  378. kfree(hwsim_radios);
  379. return PTR_ERR(hwsim_class);
  380. }
  381. memset(addr, 0, ETH_ALEN);
  382. addr[0] = 0x02;
  383. for (i = 0; i < hwsim_radio_count; i++) {
  384. printk(KERN_DEBUG "mac80211_hwsim: Initializing radio %d\n",
  385. i);
  386. hw = ieee80211_alloc_hw(sizeof(*data), &mac80211_hwsim_ops);
  387. if (hw == NULL) {
  388. printk(KERN_DEBUG "mac80211_hwsim: ieee80211_alloc_hw "
  389. "failed\n");
  390. err = -ENOMEM;
  391. goto failed;
  392. }
  393. hwsim_radios[i] = hw;
  394. data = hw->priv;
  395. data->dev = device_create_drvdata(hwsim_class, NULL, 0, hw,
  396. "hwsim%d", i);
  397. if (IS_ERR(data->dev)) {
  398. printk(KERN_DEBUG
  399. "mac80211_hwsim: device_create_drvdata "
  400. "failed (%ld)\n", PTR_ERR(data->dev));
  401. err = -ENOMEM;
  402. goto failed_drvdata;
  403. }
  404. data->dev->driver = &mac80211_hwsim_driver;
  405. SET_IEEE80211_DEV(hw, data->dev);
  406. addr[3] = i >> 8;
  407. addr[4] = i;
  408. SET_IEEE80211_PERM_ADDR(hw, addr);
  409. hw->channel_change_time = 1;
  410. hw->queues = 4;
  411. hw->wiphy->interface_modes =
  412. BIT(NL80211_IFTYPE_STATION) |
  413. BIT(NL80211_IFTYPE_AP);
  414. hw->ampdu_queues = 1;
  415. /* ask mac80211 to reserve space for magic */
  416. hw->vif_data_size = sizeof(struct hwsim_vif_priv);
  417. memcpy(data->channels, hwsim_channels, sizeof(hwsim_channels));
  418. memcpy(data->rates, hwsim_rates, sizeof(hwsim_rates));
  419. data->band.channels = data->channels;
  420. data->band.n_channels = ARRAY_SIZE(hwsim_channels);
  421. data->band.bitrates = data->rates;
  422. data->band.n_bitrates = ARRAY_SIZE(hwsim_rates);
  423. data->band.ht_info.ht_supported = 1;
  424. data->band.ht_info.cap = IEEE80211_HT_CAP_SUP_WIDTH |
  425. IEEE80211_HT_CAP_GRN_FLD |
  426. IEEE80211_HT_CAP_SGI_40 |
  427. IEEE80211_HT_CAP_DSSSCCK40;
  428. data->band.ht_info.ampdu_factor = 0x3;
  429. data->band.ht_info.ampdu_density = 0x6;
  430. memset(data->band.ht_info.supp_mcs_set, 0,
  431. sizeof(data->band.ht_info.supp_mcs_set));
  432. data->band.ht_info.supp_mcs_set[0] = 0xff;
  433. data->band.ht_info.supp_mcs_set[1] = 0xff;
  434. data->band.ht_info.supp_mcs_set[12] =
  435. IEEE80211_HT_CAP_MCS_TX_DEFINED;
  436. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &data->band;
  437. err = ieee80211_register_hw(hw);
  438. if (err < 0) {
  439. printk(KERN_DEBUG "mac80211_hwsim: "
  440. "ieee80211_register_hw failed (%d)\n", err);
  441. goto failed_hw;
  442. }
  443. printk(KERN_DEBUG "%s: hwaddr %s registered\n",
  444. wiphy_name(hw->wiphy),
  445. print_mac(mac, hw->wiphy->perm_addr));
  446. setup_timer(&data->beacon_timer, mac80211_hwsim_beacon,
  447. (unsigned long) hw);
  448. }
  449. hwsim_mon = alloc_netdev(0, "hwsim%d", hwsim_mon_setup);
  450. if (hwsim_mon == NULL)
  451. goto failed;
  452. rtnl_lock();
  453. err = dev_alloc_name(hwsim_mon, hwsim_mon->name);
  454. if (err < 0)
  455. goto failed_mon;
  456. err = register_netdevice(hwsim_mon);
  457. if (err < 0)
  458. goto failed_mon;
  459. rtnl_unlock();
  460. return 0;
  461. failed_mon:
  462. rtnl_unlock();
  463. free_netdev(hwsim_mon);
  464. mac80211_hwsim_free();
  465. return err;
  466. failed_hw:
  467. device_unregister(data->dev);
  468. failed_drvdata:
  469. ieee80211_free_hw(hw);
  470. hwsim_radios[i] = NULL;
  471. failed:
  472. mac80211_hwsim_free();
  473. return err;
  474. }
  475. static void __exit exit_mac80211_hwsim(void)
  476. {
  477. printk(KERN_DEBUG "mac80211_hwsim: unregister %d radios\n",
  478. hwsim_radio_count);
  479. unregister_netdev(hwsim_mon);
  480. mac80211_hwsim_free();
  481. }
  482. module_init(init_mac80211_hwsim);
  483. module_exit(exit_mac80211_hwsim);