mesh.c 8.0 KB

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