mesh.c 7.2 KB

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