mesh.c 7.1 KB

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