core.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  1. /*
  2. * This is the linux wireless configuration interface.
  3. *
  4. * Copyright 2006-2010 Johannes Berg <johannes@sipsolutions.net>
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/if.h>
  8. #include <linux/module.h>
  9. #include <linux/err.h>
  10. #include <linux/list.h>
  11. #include <linux/slab.h>
  12. #include <linux/nl80211.h>
  13. #include <linux/debugfs.h>
  14. #include <linux/notifier.h>
  15. #include <linux/device.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/rtnetlink.h>
  18. #include <linux/sched.h>
  19. #include <net/genetlink.h>
  20. #include <net/cfg80211.h>
  21. #include "nl80211.h"
  22. #include "core.h"
  23. #include "sysfs.h"
  24. #include "debugfs.h"
  25. #include "wext-compat.h"
  26. #include "ethtool.h"
  27. /* name for sysfs, %d is appended */
  28. #define PHY_NAME "phy"
  29. MODULE_AUTHOR("Johannes Berg");
  30. MODULE_LICENSE("GPL");
  31. MODULE_DESCRIPTION("wireless configuration support");
  32. /* RCU-protected (and cfg80211_mutex for writers) */
  33. LIST_HEAD(cfg80211_rdev_list);
  34. int cfg80211_rdev_list_generation;
  35. DEFINE_MUTEX(cfg80211_mutex);
  36. /* for debugfs */
  37. static struct dentry *ieee80211_debugfs_dir;
  38. /* for the cleanup, scan and event works */
  39. struct workqueue_struct *cfg80211_wq;
  40. static bool cfg80211_disable_40mhz_24ghz;
  41. module_param(cfg80211_disable_40mhz_24ghz, bool, 0644);
  42. MODULE_PARM_DESC(cfg80211_disable_40mhz_24ghz,
  43. "Disable 40MHz support in the 2.4GHz band");
  44. /* requires cfg80211_mutex to be held! */
  45. struct cfg80211_registered_device *cfg80211_rdev_by_wiphy_idx(int wiphy_idx)
  46. {
  47. struct cfg80211_registered_device *result = NULL, *rdev;
  48. if (!wiphy_idx_valid(wiphy_idx))
  49. return NULL;
  50. assert_cfg80211_lock();
  51. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  52. if (rdev->wiphy_idx == wiphy_idx) {
  53. result = rdev;
  54. break;
  55. }
  56. }
  57. return result;
  58. }
  59. int get_wiphy_idx(struct wiphy *wiphy)
  60. {
  61. struct cfg80211_registered_device *rdev;
  62. if (!wiphy)
  63. return WIPHY_IDX_STALE;
  64. rdev = wiphy_to_dev(wiphy);
  65. return rdev->wiphy_idx;
  66. }
  67. /* requires cfg80211_rdev_mutex to be held! */
  68. struct wiphy *wiphy_idx_to_wiphy(int wiphy_idx)
  69. {
  70. struct cfg80211_registered_device *rdev;
  71. if (!wiphy_idx_valid(wiphy_idx))
  72. return NULL;
  73. assert_cfg80211_lock();
  74. rdev = cfg80211_rdev_by_wiphy_idx(wiphy_idx);
  75. if (!rdev)
  76. return NULL;
  77. return &rdev->wiphy;
  78. }
  79. struct cfg80211_registered_device *
  80. cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
  81. {
  82. struct cfg80211_registered_device *rdev = ERR_PTR(-ENODEV);
  83. struct net_device *dev;
  84. mutex_lock(&cfg80211_mutex);
  85. dev = dev_get_by_index(net, ifindex);
  86. if (!dev)
  87. goto out;
  88. if (dev->ieee80211_ptr) {
  89. rdev = wiphy_to_dev(dev->ieee80211_ptr->wiphy);
  90. mutex_lock(&rdev->mtx);
  91. } else
  92. rdev = ERR_PTR(-ENODEV);
  93. dev_put(dev);
  94. out:
  95. mutex_unlock(&cfg80211_mutex);
  96. return rdev;
  97. }
  98. /* requires cfg80211_mutex to be held */
  99. int cfg80211_dev_rename(struct cfg80211_registered_device *rdev,
  100. char *newname)
  101. {
  102. struct cfg80211_registered_device *rdev2;
  103. int wiphy_idx, taken = -1, result, digits;
  104. assert_cfg80211_lock();
  105. /* prohibit calling the thing phy%d when %d is not its number */
  106. sscanf(newname, PHY_NAME "%d%n", &wiphy_idx, &taken);
  107. if (taken == strlen(newname) && wiphy_idx != rdev->wiphy_idx) {
  108. /* count number of places needed to print wiphy_idx */
  109. digits = 1;
  110. while (wiphy_idx /= 10)
  111. digits++;
  112. /*
  113. * deny the name if it is phy<idx> where <idx> is printed
  114. * without leading zeroes. taken == strlen(newname) here
  115. */
  116. if (taken == strlen(PHY_NAME) + digits)
  117. return -EINVAL;
  118. }
  119. /* Ignore nop renames */
  120. if (strcmp(newname, dev_name(&rdev->wiphy.dev)) == 0)
  121. return 0;
  122. /* Ensure another device does not already have this name. */
  123. list_for_each_entry(rdev2, &cfg80211_rdev_list, list)
  124. if (strcmp(newname, dev_name(&rdev2->wiphy.dev)) == 0)
  125. return -EINVAL;
  126. result = device_rename(&rdev->wiphy.dev, newname);
  127. if (result)
  128. return result;
  129. if (rdev->wiphy.debugfsdir &&
  130. !debugfs_rename(rdev->wiphy.debugfsdir->d_parent,
  131. rdev->wiphy.debugfsdir,
  132. rdev->wiphy.debugfsdir->d_parent,
  133. newname))
  134. pr_err("failed to rename debugfs dir to %s!\n", newname);
  135. nl80211_notify_dev_rename(rdev);
  136. return 0;
  137. }
  138. int cfg80211_switch_netns(struct cfg80211_registered_device *rdev,
  139. struct net *net)
  140. {
  141. struct wireless_dev *wdev;
  142. int err = 0;
  143. if (!(rdev->wiphy.flags & WIPHY_FLAG_NETNS_OK))
  144. return -EOPNOTSUPP;
  145. list_for_each_entry(wdev, &rdev->netdev_list, list) {
  146. wdev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
  147. err = dev_change_net_namespace(wdev->netdev, net, "wlan%d");
  148. if (err)
  149. break;
  150. wdev->netdev->features |= NETIF_F_NETNS_LOCAL;
  151. }
  152. if (err) {
  153. /* failed -- clean up to old netns */
  154. net = wiphy_net(&rdev->wiphy);
  155. list_for_each_entry_continue_reverse(wdev, &rdev->netdev_list,
  156. list) {
  157. wdev->netdev->features &= ~NETIF_F_NETNS_LOCAL;
  158. err = dev_change_net_namespace(wdev->netdev, net,
  159. "wlan%d");
  160. WARN_ON(err);
  161. wdev->netdev->features |= NETIF_F_NETNS_LOCAL;
  162. }
  163. return err;
  164. }
  165. wiphy_net_set(&rdev->wiphy, net);
  166. err = device_rename(&rdev->wiphy.dev, dev_name(&rdev->wiphy.dev));
  167. WARN_ON(err);
  168. return 0;
  169. }
  170. static void cfg80211_rfkill_poll(struct rfkill *rfkill, void *data)
  171. {
  172. struct cfg80211_registered_device *rdev = data;
  173. rdev->ops->rfkill_poll(&rdev->wiphy);
  174. }
  175. static int cfg80211_rfkill_set_block(void *data, bool blocked)
  176. {
  177. struct cfg80211_registered_device *rdev = data;
  178. struct wireless_dev *wdev;
  179. if (!blocked)
  180. return 0;
  181. rtnl_lock();
  182. mutex_lock(&rdev->devlist_mtx);
  183. list_for_each_entry(wdev, &rdev->netdev_list, list)
  184. dev_close(wdev->netdev);
  185. mutex_unlock(&rdev->devlist_mtx);
  186. rtnl_unlock();
  187. return 0;
  188. }
  189. static void cfg80211_rfkill_sync_work(struct work_struct *work)
  190. {
  191. struct cfg80211_registered_device *rdev;
  192. rdev = container_of(work, struct cfg80211_registered_device, rfkill_sync);
  193. cfg80211_rfkill_set_block(rdev, rfkill_blocked(rdev->rfkill));
  194. }
  195. static void cfg80211_event_work(struct work_struct *work)
  196. {
  197. struct cfg80211_registered_device *rdev;
  198. rdev = container_of(work, struct cfg80211_registered_device,
  199. event_work);
  200. rtnl_lock();
  201. cfg80211_lock_rdev(rdev);
  202. cfg80211_process_rdev_events(rdev);
  203. cfg80211_unlock_rdev(rdev);
  204. rtnl_unlock();
  205. }
  206. /* exported functions */
  207. struct wiphy *wiphy_new(const struct cfg80211_ops *ops, int sizeof_priv)
  208. {
  209. static int wiphy_counter;
  210. struct cfg80211_registered_device *rdev;
  211. int alloc_size;
  212. WARN_ON(ops->add_key && (!ops->del_key || !ops->set_default_key));
  213. WARN_ON(ops->auth && (!ops->assoc || !ops->deauth || !ops->disassoc));
  214. WARN_ON(ops->connect && !ops->disconnect);
  215. WARN_ON(ops->join_ibss && !ops->leave_ibss);
  216. WARN_ON(ops->add_virtual_intf && !ops->del_virtual_intf);
  217. WARN_ON(ops->add_station && !ops->del_station);
  218. WARN_ON(ops->add_mpath && !ops->del_mpath);
  219. WARN_ON(ops->join_mesh && !ops->leave_mesh);
  220. alloc_size = sizeof(*rdev) + sizeof_priv;
  221. rdev = kzalloc(alloc_size, GFP_KERNEL);
  222. if (!rdev)
  223. return NULL;
  224. rdev->ops = ops;
  225. mutex_lock(&cfg80211_mutex);
  226. rdev->wiphy_idx = wiphy_counter++;
  227. if (unlikely(!wiphy_idx_valid(rdev->wiphy_idx))) {
  228. wiphy_counter--;
  229. mutex_unlock(&cfg80211_mutex);
  230. /* ugh, wrapped! */
  231. kfree(rdev);
  232. return NULL;
  233. }
  234. mutex_unlock(&cfg80211_mutex);
  235. /* give it a proper name */
  236. dev_set_name(&rdev->wiphy.dev, PHY_NAME "%d", rdev->wiphy_idx);
  237. mutex_init(&rdev->mtx);
  238. mutex_init(&rdev->devlist_mtx);
  239. mutex_init(&rdev->sched_scan_mtx);
  240. INIT_LIST_HEAD(&rdev->netdev_list);
  241. spin_lock_init(&rdev->bss_lock);
  242. INIT_LIST_HEAD(&rdev->bss_list);
  243. INIT_WORK(&rdev->scan_done_wk, __cfg80211_scan_done);
  244. INIT_WORK(&rdev->sched_scan_results_wk, __cfg80211_sched_scan_results);
  245. #ifdef CONFIG_CFG80211_WEXT
  246. rdev->wiphy.wext = &cfg80211_wext_handler;
  247. #endif
  248. device_initialize(&rdev->wiphy.dev);
  249. rdev->wiphy.dev.class = &ieee80211_class;
  250. rdev->wiphy.dev.platform_data = rdev;
  251. #ifdef CONFIG_CFG80211_DEFAULT_PS
  252. rdev->wiphy.flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
  253. #endif
  254. wiphy_net_set(&rdev->wiphy, &init_net);
  255. rdev->rfkill_ops.set_block = cfg80211_rfkill_set_block;
  256. rdev->rfkill = rfkill_alloc(dev_name(&rdev->wiphy.dev),
  257. &rdev->wiphy.dev, RFKILL_TYPE_WLAN,
  258. &rdev->rfkill_ops, rdev);
  259. if (!rdev->rfkill) {
  260. kfree(rdev);
  261. return NULL;
  262. }
  263. INIT_WORK(&rdev->rfkill_sync, cfg80211_rfkill_sync_work);
  264. INIT_WORK(&rdev->conn_work, cfg80211_conn_work);
  265. INIT_WORK(&rdev->event_work, cfg80211_event_work);
  266. init_waitqueue_head(&rdev->dev_wait);
  267. /*
  268. * Initialize wiphy parameters to IEEE 802.11 MIB default values.
  269. * Fragmentation and RTS threshold are disabled by default with the
  270. * special -1 value.
  271. */
  272. rdev->wiphy.retry_short = 7;
  273. rdev->wiphy.retry_long = 4;
  274. rdev->wiphy.frag_threshold = (u32) -1;
  275. rdev->wiphy.rts_threshold = (u32) -1;
  276. rdev->wiphy.coverage_class = 0;
  277. return &rdev->wiphy;
  278. }
  279. EXPORT_SYMBOL(wiphy_new);
  280. static int wiphy_verify_combinations(struct wiphy *wiphy)
  281. {
  282. const struct ieee80211_iface_combination *c;
  283. int i, j;
  284. for (i = 0; i < wiphy->n_iface_combinations; i++) {
  285. u32 cnt = 0;
  286. u16 all_iftypes = 0;
  287. c = &wiphy->iface_combinations[i];
  288. /* Combinations with just one interface aren't real */
  289. if (WARN_ON(c->max_interfaces < 2))
  290. return -EINVAL;
  291. /* Need at least one channel */
  292. if (WARN_ON(!c->num_different_channels))
  293. return -EINVAL;
  294. /*
  295. * Put a sane limit on maximum number of different
  296. * channels to simplify channel accounting code.
  297. */
  298. if (WARN_ON(c->num_different_channels >
  299. CFG80211_MAX_NUM_DIFFERENT_CHANNELS))
  300. return -EINVAL;
  301. if (WARN_ON(!c->n_limits))
  302. return -EINVAL;
  303. for (j = 0; j < c->n_limits; j++) {
  304. u16 types = c->limits[j].types;
  305. /*
  306. * interface types shouldn't overlap, this is
  307. * used in cfg80211_can_change_interface()
  308. */
  309. if (WARN_ON(types & all_iftypes))
  310. return -EINVAL;
  311. all_iftypes |= types;
  312. if (WARN_ON(!c->limits[j].max))
  313. return -EINVAL;
  314. /* Shouldn't list software iftypes in combinations! */
  315. if (WARN_ON(wiphy->software_iftypes & types))
  316. return -EINVAL;
  317. cnt += c->limits[j].max;
  318. /*
  319. * Don't advertise an unsupported type
  320. * in a combination.
  321. */
  322. if (WARN_ON((wiphy->interface_modes & types) != types))
  323. return -EINVAL;
  324. }
  325. /* You can't even choose that many! */
  326. if (WARN_ON(cnt < c->max_interfaces))
  327. return -EINVAL;
  328. }
  329. return 0;
  330. }
  331. int wiphy_register(struct wiphy *wiphy)
  332. {
  333. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  334. int res;
  335. enum ieee80211_band band;
  336. struct ieee80211_supported_band *sband;
  337. bool have_band = false;
  338. int i;
  339. u16 ifmodes = wiphy->interface_modes;
  340. #ifdef CONFIG_PM
  341. if (WARN_ON((wiphy->wowlan.flags & WIPHY_WOWLAN_GTK_REKEY_FAILURE) &&
  342. !(wiphy->wowlan.flags & WIPHY_WOWLAN_SUPPORTS_GTK_REKEY)))
  343. return -EINVAL;
  344. #endif
  345. if (WARN_ON(wiphy->ap_sme_capa &&
  346. !(wiphy->flags & WIPHY_FLAG_HAVE_AP_SME)))
  347. return -EINVAL;
  348. if (WARN_ON(wiphy->addresses && !wiphy->n_addresses))
  349. return -EINVAL;
  350. if (WARN_ON(wiphy->addresses &&
  351. !is_zero_ether_addr(wiphy->perm_addr) &&
  352. memcmp(wiphy->perm_addr, wiphy->addresses[0].addr,
  353. ETH_ALEN)))
  354. return -EINVAL;
  355. if (wiphy->addresses)
  356. memcpy(wiphy->perm_addr, wiphy->addresses[0].addr, ETH_ALEN);
  357. /* sanity check ifmodes */
  358. WARN_ON(!ifmodes);
  359. ifmodes &= ((1 << NUM_NL80211_IFTYPES) - 1) & ~1;
  360. if (WARN_ON(ifmodes != wiphy->interface_modes))
  361. wiphy->interface_modes = ifmodes;
  362. res = wiphy_verify_combinations(wiphy);
  363. if (res)
  364. return res;
  365. /* sanity check supported bands/channels */
  366. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  367. sband = wiphy->bands[band];
  368. if (!sband)
  369. continue;
  370. sband->band = band;
  371. if (WARN_ON(!sband->n_channels))
  372. return -EINVAL;
  373. /*
  374. * on 60gHz band, there are no legacy rates, so
  375. * n_bitrates is 0
  376. */
  377. if (WARN_ON(band != IEEE80211_BAND_60GHZ &&
  378. !sband->n_bitrates))
  379. return -EINVAL;
  380. /*
  381. * Since cfg80211_disable_40mhz_24ghz is global, we can
  382. * modify the sband's ht data even if the driver uses a
  383. * global structure for that.
  384. */
  385. if (cfg80211_disable_40mhz_24ghz &&
  386. band == IEEE80211_BAND_2GHZ &&
  387. sband->ht_cap.ht_supported) {
  388. sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  389. sband->ht_cap.cap &= ~IEEE80211_HT_CAP_SGI_40;
  390. }
  391. /*
  392. * Since we use a u32 for rate bitmaps in
  393. * ieee80211_get_response_rate, we cannot
  394. * have more than 32 legacy rates.
  395. */
  396. if (WARN_ON(sband->n_bitrates > 32))
  397. return -EINVAL;
  398. for (i = 0; i < sband->n_channels; i++) {
  399. sband->channels[i].orig_flags =
  400. sband->channels[i].flags;
  401. sband->channels[i].orig_mag =
  402. sband->channels[i].max_antenna_gain;
  403. sband->channels[i].orig_mpwr =
  404. sband->channels[i].max_power;
  405. sband->channels[i].band = band;
  406. }
  407. have_band = true;
  408. }
  409. if (!have_band) {
  410. WARN_ON(1);
  411. return -EINVAL;
  412. }
  413. #ifdef CONFIG_PM
  414. if (rdev->wiphy.wowlan.n_patterns) {
  415. if (WARN_ON(!rdev->wiphy.wowlan.pattern_min_len ||
  416. rdev->wiphy.wowlan.pattern_min_len >
  417. rdev->wiphy.wowlan.pattern_max_len))
  418. return -EINVAL;
  419. }
  420. #endif
  421. /* check and set up bitrates */
  422. ieee80211_set_bitrate_flags(wiphy);
  423. mutex_lock(&cfg80211_mutex);
  424. res = device_add(&rdev->wiphy.dev);
  425. if (res) {
  426. mutex_unlock(&cfg80211_mutex);
  427. return res;
  428. }
  429. /* set up regulatory info */
  430. regulatory_update(wiphy, NL80211_REGDOM_SET_BY_CORE);
  431. list_add_rcu(&rdev->list, &cfg80211_rdev_list);
  432. cfg80211_rdev_list_generation++;
  433. /* add to debugfs */
  434. rdev->wiphy.debugfsdir =
  435. debugfs_create_dir(wiphy_name(&rdev->wiphy),
  436. ieee80211_debugfs_dir);
  437. if (IS_ERR(rdev->wiphy.debugfsdir))
  438. rdev->wiphy.debugfsdir = NULL;
  439. if (wiphy->flags & WIPHY_FLAG_CUSTOM_REGULATORY) {
  440. struct regulatory_request request;
  441. request.wiphy_idx = get_wiphy_idx(wiphy);
  442. request.initiator = NL80211_REGDOM_SET_BY_DRIVER;
  443. request.alpha2[0] = '9';
  444. request.alpha2[1] = '9';
  445. nl80211_send_reg_change_event(&request);
  446. }
  447. cfg80211_debugfs_rdev_add(rdev);
  448. mutex_unlock(&cfg80211_mutex);
  449. /*
  450. * due to a locking dependency this has to be outside of the
  451. * cfg80211_mutex lock
  452. */
  453. res = rfkill_register(rdev->rfkill);
  454. if (res)
  455. goto out_rm_dev;
  456. rtnl_lock();
  457. rdev->wiphy.registered = true;
  458. rtnl_unlock();
  459. return 0;
  460. out_rm_dev:
  461. device_del(&rdev->wiphy.dev);
  462. return res;
  463. }
  464. EXPORT_SYMBOL(wiphy_register);
  465. void wiphy_rfkill_start_polling(struct wiphy *wiphy)
  466. {
  467. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  468. if (!rdev->ops->rfkill_poll)
  469. return;
  470. rdev->rfkill_ops.poll = cfg80211_rfkill_poll;
  471. rfkill_resume_polling(rdev->rfkill);
  472. }
  473. EXPORT_SYMBOL(wiphy_rfkill_start_polling);
  474. void wiphy_rfkill_stop_polling(struct wiphy *wiphy)
  475. {
  476. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  477. rfkill_pause_polling(rdev->rfkill);
  478. }
  479. EXPORT_SYMBOL(wiphy_rfkill_stop_polling);
  480. void wiphy_unregister(struct wiphy *wiphy)
  481. {
  482. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  483. rtnl_lock();
  484. rdev->wiphy.registered = false;
  485. rtnl_unlock();
  486. rfkill_unregister(rdev->rfkill);
  487. /* protect the device list */
  488. mutex_lock(&cfg80211_mutex);
  489. wait_event(rdev->dev_wait, ({
  490. int __count;
  491. mutex_lock(&rdev->devlist_mtx);
  492. __count = rdev->opencount;
  493. mutex_unlock(&rdev->devlist_mtx);
  494. __count == 0; }));
  495. mutex_lock(&rdev->devlist_mtx);
  496. BUG_ON(!list_empty(&rdev->netdev_list));
  497. mutex_unlock(&rdev->devlist_mtx);
  498. /*
  499. * First remove the hardware from everywhere, this makes
  500. * it impossible to find from userspace.
  501. */
  502. debugfs_remove_recursive(rdev->wiphy.debugfsdir);
  503. list_del_rcu(&rdev->list);
  504. synchronize_rcu();
  505. /*
  506. * Try to grab rdev->mtx. If a command is still in progress,
  507. * hopefully the driver will refuse it since it's tearing
  508. * down the device already. We wait for this command to complete
  509. * before unlinking the item from the list.
  510. * Note: as codified by the BUG_ON above we cannot get here if
  511. * a virtual interface is still present. Hence, we can only get
  512. * to lock contention here if userspace issues a command that
  513. * identified the hardware by wiphy index.
  514. */
  515. cfg80211_lock_rdev(rdev);
  516. /* nothing */
  517. cfg80211_unlock_rdev(rdev);
  518. /* If this device got a regulatory hint tell core its
  519. * free to listen now to a new shiny device regulatory hint */
  520. reg_device_remove(wiphy);
  521. cfg80211_rdev_list_generation++;
  522. device_del(&rdev->wiphy.dev);
  523. mutex_unlock(&cfg80211_mutex);
  524. flush_work(&rdev->scan_done_wk);
  525. cancel_work_sync(&rdev->conn_work);
  526. flush_work(&rdev->event_work);
  527. if (rdev->wowlan && rdev->ops->set_wakeup)
  528. rdev->ops->set_wakeup(&rdev->wiphy, false);
  529. cfg80211_rdev_free_wowlan(rdev);
  530. }
  531. EXPORT_SYMBOL(wiphy_unregister);
  532. void cfg80211_dev_free(struct cfg80211_registered_device *rdev)
  533. {
  534. struct cfg80211_internal_bss *scan, *tmp;
  535. rfkill_destroy(rdev->rfkill);
  536. mutex_destroy(&rdev->mtx);
  537. mutex_destroy(&rdev->devlist_mtx);
  538. mutex_destroy(&rdev->sched_scan_mtx);
  539. list_for_each_entry_safe(scan, tmp, &rdev->bss_list, list)
  540. cfg80211_put_bss(&scan->pub);
  541. kfree(rdev);
  542. }
  543. void wiphy_free(struct wiphy *wiphy)
  544. {
  545. put_device(&wiphy->dev);
  546. }
  547. EXPORT_SYMBOL(wiphy_free);
  548. void wiphy_rfkill_set_hw_state(struct wiphy *wiphy, bool blocked)
  549. {
  550. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  551. if (rfkill_set_hw_state(rdev->rfkill, blocked))
  552. schedule_work(&rdev->rfkill_sync);
  553. }
  554. EXPORT_SYMBOL(wiphy_rfkill_set_hw_state);
  555. static void wdev_cleanup_work(struct work_struct *work)
  556. {
  557. struct wireless_dev *wdev;
  558. struct cfg80211_registered_device *rdev;
  559. wdev = container_of(work, struct wireless_dev, cleanup_work);
  560. rdev = wiphy_to_dev(wdev->wiphy);
  561. cfg80211_lock_rdev(rdev);
  562. if (WARN_ON(rdev->scan_req && rdev->scan_req->dev == wdev->netdev)) {
  563. rdev->scan_req->aborted = true;
  564. ___cfg80211_scan_done(rdev, true);
  565. }
  566. cfg80211_unlock_rdev(rdev);
  567. mutex_lock(&rdev->sched_scan_mtx);
  568. if (WARN_ON(rdev->sched_scan_req &&
  569. rdev->sched_scan_req->dev == wdev->netdev)) {
  570. __cfg80211_stop_sched_scan(rdev, false);
  571. }
  572. mutex_unlock(&rdev->sched_scan_mtx);
  573. mutex_lock(&rdev->devlist_mtx);
  574. rdev->opencount--;
  575. mutex_unlock(&rdev->devlist_mtx);
  576. wake_up(&rdev->dev_wait);
  577. dev_put(wdev->netdev);
  578. }
  579. static struct device_type wiphy_type = {
  580. .name = "wlan",
  581. };
  582. static struct ieee80211_channel *
  583. cfg80211_get_any_chan(struct cfg80211_registered_device *rdev)
  584. {
  585. struct ieee80211_supported_band *sband;
  586. int i;
  587. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  588. sband = rdev->wiphy.bands[i];
  589. if (sband && sband->n_channels > 0)
  590. return &sband->channels[0];
  591. }
  592. return NULL;
  593. }
  594. static void cfg80211_init_mon_chan(struct cfg80211_registered_device *rdev)
  595. {
  596. struct ieee80211_channel *chan;
  597. chan = cfg80211_get_any_chan(rdev);
  598. if (WARN_ON(!chan))
  599. return;
  600. mutex_lock(&rdev->devlist_mtx);
  601. WARN_ON(cfg80211_set_monitor_channel(rdev, chan->center_freq,
  602. NL80211_CHAN_NO_HT));
  603. mutex_unlock(&rdev->devlist_mtx);
  604. }
  605. void cfg80211_update_iface_num(struct cfg80211_registered_device *rdev,
  606. enum nl80211_iftype iftype, int num)
  607. {
  608. bool has_monitors_only_old = cfg80211_has_monitors_only(rdev);
  609. bool has_monitors_only_new;
  610. ASSERT_RTNL();
  611. rdev->num_running_ifaces += num;
  612. if (iftype == NL80211_IFTYPE_MONITOR)
  613. rdev->num_running_monitor_ifaces += num;
  614. has_monitors_only_new = cfg80211_has_monitors_only(rdev);
  615. if (has_monitors_only_new != has_monitors_only_old) {
  616. rdev->ops->set_monitor_enabled(&rdev->wiphy,
  617. has_monitors_only_new);
  618. if (!has_monitors_only_new) {
  619. rdev->monitor_channel = NULL;
  620. rdev->monitor_channel_type = NL80211_CHAN_NO_HT;
  621. } else {
  622. cfg80211_init_mon_chan(rdev);
  623. }
  624. }
  625. }
  626. static int cfg80211_netdev_notifier_call(struct notifier_block *nb,
  627. unsigned long state,
  628. void *ndev)
  629. {
  630. struct net_device *dev = ndev;
  631. struct wireless_dev *wdev = dev->ieee80211_ptr;
  632. struct cfg80211_registered_device *rdev;
  633. int ret;
  634. if (!wdev)
  635. return NOTIFY_DONE;
  636. rdev = wiphy_to_dev(wdev->wiphy);
  637. WARN_ON(wdev->iftype == NL80211_IFTYPE_UNSPECIFIED);
  638. switch (state) {
  639. case NETDEV_POST_INIT:
  640. SET_NETDEV_DEVTYPE(dev, &wiphy_type);
  641. break;
  642. case NETDEV_REGISTER:
  643. /*
  644. * NB: cannot take rdev->mtx here because this may be
  645. * called within code protected by it when interfaces
  646. * are added with nl80211.
  647. */
  648. mutex_init(&wdev->mtx);
  649. INIT_WORK(&wdev->cleanup_work, wdev_cleanup_work);
  650. INIT_LIST_HEAD(&wdev->event_list);
  651. spin_lock_init(&wdev->event_lock);
  652. INIT_LIST_HEAD(&wdev->mgmt_registrations);
  653. spin_lock_init(&wdev->mgmt_registrations_lock);
  654. mutex_lock(&rdev->devlist_mtx);
  655. list_add_rcu(&wdev->list, &rdev->netdev_list);
  656. rdev->devlist_generation++;
  657. /* can only change netns with wiphy */
  658. dev->features |= NETIF_F_NETNS_LOCAL;
  659. if (sysfs_create_link(&dev->dev.kobj, &rdev->wiphy.dev.kobj,
  660. "phy80211")) {
  661. pr_err("failed to add phy80211 symlink to netdev!\n");
  662. }
  663. wdev->netdev = dev;
  664. wdev->sme_state = CFG80211_SME_IDLE;
  665. mutex_unlock(&rdev->devlist_mtx);
  666. #ifdef CONFIG_CFG80211_WEXT
  667. wdev->wext.default_key = -1;
  668. wdev->wext.default_mgmt_key = -1;
  669. wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
  670. #endif
  671. if (wdev->wiphy->flags & WIPHY_FLAG_PS_ON_BY_DEFAULT)
  672. wdev->ps = true;
  673. else
  674. wdev->ps = false;
  675. /* allow mac80211 to determine the timeout */
  676. wdev->ps_timeout = -1;
  677. if (!dev->ethtool_ops)
  678. dev->ethtool_ops = &cfg80211_ethtool_ops;
  679. if ((wdev->iftype == NL80211_IFTYPE_STATION ||
  680. wdev->iftype == NL80211_IFTYPE_P2P_CLIENT ||
  681. wdev->iftype == NL80211_IFTYPE_ADHOC) && !wdev->use_4addr)
  682. dev->priv_flags |= IFF_DONT_BRIDGE;
  683. break;
  684. case NETDEV_GOING_DOWN:
  685. switch (wdev->iftype) {
  686. case NL80211_IFTYPE_ADHOC:
  687. cfg80211_leave_ibss(rdev, dev, true);
  688. break;
  689. case NL80211_IFTYPE_P2P_CLIENT:
  690. case NL80211_IFTYPE_STATION:
  691. mutex_lock(&rdev->sched_scan_mtx);
  692. __cfg80211_stop_sched_scan(rdev, false);
  693. mutex_unlock(&rdev->sched_scan_mtx);
  694. wdev_lock(wdev);
  695. #ifdef CONFIG_CFG80211_WEXT
  696. kfree(wdev->wext.ie);
  697. wdev->wext.ie = NULL;
  698. wdev->wext.ie_len = 0;
  699. wdev->wext.connect.auth_type = NL80211_AUTHTYPE_AUTOMATIC;
  700. #endif
  701. __cfg80211_disconnect(rdev, dev,
  702. WLAN_REASON_DEAUTH_LEAVING, true);
  703. cfg80211_mlme_down(rdev, dev);
  704. wdev_unlock(wdev);
  705. break;
  706. case NL80211_IFTYPE_MESH_POINT:
  707. cfg80211_leave_mesh(rdev, dev);
  708. break;
  709. case NL80211_IFTYPE_AP:
  710. cfg80211_stop_ap(rdev, dev);
  711. break;
  712. default:
  713. break;
  714. }
  715. wdev->beacon_interval = 0;
  716. break;
  717. case NETDEV_DOWN:
  718. cfg80211_update_iface_num(rdev, wdev->iftype, -1);
  719. dev_hold(dev);
  720. queue_work(cfg80211_wq, &wdev->cleanup_work);
  721. break;
  722. case NETDEV_UP:
  723. /*
  724. * If we have a really quick DOWN/UP succession we may
  725. * have this work still pending ... cancel it and see
  726. * if it was pending, in which case we need to account
  727. * for some of the work it would have done.
  728. */
  729. if (cancel_work_sync(&wdev->cleanup_work)) {
  730. mutex_lock(&rdev->devlist_mtx);
  731. rdev->opencount--;
  732. mutex_unlock(&rdev->devlist_mtx);
  733. dev_put(dev);
  734. }
  735. cfg80211_lock_rdev(rdev);
  736. mutex_lock(&rdev->devlist_mtx);
  737. wdev_lock(wdev);
  738. switch (wdev->iftype) {
  739. #ifdef CONFIG_CFG80211_WEXT
  740. case NL80211_IFTYPE_ADHOC:
  741. cfg80211_ibss_wext_join(rdev, wdev);
  742. break;
  743. case NL80211_IFTYPE_STATION:
  744. cfg80211_mgd_wext_connect(rdev, wdev);
  745. break;
  746. #endif
  747. #ifdef CONFIG_MAC80211_MESH
  748. case NL80211_IFTYPE_MESH_POINT:
  749. {
  750. /* backward compat code... */
  751. struct mesh_setup setup;
  752. memcpy(&setup, &default_mesh_setup,
  753. sizeof(setup));
  754. /* back compat only needed for mesh_id */
  755. setup.mesh_id = wdev->ssid;
  756. setup.mesh_id_len = wdev->mesh_id_up_len;
  757. if (wdev->mesh_id_up_len)
  758. __cfg80211_join_mesh(rdev, dev,
  759. &setup,
  760. &default_mesh_config);
  761. break;
  762. }
  763. #endif
  764. default:
  765. break;
  766. }
  767. wdev_unlock(wdev);
  768. rdev->opencount++;
  769. mutex_unlock(&rdev->devlist_mtx);
  770. cfg80211_unlock_rdev(rdev);
  771. /*
  772. * Configure power management to the driver here so that its
  773. * correctly set also after interface type changes etc.
  774. */
  775. if ((wdev->iftype == NL80211_IFTYPE_STATION ||
  776. wdev->iftype == NL80211_IFTYPE_P2P_CLIENT) &&
  777. rdev->ops->set_power_mgmt)
  778. if (rdev->ops->set_power_mgmt(wdev->wiphy, dev,
  779. wdev->ps,
  780. wdev->ps_timeout)) {
  781. /* assume this means it's off */
  782. wdev->ps = false;
  783. }
  784. break;
  785. case NETDEV_UNREGISTER:
  786. /*
  787. * NB: cannot take rdev->mtx here because this may be
  788. * called within code protected by it when interfaces
  789. * are removed with nl80211.
  790. */
  791. mutex_lock(&rdev->devlist_mtx);
  792. /*
  793. * It is possible to get NETDEV_UNREGISTER
  794. * multiple times. To detect that, check
  795. * that the interface is still on the list
  796. * of registered interfaces, and only then
  797. * remove and clean it up.
  798. */
  799. if (!list_empty(&wdev->list)) {
  800. sysfs_remove_link(&dev->dev.kobj, "phy80211");
  801. list_del_rcu(&wdev->list);
  802. rdev->devlist_generation++;
  803. cfg80211_mlme_purge_registrations(wdev);
  804. #ifdef CONFIG_CFG80211_WEXT
  805. kfree(wdev->wext.keys);
  806. #endif
  807. }
  808. mutex_unlock(&rdev->devlist_mtx);
  809. /*
  810. * synchronise (so that we won't find this netdev
  811. * from other code any more) and then clear the list
  812. * head so that the above code can safely check for
  813. * !list_empty() to avoid double-cleanup.
  814. */
  815. synchronize_rcu();
  816. INIT_LIST_HEAD(&wdev->list);
  817. break;
  818. case NETDEV_PRE_UP:
  819. if (!(wdev->wiphy->interface_modes & BIT(wdev->iftype)))
  820. return notifier_from_errno(-EOPNOTSUPP);
  821. if (rfkill_blocked(rdev->rfkill))
  822. return notifier_from_errno(-ERFKILL);
  823. mutex_lock(&rdev->devlist_mtx);
  824. ret = cfg80211_can_add_interface(rdev, wdev->iftype);
  825. mutex_unlock(&rdev->devlist_mtx);
  826. if (ret)
  827. return notifier_from_errno(ret);
  828. cfg80211_update_iface_num(rdev, wdev->iftype, 1);
  829. break;
  830. }
  831. return NOTIFY_DONE;
  832. }
  833. static struct notifier_block cfg80211_netdev_notifier = {
  834. .notifier_call = cfg80211_netdev_notifier_call,
  835. };
  836. static void __net_exit cfg80211_pernet_exit(struct net *net)
  837. {
  838. struct cfg80211_registered_device *rdev;
  839. rtnl_lock();
  840. mutex_lock(&cfg80211_mutex);
  841. list_for_each_entry(rdev, &cfg80211_rdev_list, list) {
  842. if (net_eq(wiphy_net(&rdev->wiphy), net))
  843. WARN_ON(cfg80211_switch_netns(rdev, &init_net));
  844. }
  845. mutex_unlock(&cfg80211_mutex);
  846. rtnl_unlock();
  847. }
  848. static struct pernet_operations cfg80211_pernet_ops = {
  849. .exit = cfg80211_pernet_exit,
  850. };
  851. static int __init cfg80211_init(void)
  852. {
  853. int err;
  854. err = register_pernet_device(&cfg80211_pernet_ops);
  855. if (err)
  856. goto out_fail_pernet;
  857. err = wiphy_sysfs_init();
  858. if (err)
  859. goto out_fail_sysfs;
  860. err = register_netdevice_notifier(&cfg80211_netdev_notifier);
  861. if (err)
  862. goto out_fail_notifier;
  863. err = nl80211_init();
  864. if (err)
  865. goto out_fail_nl80211;
  866. ieee80211_debugfs_dir = debugfs_create_dir("ieee80211", NULL);
  867. err = regulatory_init();
  868. if (err)
  869. goto out_fail_reg;
  870. cfg80211_wq = create_singlethread_workqueue("cfg80211");
  871. if (!cfg80211_wq)
  872. goto out_fail_wq;
  873. return 0;
  874. out_fail_wq:
  875. regulatory_exit();
  876. out_fail_reg:
  877. debugfs_remove(ieee80211_debugfs_dir);
  878. out_fail_nl80211:
  879. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  880. out_fail_notifier:
  881. wiphy_sysfs_exit();
  882. out_fail_sysfs:
  883. unregister_pernet_device(&cfg80211_pernet_ops);
  884. out_fail_pernet:
  885. return err;
  886. }
  887. subsys_initcall(cfg80211_init);
  888. static void __exit cfg80211_exit(void)
  889. {
  890. debugfs_remove(ieee80211_debugfs_dir);
  891. nl80211_exit();
  892. unregister_netdevice_notifier(&cfg80211_netdev_notifier);
  893. wiphy_sysfs_exit();
  894. regulatory_exit();
  895. unregister_pernet_device(&cfg80211_pernet_ops);
  896. destroy_workqueue(cfg80211_wq);
  897. }
  898. module_exit(cfg80211_exit);