core.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * This is the linux wireless configuration interface.
  3. *
  4. * Copyright 2006, 2007 Johannes Berg <johannes@sipsolutions.net>
  5. */
  6. #include <linux/if.h>
  7. #include <linux/module.h>
  8. #include <linux/err.h>
  9. #include <linux/mutex.h>
  10. #include <linux/list.h>
  11. #include <linux/nl80211.h>
  12. #include <linux/debugfs.h>
  13. #include <linux/notifier.h>
  14. #include <linux/device.h>
  15. #include <net/genetlink.h>
  16. #include <net/cfg80211.h>
  17. #include <net/wireless.h>
  18. #include "nl80211.h"
  19. #include "core.h"
  20. #include "sysfs.h"
  21. /* name for sysfs, %d is appended */
  22. #define PHY_NAME "phy"
  23. MODULE_AUTHOR("Johannes Berg");
  24. MODULE_LICENSE("GPL");
  25. MODULE_DESCRIPTION("wireless configuration support");
  26. /* RCU might be appropriate here since we usually
  27. * only read the list, and that can happen quite
  28. * often because we need to do it for each command */
  29. LIST_HEAD(cfg80211_drv_list);
  30. DEFINE_MUTEX(cfg80211_drv_mutex);
  31. static int wiphy_counter;
  32. /* for debugfs */
  33. static struct dentry *ieee80211_debugfs_dir;
  34. /* requires cfg80211_drv_mutex to be held! */
  35. static struct cfg80211_registered_device *cfg80211_drv_by_wiphy(int wiphy)
  36. {
  37. struct cfg80211_registered_device *result = NULL, *drv;
  38. list_for_each_entry(drv, &cfg80211_drv_list, list) {
  39. if (drv->idx == wiphy) {
  40. result = drv;
  41. break;
  42. }
  43. }
  44. return result;
  45. }
  46. /* requires cfg80211_drv_mutex to be held! */
  47. static struct cfg80211_registered_device *
  48. __cfg80211_drv_from_info(struct genl_info *info)
  49. {
  50. int ifindex;
  51. struct cfg80211_registered_device *bywiphy = NULL, *byifidx = NULL;
  52. struct net_device *dev;
  53. int err = -EINVAL;
  54. if (info->attrs[NL80211_ATTR_WIPHY]) {
  55. bywiphy = cfg80211_drv_by_wiphy(
  56. nla_get_u32(info->attrs[NL80211_ATTR_WIPHY]));
  57. err = -ENODEV;
  58. }
  59. if (info->attrs[NL80211_ATTR_IFINDEX]) {
  60. ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
  61. dev = dev_get_by_index(&init_net, ifindex);
  62. if (dev) {
  63. if (dev->ieee80211_ptr)
  64. byifidx =
  65. wiphy_to_dev(dev->ieee80211_ptr->wiphy);
  66. dev_put(dev);
  67. }
  68. err = -ENODEV;
  69. }
  70. if (bywiphy && byifidx) {
  71. if (bywiphy != byifidx)
  72. return ERR_PTR(-EINVAL);
  73. else
  74. return bywiphy; /* == byifidx */
  75. }
  76. if (bywiphy)
  77. return bywiphy;
  78. if (byifidx)
  79. return byifidx;
  80. return ERR_PTR(err);
  81. }
  82. struct cfg80211_registered_device *
  83. cfg80211_get_dev_from_info(struct genl_info *info)
  84. {
  85. struct cfg80211_registered_device *drv;
  86. mutex_lock(&cfg80211_drv_mutex);
  87. drv = __cfg80211_drv_from_info(info);
  88. /* if it is not an error we grab the lock on
  89. * it to assure it won't be going away while
  90. * we operate on it */
  91. if (!IS_ERR(drv))
  92. mutex_lock(&drv->mtx);
  93. mutex_unlock(&cfg80211_drv_mutex);
  94. return drv;
  95. }
  96. struct cfg80211_registered_device *
  97. cfg80211_get_dev_from_ifindex(int ifindex)
  98. {
  99. struct cfg80211_registered_device *drv = ERR_PTR(-ENODEV);
  100. struct net_device *dev;
  101. mutex_lock(&cfg80211_drv_mutex);
  102. dev = dev_get_by_index(&init_net, ifindex);
  103. if (!dev)
  104. goto out;
  105. if (dev->ieee80211_ptr) {
  106. drv = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
  107. mutex_lock(&drv->mtx);
  108. } else
  109. drv = ERR_PTR(-ENODEV);
  110. dev_put(dev);
  111. out:
  112. mutex_unlock(&cfg80211_drv_mutex);
  113. return drv;
  114. }
  115. void cfg80211_put_dev(struct cfg80211_registered_device *drv)
  116. {
  117. BUG_ON(IS_ERR(drv));
  118. mutex_unlock(&drv->mtx);
  119. }
  120. int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
  121. char *newname)
  122. {
  123. int idx, taken = -1, result, digits;
  124. /* prohibit calling the thing phy%d when %d is not its number */
  125. sscanf(newname, PHY_NAME "%d%n", &idx, &taken);
  126. if (taken == strlen(newname) && idx != rdev->idx) {
  127. /* count number of places needed to print idx */
  128. digits = 1;
  129. while (idx /= 10)
  130. digits++;
  131. /*
  132. * deny the name if it is phy<idx> where <idx> is printed
  133. * without leading zeroes. taken == strlen(newname) here
  134. */
  135. if (taken == strlen(PHY_NAME) + digits)
  136. return -EINVAL;
  137. }
  138. /* this will check for collisions */
  139. result = device_rename(&rdev->wiphy.dev, newname);
  140. if (result)
  141. return result;
  142. if (!debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
  143. rdev->wiphy.debugfsdir,
  144. rdev->wiphy.debugfsdir->d_parent,
  145. newname))
  146. printk(KERN_ERR "cfg80211: failed to rename debugfs dir to %s!\n",
  147. newname);
  148. nl80211_notify_dev_rename(rdev);
  149. return 0;
  150. }
  151. /* exported functions */
  152. struct wiphy *wiphy_new(struct cfg80211_ops *ops, int sizeof_priv)
  153. {
  154. struct cfg80211_registered_device *drv;
  155. int alloc_size;
  156. WARN_ON(!ops->add_key && ops->del_key);
  157. WARN_ON(ops->add_key && !ops->del_key);
  158. alloc_size = sizeof(*drv) + sizeof_priv;
  159. drv = kzalloc(alloc_size, GFP_KERNEL);
  160. if (!drv)
  161. return NULL;
  162. drv->ops = ops;
  163. mutex_lock(&cfg80211_drv_mutex);
  164. drv->idx = wiphy_counter;
  165. /* now increase counter for the next device unless
  166. * it has wrapped previously */
  167. if (wiphy_counter >= 0)
  168. wiphy_counter++;
  169. mutex_unlock(&cfg80211_drv_mutex);
  170. if (unlikely(drv->idx < 0)) {
  171. /* ugh, wrapped! */
  172. kfree(drv);
  173. return NULL;
  174. }
  175. /* give it a proper name */
  176. snprintf(drv->wiphy.dev.bus_id, BUS_ID_SIZE,
  177. PHY_NAME "%d", drv->idx);
  178. mutex_init(&drv->mtx);
  179. mutex_init(&drv->devlist_mtx);
  180. INIT_LIST_HEAD(&drv->netdev_list);
  181. device_initialize(&drv->wiphy.dev);
  182. drv->wiphy.dev.class = &ieee80211_class;
  183. drv->wiphy.dev.platform_data = drv;
  184. return &drv->wiphy;
  185. }
  186. EXPORT_SYMBOL(wiphy_new);
  187. int wiphy_register(struct wiphy *wiphy)
  188. {
  189. struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
  190. int res;
  191. enum ieee80211_band band;
  192. struct ieee80211_supported_band *sband;
  193. bool have_band = false;
  194. int i;
  195. /* sanity check supported bands/channels */
  196. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  197. sband = wiphy->bands[band];
  198. if (!sband)
  199. continue;
  200. sband->band = band;
  201. if (!sband->n_channels || !sband->n_bitrates) {
  202. WARN_ON(1);
  203. return -EINVAL;
  204. }
  205. for (i = 0; i < sband->n_channels; i++) {
  206. sband->channels[i].orig_flags =
  207. sband->channels[i].flags;
  208. sband->channels[i].orig_mag =
  209. sband->channels[i].max_antenna_gain;
  210. sband->channels[i].orig_mpwr =
  211. sband->channels[i].max_power;
  212. sband->channels[i].band = band;
  213. }
  214. have_band = true;
  215. }
  216. if (!have_band) {
  217. WARN_ON(1);
  218. return -EINVAL;
  219. }
  220. /* check and set up bitrates */
  221. ieee80211_set_bitrate_flags(wiphy);
  222. /* set up regulatory info */
  223. wiphy_update_regulatory(wiphy);
  224. mutex_lock(&cfg80211_drv_mutex);
  225. res = device_add(&drv->wiphy.dev);
  226. if (res)
  227. goto out_unlock;
  228. list_add(&drv->list, &cfg80211_drv_list);
  229. /* add to debugfs */
  230. drv->wiphy.debugfsdir =
  231. debugfs_create_dir(wiphy_name(&drv->wiphy),
  232. ieee80211_debugfs_dir);
  233. res = 0;
  234. out_unlock:
  235. mutex_unlock(&cfg80211_drv_mutex);
  236. return res;
  237. }
  238. EXPORT_SYMBOL(wiphy_register);
  239. void wiphy_unregister(struct wiphy *wiphy)
  240. {
  241. struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
  242. /* protect the device list */
  243. mutex_lock(&cfg80211_drv_mutex);
  244. BUG_ON(!list_empty(&drv->netdev_list));
  245. /*
  246. * Try to grab drv->mtx. If a command is still in progress,
  247. * hopefully the driver will refuse it since it's tearing
  248. * down the device already. We wait for this command to complete
  249. * before unlinking the item from the list.
  250. * Note: as codified by the BUG_ON above we cannot get here if
  251. * a virtual interface is still associated. Hence, we can only
  252. * get to lock contention here if userspace issues a command
  253. * that identified the hardware by wiphy index.
  254. */
  255. mutex_lock(&drv->mtx);
  256. /* unlock again before freeing */
  257. mutex_unlock(&drv->mtx);
  258. list_del(&drv->list);
  259. device_del(&drv->wiphy.dev);
  260. debugfs_remove(drv->wiphy.debugfsdir);
  261. mutex_unlock(&cfg80211_drv_mutex);
  262. }
  263. EXPORT_SYMBOL(wiphy_unregister);
  264. void cfg80211_dev_free(struct cfg80211_registered_device *drv)
  265. {
  266. mutex_destroy(&drv->mtx);
  267. mutex_destroy(&drv->devlist_mtx);
  268. kfree(drv);
  269. }
  270. void wiphy_free(struct wiphy *wiphy)
  271. {
  272. put_device(&wiphy->dev);
  273. }
  274. EXPORT_SYMBOL(wiphy_free);
  275. static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
  276. unsigned long state,
  277. void *ndev)
  278. {
  279. struct net_device *dev = ndev;
  280. struct cfg80211_registered_device *rdev;
  281. if (!dev->ieee80211_ptr)
  282. return 0;
  283. rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
  284. switch (state) {
  285. case NETDEV_REGISTER:
  286. mutex_lock(&rdev->devlist_mtx);
  287. list_add(&dev->ieee80211_ptr->list, &rdev->netdev_list);
  288. if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
  289. "phy80211")) {
  290. printk(KERN_ERR "wireless: failed to add phy80211 "
  291. "symlink to netdev!\n");
  292. }
  293. dev->ieee80211_ptr->netdev = dev;
  294. mutex_unlock(&rdev->devlist_mtx);
  295. break;
  296. case NETDEV_UNREGISTER:
  297. mutex_lock(&rdev->devlist_mtx);
  298. if (!list_empty(&dev->ieee80211_ptr->list)) {
  299. sysfs_remove_link(&dev->dev.kobj, "phy80211");
  300. list_del_init(&dev->ieee80211_ptr->list);
  301. }
  302. mutex_unlock(&rdev->devlist_mtx);
  303. break;
  304. }
  305. return 0;
  306. }
  307. static struct notifier_block cfg80211_netdev_notifier = {
  308. .notifier_call = cfg80211_netdev_notifier_call,
  309. };
  310. static int cfg80211_init(void)
  311. {
  312. int err = wiphy_sysfs_init();
  313. if (err)
  314. goto out_fail_sysfs;
  315. err = register_netdevice_notifier(&cfg80211_netdev_notifier);
  316. if (err)
  317. goto out_fail_notifier;
  318. err = nl80211_init();
  319. if (err)
  320. goto out_fail_nl80211;
  321. ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
  322. return 0;
  323. out_fail_nl80211:
  324. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  325. out_fail_notifier:
  326. wiphy_sysfs_exit();
  327. out_fail_sysfs:
  328. return err;
  329. }
  330. subsys_initcall(cfg80211_init);
  331. static void cfg80211_exit(void)
  332. {
  333. debugfs_remove(ieee80211_debugfs_dir);
  334. nl80211_exit();
  335. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  336. wiphy_sysfs_exit();
  337. }
  338. module_exit(cfg80211_exit);