mesh.c 7.5 KB

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