core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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/mutex.h>
  10. #include <linux/list.h>
  11. #include <linux/nl80211.h>
  12. #include <linux/debugfs.h>
  13. #include <linux/notifier.h>
  14. #include <linux/device.h>
  15. #include <linux/list.h>
  16. #include <net/genetlink.h>
  17. #include <net/cfg80211.h>
  18. #include <net/wireless.h>
  19. #include "nl80211.h"
  20. #include "core.h"
  21. #include "sysfs.h"
  22. #include "reg.h"
  23. /* name for sysfs, %d is appended */
  24. #define PHY_NAME "phy"
  25. MODULE_AUTHOR("Johannes Berg");
  26. MODULE_LICENSE("GPL");
  27. MODULE_DESCRIPTION("wireless configuration support");
  28. struct list_head regulatory_requests;
  29. /* Central wireless core regulatory domains, we only need two,
  30. * the current one and a world regulatory domain in case we have no
  31. * information to give us an alpha2 */
  32. struct ieee80211_regdomain *cfg80211_regdomain;
  33. /* We keep a static world regulatory domain in case of the absence of CRDA */
  34. const struct ieee80211_regdomain world_regdom = {
  35. .n_reg_rules = 1,
  36. .alpha2 = "00",
  37. .reg_rules = {
  38. REG_RULE(2412-10, 2462+10, 40, 6, 20,
  39. NL80211_RRF_PASSIVE_SCAN |
  40. NL80211_RRF_NO_IBSS),
  41. }
  42. };
  43. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  44. /* All this fucking static junk will be removed soon, so
  45. * don't fucking count on it !@#$ */
  46. static char *ieee80211_regdom = "US";
  47. module_param(ieee80211_regdom, charp, 0444);
  48. MODULE_PARM_DESC(ieee80211_regdom, "IEEE 802.11 regulatory domain code");
  49. /* We assume 40 MHz bandwidth for the old regulatory work.
  50. * We make emphasis we are using the exact same frequencies
  51. * as before */
  52. const struct ieee80211_regdomain us_regdom = {
  53. .n_reg_rules = 6,
  54. .alpha2 = "US",
  55. .reg_rules = {
  56. /* IEEE 802.11b/g, channels 1..11 */
  57. REG_RULE(2412-10, 2462+10, 40, 6, 27, 0),
  58. /* IEEE 802.11a, channel 36 */
  59. REG_RULE(5180-10, 5180+10, 40, 6, 23, 0),
  60. /* IEEE 802.11a, channel 40 */
  61. REG_RULE(5200-10, 5200+10, 40, 6, 23, 0),
  62. /* IEEE 802.11a, channel 44 */
  63. REG_RULE(5220-10, 5220+10, 40, 6, 23, 0),
  64. /* IEEE 802.11a, channels 48..64 */
  65. REG_RULE(5240-10, 5320+10, 40, 6, 23, 0),
  66. /* IEEE 802.11a, channels 149..165, outdoor */
  67. REG_RULE(5745-10, 5825+10, 40, 6, 30, 0),
  68. }
  69. };
  70. const struct ieee80211_regdomain jp_regdom = {
  71. .n_reg_rules = 3,
  72. .alpha2 = "JP",
  73. .reg_rules = {
  74. /* IEEE 802.11b/g, channels 1..14 */
  75. REG_RULE(2412-10, 2484+10, 40, 6, 20, 0),
  76. /* IEEE 802.11a, channels 34..48 */
  77. REG_RULE(5170-10, 5240+10, 40, 6, 20,
  78. NL80211_RRF_PASSIVE_SCAN),
  79. /* IEEE 802.11a, channels 52..64 */
  80. REG_RULE(5260-10, 5320+10, 40, 6, 20,
  81. NL80211_RRF_NO_IBSS |
  82. NL80211_RRF_DFS),
  83. }
  84. };
  85. const struct ieee80211_regdomain eu_regdom = {
  86. .n_reg_rules = 6,
  87. /* This alpha2 is bogus, we leave it here just for stupid
  88. * backward compatibility */
  89. .alpha2 = "EU",
  90. .reg_rules = {
  91. /* IEEE 802.11b/g, channels 1..13 */
  92. REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
  93. /* IEEE 802.11a, channel 36 */
  94. REG_RULE(5180-10, 5180+10, 40, 6, 23,
  95. NL80211_RRF_PASSIVE_SCAN),
  96. /* IEEE 802.11a, channel 40 */
  97. REG_RULE(5200-10, 5200+10, 40, 6, 23,
  98. NL80211_RRF_PASSIVE_SCAN),
  99. /* IEEE 802.11a, channel 44 */
  100. REG_RULE(5220-10, 5220+10, 40, 6, 23,
  101. NL80211_RRF_PASSIVE_SCAN),
  102. /* IEEE 802.11a, channels 48..64 */
  103. REG_RULE(5240-10, 5320+10, 40, 6, 20,
  104. NL80211_RRF_NO_IBSS |
  105. NL80211_RRF_DFS),
  106. /* IEEE 802.11a, channels 100..140 */
  107. REG_RULE(5500-10, 5700+10, 40, 6, 30,
  108. NL80211_RRF_NO_IBSS |
  109. NL80211_RRF_DFS),
  110. }
  111. };
  112. #endif
  113. struct ieee80211_regdomain *cfg80211_world_regdom =
  114. (struct ieee80211_regdomain *) &world_regdom;
  115. LIST_HEAD(regulatory_requests);
  116. DEFINE_MUTEX(cfg80211_reg_mutex);
  117. /* RCU might be appropriate here since we usually
  118. * only read the list, and that can happen quite
  119. * often because we need to do it for each command */
  120. LIST_HEAD(cfg80211_drv_list);
  121. DEFINE_MUTEX(cfg80211_drv_mutex);
  122. static int wiphy_counter;
  123. /* for debugfs */
  124. static struct dentry *ieee80211_debugfs_dir;
  125. /* requires cfg80211_drv_mutex to be held! */
  126. static struct cfg80211_registered_device *cfg80211_drv_by_wiphy(int wiphy)
  127. {
  128. struct cfg80211_registered_device *result = NULL, *drv;
  129. list_for_each_entry(drv, &cfg80211_drv_list, list) {
  130. if (drv->idx == wiphy) {
  131. result = drv;
  132. break;
  133. }
  134. }
  135. return result;
  136. }
  137. /* requires cfg80211_drv_mutex to be held! */
  138. static struct cfg80211_registered_device *
  139. __cfg80211_drv_from_info(struct genl_info *info)
  140. {
  141. int ifindex;
  142. struct cfg80211_registered_device *bywiphy = NULL, *byifidx = NULL;
  143. struct net_device *dev;
  144. int err = -EINVAL;
  145. if (info->attrs[NL80211_ATTR_WIPHY]) {
  146. bywiphy = cfg80211_drv_by_wiphy(
  147. nla_get_u32(info->attrs[NL80211_ATTR_WIPHY]));
  148. err = -ENODEV;
  149. }
  150. if (info->attrs[NL80211_ATTR_IFINDEX]) {
  151. ifindex = nla_get_u32(info->attrs[NL80211_ATTR_IFINDEX]);
  152. dev = dev_get_by_index(&init_net, ifindex);
  153. if (dev) {
  154. if (dev->ieee80211_ptr)
  155. byifidx =
  156. wiphy_to_dev(dev->ieee80211_ptr->wiphy);
  157. dev_put(dev);
  158. }
  159. err = -ENODEV;
  160. }
  161. if (bywiphy && byifidx) {
  162. if (bywiphy != byifidx)
  163. return ERR_PTR(-EINVAL);
  164. else
  165. return bywiphy; /* == byifidx */
  166. }
  167. if (bywiphy)
  168. return bywiphy;
  169. if (byifidx)
  170. return byifidx;
  171. return ERR_PTR(err);
  172. }
  173. struct cfg80211_registered_device *
  174. cfg80211_get_dev_from_info(struct genl_info *info)
  175. {
  176. struct cfg80211_registered_device *drv;
  177. mutex_lock(&cfg80211_drv_mutex);
  178. drv = __cfg80211_drv_from_info(info);
  179. /* if it is not an error we grab the lock on
  180. * it to assure it won't be going away while
  181. * we operate on it */
  182. if (!IS_ERR(drv))
  183. mutex_lock(&drv->mtx);
  184. mutex_unlock(&cfg80211_drv_mutex);
  185. return drv;
  186. }
  187. struct cfg80211_registered_device *
  188. cfg80211_get_dev_from_ifindex(int ifindex)
  189. {
  190. struct cfg80211_registered_device *drv = ERR_PTR(-ENODEV);
  191. struct net_device *dev;
  192. mutex_lock(&cfg80211_drv_mutex);
  193. dev = dev_get_by_index(&init_net, ifindex);
  194. if (!dev)
  195. goto out;
  196. if (dev->ieee80211_ptr) {
  197. drv = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
  198. mutex_lock(&drv->mtx);
  199. } else
  200. drv = ERR_PTR(-ENODEV);
  201. dev_put(dev);
  202. out:
  203. mutex_unlock(&cfg80211_drv_mutex);
  204. return drv;
  205. }
  206. void cfg80211_put_dev(struct cfg80211_registered_device *drv)
  207. {
  208. BUG_ON(IS_ERR(drv));
  209. mutex_unlock(&drv->mtx);
  210. }
  211. int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
  212. char *newname)
  213. {
  214. struct cfg80211_registered_device *drv;
  215. int idx, taken = -1, result, digits;
  216. mutex_lock(&cfg80211_drv_mutex);
  217. /* prohibit calling the thing phy%d when %d is not its number */
  218. sscanf(newname, PHY_NAME "%d%n", &idx, &taken);
  219. if (taken == strlen(newname) && idx != rdev->idx) {
  220. /* count number of places needed to print idx */
  221. digits = 1;
  222. while (idx /= 10)
  223. digits++;
  224. /*
  225. * deny the name if it is phy<idx> where <idx> is printed
  226. * without leading zeroes. taken == strlen(newname) here
  227. */
  228. result = -EINVAL;
  229. if (taken == strlen(PHY_NAME) + digits)
  230. goto out_unlock;
  231. }
  232. /* Ignore nop renames */
  233. result = 0;
  234. if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0)
  235. goto out_unlock;
  236. /* Ensure another device does not already have this name. */
  237. list_for_each_entry(drv, &cfg80211_drv_list, list) {
  238. result = -EINVAL;
  239. if (strcmp(newname, dev_name(&drv->wiphy.dev)) == 0)
  240. goto out_unlock;
  241. }
  242. /* this will only check for collisions in sysfs
  243. * which is not even always compiled in.
  244. */
  245. result = device_rename(&rdev->wiphy.dev, newname);
  246. if (result)
  247. goto out_unlock;
  248. if (!debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
  249. rdev->wiphy.debugfsdir,
  250. rdev->wiphy.debugfsdir->d_parent,
  251. newname))
  252. printk(KERN_ERR "cfg80211: failed to rename debugfs dir to %s!\n",
  253. newname);
  254. result = 0;
  255. out_unlock:
  256. mutex_unlock(&cfg80211_drv_mutex);
  257. if (result == 0)
  258. nl80211_notify_dev_rename(rdev);
  259. return result;
  260. }
  261. /* exported functions */
  262. struct wiphy *wiphy_new(struct cfg80211_ops *ops, int sizeof_priv)
  263. {
  264. struct cfg80211_registered_device *drv;
  265. int alloc_size;
  266. WARN_ON(!ops->add_key && ops->del_key);
  267. WARN_ON(ops->add_key && !ops->del_key);
  268. alloc_size = sizeof(*drv) + sizeof_priv;
  269. drv = kzalloc(alloc_size, GFP_KERNEL);
  270. if (!drv)
  271. return NULL;
  272. drv->ops = ops;
  273. mutex_lock(&cfg80211_drv_mutex);
  274. drv->idx = wiphy_counter;
  275. /* now increase counter for the next device unless
  276. * it has wrapped previously */
  277. if (wiphy_counter >= 0)
  278. wiphy_counter++;
  279. mutex_unlock(&cfg80211_drv_mutex);
  280. if (unlikely(drv->idx < 0)) {
  281. /* ugh, wrapped! */
  282. kfree(drv);
  283. return NULL;
  284. }
  285. /* give it a proper name */
  286. snprintf(drv->wiphy.dev.bus_id, BUS_ID_SIZE,
  287. PHY_NAME "%d", drv->idx);
  288. mutex_init(&drv->mtx);
  289. mutex_init(&drv->devlist_mtx);
  290. INIT_LIST_HEAD(&drv->netdev_list);
  291. device_initialize(&drv->wiphy.dev);
  292. drv->wiphy.dev.class = &ieee80211_class;
  293. drv->wiphy.dev.platform_data = drv;
  294. return &drv->wiphy;
  295. }
  296. EXPORT_SYMBOL(wiphy_new);
  297. int wiphy_register(struct wiphy *wiphy)
  298. {
  299. struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
  300. int res;
  301. enum ieee80211_band band;
  302. struct ieee80211_supported_band *sband;
  303. bool have_band = false;
  304. int i;
  305. u16 ifmodes = wiphy->interface_modes;
  306. /* sanity check ifmodes */
  307. WARN_ON(!ifmodes);
  308. ifmodes &= ((1 << __NL80211_IFTYPE_AFTER_LAST) - 1) & ~1;
  309. if (WARN_ON(ifmodes != wiphy->interface_modes))
  310. wiphy->interface_modes = ifmodes;
  311. /* sanity check supported bands/channels */
  312. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  313. sband = wiphy->bands[band];
  314. if (!sband)
  315. continue;
  316. sband->band = band;
  317. if (!sband->n_channels || !sband->n_bitrates) {
  318. WARN_ON(1);
  319. return -EINVAL;
  320. }
  321. for (i = 0; i < sband->n_channels; i++) {
  322. sband->channels[i].orig_flags =
  323. sband->channels[i].flags;
  324. sband->channels[i].orig_mag =
  325. sband->channels[i].max_antenna_gain;
  326. sband->channels[i].orig_mpwr =
  327. sband->channels[i].max_power;
  328. sband->channels[i].band = band;
  329. }
  330. have_band = true;
  331. }
  332. if (!have_band) {
  333. WARN_ON(1);
  334. return -EINVAL;
  335. }
  336. /* check and set up bitrates */
  337. ieee80211_set_bitrate_flags(wiphy);
  338. /* set up regulatory info */
  339. mutex_lock(&cfg80211_reg_mutex);
  340. wiphy_update_regulatory(wiphy, REGDOM_SET_BY_CORE);
  341. mutex_unlock(&cfg80211_reg_mutex);
  342. mutex_lock(&cfg80211_drv_mutex);
  343. res = device_add(&drv->wiphy.dev);
  344. if (res)
  345. goto out_unlock;
  346. list_add(&drv->list, &cfg80211_drv_list);
  347. /* add to debugfs */
  348. drv->wiphy.debugfsdir =
  349. debugfs_create_dir(wiphy_name(&drv->wiphy),
  350. ieee80211_debugfs_dir);
  351. res = 0;
  352. out_unlock:
  353. mutex_unlock(&cfg80211_drv_mutex);
  354. return res;
  355. }
  356. EXPORT_SYMBOL(wiphy_register);
  357. void wiphy_unregister(struct wiphy *wiphy)
  358. {
  359. struct cfg80211_registered_device *drv = wiphy_to_dev(wiphy);
  360. /* protect the device list */
  361. mutex_lock(&cfg80211_drv_mutex);
  362. BUG_ON(!list_empty(&drv->netdev_list));
  363. /*
  364. * Try to grab drv->mtx. If a command is still in progress,
  365. * hopefully the driver will refuse it since it's tearing
  366. * down the device already. We wait for this command to complete
  367. * before unlinking the item from the list.
  368. * Note: as codified by the BUG_ON above we cannot get here if
  369. * a virtual interface is still associated. Hence, we can only
  370. * get to lock contention here if userspace issues a command
  371. * that identified the hardware by wiphy index.
  372. */
  373. mutex_lock(&drv->mtx);
  374. /* unlock again before freeing */
  375. mutex_unlock(&drv->mtx);
  376. list_del(&drv->list);
  377. device_del(&drv->wiphy.dev);
  378. debugfs_remove(drv->wiphy.debugfsdir);
  379. mutex_unlock(&cfg80211_drv_mutex);
  380. }
  381. EXPORT_SYMBOL(wiphy_unregister);
  382. void cfg80211_dev_free(struct cfg80211_registered_device *drv)
  383. {
  384. mutex_destroy(&drv->mtx);
  385. mutex_destroy(&drv->devlist_mtx);
  386. kfree(drv);
  387. }
  388. void wiphy_free(struct wiphy *wiphy)
  389. {
  390. put_device(&wiphy->dev);
  391. }
  392. EXPORT_SYMBOL(wiphy_free);
  393. static int cfg80211_netdev_notifier_call(struct notifier_block * nb,
  394. unsigned long state,
  395. void *ndev)
  396. {
  397. struct net_device *dev = ndev;
  398. struct cfg80211_registered_device *rdev;
  399. if (!dev->ieee80211_ptr)
  400. return 0;
  401. rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
  402. switch (state) {
  403. case NETDEV_REGISTER:
  404. mutex_lock(&rdev->devlist_mtx);
  405. list_add(&dev->ieee80211_ptr->list, &rdev->netdev_list);
  406. if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
  407. "phy80211")) {
  408. printk(KERN_ERR "wireless: failed to add phy80211 "
  409. "symlink to netdev!\n");
  410. }
  411. dev->ieee80211_ptr->netdev = dev;
  412. mutex_unlock(&rdev->devlist_mtx);
  413. break;
  414. case NETDEV_UNREGISTER:
  415. mutex_lock(&rdev->devlist_mtx);
  416. if (!list_empty(&dev->ieee80211_ptr->list)) {
  417. sysfs_remove_link(&dev->dev.kobj, "phy80211");
  418. list_del_init(&dev->ieee80211_ptr->list);
  419. }
  420. mutex_unlock(&rdev->devlist_mtx);
  421. break;
  422. }
  423. return 0;
  424. }
  425. static struct notifier_block cfg80211_netdev_notifier = {
  426. .notifier_call = cfg80211_netdev_notifier_call,
  427. };
  428. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  429. const struct ieee80211_regdomain *static_regdom(char *alpha2)
  430. {
  431. if (alpha2[0] == 'U' && alpha2[1] == 'S')
  432. return &us_regdom;
  433. if (alpha2[0] == 'J' && alpha2[1] == 'P')
  434. return &jp_regdom;
  435. if (alpha2[0] == 'E' && alpha2[1] == 'U')
  436. return &eu_regdom;
  437. /* Default, as per the old rules */
  438. return &us_regdom;
  439. }
  440. #endif
  441. static int cfg80211_init(void)
  442. {
  443. int err;
  444. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  445. cfg80211_regdomain =
  446. (struct ieee80211_regdomain *) static_regdom(ieee80211_regdom);
  447. /* Used during reset_regdomains_static() */
  448. cfg80211_world_regdom = cfg80211_regdomain;
  449. #else
  450. cfg80211_regdomain =
  451. (struct ieee80211_regdomain *) cfg80211_world_regdom;
  452. #endif
  453. err = wiphy_sysfs_init();
  454. if (err)
  455. goto out_fail_sysfs;
  456. err = register_netdevice_notifier(&cfg80211_netdev_notifier);
  457. if (err)
  458. goto out_fail_notifier;
  459. err = nl80211_init();
  460. if (err)
  461. goto out_fail_nl80211;
  462. ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
  463. err = regulatory_init();
  464. if (err)
  465. goto out_fail_reg;
  466. #ifdef CONFIG_WIRELESS_OLD_REGULATORY
  467. printk(KERN_INFO "cfg80211: Using old static regulatory domain:\n");
  468. print_regdomain_info(cfg80211_regdomain);
  469. /* The old code still requests for a new regdomain and if
  470. * you have CRDA you get it updated, otherwise you get
  471. * stuck with the static values. We ignore "EU" code as
  472. * that is not a valid ISO / IEC 3166 alpha2 */
  473. if (ieee80211_regdom[0] != 'E' &&
  474. ieee80211_regdom[1] != 'U')
  475. err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE,
  476. ieee80211_regdom, NULL);
  477. #else
  478. err = __regulatory_hint(NULL, REGDOM_SET_BY_CORE, "00", NULL);
  479. if (err)
  480. printk(KERN_ERR "cfg80211: calling CRDA failed - "
  481. "unable to update world regulatory domain, "
  482. "using static definition\n");
  483. #endif
  484. return 0;
  485. out_fail_reg:
  486. debugfs_remove(ieee80211_debugfs_dir);
  487. out_fail_nl80211:
  488. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  489. out_fail_notifier:
  490. wiphy_sysfs_exit();
  491. out_fail_sysfs:
  492. return err;
  493. }
  494. subsys_initcall(cfg80211_init);
  495. static void cfg80211_exit(void)
  496. {
  497. debugfs_remove(ieee80211_debugfs_dir);
  498. nl80211_exit();
  499. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  500. wiphy_sysfs_exit();
  501. regulatory_exit();
  502. }
  503. module_exit(cfg80211_exit);