mesh.c 6.7 KB

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