core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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. 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. /* requires cfg80211_mutex to be held */
  148. int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
  149. char *newname)
  150. {
  151. struct cfg80211_registered_device *drv;
  152. int wiphy_idx, taken = -1, result, digits;
  153. assert_cfg80211_lock();
  154. /* prohibit calling the thing phy%d when %d is not its number */
  155. sscanf(newname, PHY_NAME "%d%n", &wiphy_idx, &taken);
  156. if (taken == strlen(newname) && wiphy_idx != rdev->wiphy_idx) {
  157. /* count number of places needed to print wiphy_idx */
  158. digits = 1;
  159. while (wiphy_idx /= 10)
  160. digits++;
  161. /*
  162. * deny the name if it is phy<idx> where <idx> is printed
  163. * without leading zeroes. taken == strlen(newname) here
  164. */
  165. if (taken == strlen(PHY_NAME) + digits)
  166. return -EINVAL;
  167. }
  168. /* Ignore nop renames */
  169. if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0)
  170. return 0;
  171. /* Ensure another device does not already have this name. */
  172. list_for_each_entry(drv, &cfg80211_drv_list, list)
  173. if (strcmp(newname, dev_name(&drv->wiphy.dev)) == 0)
  174. return -EINVAL;
  175. result = device_rename(&rdev->wiphy.dev, newname);
  176. if (result)
  177. return result;
  178. if (rdev->wiphy.debugfsdir &&
  179. !debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
  180. rdev->wiphy.debugfsdir,
  181. rdev->wiphy.debugfsdir->d_parent,
  182. newname))
  183. printk(KERN_ERR "cfg80211: failed to rename debugfs dir to %s!\n",
  184. newname);
  185. nl80211_notify_dev_rename(rdev);
  186. return 0;
  187. }
  188. /* exported functions */
  189. struct wiphy *wiphy_new(struct cfg80211_ops *ops, int sizeof_priv)
  190. {
  191. static int wiphy_counter;
  192. struct cfg80211_registered_device *drv;
  193. int alloc_size;
  194. WARN_ON(!ops->add_key && ops->del_key);
  195. WARN_ON(ops->add_key && !ops->del_key);
  196. alloc_size = sizeof(*drv) + sizeof_priv;
  197. drv = kzalloc(alloc_size, GFP_KERNEL);
  198. if (!drv)
  199. return NULL;
  200. drv->ops = ops;
  201. mutex_lock(&cfg80211_mutex);
  202. drv->wiphy_idx = wiphy_counter++;
  203. if (unlikely(!wiphy_idx_valid(drv->wiphy_idx))) {
  204. wiphy_counter--;
  205. mutex_unlock(&cfg80211_mutex);
  206. /* ugh, wrapped! */
  207. kfree(drv);
  208. return NULL;
  209. }
  210. mutex_unlock(&cfg80211_mutex);
  211. /* give it a proper name */
  212. dev_set_name(&drv->wiphy.dev, PHY_NAME "%d", drv->wiphy_idx);
  213. mutex_init(&drv->mtx);
  214. mutex_init(&drv->devlist_mtx);
  215. INIT_LIST_HEAD(&drv->netdev_list);
  216. spin_lock_init(&drv->bss_lock);
  217. INIT_LIST_HEAD(&drv->bss_list);
  218. device_initialize(&drv->wiphy.dev);
  219. drv->wiphy.dev.class = &ieee80211_class;
  220. drv->wiphy.dev.platform_data = drv;
  221. return &drv->wiphy;
  222. }
  223. EXPORT_SYMBOL(wiphy_new);
  224. int wiphy_register(struct wiphy *wiphy)
  225. {
  226. struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
  227. int res;
  228. enum ieee80211_band band;
  229. struct ieee80211_supported_band *sband;
  230. bool have_band = false;
  231. int i;
  232. u16 ifmodes = wiphy->interface_modes;
  233. if (WARN_ON(wiphy->max_scan_ssids < 1))
  234. return -EINVAL;
  235. /* sanity check ifmodes */
  236. WARN_ON(!ifmodes);
  237. ifmodes &= ((1 << __NL80211_IFTYPE_AFTER_LAST) - 1) & ~1;
  238. if (WARN_ON(ifmodes != wiphy->interface_modes))
  239. wiphy->interface_modes = ifmodes;
  240. /* sanity check supported bands/channels */
  241. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  242. sband = wiphy->bands[band];
  243. if (!sband)
  244. continue;
  245. sband->band = band;
  246. if (WARN_ON(!sband->n_channels || !sband->n_bitrates))
  247. return -EINVAL;
  248. /*
  249. * Since we use a u32 for rate bitmaps in
  250. * ieee80211_get_response_rate, we cannot
  251. * have more than 32 legacy rates.
  252. */
  253. if (WARN_ON(sband->n_bitrates > 32))
  254. return -EINVAL;
  255. for (i = 0; i < sband->n_channels; i++) {
  256. sband->channels[i].orig_flags =
  257. sband->channels[i].flags;
  258. sband->channels[i].orig_mag =
  259. sband->channels[i].max_antenna_gain;
  260. sband->channels[i].orig_mpwr =
  261. sband->channels[i].max_power;
  262. sband->channels[i].band = band;
  263. }
  264. have_band = true;
  265. }
  266. if (!have_band) {
  267. WARN_ON(1);
  268. return -EINVAL;
  269. }
  270. /* check and set up bitrates */
  271. ieee80211_set_bitrate_flags(wiphy);
  272. mutex_lock(&cfg80211_mutex);
  273. /* set up regulatory info */
  274. wiphy_update_regulatory(wiphy, NL80211_REGDOM_SET_BY_CORE);
  275. res = device_add(&drv->wiphy.dev);
  276. if (res)
  277. goto out_unlock;
  278. list_add(&drv->list, &cfg80211_drv_list);
  279. /* add to debugfs */
  280. drv->wiphy.debugfsdir =
  281. debugfs_create_dir(wiphy_name(&drv->wiphy),
  282. ieee80211_debugfs_dir);
  283. if (IS_ERR(drv->wiphy.debugfsdir))
  284. drv->wiphy.debugfsdir = NULL;
  285. if (wiphy->custom_regulatory) {
  286. struct regulatory_request request;
  287. request.wiphy_idx = get_wiphy_idx(wiphy);
  288. request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
  289. request.alpha2[0] = '9';
  290. request.alpha2[1] = '9';
  291. nl80211_send_reg_change_event(&request);
  292. }
  293. res = 0;
  294. out_unlock:
  295. mutex_unlock(&cfg80211_mutex);
  296. return res;
  297. }
  298. EXPORT_SYMBOL(wiphy_register);
  299. void wiphy_unregister(struct wiphy *wiphy)
  300. {
  301. struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
  302. /* protect the device list */
  303. mutex_lock(&cfg80211_mutex);
  304. BUG_ON(!list_empty(&drv->netdev_list));
  305. /*
  306. * Try to grab drv->mtx. If a command is still in progress,
  307. * hopefully the driver will refuse it since it's tearing
  308. * down the device already. We wait for this command to complete
  309. * before unlinking the item from the list.
  310. * Note: as codified by the BUG_ON above we cannot get here if
  311. * a virtual interface is still associated. Hence, we can only
  312. * get to lock contention here if userspace issues a command
  313. * that identified the hardware by wiphy index.
  314. */
  315. mutex_lock(&drv->mtx);
  316. /* unlock again before freeing */
  317. mutex_unlock(&drv->mtx);
  318. /* If this device got a regulatory hint tell core its
  319. * free to listen now to a new shiny device regulatory hint */
  320. reg_device_remove(wiphy);
  321. list_del(&drv->list);
  322. device_del(&drv->wiphy.dev);
  323. debugfs_remove(drv->wiphy.debugfsdir);
  324. mutex_unlock(&cfg80211_mutex);
  325. }
  326. EXPORT_SYMBOL(wiphy_unregister);
  327. void cfg80211_dev_free(struct cfg80211_registered_device *drv)
  328. {
  329. struct cfg80211_internal_bss *scan, *tmp;
  330. mutex_destroy(&drv->mtx);
  331. mutex_destroy(&drv->devlist_mtx);
  332. list_for_each_entry_safe(scan, tmp, &drv->bss_list, list)
  333. cfg80211_put_bss(&scan->pub);
  334. kfree(drv);
  335. }
  336. void wiphy_free(struct wiphy *wiphy)
  337. {
  338. put_device(&wiphy->dev);
  339. }
  340. EXPORT_SYMBOL(wiphy_free);
  341. static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
  342. unsigned long state,
  343. void *ndev)
  344. {
  345. struct net_device *dev = ndev;
  346. struct cfg80211_registered_device *rdev;
  347. if (!dev->ieee80211_ptr)
  348. return 0;
  349. rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
  350. WARN_ON(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_UNSPECIFIED);
  351. switch (state) {
  352. case NETDEV_REGISTER:
  353. mutex_lock(&rdev->devlist_mtx);
  354. list_add(&dev->ieee80211_ptr->list, &rdev->netdev_list);
  355. if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
  356. "phy80211")) {
  357. printk(KERN_ERR "wireless: failed to add phy80211 "
  358. "symlink to netdev!\n");
  359. }
  360. dev->ieee80211_ptr->netdev = dev;
  361. mutex_unlock(&rdev->devlist_mtx);
  362. break;
  363. case NETDEV_UNREGISTER:
  364. mutex_lock(&rdev->devlist_mtx);
  365. if (!list_empty(&dev->ieee80211_ptr->list)) {
  366. sysfs_remove_link(&dev->dev.kobj, "phy80211");
  367. list_del_init(&dev->ieee80211_ptr->list);
  368. }
  369. mutex_unlock(&rdev->devlist_mtx);
  370. break;
  371. }
  372. return 0;
  373. }
  374. static struct notifier_block cfg80211_netdev_notifier = {
  375. .notifier_call = cfg80211_netdev_notifier_call,
  376. };
  377. static int cfg80211_init(void)
  378. {
  379. int err;
  380. err = wiphy_sysfs_init();
  381. if (err)
  382. goto out_fail_sysfs;
  383. err = register_netdevice_notifier(&cfg80211_netdev_notifier);
  384. if (err)
  385. goto out_fail_notifier;
  386. err = nl80211_init();
  387. if (err)
  388. goto out_fail_nl80211;
  389. ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
  390. err = regulatory_init();
  391. if (err)
  392. goto out_fail_reg;
  393. return 0;
  394. out_fail_reg:
  395. debugfs_remove(ieee80211_debugfs_dir);
  396. out_fail_nl80211:
  397. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  398. out_fail_notifier:
  399. wiphy_sysfs_exit();
  400. out_fail_sysfs:
  401. return err;
  402. }
  403. subsys_initcall(cfg80211_init);
  404. static void cfg80211_exit(void)
  405. {
  406. debugfs_remove(ieee80211_debugfs_dir);
  407. nl80211_exit();
  408. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  409. wiphy_sysfs_exit();
  410. regulatory_exit();
  411. }
  412. module_exit(cfg80211_exit);