mesh.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. .dot11MeshNolearn = false,
  72. };
  73. const struct mesh_setup default_mesh_setup = {
  74. /* cfg80211_join_mesh() will pick a channel if needed */
  75. .sync_method = IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET,
  76. .path_sel_proto = IEEE80211_PATH_PROTOCOL_HWMP,
  77. .path_metric = IEEE80211_PATH_METRIC_AIRTIME,
  78. .auth_id = 0, /* open */
  79. .ie = NULL,
  80. .ie_len = 0,
  81. .is_secure = false,
  82. .user_mpm = false,
  83. .beacon_interval = MESH_DEFAULT_BEACON_INTERVAL,
  84. .dtim_period = MESH_DEFAULT_DTIM_PERIOD,
  85. };
  86. int __cfg80211_join_mesh(struct cfg80211_registered_device *rdev,
  87. struct net_device *dev,
  88. struct mesh_setup *setup,
  89. const struct mesh_config *conf)
  90. {
  91. struct wireless_dev *wdev = dev->ieee80211_ptr;
  92. int err;
  93. BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN != IEEE80211_MAX_MESH_ID_LEN);
  94. ASSERT_WDEV_LOCK(wdev);
  95. if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
  96. return -EOPNOTSUPP;
  97. if (!(rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
  98. setup->is_secure)
  99. return -EOPNOTSUPP;
  100. if (wdev->mesh_id_len)
  101. return -EALREADY;
  102. if (!setup->mesh_id_len)
  103. return -EINVAL;
  104. if (!rdev->ops->join_mesh)
  105. return -EOPNOTSUPP;
  106. if (!setup->chandef.chan) {
  107. /* if no channel explicitly given, use preset channel */
  108. setup->chandef = wdev->preset_chandef;
  109. }
  110. if (!setup->chandef.chan) {
  111. /* if we don't have that either, use the first usable channel */
  112. enum nl80211_band band;
  113. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  114. struct ieee80211_supported_band *sband;
  115. struct ieee80211_channel *chan;
  116. int i;
  117. sband = rdev->wiphy.bands[band];
  118. if (!sband)
  119. continue;
  120. for (i = 0; i < sband->n_channels; i++) {
  121. chan = &sband->channels[i];
  122. if (chan->flags & (IEEE80211_CHAN_NO_IR |
  123. IEEE80211_CHAN_DISABLED |
  124. IEEE80211_CHAN_RADAR))
  125. continue;
  126. setup->chandef.chan = chan;
  127. break;
  128. }
  129. if (setup->chandef.chan)
  130. break;
  131. }
  132. /* no usable channel ... */
  133. if (!setup->chandef.chan)
  134. return -EINVAL;
  135. setup->chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
  136. setup->chandef.center_freq1 = setup->chandef.chan->center_freq;
  137. }
  138. /*
  139. * check if basic rates are available otherwise use mandatory rates as
  140. * basic rates
  141. */
  142. if (!setup->basic_rates) {
  143. enum nl80211_bss_scan_width scan_width;
  144. struct ieee80211_supported_band *sband =
  145. rdev->wiphy.bands[setup->chandef.chan->band];
  146. if (setup->chandef.chan->band == NL80211_BAND_2GHZ) {
  147. int i;
  148. /*
  149. * Older versions selected the mandatory rates for
  150. * 2.4 GHz as well, but were broken in that only
  151. * 1 Mbps was regarded as a mandatory rate. Keep
  152. * using just 1 Mbps as the default basic rate for
  153. * mesh to be interoperable with older versions.
  154. */
  155. for (i = 0; i < sband->n_bitrates; i++) {
  156. if (sband->bitrates[i].bitrate == 10) {
  157. setup->basic_rates = BIT(i);
  158. break;
  159. }
  160. }
  161. } else {
  162. scan_width = cfg80211_chandef_to_scan_width(&setup->chandef);
  163. setup->basic_rates = ieee80211_mandatory_rates(sband,
  164. scan_width);
  165. }
  166. }
  167. err = cfg80211_chandef_dfs_required(&rdev->wiphy,
  168. &setup->chandef,
  169. NL80211_IFTYPE_MESH_POINT);
  170. if (err < 0)
  171. return err;
  172. if (err > 0 && !setup->userspace_handles_dfs)
  173. return -EINVAL;
  174. if (!cfg80211_reg_can_beacon(&rdev->wiphy, &setup->chandef,
  175. NL80211_IFTYPE_MESH_POINT))
  176. return -EINVAL;
  177. err = rdev_join_mesh(rdev, dev, conf, setup);
  178. if (!err) {
  179. memcpy(wdev->ssid, setup->mesh_id, setup->mesh_id_len);
  180. wdev->mesh_id_len = setup->mesh_id_len;
  181. wdev->chandef = setup->chandef;
  182. wdev->beacon_interval = setup->beacon_interval;
  183. }
  184. return err;
  185. }
  186. int cfg80211_set_mesh_channel(struct cfg80211_registered_device *rdev,
  187. struct wireless_dev *wdev,
  188. struct cfg80211_chan_def *chandef)
  189. {
  190. int err;
  191. /*
  192. * Workaround for libertas (only!), it puts the interface
  193. * into mesh mode but doesn't implement join_mesh. Instead,
  194. * it is configured via sysfs and then joins the mesh when
  195. * you set the channel. Note that the libertas mesh isn't
  196. * compatible with 802.11 mesh.
  197. */
  198. if (rdev->ops->libertas_set_mesh_channel) {
  199. if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
  200. return -EINVAL;
  201. if (!netif_running(wdev->netdev))
  202. return -ENETDOWN;
  203. err = rdev_libertas_set_mesh_channel(rdev, wdev->netdev,
  204. chandef->chan);
  205. if (!err)
  206. wdev->chandef = *chandef;
  207. return err;
  208. }
  209. if (wdev->mesh_id_len)
  210. return -EBUSY;
  211. wdev->preset_chandef = *chandef;
  212. return 0;
  213. }
  214. int __cfg80211_leave_mesh(struct cfg80211_registered_device *rdev,
  215. struct net_device *dev)
  216. {
  217. struct wireless_dev *wdev = dev->ieee80211_ptr;
  218. int err;
  219. ASSERT_WDEV_LOCK(wdev);
  220. if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
  221. return -EOPNOTSUPP;
  222. if (!rdev->ops->leave_mesh)
  223. return -EOPNOTSUPP;
  224. if (!wdev->mesh_id_len)
  225. return -ENOTCONN;
  226. err = rdev_leave_mesh(rdev, dev);
  227. if (!err) {
  228. wdev->conn_owner_nlportid = 0;
  229. wdev->mesh_id_len = 0;
  230. wdev->beacon_interval = 0;
  231. memset(&wdev->chandef, 0, sizeof(wdev->chandef));
  232. rdev_set_qos_map(rdev, dev, NULL);
  233. cfg80211_sched_dfs_chan_update(rdev);
  234. }
  235. return err;
  236. }
  237. int cfg80211_leave_mesh(struct cfg80211_registered_device *rdev,
  238. struct net_device *dev)
  239. {
  240. struct wireless_dev *wdev = dev->ieee80211_ptr;
  241. int err;
  242. wdev_lock(wdev);
  243. err = __cfg80211_leave_mesh(rdev, dev);
  244. wdev_unlock(wdev);
  245. return err;
  246. }