mesh.c 7.2 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. /*
  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. const struct mesh_config default_mesh_config = {
  39. .dot11MeshRetryTimeout = MESH_RET_T,
  40. .dot11MeshConfirmTimeout = MESH_CONF_T,
  41. .dot11MeshHoldingTimeout = MESH_HOLD_T,
  42. .dot11MeshMaxRetries = MESH_MAX_RETR,
  43. .dot11MeshTTL = MESH_TTL,
  44. .element_ttl = MESH_DEFAULT_ELEMENT_TTL,
  45. .auto_open_plinks = true,
  46. .dot11MeshMaxPeerLinks = MESH_MAX_ESTAB_PLINKS,
  47. .dot11MeshNbrOffsetMaxNeighbor = MESH_SYNC_NEIGHBOR_OFFSET_MAX,
  48. .dot11MeshHWMPactivePathTimeout = MESH_PATH_TIMEOUT,
  49. .dot11MeshHWMPpreqMinInterval = MESH_PREQ_MIN_INT,
  50. .dot11MeshHWMPperrMinInterval = MESH_PERR_MIN_INT,
  51. .dot11MeshHWMPnetDiameterTraversalTime = MESH_DIAM_TRAVERSAL_TIME,
  52. .dot11MeshHWMPmaxPREQretries = MESH_MAX_PREQ_RETRIES,
  53. .path_refresh_time = MESH_PATH_REFRESH_TIME,
  54. .min_discovery_timeout = MESH_MIN_DISCOVERY_TIMEOUT,
  55. .dot11MeshHWMPRannInterval = MESH_RANN_INTERVAL,
  56. .dot11MeshGateAnnouncementProtocol = false,
  57. .dot11MeshForwarding = true,
  58. .rssi_threshold = MESH_RSSI_THRESHOLD,
  59. .ht_opmode = IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED,
  60. .dot11MeshHWMPactivePathToRootTimeout = MESH_PATH_TO_ROOT_TIMEOUT,
  61. .dot11MeshHWMProotInterval = MESH_ROOT_INTERVAL,
  62. .dot11MeshHWMPconfirmationInterval = MESH_ROOT_CONFIRMATION_INTERVAL,
  63. };
  64. const struct mesh_setup default_mesh_setup = {
  65. /* cfg80211_join_mesh() will pick a channel if needed */
  66. .sync_method = IEEE80211_SYNC_METHOD_NEIGHBOR_OFFSET,
  67. .path_sel_proto = IEEE80211_PATH_PROTOCOL_HWMP,
  68. .path_metric = IEEE80211_PATH_METRIC_AIRTIME,
  69. .ie = NULL,
  70. .ie_len = 0,
  71. .is_secure = false,
  72. };
  73. int __cfg80211_join_mesh(struct cfg80211_registered_device *rdev,
  74. struct net_device *dev,
  75. struct mesh_setup *setup,
  76. const struct mesh_config *conf)
  77. {
  78. struct wireless_dev *wdev = dev->ieee80211_ptr;
  79. int err;
  80. BUILD_BUG_ON(IEEE80211_MAX_SSID_LEN != IEEE80211_MAX_MESH_ID_LEN);
  81. ASSERT_WDEV_LOCK(wdev);
  82. if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
  83. return -EOPNOTSUPP;
  84. if (!(rdev->wiphy.flags & WIPHY_FLAG_MESH_AUTH) &&
  85. setup->is_secure)
  86. return -EOPNOTSUPP;
  87. if (wdev->mesh_id_len)
  88. return -EALREADY;
  89. if (!setup->mesh_id_len)
  90. return -EINVAL;
  91. if (!rdev->ops->join_mesh)
  92. return -EOPNOTSUPP;
  93. if (!setup->chandef.chan) {
  94. /* if no channel explicitly given, use preset channel */
  95. setup->chandef = wdev->preset_chandef;
  96. }
  97. if (!setup->chandef.chan) {
  98. /* if we don't have that either, use the first usable channel */
  99. enum ieee80211_band band;
  100. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  101. struct ieee80211_supported_band *sband;
  102. struct ieee80211_channel *chan;
  103. int i;
  104. sband = rdev->wiphy.bands[band];
  105. if (!sband)
  106. continue;
  107. for (i = 0; i < sband->n_channels; i++) {
  108. chan = &sband->channels[i];
  109. if (chan->flags & (IEEE80211_CHAN_NO_IBSS |
  110. IEEE80211_CHAN_PASSIVE_SCAN |
  111. IEEE80211_CHAN_DISABLED |
  112. IEEE80211_CHAN_RADAR))
  113. continue;
  114. setup->chandef.chan = chan;
  115. break;
  116. }
  117. if (setup->chandef.chan)
  118. break;
  119. }
  120. /* no usable channel ... */
  121. if (!setup->chandef.chan)
  122. return -EINVAL;
  123. setup->chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
  124. setup->chandef.center_freq1 = setup->chandef.chan->center_freq;
  125. }
  126. if (!cfg80211_reg_can_beacon(&rdev->wiphy, &setup->chandef))
  127. return -EINVAL;
  128. err = cfg80211_can_use_chan(rdev, wdev, setup->chandef.chan,
  129. CHAN_MODE_SHARED);
  130. if (err)
  131. return err;
  132. err = rdev_join_mesh(rdev, dev, conf, setup);
  133. if (!err) {
  134. memcpy(wdev->ssid, setup->mesh_id, setup->mesh_id_len);
  135. wdev->mesh_id_len = setup->mesh_id_len;
  136. wdev->channel = setup->chandef.chan;
  137. }
  138. return err;
  139. }
  140. int cfg80211_join_mesh(struct cfg80211_registered_device *rdev,
  141. struct net_device *dev,
  142. struct mesh_setup *setup,
  143. const struct mesh_config *conf)
  144. {
  145. struct wireless_dev *wdev = dev->ieee80211_ptr;
  146. int err;
  147. mutex_lock(&rdev->devlist_mtx);
  148. wdev_lock(wdev);
  149. err = __cfg80211_join_mesh(rdev, dev, setup, conf);
  150. wdev_unlock(wdev);
  151. mutex_unlock(&rdev->devlist_mtx);
  152. return err;
  153. }
  154. int cfg80211_set_mesh_channel(struct cfg80211_registered_device *rdev,
  155. struct wireless_dev *wdev,
  156. struct cfg80211_chan_def *chandef)
  157. {
  158. int err;
  159. /*
  160. * Workaround for libertas (only!), it puts the interface
  161. * into mesh mode but doesn't implement join_mesh. Instead,
  162. * it is configured via sysfs and then joins the mesh when
  163. * you set the channel. Note that the libertas mesh isn't
  164. * compatible with 802.11 mesh.
  165. */
  166. if (rdev->ops->libertas_set_mesh_channel) {
  167. if (chandef->width != NL80211_CHAN_WIDTH_20_NOHT)
  168. return -EINVAL;
  169. if (!netif_running(wdev->netdev))
  170. return -ENETDOWN;
  171. err = cfg80211_can_use_chan(rdev, wdev, chandef->chan,
  172. CHAN_MODE_SHARED);
  173. if (err)
  174. return err;
  175. err = rdev_libertas_set_mesh_channel(rdev, wdev->netdev,
  176. chandef->chan);
  177. if (!err)
  178. wdev->channel = chandef->chan;
  179. return err;
  180. }
  181. if (wdev->mesh_id_len)
  182. return -EBUSY;
  183. wdev->preset_chandef = *chandef;
  184. return 0;
  185. }
  186. void cfg80211_notify_new_peer_candidate(struct net_device *dev,
  187. const u8 *macaddr, const u8* ie, u8 ie_len, gfp_t gfp)
  188. {
  189. struct wireless_dev *wdev = dev->ieee80211_ptr;
  190. trace_cfg80211_notify_new_peer_candidate(dev, macaddr);
  191. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
  192. return;
  193. nl80211_send_new_peer_candidate(wiphy_to_dev(wdev->wiphy), dev,
  194. macaddr, ie, ie_len, gfp);
  195. }
  196. EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
  197. static int __cfg80211_leave_mesh(struct cfg80211_registered_device *rdev,
  198. struct net_device *dev)
  199. {
  200. struct wireless_dev *wdev = dev->ieee80211_ptr;
  201. int err;
  202. ASSERT_WDEV_LOCK(wdev);
  203. if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
  204. return -EOPNOTSUPP;
  205. if (!rdev->ops->leave_mesh)
  206. return -EOPNOTSUPP;
  207. if (!wdev->mesh_id_len)
  208. return -ENOTCONN;
  209. err = rdev_leave_mesh(rdev, dev);
  210. if (!err) {
  211. wdev->mesh_id_len = 0;
  212. wdev->channel = NULL;
  213. }
  214. return err;
  215. }
  216. int cfg80211_leave_mesh(struct cfg80211_registered_device *rdev,
  217. struct net_device *dev)
  218. {
  219. struct wireless_dev *wdev = dev->ieee80211_ptr;
  220. int err;
  221. wdev_lock(wdev);
  222. err = __cfg80211_leave_mesh(rdev, dev);
  223. wdev_unlock(wdev);
  224. return err;
  225. }