core.c 12 KB

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