mesh.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. .beacon_interval = MESH_DEFAULT_BEACON_INTERVAL,
  78. .dtim_period = MESH_DEFAULT_DTIM_PERIOD,
  79. };
  80. int __cfg80211_join_mesh(struct cfg80211_registered_device *rdev,
  81. struct net_device *dev,
  82. struct mesh_setup *setup,
  83. const struct mesh_config *conf)
  84. {
  85. struct wireless_dev *wdev = dev->ieee80211_ptr;
  86. int err;
  87. BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN != IEEE80211_MAX_MESH_ID_LEN);
  88. ASSERT_WDEV_LOCK(wdev);
  89. if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
  90. return -EOPNOTSUPP;
  91. if (!(rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
  92. setup->is_secure)
  93. return -EOPNOTSUPP;
  94. if (wdev->mesh_id_len)
  95. return -EALREADY;
  96. if (!setup->mesh_id_len)
  97. return -EINVAL;
  98. if (!rdev->ops->join_mesh)
  99. return -EOPNOTSUPP;
  100. if (!setup->chandef.chan) {
  101. /* if no channel explicitly given, use preset channel */
  102. setup->chandef = wdev->preset_chandef;
  103. }
  104. if (!setup->chandef.chan) {
  105. /* if we don't have that either, use the first usable channel */
  106. enum ieee80211_band band;
  107. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  108. struct ieee80211_supported_band *sband;
  109. struct ieee80211_channel *chan;
  110. int i;
  111. sband = rdev->wiphy.bands[band];
  112. if (!sband)
  113. continue;
  114. for (i = 0; i < sband->n_channels; i++) {
  115. chan = &sband->channels[i];
  116. if (chan->flags & (IEEE80211_CHAN_NO_IBSS |
  117. IEEE80211_CHAN_PASSIVE_SCAN |
  118. IEEE80211_CHAN_DISABLED |
  119. IEEE80211_CHAN_RADAR))
  120. continue;
  121. setup->chandef.chan = chan;
  122. break;
  123. }
  124. if (setup->chandef.chan)
  125. break;
  126. }
  127. /* no usable channel ... */
  128. if (!setup->chandef.chan)
  129. return -EINVAL;
  130. setup->chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
  131. setup->chandef.center_freq1 = setup->chandef.chan->center_freq;
  132. }
  133. if (!cfg80211_reg_can_beacon(&rdev->wiphy, &setup->chandef))
  134. return -EINVAL;
  135. err = cfg80211_can_use_chan(rdev, wdev, setup->chandef.chan,
  136. CHAN_MODE_SHARED);
  137. if (err)
  138. return err;
  139. err = rdev_join_mesh(rdev, dev, conf, setup);
  140. if (!err) {
  141. memcpy(wdev->ssid, setup->mesh_id, setup->mesh_id_len);
  142. wdev->mesh_id_len = setup->mesh_id_len;
  143. wdev->channel = setup->chandef.chan;
  144. }
  145. return err;
  146. }
  147. int cfg80211_join_mesh(struct cfg80211_registered_device *rdev,
  148. struct net_device *dev,
  149. struct mesh_setup *setup,
  150. const struct mesh_config *conf)
  151. {
  152. struct wireless_dev *wdev = dev->ieee80211_ptr;
  153. int err;
  154. mutex_lock(&rdev->devlist_mtx);
  155. wdev_lock(wdev);
  156. err = __cfg80211_join_mesh(rdev, dev, setup, conf);
  157. wdev_unlock(wdev);
  158. mutex_unlock(&rdev->devlist_mtx);
  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. void cfg80211_notify_new_peer_candidate(struct net_device *dev,
  194. const u8 *macaddr, const u8* ie, u8 ie_len, gfp_t gfp)
  195. {
  196. struct wireless_dev *wdev = dev->ieee80211_ptr;
  197. trace_cfg80211_notify_new_peer_candidate(dev, macaddr);
  198. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
  199. return;
  200. nl80211_send_new_peer_candidate(wiphy_to_dev(wdev->wiphy), dev,
  201. macaddr, ie, ie_len, gfp);
  202. }
  203. EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
  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. }