mesh.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 = cfg80211_can_use_chan(rdev, wdev, setup->channel,
  131. CHAN_MODE_SHARED);
  132. if (err)
  133. return err;
  134. err = rdev->ops->join_mesh(&rdev->wiphy, dev, conf, setup);
  135. if (!err) {
  136. memcpy(wdev->ssid, setup->mesh_id, setup->mesh_id_len);
  137. wdev->mesh_id_len = setup->mesh_id_len;
  138. wdev->channel = setup->channel;
  139. }
  140. return err;
  141. }
  142. int cfg80211_join_mesh(struct cfg80211_registered_device *rdev,
  143. struct net_device *dev,
  144. struct mesh_setup *setup,
  145. const struct mesh_config *conf)
  146. {
  147. struct wireless_dev *wdev = dev->ieee80211_ptr;
  148. int err;
  149. mutex_lock(&rdev->devlist_mtx);
  150. wdev_lock(wdev);
  151. err = __cfg80211_join_mesh(rdev, dev, setup, conf);
  152. wdev_unlock(wdev);
  153. mutex_unlock(&rdev->devlist_mtx);
  154. return err;
  155. }
  156. int cfg80211_set_mesh_freq(struct cfg80211_registered_device *rdev,
  157. struct wireless_dev *wdev, int freq,
  158. enum nl80211_channel_type channel_type)
  159. {
  160. struct ieee80211_channel *channel;
  161. int err;
  162. channel = rdev_freq_to_chan(rdev, freq, channel_type);
  163. if (!channel || !cfg80211_can_beacon_sec_chan(&rdev->wiphy,
  164. channel,
  165. channel_type)) {
  166. return -EINVAL;
  167. }
  168. /*
  169. * Workaround for libertas (only!), it puts the interface
  170. * into mesh mode but doesn't implement join_mesh. Instead,
  171. * it is configured via sysfs and then joins the mesh when
  172. * you set the channel. Note that the libertas mesh isn't
  173. * compatible with 802.11 mesh.
  174. */
  175. if (rdev->ops->libertas_set_mesh_channel) {
  176. if (channel_type != NL80211_CHAN_NO_HT)
  177. return -EINVAL;
  178. if (!netif_running(wdev->netdev))
  179. return -ENETDOWN;
  180. err = cfg80211_can_use_chan(rdev, wdev, channel,
  181. CHAN_MODE_SHARED);
  182. if (err)
  183. return err;
  184. err = rdev->ops->libertas_set_mesh_channel(&rdev->wiphy,
  185. wdev->netdev,
  186. channel);
  187. if (!err)
  188. wdev->channel = channel;
  189. return err;
  190. }
  191. if (wdev->mesh_id_len)
  192. return -EBUSY;
  193. wdev->preset_chan = channel;
  194. wdev->preset_chantype = channel_type;
  195. return 0;
  196. }
  197. void cfg80211_notify_new_peer_candidate(struct net_device *dev,
  198. const u8 *macaddr, const u8* ie, u8 ie_len, gfp_t gfp)
  199. {
  200. struct wireless_dev *wdev = dev->ieee80211_ptr;
  201. if (WARN_ON(wdev->iftype != NL80211_IFTYPE_MESH_POINT))
  202. return;
  203. nl80211_send_new_peer_candidate(wiphy_to_dev(wdev->wiphy), dev,
  204. macaddr, ie, ie_len, gfp);
  205. }
  206. EXPORT_SYMBOL(cfg80211_notify_new_peer_candidate);
  207. static int __cfg80211_leave_mesh(struct cfg80211_registered_device *rdev,
  208. struct net_device *dev)
  209. {
  210. struct wireless_dev *wdev = dev->ieee80211_ptr;
  211. int err;
  212. ASSERT_WDEV_LOCK(wdev);
  213. if (dev->ieee80211_ptr->iftype != NL80211_IFTYPE_MESH_POINT)
  214. return -EOPNOTSUPP;
  215. if (!rdev->ops->leave_mesh)
  216. return -EOPNOTSUPP;
  217. if (!wdev->mesh_id_len)
  218. return -ENOTCONN;
  219. err = rdev->ops->leave_mesh(&rdev->wiphy, dev);
  220. if (!err) {
  221. wdev->mesh_id_len = 0;
  222. wdev->channel = NULL;
  223. }
  224. return err;
  225. }
  226. int cfg80211_leave_mesh(struct cfg80211_registered_device *rdev,
  227. struct net_device *dev)
  228. {
  229. struct wireless_dev *wdev = dev->ieee80211_ptr;
  230. int err;
  231. wdev_lock(wdev);
  232. err = __cfg80211_leave_mesh(rdev, dev);
  233. wdev_unlock(wdev);
  234. return err;
  235. }