mesh.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Portions
  4. * Copyright (C) 2022 Intel Corporation
  5. */
  6. #include <linux/ieee80211.h>
  7. #include <linux/export.h>
  8. #include <net/cfg80211.h>
  9. #include "nl80211.h"
  10. #include "core.h"
  11. #include "rdev-ops.h"
  12. /* Default values, timeouts in ms */
  13. #define MESH_TTL 31
  14. #define MESH_DEFAULT_ELEMENT_TTL 31
  15. #define MESH_MAX_RETR 3
  16. #define MESH_RET_T 100
  17. #define MESH_CONF_T 100
  18. #define MESH_HOLD_T 100
  19. #define MESH_PATH_TIMEOUT 5000
  20. #define MESH_RANN_INTERVAL 5000
  21. #define MESH_PATH_TO_ROOT_TIMEOUT 6000
  22. #define MESH_ROOT_INTERVAL 5000
  23. #define MESH_ROOT_CONFIRMATION_INTERVAL 2000
  24. #define MESH_DEFAULT_PLINK_TIMEOUT 1800 /* timeout in seconds */
  25. /*
  26. * Minimum interval between two consecutive PREQs originated by the same
  27. * interface
  28. */
  29. #define MESH_PREQ_MIN_INT 10
  30. #define MESH_PERR_MIN_INT 100
  31. #define MESH_DIAM_TRAVERSAL_TIME 50
  32. #define MESH_RSSI_THRESHOLD 0
  33. /*
  34. * A path will be refreshed if it is used PATH_REFRESH_TIME milliseconds
  35. * before timing out. This way it will remain ACTIVE and no data frames
  36. * will be unnecessarily held in the pending queue.
  37. */
  38. #define MESH_PATH_REFRESH_TIME 1000
  39. #define MESH_MIN_DISCOVERY_TIMEOUT (2 * MESH_DIAM_TRAVERSAL_TIME)
  40. /* Default maximum number of established plinks per interface */
  41. #define MESH_MAX_ESTAB_PLINKS 32
  42. #define MESH_MAX_PREQ_RETRIES 4
  43. #define MESH_SYNC_NEIGHBOR_OFFSET_MAX 50
  44. #define MESH_DEFAULT_BEACON_INTERVAL 1000 /* in 1024 us units (=TUs) */
  45. #define MESH_DEFAULT_DTIM_PERIOD 2
  46. #define MESH_DEFAULT_AWAKE_WINDOW 10 /* in 1024 us units (=TUs) */
  47. const struct mesh_config default_mesh_config = {
  48. .dot11MeshRetryTimeout = MESH_RET_T,
  49. .dot11MeshConfirmTimeout = MESH_CONF_T,
  50. .dot11MeshHoldingTimeout = MESH_HOLD_T,
  51. .dot11MeshMaxRetries = MESH_MAX_RETR,
  52. .dot11MeshTTL = MESH_TTL,
  53. .element_ttl = MESH_DEFAULT_ELEMENT_TTL,
  54. .auto_open_plinks = true,
  55. .dot11MeshMaxPeerLinks = MESH_MAX_ESTAB_PLINKS,
  56. .dot11MeshNbrOffsetMaxNeighbor = MESH_SYNC_NEIGHBOR_OFFSET_MAX,
  57. .dot11MeshHWMPactivePathTimeout = MESH_PATH_TIMEOUT,
  58. .dot11MeshHWMPpreqMinInterval = MESH_PREQ_MIN_INT,
  59. .dot11MeshHWMPperrMinInterval = MESH_PERR_MIN_INT,
  60. .dot11MeshHWMPnetDiameterTraversalTime = MESH_DIAM_TRAVERSAL_TIME,
  61. .dot11MeshHWMPmaxPREQretries = MESH_MAX_PREQ_RETRIES,
  62. .path_refresh_time = MESH_PATH_REFRESH_TIME,
  63. .min_discovery_timeout = MESH_MIN_DISCOVERY_TIMEOUT,
  64. .dot11MeshHWMPRannInterval = MESH_RANN_INTERVAL,
  65. .dot11MeshGateAnnouncementProtocol = false,
  66. .dot11MeshForwarding = true,
  67. .rssi_threshold = MESH_RSSI_THRESHOLD,
  68. .ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED,
  69. .dot11MeshHWMPactivePathToRootTimeout = MESH_PATH_TO_ROOT_TIMEOUT,
  70. .dot11MeshHWMProotInterval = MESH_ROOT_INTERVAL,
  71. .dot11MeshHWMPconfirmationInterval = MESH_ROOT_CONFIRMATION_INTERVAL,
  72. .power_mode = NL80211_MESH_POWER_ACTIVE,
  73. .dot11MeshAwakeWindowDuration = MESH_DEFAULT_AWAKE_WINDOW,
  74. .plink_timeout = MESH_DEFAULT_PLINK_TIMEOUT,
  75. .dot11MeshNolearn = false,
  76. };
  77. const struct mesh_setup default_mesh_setup = {
  78. /* cfg80211_join_mesh() will pick a channel if needed */
  79. .sync_method = IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET,
  80. .path_sel_proto = IEEE80211_PATH_PROTOCOL_HWMP,
  81. .path_metric = IEEE80211_PATH_METRIC_AIRTIME,
  82. .auth_id = 0, /* open */
  83. .ie = NULL,
  84. .ie_len = 0,
  85. .is_secure = false,
  86. .user_mpm = false,
  87. .beacon_interval = MESH_DEFAULT_BEACON_INTERVAL,
  88. .dtim_period = MESH_DEFAULT_DTIM_PERIOD,
  89. };
  90. int __cfg80211_join_mesh(struct cfg80211_registered_device *rdev,
  91. struct net_device *dev,
  92. struct mesh_setup *setup,
  93. const struct mesh_config *conf)
  94. {
  95. struct wireless_dev *wdev = dev->ieee80211_ptr;
  96. int err;
  97. BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN != IEEE80211_MAX_MESH_ID_LEN);
  98. ASSERT_WDEV_LOCK(wdev);
  99. if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
  100. return -EOPNOTSUPP;
  101. if (!(rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
  102. setup->is_secure)
  103. return -EOPNOTSUPP;
  104. if (wdev->u.mesh.id_len)
  105. return -EALREADY;
  106. if (!setup->mesh_id_len)
  107. return -EINVAL;
  108. if (!rdev->ops->join_mesh)
  109. return -EOPNOTSUPP;
  110. if (!setup->chandef.chan) {
  111. /* if no channel explicitly given, use preset channel */
  112. setup->chandef = wdev->u.mesh.preset_chandef;
  113. }
  114. if (!setup->chandef.chan) {
  115. /* if we don't have that either, use the first usable channel */
  116. enum nl80211_band band;
  117. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  118. struct ieee80211_supported_band *sband;
  119. struct ieee80211_channel *chan;
  120. int i;
  121. sband = rdev->wiphy.bands[band];
  122. if (!sband)
  123. continue;
  124. for (i = 0; i < sband->n_channels; i++) {
  125. chan = &sband->channels[i];
  126. if (chan->flags & (IEEE80211_CHAN_NO_IR |
  127. IEEE80211_CHAN_DISABLED |
  128. IEEE80211_CHAN_RADAR))
  129. continue;
  130. setup->chandef.chan = chan;
  131. break;
  132. }
  133. if (setup->chandef.chan)
  134. break;
  135. }
  136. /* no usable channel ... */
  137. if (!setup->chandef.chan)
  138. return -EINVAL;
  139. setup->chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
  140. setup->chandef.center_freq1 = setup->chandef.chan->center_freq;
  141. }
  142. /*
  143. * check if basic rates are available otherwise use mandatory rates as
  144. * basic rates
  145. */
  146. if (!setup->basic_rates) {
  147. enum nl80211_bss_scan_width scan_width;
  148. struct ieee80211_supported_band *sband =
  149. rdev->wiphy.bands[setup->chandef.chan->band];
  150. if (setup->chandef.chan->band == NL80211_BAND_2GHZ) {
  151. int i;
  152. /*
  153. * Older versions selected the mandatory rates for
  154. * 2.4 GHz as well, but were broken in that only
  155. * 1 Mbps was regarded as a mandatory rate. Keep
  156. * using just 1 Mbps as the default basic rate for
  157. * mesh to be interoperable with older versions.
  158. */
  159. for (i = 0; i < sband->n_bitrates; i++) {
  160. if (sband->bitrates[i].bitrate == 10) {
  161. setup->basic_rates = BIT(i);
  162. break;
  163. }
  164. }
  165. } else {
  166. scan_width = cfg80211_chandef_to_scan_width(&setup->chandef);
  167. setup->basic_rates = ieee80211_mandatory_rates(sband,
  168. scan_width);
  169. }
  170. }
  171. err = cfg80211_chandef_dfs_required(&rdev->wiphy,
  172. &setup->chandef,
  173. NL80211_IFTYPE_MESH_POINT);
  174. if (err < 0)
  175. return err;
  176. if (err > 0 && !setup->userspace_handles_dfs)
  177. return -EINVAL;
  178. if (!cfg80211_reg_can_beacon(&rdev->wiphy, &setup->chandef,
  179. NL80211_IFTYPE_MESH_POINT))
  180. return -EINVAL;
  181. err = rdev_join_mesh(rdev, dev, conf, setup);
  182. if (!err) {
  183. memcpy(wdev->u.mesh.id, setup->mesh_id, setup->mesh_id_len);
  184. wdev->u.mesh.id_len = setup->mesh_id_len;
  185. wdev->u.mesh.chandef = setup->chandef;
  186. wdev->u.mesh.beacon_interval = setup->beacon_interval;
  187. }
  188. return err;
  189. }
  190. int cfg80211_set_mesh_channel(struct cfg80211_registered_device *rdev,
  191. struct wireless_dev *wdev,
  192. struct cfg80211_chan_def *chandef)
  193. {
  194. int err;
  195. /*
  196. * Workaround for libertas (only!), it puts the interface
  197. * into mesh mode but doesn't implement join_mesh. Instead,
  198. * it is configured via sysfs and then joins the mesh when
  199. * you set the channel. Note that the libertas mesh isn't
  200. * compatible with 802.11 mesh.
  201. */
  202. if (rdev->ops->libertas_set_mesh_channel) {
  203. if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
  204. return -EINVAL;
  205. if (!netif_running(wdev->netdev))
  206. return -ENETDOWN;
  207. err = rdev_libertas_set_mesh_channel(rdev, wdev->netdev,
  208. chandef->chan);
  209. if (!err)
  210. wdev->u.mesh.chandef = *chandef;
  211. return err;
  212. }
  213. if (wdev->u.mesh.id_len)
  214. return -EBUSY;
  215. wdev->u.mesh.preset_chandef = *chandef;
  216. return 0;
  217. }
  218. int __cfg80211_leave_mesh(struct cfg80211_registered_device *rdev,
  219. struct net_device *dev)
  220. {
  221. struct wireless_dev *wdev = dev->ieee80211_ptr;
  222. int err;
  223. ASSERT_WDEV_LOCK(wdev);
  224. if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
  225. return -EOPNOTSUPP;
  226. if (!rdev->ops->leave_mesh)
  227. return -EOPNOTSUPP;
  228. if (!wdev->u.mesh.id_len)
  229. return -ENOTCONN;
  230. err = rdev_leave_mesh(rdev, dev);
  231. if (!err) {
  232. wdev->conn_owner_nlportid = 0;
  233. wdev->u.mesh.id_len = 0;
  234. wdev->u.mesh.beacon_interval = 0;
  235. memset(&wdev->u.mesh.chandef, 0,
  236. sizeof(wdev->u.mesh.chandef));
  237. rdev_set_qos_map(rdev, dev, NULL);
  238. cfg80211_sched_dfs_chan_update(rdev);
  239. }
  240. return err;
  241. }
  242. int cfg80211_leave_mesh(struct cfg80211_registered_device *rdev,
  243. struct net_device *dev)
  244. {
  245. struct wireless_dev *wdev = dev->ieee80211_ptr;
  246. int err;
  247. wdev_lock(wdev);
  248. err = __cfg80211_leave_mesh(rdev, dev);
  249. wdev_unlock(wdev);
  250. return err;
  251. }