core.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  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 "nl80211.h"
  17. #include "core.h"
  18. #include "sysfs.h"
  19. /* name for sysfs, %d is appended */
  20. #define PHY_NAME "phy"
  21. MODULE_AUTHOR("Johannes Berg");
  22. MODULE_LICENSE("GPL");
  23. MODULE_DESCRIPTION("wireless configuration support");
  24. /* RCU might be appropriate here since we usually
  25. * only read the list, and that can happen quite
  26. * often because we need to do it for each command */
  27. LIST_HEAD(cfg80211_drv_list);
  28. /*
  29. * This is used to protect the cfg80211_drv_list, cfg80211_regdomain,
  30. * country_ie_regdomain, the reg_beacon_list and the the last regulatory
  31. * request receipt (last_request).
  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. 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. /* requires cfg80211_mutex to be held */
  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. assert_cfg80211_lock();
  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. if (taken == strlen(PHY_NAME) + digits)
  165. return -EINVAL;
  166. }
  167. /* Ignore nop renames */
  168. if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0)
  169. return 0;
  170. /* Ensure another device does not already have this name. */
  171. list_for_each_entry(drv, &cfg80211_drv_list, list)
  172. if (strcmp(newname, dev_name(&drv->wiphy.dev)) == 0)
  173. return -EINVAL;
  174. result = device_rename(&rdev->wiphy.dev, newname);
  175. if (result)
  176. return result;
  177. if (rdev->wiphy.debugfsdir &&
  178. !debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
  179. rdev->wiphy.debugfsdir,
  180. rdev->wiphy.debugfsdir->d_parent,
  181. newname))
  182. printk(KERN_ERR "cfg80211: failed to rename debugfs dir to %s!\n",
  183. newname);
  184. nl80211_notify_dev_rename(rdev);
  185. return 0;
  186. }
  187. /* exported functions */
  188. struct wiphy *wiphy_new(struct cfg80211_ops *ops, int sizeof_priv)
  189. {
  190. static int wiphy_counter;
  191. struct cfg80211_registered_device *drv;
  192. int alloc_size;
  193. WARN_ON(!ops->add_key && ops->del_key);
  194. WARN_ON(ops->add_key && !ops->del_key);
  195. alloc_size = sizeof(*drv) + sizeof_priv;
  196. drv = kzalloc(alloc_size, GFP_KERNEL);
  197. if (!drv)
  198. return NULL;
  199. drv->ops = ops;
  200. mutex_lock(&cfg80211_mutex);
  201. drv->wiphy_idx = wiphy_counter++;
  202. if (unlikely(!wiphy_idx_valid(drv->wiphy_idx))) {
  203. wiphy_counter--;
  204. mutex_unlock(&cfg80211_mutex);
  205. /* ugh, wrapped! */
  206. kfree(drv);
  207. return NULL;
  208. }
  209. mutex_unlock(&cfg80211_mutex);
  210. /* give it a proper name */
  211. dev_set_name(&drv->wiphy.dev, PHY_NAME "%d", drv->wiphy_idx);
  212. mutex_init(&drv->mtx);
  213. mutex_init(&drv->devlist_mtx);
  214. INIT_LIST_HEAD(&drv->netdev_list);
  215. spin_lock_init(&drv->bss_lock);
  216. INIT_LIST_HEAD(&drv->bss_list);
  217. device_initialize(&drv->wiphy.dev);
  218. drv->wiphy.dev.class = &ieee80211_class;
  219. drv->wiphy.dev.platform_data = drv;
  220. /*
  221. * Initialize wiphy parameters to IEEE 802.11 MIB default values.
  222. * Fragmentation and RTS threshold are disabled by default with the
  223. * special -1 value.
  224. */
  225. drv->wiphy.retry_short = 7;
  226. drv->wiphy.retry_long = 4;
  227. drv->wiphy.frag_threshold = (u32) -1;
  228. drv->wiphy.rts_threshold = (u32) -1;
  229. return &drv->wiphy;
  230. }
  231. EXPORT_SYMBOL(wiphy_new);
  232. int wiphy_register(struct wiphy *wiphy)
  233. {
  234. struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
  235. int res;
  236. enum ieee80211_band band;
  237. struct ieee80211_supported_band *sband;
  238. bool have_band = false;
  239. int i;
  240. u16 ifmodes = wiphy->interface_modes;
  241. if (WARN_ON(wiphy->max_scan_ssids < 1))
  242. return -EINVAL;
  243. /* sanity check ifmodes */
  244. WARN_ON(!ifmodes);
  245. ifmodes &= ((1 << __NL80211_IFTYPE_AFTER_LAST) - 1) & ~1;
  246. if (WARN_ON(ifmodes != wiphy->interface_modes))
  247. wiphy->interface_modes = ifmodes;
  248. /* sanity check supported bands/channels */
  249. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  250. sband = wiphy->bands[band];
  251. if (!sband)
  252. continue;
  253. sband->band = band;
  254. if (WARN_ON(!sband->n_channels || !sband->n_bitrates))
  255. return -EINVAL;
  256. /*
  257. * Since we use a u32 for rate bitmaps in
  258. * ieee80211_get_response_rate, we cannot
  259. * have more than 32 legacy rates.
  260. */
  261. if (WARN_ON(sband->n_bitrates > 32))
  262. return -EINVAL;
  263. for (i = 0; i < sband->n_channels; i++) {
  264. sband->channels[i].orig_flags =
  265. sband->channels[i].flags;
  266. sband->channels[i].orig_mag =
  267. sband->channels[i].max_antenna_gain;
  268. sband->channels[i].orig_mpwr =
  269. sband->channels[i].max_power;
  270. sband->channels[i].band = band;
  271. }
  272. have_band = true;
  273. }
  274. if (!have_band) {
  275. WARN_ON(1);
  276. return -EINVAL;
  277. }
  278. /* check and set up bitrates */
  279. ieee80211_set_bitrate_flags(wiphy);
  280. mutex_lock(&cfg80211_mutex);
  281. /* set up regulatory info */
  282. wiphy_update_regulatory(wiphy, NL80211_REGDOM_SET_BY_CORE);
  283. res = device_add(&drv->wiphy.dev);
  284. if (res)
  285. goto out_unlock;
  286. list_add(&drv->list, &cfg80211_drv_list);
  287. /* add to debugfs */
  288. drv->wiphy.debugfsdir =
  289. debugfs_create_dir(wiphy_name(&drv->wiphy),
  290. ieee80211_debugfs_dir);
  291. if (IS_ERR(drv->wiphy.debugfsdir))
  292. drv->wiphy.debugfsdir = NULL;
  293. if (wiphy->custom_regulatory) {
  294. struct regulatory_request request;
  295. request.wiphy_idx = get_wiphy_idx(wiphy);
  296. request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
  297. request.alpha2[0] = '9';
  298. request.alpha2[1] = '9';
  299. nl80211_send_reg_change_event(&request);
  300. }
  301. res = 0;
  302. out_unlock:
  303. mutex_unlock(&cfg80211_mutex);
  304. return res;
  305. }
  306. EXPORT_SYMBOL(wiphy_register);
  307. void wiphy_unregister(struct wiphy *wiphy)
  308. {
  309. struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
  310. /* protect the device list */
  311. mutex_lock(&cfg80211_mutex);
  312. BUG_ON(!list_empty(&drv->netdev_list));
  313. /*
  314. * Try to grab drv->mtx. If a command is still in progress,
  315. * hopefully the driver will refuse it since it's tearing
  316. * down the device already. We wait for this command to complete
  317. * before unlinking the item from the list.
  318. * Note: as codified by the BUG_ON above we cannot get here if
  319. * a virtual interface is still associated. Hence, we can only
  320. * get to lock contention here if userspace issues a command
  321. * that identified the hardware by wiphy index.
  322. */
  323. mutex_lock(&drv->mtx);
  324. /* unlock again before freeing */
  325. mutex_unlock(&drv->mtx);
  326. /* If this device got a regulatory hint tell core its
  327. * free to listen now to a new shiny device regulatory hint */
  328. reg_device_remove(wiphy);
  329. list_del(&drv->list);
  330. device_del(&drv->wiphy.dev);
  331. debugfs_remove(drv->wiphy.debugfsdir);
  332. mutex_unlock(&cfg80211_mutex);
  333. }
  334. EXPORT_SYMBOL(wiphy_unregister);
  335. void cfg80211_dev_free(struct cfg80211_registered_device *drv)
  336. {
  337. struct cfg80211_internal_bss *scan, *tmp;
  338. mutex_destroy(&drv->mtx);
  339. mutex_destroy(&drv->devlist_mtx);
  340. list_for_each_entry_safe(scan, tmp, &drv->bss_list, list)
  341. cfg80211_put_bss(&scan->pub);
  342. kfree(drv);
  343. }
  344. void wiphy_free(struct wiphy *wiphy)
  345. {
  346. put_device(&wiphy->dev);
  347. }
  348. EXPORT_SYMBOL(wiphy_free);
  349. static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
  350. unsigned long state,
  351. void *ndev)
  352. {
  353. struct net_device *dev = ndev;
  354. struct cfg80211_registered_device *rdev;
  355. if (!dev->ieee80211_ptr)
  356. return 0;
  357. rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
  358. WARN_ON(dev->ieee80211_ptr->iftype == NL80211_IFTYPE_UNSPECIFIED);
  359. switch (state) {
  360. case NETDEV_REGISTER:
  361. mutex_lock(&rdev->devlist_mtx);
  362. list_add(&dev->ieee80211_ptr->list, &rdev->netdev_list);
  363. if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
  364. "phy80211")) {
  365. printk(KERN_ERR "wireless: failed to add phy80211 "
  366. "symlink to netdev!\n");
  367. }
  368. dev->ieee80211_ptr->netdev = dev;
  369. mutex_unlock(&rdev->devlist_mtx);
  370. break;
  371. case NETDEV_GOING_DOWN:
  372. if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
  373. break;
  374. if (!dev->ieee80211_ptr->ssid_len)
  375. break;
  376. cfg80211_leave_ibss(rdev, dev, true);
  377. break;
  378. case NETDEV_UP:
  379. #ifdef CONFIG_WIRELESS_EXT
  380. if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_ADHOC)
  381. break;
  382. if (!dev->ieee80211_ptr->wext.ssid_len)
  383. break;
  384. cfg80211_join_ibss(rdev, dev, &dev->ieee80211_ptr->wext);
  385. break;
  386. #endif
  387. case NETDEV_UNREGISTER:
  388. mutex_lock(&rdev->devlist_mtx);
  389. if (!list_empty(&dev->ieee80211_ptr->list)) {
  390. sysfs_remove_link(&dev->dev.kobj, "phy80211");
  391. list_del_init(&dev->ieee80211_ptr->list);
  392. }
  393. mutex_unlock(&rdev->devlist_mtx);
  394. break;
  395. }
  396. return 0;
  397. }
  398. static struct notifier_block cfg80211_netdev_notifier = {
  399. .notifier_call = cfg80211_netdev_notifier_call,
  400. };
  401. static int cfg80211_init(void)
  402. {
  403. int err;
  404. err = wiphy_sysfs_init();
  405. if (err)
  406. goto out_fail_sysfs;
  407. err = register_netdevice_notifier(&cfg80211_netdev_notifier);
  408. if (err)
  409. goto out_fail_notifier;
  410. err = nl80211_init();
  411. if (err)
  412. goto out_fail_nl80211;
  413. ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
  414. err = regulatory_init();
  415. if (err)
  416. goto out_fail_reg;
  417. return 0;
  418. out_fail_reg:
  419. debugfs_remove(ieee80211_debugfs_dir);
  420. out_fail_nl80211:
  421. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  422. out_fail_notifier:
  423. wiphy_sysfs_exit();
  424. out_fail_sysfs:
  425. return err;
  426. }
  427. subsys_initcall(cfg80211_init);
  428. static void cfg80211_exit(void)
  429. {
  430. debugfs_remove(ieee80211_debugfs_dir);
  431. nl80211_exit();
  432. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  433. wiphy_sysfs_exit();
  434. regulatory_exit();
  435. }
  436. module_exit(cfg80211_exit);