core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * This is the linux wireless configuration interface.
  3. *
  4. * Copyright 2006-2008 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. /* for debugfs */
  32. static struct dentry *ieee80211_debugfs_dir;
  33. /* requires cfg80211_drv_mutex to be held! */
  34. static struct cfg80211_registered_device *cfg80211_drv_by_wiphy(int wiphy)
  35. {
  36. struct cfg80211_registered_device *result = NULL, *drv;
  37. list_for_each_entry(drv, &cfg80211_drv_list, list) {
  38. if (drv->idx == wiphy) {
  39. result = drv;
  40. break;
  41. }
  42. }
  43. return result;
  44. }
  45. /* requires cfg80211_drv_mutex to be held! */
  46. static struct cfg80211_registered_device *
  47. __cfg80211_drv_from_info(struct genl_info *info)
  48. {
  49. int ifindex;
  50. struct cfg80211_registered_device *bywiphy = NULL, *byifidx = NULL;
  51. struct net_device *dev;
  52. int err = -EINVAL;
  53. if (info->attrs[NL80211_ATTR_WIPHY]) {
  54. bywiphy = cfg80211_drv_by_wiphy(
  55. nla_get_u32(info->attrs[NL80211_ATTR_WIPHY]));
  56. err = -ENODEV;
  57. }
  58. if (info->attrs[NL80211_ATTR_IFINDEX]) {
  59. ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
  60. dev = dev_get_by_index(&init_net, ifindex);
  61. if (dev) {
  62. if (dev->ieee80211_ptr)
  63. byifidx =
  64. wiphy_to_dev(dev->ieee80211_ptr->wiphy);
  65. dev_put(dev);
  66. }
  67. err = -ENODEV;
  68. }
  69. if (bywiphy && byifidx) {
  70. if (bywiphy != byifidx)
  71. return ERR_PTR(-EINVAL);
  72. else
  73. return bywiphy; /* == byifidx */
  74. }
  75. if (bywiphy)
  76. return bywiphy;
  77. if (byifidx)
  78. return byifidx;
  79. return ERR_PTR(err);
  80. }
  81. struct cfg80211_registered_device *
  82. cfg80211_get_dev_from_info(struct genl_info *info)
  83. {
  84. struct cfg80211_registered_device *drv;
  85. mutex_lock(&cfg80211_drv_mutex);
  86. drv = __cfg80211_drv_from_info(info);
  87. /* if it is not an error we grab the lock on
  88. * it to assure it won't be going away while
  89. * we operate on it */
  90. if (!IS_ERR(drv))
  91. mutex_lock(&drv->mtx);
  92. mutex_unlock(&cfg80211_drv_mutex);
  93. return drv;
  94. }
  95. struct cfg80211_registered_device *
  96. cfg80211_get_dev_from_ifindex(int ifindex)
  97. {
  98. struct cfg80211_registered_device *drv = ERR_PTR(-ENODEV);
  99. struct net_device *dev;
  100. mutex_lock(&cfg80211_drv_mutex);
  101. dev = dev_get_by_index(&init_net, ifindex);
  102. if (!dev)
  103. goto out;
  104. if (dev->ieee80211_ptr) {
  105. drv = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
  106. mutex_lock(&drv->mtx);
  107. } else
  108. drv = ERR_PTR(-ENODEV);
  109. dev_put(dev);
  110. out:
  111. mutex_unlock(&cfg80211_drv_mutex);
  112. return drv;
  113. }
  114. void cfg80211_put_dev(struct cfg80211_registered_device *drv)
  115. {
  116. BUG_ON(IS_ERR(drv));
  117. mutex_unlock(&drv->mtx);
  118. }
  119. int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
  120. char *newname)
  121. {
  122. struct cfg80211_registered_device *drv;
  123. int idx, taken = -1, result, digits;
  124. mutex_lock(&cfg80211_drv_mutex);
  125. /* prohibit calling the thing phy%d when %d is not its number */
  126. sscanf(newname, PHY_NAME "%d%n", &idx, &taken);
  127. if (taken == strlen(newname) && idx != rdev->idx) {
  128. /* count number of places needed to print idx */
  129. digits = 1;
  130. while (idx /= 10)
  131. digits++;
  132. /*
  133. * deny the name if it is phy<idx> where <idx> is printed
  134. * without leading zeroes. taken == strlen(newname) here
  135. */
  136. result = -EINVAL;
  137. if (taken == strlen(PHY_NAME) + digits)
  138. goto out_unlock;
  139. }
  140. /* Ignore nop renames */
  141. result = 0;
  142. if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0)
  143. goto out_unlock;
  144. /* Ensure another device does not already have this name. */
  145. list_for_each_entry(drv, &cfg80211_drv_list, list) {
  146. result = -EINVAL;
  147. if (strcmp(newname, dev_name(&drv->wiphy.dev)) == 0)
  148. goto out_unlock;
  149. }
  150. /* this will only check for collisions in sysfs
  151. * which is not even always compiled in.
  152. */
  153. result = device_rename(&rdev->wiphy.dev, newname);
  154. if (result)
  155. goto out_unlock;
  156. if (rdev->wiphy.debugfsdir &&
  157. !debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
  158. rdev->wiphy.debugfsdir,
  159. rdev->wiphy.debugfsdir->d_parent,
  160. newname))
  161. printk(KERN_ERR "cfg80211: failed to rename debugfs dir to %s!\n",
  162. newname);
  163. result = 0;
  164. out_unlock:
  165. mutex_unlock(&cfg80211_drv_mutex);
  166. if (result == 0)
  167. nl80211_notify_dev_rename(rdev);
  168. return result;
  169. }
  170. /* exported functions */
  171. struct wiphy *wiphy_new(struct cfg80211_ops *ops, int sizeof_priv)
  172. {
  173. static int wiphy_counter;
  174. struct cfg80211_registered_device *drv;
  175. int alloc_size;
  176. WARN_ON(!ops->add_key && ops->del_key);
  177. WARN_ON(ops->add_key && !ops->del_key);
  178. alloc_size = sizeof(*drv) + sizeof_priv;
  179. drv = kzalloc(alloc_size, GFP_KERNEL);
  180. if (!drv)
  181. return NULL;
  182. drv->ops = ops;
  183. mutex_lock(&cfg80211_drv_mutex);
  184. drv->idx = wiphy_counter++;
  185. if (unlikely(drv->idx < 0)) {
  186. wiphy_counter--;
  187. mutex_unlock(&cfg80211_drv_mutex);
  188. /* ugh, wrapped! */
  189. kfree(drv);
  190. return NULL;
  191. }
  192. mutex_unlock(&cfg80211_drv_mutex);
  193. /* give it a proper name */
  194. dev_set_name(&drv->wiphy.dev, PHY_NAME "%d", drv->idx);
  195. mutex_init(&drv->mtx);
  196. mutex_init(&drv->devlist_mtx);
  197. INIT_LIST_HEAD(&drv->netdev_list);
  198. device_initialize(&drv->wiphy.dev);
  199. drv->wiphy.dev.class = &ieee80211_class;
  200. drv->wiphy.dev.platform_data = drv;
  201. return &drv->wiphy;
  202. }
  203. EXPORT_SYMBOL(wiphy_new);
  204. int wiphy_register(struct wiphy *wiphy)
  205. {
  206. struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
  207. int res;
  208. enum ieee80211_band band;
  209. struct ieee80211_supported_band *sband;
  210. bool have_band = false;
  211. int i;
  212. u16 ifmodes = wiphy->interface_modes;
  213. /* sanity check ifmodes */
  214. WARN_ON(!ifmodes);
  215. ifmodes &= ((1 << __NL80211_IFTYPE_AFTER_LAST) - 1) & ~1;
  216. if (WARN_ON(ifmodes != wiphy->interface_modes))
  217. wiphy->interface_modes = ifmodes;
  218. /* sanity check supported bands/channels */
  219. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  220. sband = wiphy->bands[band];
  221. if (!sband)
  222. continue;
  223. sband->band = band;
  224. if (WARN_ON(!sband->n_channels || !sband->n_bitrates))
  225. return -EINVAL;
  226. /*
  227. * Since we use a u32 for rate bitmaps in
  228. * ieee80211_get_response_rate, we cannot
  229. * have more than 32 legacy rates.
  230. */
  231. if (WARN_ON(sband->n_bitrates > 32))
  232. return -EINVAL;
  233. for (i = 0; i < sband->n_channels; i++) {
  234. sband->channels[i].orig_flags =
  235. sband->channels[i].flags;
  236. sband->channels[i].orig_mag =
  237. sband->channels[i].max_antenna_gain;
  238. sband->channels[i].orig_mpwr =
  239. sband->channels[i].max_power;
  240. sband->channels[i].band = band;
  241. }
  242. have_band = true;
  243. }
  244. if (!have_band) {
  245. WARN_ON(1);
  246. return -EINVAL;
  247. }
  248. /* check and set up bitrates */
  249. ieee80211_set_bitrate_flags(wiphy);
  250. mutex_lock(&cfg80211_drv_mutex);
  251. /* set up regulatory info */
  252. wiphy_update_regulatory(wiphy, REGDOM_SET_BY_CORE);
  253. res = device_add(&drv->wiphy.dev);
  254. if (res)
  255. goto out_unlock;
  256. list_add(&drv->list, &cfg80211_drv_list);
  257. /* add to debugfs */
  258. drv->wiphy.debugfsdir =
  259. debugfs_create_dir(wiphy_name(&drv->wiphy),
  260. ieee80211_debugfs_dir);
  261. if (IS_ERR(drv->wiphy.debugfsdir))
  262. drv->wiphy.debugfsdir = NULL;
  263. res = 0;
  264. out_unlock:
  265. mutex_unlock(&cfg80211_drv_mutex);
  266. return res;
  267. }
  268. EXPORT_SYMBOL(wiphy_register);
  269. void wiphy_unregister(struct wiphy *wiphy)
  270. {
  271. struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
  272. /* protect the device list */
  273. mutex_lock(&cfg80211_drv_mutex);
  274. BUG_ON(!list_empty(&drv->netdev_list));
  275. /*
  276. * Try to grab drv->mtx. If a command is still in progress,
  277. * hopefully the driver will refuse it since it's tearing
  278. * down the device already. We wait for this command to complete
  279. * before unlinking the item from the list.
  280. * Note: as codified by the BUG_ON above we cannot get here if
  281. * a virtual interface is still associated. Hence, we can only
  282. * get to lock contention here if userspace issues a command
  283. * that identified the hardware by wiphy index.
  284. */
  285. mutex_lock(&drv->mtx);
  286. /* unlock again before freeing */
  287. mutex_unlock(&drv->mtx);
  288. /* If this device got a regulatory hint tell core its
  289. * free to listen now to a new shiny device regulatory hint */
  290. reg_device_remove(wiphy);
  291. list_del(&drv->list);
  292. device_del(&drv->wiphy.dev);
  293. debugfs_remove(drv->wiphy.debugfsdir);
  294. mutex_unlock(&cfg80211_drv_mutex);
  295. }
  296. EXPORT_SYMBOL(wiphy_unregister);
  297. void cfg80211_dev_free(struct cfg80211_registered_device *drv)
  298. {
  299. mutex_destroy(&drv->mtx);
  300. mutex_destroy(&drv->devlist_mtx);
  301. kfree(drv);
  302. }
  303. void wiphy_free(struct wiphy *wiphy)
  304. {
  305. put_device(&wiphy->dev);
  306. }
  307. EXPORT_SYMBOL(wiphy_free);
  308. static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
  309. unsigned long state,
  310. void *ndev)
  311. {
  312. struct net_device *dev = ndev;
  313. struct cfg80211_registered_device *rdev;
  314. if (!dev->ieee80211_ptr)
  315. return 0;
  316. rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
  317. WARN_ON(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_UNSPECIFIED);
  318. switch (state) {
  319. case NETDEV_REGISTER:
  320. mutex_lock(&rdev->devlist_mtx);
  321. list_add(&dev->ieee80211_ptr->list, &rdev->netdev_list);
  322. if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
  323. "phy80211")) {
  324. printk(KERN_ERR "wireless: failed to add phy80211 "
  325. "symlink to netdev!\n");
  326. }
  327. dev->ieee80211_ptr->netdev = dev;
  328. mutex_unlock(&rdev->devlist_mtx);
  329. break;
  330. case NETDEV_UNREGISTER:
  331. mutex_lock(&rdev->devlist_mtx);
  332. if (!list_empty(&dev->ieee80211_ptr->list)) {
  333. sysfs_remove_link(&dev->dev.kobj, "phy80211");
  334. list_del_init(&dev->ieee80211_ptr->list);
  335. }
  336. mutex_unlock(&rdev->devlist_mtx);
  337. break;
  338. }
  339. return 0;
  340. }
  341. static struct notifier_block cfg80211_netdev_notifier = {
  342. .notifier_call = cfg80211_netdev_notifier_call,
  343. };
  344. static int cfg80211_init(void)
  345. {
  346. int err;
  347. err = wiphy_sysfs_init();
  348. if (err)
  349. goto out_fail_sysfs;
  350. err = register_netdevice_notifier(&cfg80211_netdev_notifier);
  351. if (err)
  352. goto out_fail_notifier;
  353. err = nl80211_init();
  354. if (err)
  355. goto out_fail_nl80211;
  356. ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
  357. err = regulatory_init();
  358. if (err)
  359. goto out_fail_reg;
  360. return 0;
  361. out_fail_reg:
  362. debugfs_remove(ieee80211_debugfs_dir);
  363. out_fail_nl80211:
  364. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  365. out_fail_notifier:
  366. wiphy_sysfs_exit();
  367. out_fail_sysfs:
  368. return err;
  369. }
  370. subsys_initcall(cfg80211_init);
  371. static void cfg80211_exit(void)
  372. {
  373. debugfs_remove(ieee80211_debugfs_dir);
  374. nl80211_exit();
  375. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  376. wiphy_sysfs_exit();
  377. regulatory_exit();
  378. }
  379. module_exit(cfg80211_exit);