core.c 10 KB

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