mesh.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. .auth_id = 0, /* open */
  75. .ie = NULL,
  76. .ie_len = 0,
  77. .is_secure = false,
  78. .user_mpm = false,
  79. .beacon_interval = MESH_DEFAULT_BEACON_INTERVAL,
  80. .dtim_period = MESH_DEFAULT_DTIM_PERIOD,
  81. };
  82. int __cfg80211_join_mesh(struct cfg80211_registered_device *rdev,
  83. struct net_device *dev,
  84. struct mesh_setup *setup,
  85. const struct mesh_config *conf)
  86. {
  87. struct wireless_dev *wdev = dev->ieee80211_ptr;
  88. int err;
  89. BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN != IEEE80211_MAX_MESH_ID_LEN);
  90. ASSERT_WDEV_LOCK(wdev);
  91. if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
  92. return -EOPNOTSUPP;
  93. if (!(rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
  94. setup->is_secure)
  95. return -EOPNOTSUPP;
  96. if (wdev->mesh_id_len)
  97. return -EALREADY;
  98. if (!setup->mesh_id_len)
  99. return -EINVAL;
  100. if (!rdev->ops->join_mesh)
  101. return -EOPNOTSUPP;
  102. if (!setup->chandef.chan) {
  103. /* if no channel explicitly given, use preset channel */
  104. setup->chandef = wdev->preset_chandef;
  105. }
  106. if (!setup->chandef.chan) {
  107. /* if we don't have that either, use the first usable channel */
  108. enum ieee80211_band band;
  109. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  110. struct ieee80211_supported_band *sband;
  111. struct ieee80211_channel *chan;
  112. int i;
  113. sband = rdev->wiphy.bands[band];
  114. if (!sband)
  115. continue;
  116. for (i = 0; i < sband->n_channels; i++) {
  117. chan = &sband->channels[i];
  118. if (chan->flags & (IEEE80211_CHAN_NO_IBSS |
  119. IEEE80211_CHAN_PASSIVE_SCAN |
  120. IEEE80211_CHAN_DISABLED |
  121. IEEE80211_CHAN_RADAR))
  122. continue;
  123. setup->chandef.chan = chan;
  124. break;
  125. }
  126. if (setup->chandef.chan)
  127. break;
  128. }
  129. /* no usable channel ... */
  130. if (!setup->chandef.chan)
  131. return -EINVAL;
  132. setup->chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
  133. setup->chandef.center_freq1 = setup->chandef.chan->center_freq;
  134. }
  135. if (!cfg80211_reg_can_beacon(&rdev->wiphy, &setup->chandef))
  136. return -EINVAL;
  137. err = cfg80211_can_use_chan(rdev, wdev, setup->chandef.chan,
  138. CHAN_MODE_SHARED);
  139. if (err)
  140. return err;
  141. err = rdev_join_mesh(rdev, dev, conf, setup);
  142. if (!err) {
  143. memcpy(wdev->ssid, setup->mesh_id, setup->mesh_id_len);
  144. wdev->mesh_id_len = setup->mesh_id_len;
  145. wdev->channel = setup->chandef.chan;
  146. }
  147. return err;
  148. }
  149. int cfg80211_join_mesh(struct cfg80211_registered_device *rdev,
  150. struct net_device *dev,
  151. struct mesh_setup *setup,
  152. const struct mesh_config *conf)
  153. {
  154. struct wireless_dev *wdev = dev->ieee80211_ptr;
  155. int err;
  156. wdev_lock(wdev);
  157. err = __cfg80211_join_mesh(rdev, dev, setup, conf);
  158. wdev_unlock(wdev);
  159. return err;
  160. }
  161. int cfg80211_set_mesh_channel(struct cfg80211_registered_device *rdev,
  162. struct wireless_dev *wdev,
  163. struct cfg80211_chan_def *chandef)
  164. {
  165. int err;
  166. /*
  167. * Workaround for libertas (only!), it puts the interface
  168. * into mesh mode but doesn't implement join_mesh. Instead,
  169. * it is configured via sysfs and then joins the mesh when
  170. * you set the channel. Note that the libertas mesh isn't
  171. * compatible with 802.11 mesh.
  172. */
  173. if (rdev->ops->libertas_set_mesh_channel) {
  174. if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
  175. return -EINVAL;
  176. if (!netif_running(wdev->netdev))
  177. return -ENETDOWN;
  178. err = cfg80211_can_use_chan(rdev, wdev, chandef->chan,
  179. CHAN_MODE_SHARED);
  180. if (err)
  181. return err;
  182. err = rdev_libertas_set_mesh_channel(rdev, wdev->netdev,
  183. chandef->chan);
  184. if (!err)
  185. wdev->channel = chandef->chan;
  186. return err;
  187. }
  188. if (wdev->mesh_id_len)
  189. return -EBUSY;
  190. wdev->preset_chandef = *chandef;
  191. return 0;
  192. }
  193. static int __cfg80211_leave_mesh(struct cfg80211_registered_device *rdev,
  194. struct net_device *dev)
  195. {
  196. struct wireless_dev *wdev = dev->ieee80211_ptr;
  197. int err;
  198. ASSERT_WDEV_LOCK(wdev);
  199. if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
  200. return -EOPNOTSUPP;
  201. if (!rdev->ops->leave_mesh)
  202. return -EOPNOTSUPP;
  203. if (!wdev->mesh_id_len)
  204. return -ENOTCONN;
  205. err = rdev_leave_mesh(rdev, dev);
  206. if (!err) {
  207. wdev->mesh_id_len = 0;
  208. wdev->channel = NULL;
  209. }
  210. return err;
  211. }
  212. int cfg80211_leave_mesh(struct cfg80211_registered_device *rdev,
  213. struct net_device *dev)
  214. {
  215. struct wireless_dev *wdev = dev->ieee80211_ptr;
  216. int err;
  217. wdev_lock(wdev);
  218. err = __cfg80211_leave_mesh(rdev, dev);
  219. wdev_unlock(wdev);
  220. return err;
  221. }