mac80211_hwsim.c 13 KB

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