core.c 12 KB

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