mesh.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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_IR |
  121. IEEE80211_CHAN_DISABLED |
  122. IEEE80211_CHAN_RADAR))
  123. continue;
  124. setup->chandef.chan = chan;
  125. break;
  126. }
  127. if (setup->chandef.chan)
  128. break;
  129. }
  130. /* no usable channel ... */
  131. if (!setup->chandef.chan)
  132. return -EINVAL;
  133. setup->chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
  134. setup->chandef.center_freq1 = setup->chandef.chan->center_freq;
  135. }
  136. /*
  137. * check if basic rates are available otherwise use mandatory rates as
  138. * basic rates
  139. */
  140. if (!setup->basic_rates) {
  141. enum nl80211_bss_scan_width scan_width;
  142. struct ieee80211_supported_band *sband =
  143. rdev->wiphy.bands[setup->chandef.chan->band];
  144. scan_width = cfg80211_chandef_to_scan_width(&setup->chandef);
  145. setup->basic_rates = ieee80211_mandatory_rates(sband,
  146. scan_width);
  147. }
  148. if (!cfg80211_reg_can_beacon(&rdev->wiphy, &setup->chandef,
  149. NL80211_IFTYPE_MESH_POINT))
  150. return -EINVAL;
  151. err = rdev_join_mesh(rdev, dev, conf, setup);
  152. if (!err) {
  153. memcpy(wdev->ssid, setup->mesh_id, setup->mesh_id_len);
  154. wdev->mesh_id_len = setup->mesh_id_len;
  155. wdev->chandef = setup->chandef;
  156. }
  157. return err;
  158. }
  159. int cfg80211_join_mesh(struct cfg80211_registered_device *rdev,
  160. struct net_device *dev,
  161. struct mesh_setup *setup,
  162. const struct mesh_config *conf)
  163. {
  164. struct wireless_dev *wdev = dev->ieee80211_ptr;
  165. int err;
  166. wdev_lock(wdev);
  167. err = __cfg80211_join_mesh(rdev, dev, setup, conf);
  168. wdev_unlock(wdev);
  169. return err;
  170. }
  171. int cfg80211_set_mesh_channel(struct cfg80211_registered_device *rdev,
  172. struct wireless_dev *wdev,
  173. struct cfg80211_chan_def *chandef)
  174. {
  175. int err;
  176. /*
  177. * Workaround for libertas (only!), it puts the interface
  178. * into mesh mode but doesn't implement join_mesh. Instead,
  179. * it is configured via sysfs and then joins the mesh when
  180. * you set the channel. Note that the libertas mesh isn't
  181. * compatible with 802.11 mesh.
  182. */
  183. if (rdev->ops->libertas_set_mesh_channel) {
  184. if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
  185. return -EINVAL;
  186. if (!netif_running(wdev->netdev))
  187. return -ENETDOWN;
  188. err = rdev_libertas_set_mesh_channel(rdev, wdev->netdev,
  189. chandef->chan);
  190. if (!err)
  191. wdev->chandef = *chandef;
  192. return err;
  193. }
  194. if (wdev->mesh_id_len)
  195. return -EBUSY;
  196. wdev->preset_chandef = *chandef;
  197. return 0;
  198. }
  199. int __cfg80211_leave_mesh(struct cfg80211_registered_device *rdev,
  200. struct net_device *dev)
  201. {
  202. struct wireless_dev *wdev = dev->ieee80211_ptr;
  203. int err;
  204. ASSERT_WDEV_LOCK(wdev);
  205. if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
  206. return -EOPNOTSUPP;
  207. if (!rdev->ops->leave_mesh)
  208. return -EOPNOTSUPP;
  209. if (!wdev->mesh_id_len)
  210. return -ENOTCONN;
  211. err = rdev_leave_mesh(rdev, dev);
  212. if (!err) {
  213. wdev->mesh_id_len = 0;
  214. memset(&wdev->chandef, 0, sizeof(wdev->chandef));
  215. rdev_set_qos_map(rdev, dev, NULL);
  216. }
  217. return err;
  218. }
  219. int cfg80211_leave_mesh(struct cfg80211_registered_device *rdev,
  220. struct net_device *dev)
  221. {
  222. struct wireless_dev *wdev = dev->ieee80211_ptr;
  223. int err;
  224. wdev_lock(wdev);
  225. err = __cfg80211_leave_mesh(rdev, dev);
  226. wdev_unlock(wdev);
  227. return err;
  228. }