core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. /*
  2. * This is the linux wireless configuration interface.
  3. *
  4. * Copyright 2006-2009 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 "nl80211.h"
  17. #include "core.h"
  18. #include "sysfs.h"
  19. #include "debugfs.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(const 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. /*
  222. * Initialize wiphy parameters to IEEE 802.11 MIB default values.
  223. * Fragmentation and RTS threshold are disabled by default with the
  224. * special -1 value.
  225. */
  226. drv->wiphy.retry_short = 7;
  227. drv->wiphy.retry_long = 4;
  228. drv->wiphy.frag_threshold = (u32) -1;
  229. drv->wiphy.rts_threshold = (u32) -1;
  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, NL80211_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. if (wiphy->custom_regulatory) {
  295. struct regulatory_request request;
  296. request.wiphy_idx = get_wiphy_idx(wiphy);
  297. request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
  298. request.alpha2[0] = '9';
  299. request.alpha2[1] = '9';
  300. nl80211_send_reg_change_event(&request);
  301. }
  302. cfg80211_debugfs_drv_add(drv);
  303. res = 0;
  304. out_unlock:
  305. mutex_unlock(&cfg80211_mutex);
  306. return res;
  307. }
  308. EXPORT_SYMBOL(wiphy_register);
  309. void wiphy_unregister(struct wiphy *wiphy)
  310. {
  311. struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
  312. /* protect the device list */
  313. mutex_lock(&cfg80211_mutex);
  314. BUG_ON(!list_empty(&drv->netdev_list));
  315. /*
  316. * Try to grab drv->mtx. If a command is still in progress,
  317. * hopefully the driver will refuse it since it's tearing
  318. * down the device already. We wait for this command to complete
  319. * before unlinking the item from the list.
  320. * Note: as codified by the BUG_ON above we cannot get here if
  321. * a virtual interface is still associated. Hence, we can only
  322. * get to lock contention here if userspace issues a command
  323. * that identified the hardware by wiphy index.
  324. */
  325. mutex_lock(&drv->mtx);
  326. /* unlock again before freeing */
  327. mutex_unlock(&drv->mtx);
  328. cfg80211_debugfs_drv_del(drv);
  329. /* If this device got a regulatory hint tell core its
  330. * free to listen now to a new shiny device regulatory hint */
  331. reg_device_remove(wiphy);
  332. list_del(&drv->list);
  333. device_del(&drv->wiphy.dev);
  334. debugfs_remove(drv->wiphy.debugfsdir);
  335. mutex_unlock(&cfg80211_mutex);
  336. }
  337. EXPORT_SYMBOL(wiphy_unregister);
  338. void cfg80211_dev_free(struct cfg80211_registered_device *drv)
  339. {
  340. struct cfg80211_internal_bss *scan, *tmp;
  341. mutex_destroy(&drv->mtx);
  342. mutex_destroy(&drv->devlist_mtx);
  343. list_for_each_entry_safe(scan, tmp, &drv->bss_list, list)
  344. cfg80211_put_bss(&scan->pub);
  345. kfree(drv);
  346. }
  347. void wiphy_free(struct wiphy *wiphy)
  348. {
  349. put_device(&wiphy->dev);
  350. }
  351. EXPORT_SYMBOL(wiphy_free);
  352. static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
  353. unsigned long state,
  354. void *ndev)
  355. {
  356. struct net_device *dev = ndev;
  357. struct cfg80211_registered_device *rdev;
  358. if (!dev->ieee80211_ptr)
  359. return 0;
  360. rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
  361. WARN_ON(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_UNSPECIFIED);
  362. switch (state) {
  363. case NETDEV_REGISTER:
  364. mutex_lock(&rdev->devlist_mtx);
  365. list_add(&dev->ieee80211_ptr->list, &rdev->netdev_list);
  366. if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
  367. "phy80211")) {
  368. printk(KERN_ERR "wireless: failed to add phy80211 "
  369. "symlink to netdev!\n");
  370. }
  371. dev->ieee80211_ptr->netdev = dev;
  372. #ifdef CONFIG_WIRELESS_EXT
  373. dev->ieee80211_ptr->wext.default_key = -1;
  374. dev->ieee80211_ptr->wext.default_mgmt_key = -1;
  375. #endif
  376. mutex_unlock(&rdev->devlist_mtx);
  377. break;
  378. case NETDEV_GOING_DOWN:
  379. if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
  380. break;
  381. if (!dev->ieee80211_ptr->ssid_len)
  382. break;
  383. cfg80211_leave_ibss(rdev, dev, true);
  384. break;
  385. case NETDEV_UP:
  386. #ifdef CONFIG_WIRELESS_EXT
  387. if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
  388. break;
  389. if (!dev->ieee80211_ptr->wext.ibss.ssid_len)
  390. break;
  391. cfg80211_join_ibss(rdev, dev, &dev->ieee80211_ptr->wext.ibss);
  392. break;
  393. #endif
  394. case NETDEV_UNREGISTER:
  395. mutex_lock(&rdev->devlist_mtx);
  396. if (!list_empty(&dev->ieee80211_ptr->list)) {
  397. sysfs_remove_link(&dev->dev.kobj, "phy80211");
  398. list_del_init(&dev->ieee80211_ptr->list);
  399. }
  400. mutex_unlock(&rdev->devlist_mtx);
  401. break;
  402. }
  403. return 0;
  404. }
  405. static struct notifier_block cfg80211_netdev_notifier = {
  406. .notifier_call = cfg80211_netdev_notifier_call,
  407. };
  408. static int cfg80211_init(void)
  409. {
  410. int err;
  411. err = wiphy_sysfs_init();
  412. if (err)
  413. goto out_fail_sysfs;
  414. err = register_netdevice_notifier(&cfg80211_netdev_notifier);
  415. if (err)
  416. goto out_fail_notifier;
  417. err = nl80211_init();
  418. if (err)
  419. goto out_fail_nl80211;
  420. ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
  421. err = regulatory_init();
  422. if (err)
  423. goto out_fail_reg;
  424. return 0;
  425. out_fail_reg:
  426. debugfs_remove(ieee80211_debugfs_dir);
  427. out_fail_nl80211:
  428. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  429. out_fail_notifier:
  430. wiphy_sysfs_exit();
  431. out_fail_sysfs:
  432. return err;
  433. }
  434. subsys_initcall(cfg80211_init);
  435. static void cfg80211_exit(void)
  436. {
  437. debugfs_remove(ieee80211_debugfs_dir);
  438. nl80211_exit();
  439. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  440. wiphy_sysfs_exit();
  441. regulatory_exit();
  442. }
  443. module_exit(cfg80211_exit);