mesh.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. /*
  2. * Copyright (c) 2008, 2009 open80211s Ltd.
  3. * Authors: Luis Carlos Cobo <luisca@cozybit.com>
  4. * Javier Cardona <javier@cozybit.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/slab.h>
  11. #include <asm/unaligned.h>
  12. #include "ieee80211_i.h"
  13. #include "mesh.h"
  14. #define TMR_RUNNING_HK 0
  15. #define TMR_RUNNING_MP 1
  16. #define TMR_RUNNING_MPR 2
  17. int mesh_allocated;
  18. static struct kmem_cache *rm_cache;
  19. bool mesh_action_is_path_sel(struct ieee80211_mgmt *mgmt)
  20. {
  21. return (mgmt->u.action.u.mesh_action.action_code ==
  22. WLAN_MESH_ACTION_HWMP_PATH_SELECTION);
  23. }
  24. void ieee80211s_init(void)
  25. {
  26. mesh_pathtbl_init();
  27. mesh_allocated = 1;
  28. rm_cache = kmem_cache_create("mesh_rmc", sizeof(struct rmc_entry),
  29. 0, 0, NULL);
  30. }
  31. void ieee80211s_stop(void)
  32. {
  33. mesh_pathtbl_unregister();
  34. kmem_cache_destroy(rm_cache);
  35. }
  36. static void ieee80211_mesh_housekeeping_timer(unsigned long data)
  37. {
  38. struct ieee80211_sub_if_data *sdata = (void *) data;
  39. struct ieee80211_local *local = sdata->local;
  40. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  41. set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
  42. if (local->quiescing) {
  43. set_bit(TMR_RUNNING_HK, &ifmsh->timers_running);
  44. return;
  45. }
  46. ieee80211_queue_work(&local->hw, &sdata->work);
  47. }
  48. /**
  49. * mesh_matches_local - check if the config of a mesh point matches ours
  50. *
  51. * @sdata: local mesh subif
  52. * @ie: information elements of a management frame from the mesh peer
  53. *
  54. * This function checks if the mesh configuration of a mesh point matches the
  55. * local mesh configuration, i.e. if both nodes belong to the same mesh network.
  56. */
  57. bool mesh_matches_local(struct ieee80211_sub_if_data *sdata,
  58. struct ieee802_11_elems *ie)
  59. {
  60. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  61. struct ieee80211_local *local = sdata->local;
  62. u32 basic_rates = 0;
  63. struct cfg80211_chan_def sta_chan_def;
  64. /*
  65. * As support for each feature is added, check for matching
  66. * - On mesh config capabilities
  67. * - Power Save Support En
  68. * - Sync support enabled
  69. * - Sync support active
  70. * - Sync support required from peer
  71. * - MDA enabled
  72. * - Power management control on fc
  73. */
  74. if (!(ifmsh->mesh_id_len == ie->mesh_id_len &&
  75. memcmp(ifmsh->mesh_id, ie->mesh_id, ie->mesh_id_len) == 0 &&
  76. (ifmsh->mesh_pp_id == ie->mesh_config->meshconf_psel) &&
  77. (ifmsh->mesh_pm_id == ie->mesh_config->meshconf_pmetric) &&
  78. (ifmsh->mesh_cc_id == ie->mesh_config->meshconf_congest) &&
  79. (ifmsh->mesh_sp_id == ie->mesh_config->meshconf_synch) &&
  80. (ifmsh->mesh_auth_id == ie->mesh_config->meshconf_auth)))
  81. goto mismatch;
  82. ieee80211_sta_get_rates(local, ie, ieee80211_get_sdata_band(sdata),
  83. &basic_rates);
  84. if (sdata->vif.bss_conf.basic_rates != basic_rates)
  85. goto mismatch;
  86. ieee80211_ht_oper_to_chandef(sdata->vif.bss_conf.chandef.chan,
  87. ie->ht_operation, &sta_chan_def);
  88. if (!cfg80211_chandef_compatible(&sdata->vif.bss_conf.chandef,
  89. &sta_chan_def))
  90. goto mismatch;
  91. return true;
  92. mismatch:
  93. return false;
  94. }
  95. /**
  96. * mesh_peer_accepts_plinks - check if an mp is willing to establish peer links
  97. *
  98. * @ie: information elements of a management frame from the mesh peer
  99. */
  100. bool mesh_peer_accepts_plinks(struct ieee802_11_elems *ie)
  101. {
  102. return (ie->mesh_config->meshconf_cap &
  103. IEEE80211_MESHCONF_CAPAB_ACCEPT_PLINKS) != 0;
  104. }
  105. /**
  106. * mesh_accept_plinks_update - update accepting_plink in local mesh beacons
  107. *
  108. * @sdata: mesh interface in which mesh beacons are going to be updated
  109. *
  110. * Returns: beacon changed flag if the beacon content changed.
  111. */
  112. u32 mesh_accept_plinks_update(struct ieee80211_sub_if_data *sdata)
  113. {
  114. bool free_plinks;
  115. u32 changed = 0;
  116. /* In case mesh_plink_free_count > 0 and mesh_plinktbl_capacity == 0,
  117. * the mesh interface might be able to establish plinks with peers that
  118. * are already on the table but are not on PLINK_ESTAB state. However,
  119. * in general the mesh interface is not accepting peer link requests
  120. * from new peers, and that must be reflected in the beacon
  121. */
  122. free_plinks = mesh_plink_availables(sdata);
  123. if (free_plinks != sdata->u.mesh.accepting_plinks) {
  124. sdata->u.mesh.accepting_plinks = free_plinks;
  125. changed = BSS_CHANGED_BEACON;
  126. }
  127. return changed;
  128. }
  129. int mesh_rmc_init(struct ieee80211_sub_if_data *sdata)
  130. {
  131. int i;
  132. sdata->u.mesh.rmc = kmalloc(sizeof(struct mesh_rmc), GFP_KERNEL);
  133. if (!sdata->u.mesh.rmc)
  134. return -ENOMEM;
  135. sdata->u.mesh.rmc->idx_mask = RMC_BUCKETS - 1;
  136. for (i = 0; i < RMC_BUCKETS; i++)
  137. INIT_LIST_HEAD(&sdata->u.mesh.rmc->bucket[i]);
  138. return 0;
  139. }
  140. void mesh_rmc_free(struct ieee80211_sub_if_data *sdata)
  141. {
  142. struct mesh_rmc *rmc = sdata->u.mesh.rmc;
  143. struct rmc_entry *p, *n;
  144. int i;
  145. if (!sdata->u.mesh.rmc)
  146. return;
  147. for (i = 0; i < RMC_BUCKETS; i++)
  148. list_for_each_entry_safe(p, n, &rmc->bucket[i], list) {
  149. list_del(&p->list);
  150. kmem_cache_free(rm_cache, p);
  151. }
  152. kfree(rmc);
  153. sdata->u.mesh.rmc = NULL;
  154. }
  155. /**
  156. * mesh_rmc_check - Check frame in recent multicast cache and add if absent.
  157. *
  158. * @sa: source address
  159. * @mesh_hdr: mesh_header
  160. *
  161. * Returns: 0 if the frame is not in the cache, nonzero otherwise.
  162. *
  163. * Checks using the source address and the mesh sequence number if we have
  164. * received this frame lately. If the frame is not in the cache, it is added to
  165. * it.
  166. */
  167. int mesh_rmc_check(u8 *sa, struct ieee80211s_hdr *mesh_hdr,
  168. struct ieee80211_sub_if_data *sdata)
  169. {
  170. struct mesh_rmc *rmc = sdata->u.mesh.rmc;
  171. u32 seqnum = 0;
  172. int entries = 0;
  173. u8 idx;
  174. struct rmc_entry *p, *n;
  175. /* Don't care about endianness since only match matters */
  176. memcpy(&seqnum, &mesh_hdr->seqnum, sizeof(mesh_hdr->seqnum));
  177. idx = le32_to_cpu(mesh_hdr->seqnum) & rmc->idx_mask;
  178. list_for_each_entry_safe(p, n, &rmc->bucket[idx], list) {
  179. ++entries;
  180. if (time_after(jiffies, p->exp_time) ||
  181. (entries == RMC_QUEUE_MAX_LEN)) {
  182. list_del(&p->list);
  183. kmem_cache_free(rm_cache, p);
  184. --entries;
  185. } else if ((seqnum == p->seqnum) &&
  186. (ether_addr_equal(sa, p->sa)))
  187. return -1;
  188. }
  189. p = kmem_cache_alloc(rm_cache, GFP_ATOMIC);
  190. if (!p)
  191. return 0;
  192. p->seqnum = seqnum;
  193. p->exp_time = jiffies + RMC_TIMEOUT;
  194. memcpy(p->sa, sa, ETH_ALEN);
  195. list_add(&p->list, &rmc->bucket[idx]);
  196. return 0;
  197. }
  198. int
  199. mesh_add_meshconf_ie(struct sk_buff *skb, struct ieee80211_sub_if_data *sdata)
  200. {
  201. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  202. u8 *pos, neighbors;
  203. u8 meshconf_len = sizeof(struct ieee80211_meshconf_ie);
  204. if (skb_tailroom(skb) < 2 + meshconf_len)
  205. return -ENOMEM;
  206. pos = skb_put(skb, 2 + meshconf_len);
  207. *pos++ = WLAN_EID_MESH_CONFIG;
  208. *pos++ = meshconf_len;
  209. /* Active path selection protocol ID */
  210. *pos++ = ifmsh->mesh_pp_id;
  211. /* Active path selection metric ID */
  212. *pos++ = ifmsh->mesh_pm_id;
  213. /* Congestion control mode identifier */
  214. *pos++ = ifmsh->mesh_cc_id;
  215. /* Synchronization protocol identifier */
  216. *pos++ = ifmsh->mesh_sp_id;
  217. /* Authentication Protocol identifier */
  218. *pos++ = ifmsh->mesh_auth_id;
  219. /* Mesh Formation Info - number of neighbors */
  220. neighbors = atomic_read(&ifmsh->estab_plinks);
  221. /* Number of neighbor mesh STAs or 15 whichever is smaller */
  222. neighbors = (neighbors > 15) ? 15 : neighbors;
  223. *pos++ = neighbors << 1;
  224. /* Mesh capability */
  225. *pos = IEEE80211_MESHCONF_CAPAB_FORWARDING;
  226. *pos |= ifmsh->accepting_plinks ?
  227. IEEE80211_MESHCONF_CAPAB_ACCEPT_PLINKS : 0x00;
  228. *pos++ |= ifmsh->adjusting_tbtt ?
  229. IEEE80211_MESHCONF_CAPAB_TBTT_ADJUSTING : 0x00;
  230. *pos++ = 0x00;
  231. return 0;
  232. }
  233. int
  234. mesh_add_meshid_ie(struct sk_buff *skb, struct ieee80211_sub_if_data *sdata)
  235. {
  236. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  237. u8 *pos;
  238. if (skb_tailroom(skb) < 2 + ifmsh->mesh_id_len)
  239. return -ENOMEM;
  240. pos = skb_put(skb, 2 + ifmsh->mesh_id_len);
  241. *pos++ = WLAN_EID_MESH_ID;
  242. *pos++ = ifmsh->mesh_id_len;
  243. if (ifmsh->mesh_id_len)
  244. memcpy(pos, ifmsh->mesh_id, ifmsh->mesh_id_len);
  245. return 0;
  246. }
  247. int
  248. mesh_add_vendor_ies(struct sk_buff *skb, struct ieee80211_sub_if_data *sdata)
  249. {
  250. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  251. u8 offset, len;
  252. const u8 *data;
  253. if (!ifmsh->ie || !ifmsh->ie_len)
  254. return 0;
  255. /* fast-forward to vendor IEs */
  256. offset = ieee80211_ie_split_vendor(ifmsh->ie, ifmsh->ie_len, 0);
  257. if (offset) {
  258. len = ifmsh->ie_len - offset;
  259. data = ifmsh->ie + offset;
  260. if (skb_tailroom(skb) < len)
  261. return -ENOMEM;
  262. memcpy(skb_put(skb, len), data, len);
  263. }
  264. return 0;
  265. }
  266. int
  267. mesh_add_rsn_ie(struct sk_buff *skb, struct ieee80211_sub_if_data *sdata)
  268. {
  269. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  270. u8 len = 0;
  271. const u8 *data;
  272. if (!ifmsh->ie || !ifmsh->ie_len)
  273. return 0;
  274. /* find RSN IE */
  275. data = ifmsh->ie;
  276. while (data < ifmsh->ie + ifmsh->ie_len) {
  277. if (*data == WLAN_EID_RSN) {
  278. len = data[1] + 2;
  279. break;
  280. }
  281. data++;
  282. }
  283. if (len) {
  284. if (skb_tailroom(skb) < len)
  285. return -ENOMEM;
  286. memcpy(skb_put(skb, len), data, len);
  287. }
  288. return 0;
  289. }
  290. int mesh_add_ds_params_ie(struct sk_buff *skb,
  291. struct ieee80211_sub_if_data *sdata)
  292. {
  293. struct ieee80211_local *local = sdata->local;
  294. struct ieee80211_supported_band *sband;
  295. struct ieee80211_chanctx_conf *chanctx_conf;
  296. struct ieee80211_channel *chan;
  297. u8 *pos;
  298. if (skb_tailroom(skb) < 3)
  299. return -ENOMEM;
  300. rcu_read_lock();
  301. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  302. if (WARN_ON(!chanctx_conf)) {
  303. rcu_read_unlock();
  304. return -EINVAL;
  305. }
  306. chan = chanctx_conf->def.chan;
  307. rcu_read_unlock();
  308. sband = local->hw.wiphy->bands[chan->band];
  309. if (sband->band == IEEE80211_BAND_2GHZ) {
  310. pos = skb_put(skb, 2 + 1);
  311. *pos++ = WLAN_EID_DS_PARAMS;
  312. *pos++ = 1;
  313. *pos++ = ieee80211_frequency_to_channel(chan->center_freq);
  314. }
  315. return 0;
  316. }
  317. int mesh_add_ht_cap_ie(struct sk_buff *skb,
  318. struct ieee80211_sub_if_data *sdata)
  319. {
  320. struct ieee80211_local *local = sdata->local;
  321. enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
  322. struct ieee80211_supported_band *sband;
  323. u8 *pos;
  324. sband = local->hw.wiphy->bands[band];
  325. if (!sband->ht_cap.ht_supported ||
  326. sdata->vif.bss_conf.chandef.width == NL80211_CHAN_WIDTH_20_NOHT)
  327. return 0;
  328. if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_ht_cap))
  329. return -ENOMEM;
  330. pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_cap));
  331. ieee80211_ie_build_ht_cap(pos, &sband->ht_cap, sband->ht_cap.cap);
  332. return 0;
  333. }
  334. int mesh_add_ht_oper_ie(struct sk_buff *skb,
  335. struct ieee80211_sub_if_data *sdata)
  336. {
  337. struct ieee80211_local *local = sdata->local;
  338. struct ieee80211_chanctx_conf *chanctx_conf;
  339. struct ieee80211_channel *channel;
  340. enum nl80211_channel_type channel_type =
  341. cfg80211_get_chandef_type(&sdata->vif.bss_conf.chandef);
  342. struct ieee80211_supported_band *sband;
  343. struct ieee80211_sta_ht_cap *ht_cap;
  344. u8 *pos;
  345. rcu_read_lock();
  346. chanctx_conf = rcu_dereference(sdata->vif.chanctx_conf);
  347. if (WARN_ON(!chanctx_conf)) {
  348. rcu_read_unlock();
  349. return -EINVAL;
  350. }
  351. channel = chanctx_conf->def.chan;
  352. rcu_read_unlock();
  353. sband = local->hw.wiphy->bands[channel->band];
  354. ht_cap = &sband->ht_cap;
  355. if (!ht_cap->ht_supported || channel_type == NL80211_CHAN_NO_HT)
  356. return 0;
  357. if (skb_tailroom(skb) < 2 + sizeof(struct ieee80211_ht_operation))
  358. return -ENOMEM;
  359. pos = skb_put(skb, 2 + sizeof(struct ieee80211_ht_operation));
  360. ieee80211_ie_build_ht_oper(pos, ht_cap, &sdata->vif.bss_conf.chandef,
  361. sdata->vif.bss_conf.ht_operation_mode);
  362. return 0;
  363. }
  364. static void ieee80211_mesh_path_timer(unsigned long data)
  365. {
  366. struct ieee80211_sub_if_data *sdata =
  367. (struct ieee80211_sub_if_data *) data;
  368. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  369. struct ieee80211_local *local = sdata->local;
  370. if (local->quiescing) {
  371. set_bit(TMR_RUNNING_MP, &ifmsh->timers_running);
  372. return;
  373. }
  374. ieee80211_queue_work(&local->hw, &sdata->work);
  375. }
  376. static void ieee80211_mesh_path_root_timer(unsigned long data)
  377. {
  378. struct ieee80211_sub_if_data *sdata =
  379. (struct ieee80211_sub_if_data *) data;
  380. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  381. struct ieee80211_local *local = sdata->local;
  382. set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
  383. if (local->quiescing) {
  384. set_bit(TMR_RUNNING_MPR, &ifmsh->timers_running);
  385. return;
  386. }
  387. ieee80211_queue_work(&local->hw, &sdata->work);
  388. }
  389. void ieee80211_mesh_root_setup(struct ieee80211_if_mesh *ifmsh)
  390. {
  391. if (ifmsh->mshcfg.dot11MeshHWMPRootMode > IEEE80211_ROOTMODE_ROOT)
  392. set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
  393. else {
  394. clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
  395. /* stop running timer */
  396. del_timer_sync(&ifmsh->mesh_path_root_timer);
  397. }
  398. }
  399. /**
  400. * ieee80211_fill_mesh_addresses - fill addresses of a locally originated mesh frame
  401. * @hdr: 802.11 frame header
  402. * @fc: frame control field
  403. * @meshda: destination address in the mesh
  404. * @meshsa: source address address in the mesh. Same as TA, as frame is
  405. * locally originated.
  406. *
  407. * Return the length of the 802.11 (does not include a mesh control header)
  408. */
  409. int ieee80211_fill_mesh_addresses(struct ieee80211_hdr *hdr, __le16 *fc,
  410. const u8 *meshda, const u8 *meshsa)
  411. {
  412. if (is_multicast_ether_addr(meshda)) {
  413. *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
  414. /* DA TA SA */
  415. memcpy(hdr->addr1, meshda, ETH_ALEN);
  416. memcpy(hdr->addr2, meshsa, ETH_ALEN);
  417. memcpy(hdr->addr3, meshsa, ETH_ALEN);
  418. return 24;
  419. } else {
  420. *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS);
  421. /* RA TA DA SA */
  422. memset(hdr->addr1, 0, ETH_ALEN); /* RA is resolved later */
  423. memcpy(hdr->addr2, meshsa, ETH_ALEN);
  424. memcpy(hdr->addr3, meshda, ETH_ALEN);
  425. memcpy(hdr->addr4, meshsa, ETH_ALEN);
  426. return 30;
  427. }
  428. }
  429. /**
  430. * ieee80211_new_mesh_header - create a new mesh header
  431. * @meshhdr: uninitialized mesh header
  432. * @sdata: mesh interface to be used
  433. * @addr4or5: 1st address in the ae header, which may correspond to address 4
  434. * (if addr6 is NULL) or address 5 (if addr6 is present). It may
  435. * be NULL.
  436. * @addr6: 2nd address in the ae header, which corresponds to addr6 of the
  437. * mesh frame
  438. *
  439. * Return the header length.
  440. */
  441. int ieee80211_new_mesh_header(struct ieee80211s_hdr *meshhdr,
  442. struct ieee80211_sub_if_data *sdata, char *addr4or5,
  443. char *addr6)
  444. {
  445. int aelen = 0;
  446. BUG_ON(!addr4or5 && addr6);
  447. memset(meshhdr, 0, sizeof(*meshhdr));
  448. meshhdr->ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
  449. put_unaligned(cpu_to_le32(sdata->u.mesh.mesh_seqnum), &meshhdr->seqnum);
  450. sdata->u.mesh.mesh_seqnum++;
  451. if (addr4or5 && !addr6) {
  452. meshhdr->flags |= MESH_FLAGS_AE_A4;
  453. aelen += ETH_ALEN;
  454. memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
  455. } else if (addr4or5 && addr6) {
  456. meshhdr->flags |= MESH_FLAGS_AE_A5_A6;
  457. aelen += 2 * ETH_ALEN;
  458. memcpy(meshhdr->eaddr1, addr4or5, ETH_ALEN);
  459. memcpy(meshhdr->eaddr2, addr6, ETH_ALEN);
  460. }
  461. return 6 + aelen;
  462. }
  463. static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata,
  464. struct ieee80211_if_mesh *ifmsh)
  465. {
  466. u32 changed;
  467. ieee80211_sta_expire(sdata, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
  468. mesh_path_expire(sdata);
  469. changed = mesh_accept_plinks_update(sdata);
  470. ieee80211_bss_info_change_notify(sdata, changed);
  471. mod_timer(&ifmsh->housekeeping_timer,
  472. round_jiffies(jiffies + IEEE80211_MESH_HOUSEKEEPING_INTERVAL));
  473. }
  474. static void ieee80211_mesh_rootpath(struct ieee80211_sub_if_data *sdata)
  475. {
  476. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  477. u32 interval;
  478. mesh_path_tx_root_frame(sdata);
  479. if (ifmsh->mshcfg.dot11MeshHWMPRootMode == IEEE80211_PROACTIVE_RANN)
  480. interval = ifmsh->mshcfg.dot11MeshHWMPRannInterval;
  481. else
  482. interval = ifmsh->mshcfg.dot11MeshHWMProotInterval;
  483. mod_timer(&ifmsh->mesh_path_root_timer,
  484. round_jiffies(TU_TO_EXP_TIME(interval)));
  485. }
  486. #ifdef CONFIG_PM
  487. void ieee80211_mesh_quiesce(struct ieee80211_sub_if_data *sdata)
  488. {
  489. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  490. /* use atomic bitops in case all timers fire at the same time */
  491. if (del_timer_sync(&ifmsh->housekeeping_timer))
  492. set_bit(TMR_RUNNING_HK, &ifmsh->timers_running);
  493. if (del_timer_sync(&ifmsh->mesh_path_timer))
  494. set_bit(TMR_RUNNING_MP, &ifmsh->timers_running);
  495. if (del_timer_sync(&ifmsh->mesh_path_root_timer))
  496. set_bit(TMR_RUNNING_MPR, &ifmsh->timers_running);
  497. }
  498. void ieee80211_mesh_restart(struct ieee80211_sub_if_data *sdata)
  499. {
  500. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  501. if (test_and_clear_bit(TMR_RUNNING_HK, &ifmsh->timers_running))
  502. add_timer(&ifmsh->housekeeping_timer);
  503. if (test_and_clear_bit(TMR_RUNNING_MP, &ifmsh->timers_running))
  504. add_timer(&ifmsh->mesh_path_timer);
  505. if (test_and_clear_bit(TMR_RUNNING_MPR, &ifmsh->timers_running))
  506. add_timer(&ifmsh->mesh_path_root_timer);
  507. ieee80211_mesh_root_setup(ifmsh);
  508. }
  509. #endif
  510. void ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
  511. {
  512. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  513. struct ieee80211_local *local = sdata->local;
  514. u32 changed = BSS_CHANGED_BEACON |
  515. BSS_CHANGED_BEACON_ENABLED |
  516. BSS_CHANGED_HT |
  517. BSS_CHANGED_BASIC_RATES |
  518. BSS_CHANGED_BEACON_INT;
  519. enum ieee80211_band band = ieee80211_get_sdata_band(sdata);
  520. local->fif_other_bss++;
  521. /* mesh ifaces must set allmulti to forward mcast traffic */
  522. atomic_inc(&local->iff_allmultis);
  523. ieee80211_configure_filter(local);
  524. ifmsh->mesh_cc_id = 0; /* Disabled */
  525. ifmsh->mesh_auth_id = 0; /* Disabled */
  526. /* register sync ops from extensible synchronization framework */
  527. ifmsh->sync_ops = ieee80211_mesh_sync_ops_get(ifmsh->mesh_sp_id);
  528. ifmsh->adjusting_tbtt = false;
  529. ifmsh->sync_offset_clockdrift_max = 0;
  530. set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
  531. ieee80211_mesh_root_setup(ifmsh);
  532. ieee80211_queue_work(&local->hw, &sdata->work);
  533. sdata->vif.bss_conf.ht_operation_mode =
  534. ifmsh->mshcfg.ht_opmode;
  535. sdata->vif.bss_conf.enable_beacon = true;
  536. sdata->vif.bss_conf.basic_rates =
  537. ieee80211_mandatory_rates(local, band);
  538. if (band == IEEE80211_BAND_5GHZ) {
  539. sdata->vif.bss_conf.use_short_slot = true;
  540. changed |= BSS_CHANGED_ERP_SLOT;
  541. }
  542. ieee80211_bss_info_change_notify(sdata, changed);
  543. netif_carrier_on(sdata->dev);
  544. }
  545. void ieee80211_stop_mesh(struct ieee80211_sub_if_data *sdata)
  546. {
  547. struct ieee80211_local *local = sdata->local;
  548. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  549. netif_carrier_off(sdata->dev);
  550. /* stop the beacon */
  551. ifmsh->mesh_id_len = 0;
  552. sdata->vif.bss_conf.enable_beacon = false;
  553. clear_bit(SDATA_STATE_OFFCHANNEL_BEACON_STOPPED, &sdata->state);
  554. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON_ENABLED);
  555. /* flush STAs and mpaths on this iface */
  556. sta_info_flush(sdata);
  557. mesh_path_flush_by_iface(sdata);
  558. del_timer_sync(&sdata->u.mesh.housekeeping_timer);
  559. del_timer_sync(&sdata->u.mesh.mesh_path_root_timer);
  560. del_timer_sync(&sdata->u.mesh.mesh_path_timer);
  561. /*
  562. * If the timer fired while we waited for it, it will have
  563. * requeued the work. Now the work will be running again
  564. * but will not rearm the timer again because it checks
  565. * whether the interface is running, which, at this point,
  566. * it no longer is.
  567. */
  568. cancel_work_sync(&sdata->work);
  569. local->fif_other_bss--;
  570. atomic_dec(&local->iff_allmultis);
  571. ieee80211_configure_filter(local);
  572. sdata->u.mesh.timers_running = 0;
  573. }
  574. static void ieee80211_mesh_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
  575. u16 stype,
  576. struct ieee80211_mgmt *mgmt,
  577. size_t len,
  578. struct ieee80211_rx_status *rx_status)
  579. {
  580. struct ieee80211_local *local = sdata->local;
  581. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  582. struct ieee802_11_elems elems;
  583. struct ieee80211_channel *channel;
  584. size_t baselen;
  585. int freq;
  586. enum ieee80211_band band = rx_status->band;
  587. /* ignore ProbeResp to foreign address */
  588. if (stype == IEEE80211_STYPE_PROBE_RESP &&
  589. !ether_addr_equal(mgmt->da, sdata->vif.addr))
  590. return;
  591. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  592. if (baselen > len)
  593. return;
  594. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  595. &elems);
  596. /* ignore non-mesh or secure / unsecure mismatch */
  597. if ((!elems.mesh_id || !elems.mesh_config) ||
  598. (elems.rsn && sdata->u.mesh.security == IEEE80211_MESH_SEC_NONE) ||
  599. (!elems.rsn && sdata->u.mesh.security != IEEE80211_MESH_SEC_NONE))
  600. return;
  601. if (elems.ds_params && elems.ds_params_len == 1)
  602. freq = ieee80211_channel_to_frequency(elems.ds_params[0], band);
  603. else
  604. freq = rx_status->freq;
  605. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  606. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  607. return;
  608. if (mesh_matches_local(sdata, &elems))
  609. mesh_neighbour_update(sdata, mgmt->sa, &elems);
  610. if (ifmsh->sync_ops)
  611. ifmsh->sync_ops->rx_bcn_presp(sdata,
  612. stype, mgmt, &elems, rx_status);
  613. }
  614. static void ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
  615. struct ieee80211_mgmt *mgmt,
  616. size_t len,
  617. struct ieee80211_rx_status *rx_status)
  618. {
  619. switch (mgmt->u.action.category) {
  620. case WLAN_CATEGORY_SELF_PROTECTED:
  621. switch (mgmt->u.action.u.self_prot.action_code) {
  622. case WLAN_SP_MESH_PEERING_OPEN:
  623. case WLAN_SP_MESH_PEERING_CLOSE:
  624. case WLAN_SP_MESH_PEERING_CONFIRM:
  625. mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
  626. break;
  627. }
  628. break;
  629. case WLAN_CATEGORY_MESH_ACTION:
  630. if (mesh_action_is_path_sel(mgmt))
  631. mesh_rx_path_sel_frame(sdata, mgmt, len);
  632. break;
  633. }
  634. }
  635. void ieee80211_mesh_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  636. struct sk_buff *skb)
  637. {
  638. struct ieee80211_rx_status *rx_status;
  639. struct ieee80211_mgmt *mgmt;
  640. u16 stype;
  641. rx_status = IEEE80211_SKB_RXCB(skb);
  642. mgmt = (struct ieee80211_mgmt *) skb->data;
  643. stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
  644. switch (stype) {
  645. case IEEE80211_STYPE_PROBE_RESP:
  646. case IEEE80211_STYPE_BEACON:
  647. ieee80211_mesh_rx_bcn_presp(sdata, stype, mgmt, skb->len,
  648. rx_status);
  649. break;
  650. case IEEE80211_STYPE_ACTION:
  651. ieee80211_mesh_rx_mgmt_action(sdata, mgmt, skb->len, rx_status);
  652. break;
  653. }
  654. }
  655. void ieee80211_mesh_work(struct ieee80211_sub_if_data *sdata)
  656. {
  657. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  658. if (ifmsh->preq_queue_len &&
  659. time_after(jiffies,
  660. ifmsh->last_preq + msecs_to_jiffies(ifmsh->mshcfg.dot11MeshHWMPpreqMinInterval)))
  661. mesh_path_start_discovery(sdata);
  662. if (test_and_clear_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags))
  663. mesh_mpath_table_grow();
  664. if (test_and_clear_bit(MESH_WORK_GROW_MPP_TABLE, &ifmsh->wrkq_flags))
  665. mesh_mpp_table_grow();
  666. if (test_and_clear_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags))
  667. ieee80211_mesh_housekeeping(sdata, ifmsh);
  668. if (test_and_clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags))
  669. ieee80211_mesh_rootpath(sdata);
  670. if (test_and_clear_bit(MESH_WORK_DRIFT_ADJUST, &ifmsh->wrkq_flags))
  671. mesh_sync_adjust_tbtt(sdata);
  672. }
  673. void ieee80211_mesh_notify_scan_completed(struct ieee80211_local *local)
  674. {
  675. struct ieee80211_sub_if_data *sdata;
  676. rcu_read_lock();
  677. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  678. if (ieee80211_vif_is_mesh(&sdata->vif))
  679. ieee80211_queue_work(&local->hw, &sdata->work);
  680. rcu_read_unlock();
  681. }
  682. void ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data *sdata)
  683. {
  684. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  685. static u8 zero_addr[ETH_ALEN] = {};
  686. setup_timer(&ifmsh->housekeeping_timer,
  687. ieee80211_mesh_housekeeping_timer,
  688. (unsigned long) sdata);
  689. ifmsh->accepting_plinks = true;
  690. ifmsh->preq_id = 0;
  691. ifmsh->sn = 0;
  692. ifmsh->num_gates = 0;
  693. atomic_set(&ifmsh->mpaths, 0);
  694. mesh_rmc_init(sdata);
  695. ifmsh->last_preq = jiffies;
  696. ifmsh->next_perr = jiffies;
  697. /* Allocate all mesh structures when creating the first mesh interface. */
  698. if (!mesh_allocated)
  699. ieee80211s_init();
  700. setup_timer(&ifmsh->mesh_path_timer,
  701. ieee80211_mesh_path_timer,
  702. (unsigned long) sdata);
  703. setup_timer(&ifmsh->mesh_path_root_timer,
  704. ieee80211_mesh_path_root_timer,
  705. (unsigned long) sdata);
  706. INIT_LIST_HEAD(&ifmsh->preq_queue.list);
  707. spin_lock_init(&ifmsh->mesh_preq_queue_lock);
  708. spin_lock_init(&ifmsh->sync_offset_lock);
  709. sdata->vif.bss_conf.bssid = zero_addr;
  710. }