mesh.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. * Copyright (c) 2008 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 <asm/unaligned.h>
  11. #include "ieee80211_i.h"
  12. #include "mesh.h"
  13. #define IEEE80211_MESH_PEER_INACTIVITY_LIMIT (1800 * HZ)
  14. #define IEEE80211_MESH_HOUSEKEEPING_INTERVAL (60 * HZ)
  15. #define IEEE80211_MESH_RANN_INTERVAL (1 * HZ)
  16. #define MESHCONF_PP_OFFSET 0 /* Path Selection Protocol */
  17. #define MESHCONF_PM_OFFSET 1 /* Path Selection Metric */
  18. #define MESHCONF_CC_OFFSET 2 /* Congestion Control Mode */
  19. #define MESHCONF_SP_OFFSET 3 /* Synchronization Protocol */
  20. #define MESHCONF_AUTH_OFFSET 4 /* Authentication Protocol */
  21. #define MESHCONF_CAPAB_OFFSET 6
  22. #define MESHCONF_CAPAB_ACCEPT_PLINKS 0x01
  23. #define MESHCONF_CAPAB_FORWARDING 0x08
  24. #define TMR_RUNNING_HK 0
  25. #define TMR_RUNNING_MP 1
  26. #define TMR_RUNNING_MPR 2
  27. int mesh_allocated;
  28. static struct kmem_cache *rm_cache;
  29. void ieee80211s_init(void)
  30. {
  31. mesh_pathtbl_init();
  32. mesh_allocated = 1;
  33. rm_cache = kmem_cache_create("mesh_rmc", sizeof(struct rmc_entry),
  34. 0, 0, NULL);
  35. }
  36. void ieee80211s_stop(void)
  37. {
  38. mesh_pathtbl_unregister();
  39. kmem_cache_destroy(rm_cache);
  40. }
  41. static void ieee80211_mesh_housekeeping_timer(unsigned long data)
  42. {
  43. struct ieee80211_sub_if_data *sdata = (void *) data;
  44. struct ieee80211_local *local = sdata->local;
  45. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  46. set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
  47. if (local->quiescing) {
  48. set_bit(TMR_RUNNING_HK, &ifmsh->timers_running);
  49. return;
  50. }
  51. ieee80211_queue_work(&local->hw, &ifmsh->work);
  52. }
  53. /**
  54. * mesh_matches_local - check if the config of a mesh point matches ours
  55. *
  56. * @ie: information elements of a management frame from the mesh peer
  57. * @sdata: local mesh subif
  58. *
  59. * This function checks if the mesh configuration of a mesh point matches the
  60. * local mesh configuration, i.e. if both nodes belong to the same mesh network.
  61. */
  62. bool mesh_matches_local(struct ieee802_11_elems *ie, struct ieee80211_sub_if_data *sdata)
  63. {
  64. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  65. /*
  66. * As support for each feature is added, check for matching
  67. * - On mesh config capabilities
  68. * - Power Save Support En
  69. * - Sync support enabled
  70. * - Sync support active
  71. * - Sync support required from peer
  72. * - MDA enabled
  73. * - Power management control on fc
  74. */
  75. if (ifmsh->mesh_id_len == ie->mesh_id_len &&
  76. memcmp(ifmsh->mesh_id, ie->mesh_id, ie->mesh_id_len) == 0 &&
  77. (ifmsh->mesh_pp_id == *(ie->mesh_config + MESHCONF_PP_OFFSET))&&
  78. (ifmsh->mesh_pm_id == *(ie->mesh_config + MESHCONF_PM_OFFSET))&&
  79. (ifmsh->mesh_cc_id == *(ie->mesh_config + MESHCONF_CC_OFFSET))&&
  80. (ifmsh->mesh_sp_id == *(ie->mesh_config + MESHCONF_SP_OFFSET))&&
  81. (ifmsh->mesh_auth_id == *(ie->mesh_config +
  82. MESHCONF_AUTH_OFFSET)))
  83. return true;
  84. return false;
  85. }
  86. /**
  87. * mesh_peer_accepts_plinks - check if an mp is willing to establish peer links
  88. *
  89. * @ie: information elements of a management frame from the mesh peer
  90. */
  91. bool mesh_peer_accepts_plinks(struct ieee802_11_elems *ie)
  92. {
  93. return (*(ie->mesh_config + MESHCONF_CAPAB_OFFSET) &
  94. MESHCONF_CAPAB_ACCEPT_PLINKS) != 0;
  95. }
  96. /**
  97. * mesh_accept_plinks_update: update accepting_plink in local mesh beacons
  98. *
  99. * @sdata: mesh interface in which mesh beacons are going to be updated
  100. */
  101. void mesh_accept_plinks_update(struct ieee80211_sub_if_data *sdata)
  102. {
  103. bool free_plinks;
  104. /* In case mesh_plink_free_count > 0 and mesh_plinktbl_capacity == 0,
  105. * the mesh interface might be able to establish plinks with peers that
  106. * are already on the table but are not on PLINK_ESTAB state. However,
  107. * in general the mesh interface is not accepting peer link requests
  108. * from new peers, and that must be reflected in the beacon
  109. */
  110. free_plinks = mesh_plink_availables(sdata);
  111. if (free_plinks != sdata->u.mesh.accepting_plinks)
  112. ieee80211_mesh_housekeeping_timer((unsigned long) sdata);
  113. }
  114. void mesh_ids_set_default(struct ieee80211_if_mesh *sta)
  115. {
  116. sta->mesh_pp_id = 0; /* HWMP */
  117. sta->mesh_pm_id = 0; /* Airtime */
  118. sta->mesh_cc_id = 0; /* Disabled */
  119. sta->mesh_sp_id = 0; /* Neighbor Offset */
  120. sta->mesh_auth_id = 0; /* Disabled */
  121. }
  122. int mesh_rmc_init(struct ieee80211_sub_if_data *sdata)
  123. {
  124. int i;
  125. sdata->u.mesh.rmc = kmalloc(sizeof(struct mesh_rmc), GFP_KERNEL);
  126. if (!sdata->u.mesh.rmc)
  127. return -ENOMEM;
  128. sdata->u.mesh.rmc->idx_mask = RMC_BUCKETS - 1;
  129. for (i = 0; i < RMC_BUCKETS; i++)
  130. INIT_LIST_HEAD(&sdata->u.mesh.rmc->bucket[i].list);
  131. return 0;
  132. }
  133. void mesh_rmc_free(struct ieee80211_sub_if_data *sdata)
  134. {
  135. struct mesh_rmc *rmc = sdata->u.mesh.rmc;
  136. struct rmc_entry *p, *n;
  137. int i;
  138. if (!sdata->u.mesh.rmc)
  139. return;
  140. for (i = 0; i < RMC_BUCKETS; i++)
  141. list_for_each_entry_safe(p, n, &rmc->bucket[i].list, list) {
  142. list_del(&p->list);
  143. kmem_cache_free(rm_cache, p);
  144. }
  145. kfree(rmc);
  146. sdata->u.mesh.rmc = NULL;
  147. }
  148. /**
  149. * mesh_rmc_check - Check frame in recent multicast cache and add if absent.
  150. *
  151. * @sa: source address
  152. * @mesh_hdr: mesh_header
  153. *
  154. * Returns: 0 if the frame is not in the cache, nonzero otherwise.
  155. *
  156. * Checks using the source address and the mesh sequence number if we have
  157. * received this frame lately. If the frame is not in the cache, it is added to
  158. * it.
  159. */
  160. int mesh_rmc_check(u8 *sa, struct ieee80211s_hdr *mesh_hdr,
  161. struct ieee80211_sub_if_data *sdata)
  162. {
  163. struct mesh_rmc *rmc = sdata->u.mesh.rmc;
  164. u32 seqnum = 0;
  165. int entries = 0;
  166. u8 idx;
  167. struct rmc_entry *p, *n;
  168. /* Don't care about endianness since only match matters */
  169. memcpy(&seqnum, &mesh_hdr->seqnum, sizeof(mesh_hdr->seqnum));
  170. idx = le32_to_cpu(mesh_hdr->seqnum) & rmc->idx_mask;
  171. list_for_each_entry_safe(p, n, &rmc->bucket[idx].list, list) {
  172. ++entries;
  173. if (time_after(jiffies, p->exp_time) ||
  174. (entries == RMC_QUEUE_MAX_LEN)) {
  175. list_del(&p->list);
  176. kmem_cache_free(rm_cache, p);
  177. --entries;
  178. } else if ((seqnum == p->seqnum)
  179. && (memcmp(sa, p->sa, ETH_ALEN) == 0))
  180. return -1;
  181. }
  182. p = kmem_cache_alloc(rm_cache, GFP_ATOMIC);
  183. if (!p) {
  184. printk(KERN_DEBUG "o11s: could not allocate RMC entry\n");
  185. return 0;
  186. }
  187. p->seqnum = seqnum;
  188. p->exp_time = jiffies + RMC_TIMEOUT;
  189. memcpy(p->sa, sa, ETH_ALEN);
  190. list_add(&p->list, &rmc->bucket[idx].list);
  191. return 0;
  192. }
  193. void mesh_mgmt_ies_add(struct sk_buff *skb, struct ieee80211_sub_if_data *sdata)
  194. {
  195. struct ieee80211_local *local = sdata->local;
  196. struct ieee80211_supported_band *sband;
  197. u8 *pos;
  198. int len, i, rate;
  199. u8 neighbors;
  200. sband = local->hw.wiphy->bands[local->hw.conf.channel->band];
  201. len = sband->n_bitrates;
  202. if (len > 8)
  203. len = 8;
  204. pos = skb_put(skb, len + 2);
  205. *pos++ = WLAN_EID_SUPP_RATES;
  206. *pos++ = len;
  207. for (i = 0; i < len; i++) {
  208. rate = sband->bitrates[i].bitrate;
  209. *pos++ = (u8) (rate / 5);
  210. }
  211. if (sband->n_bitrates > len) {
  212. pos = skb_put(skb, sband->n_bitrates - len + 2);
  213. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  214. *pos++ = sband->n_bitrates - len;
  215. for (i = len; i < sband->n_bitrates; i++) {
  216. rate = sband->bitrates[i].bitrate;
  217. *pos++ = (u8) (rate / 5);
  218. }
  219. }
  220. if (sband->band == IEEE80211_BAND_2GHZ) {
  221. pos = skb_put(skb, 2 + 1);
  222. *pos++ = WLAN_EID_DS_PARAMS;
  223. *pos++ = 1;
  224. *pos++ = ieee80211_frequency_to_channel(local->hw.conf.channel->center_freq);
  225. }
  226. pos = skb_put(skb, 2 + sdata->u.mesh.mesh_id_len);
  227. *pos++ = WLAN_EID_MESH_ID;
  228. *pos++ = sdata->u.mesh.mesh_id_len;
  229. if (sdata->u.mesh.mesh_id_len)
  230. memcpy(pos, sdata->u.mesh.mesh_id, sdata->u.mesh.mesh_id_len);
  231. pos = skb_put(skb, 2 + IEEE80211_MESH_CONFIG_LEN);
  232. *pos++ = WLAN_EID_MESH_CONFIG;
  233. *pos++ = IEEE80211_MESH_CONFIG_LEN;
  234. /* Active path selection protocol ID */
  235. *pos++ = sdata->u.mesh.mesh_pp_id;
  236. /* Active path selection metric ID */
  237. *pos++ = sdata->u.mesh.mesh_pm_id;
  238. /* Congestion control mode identifier */
  239. *pos++ = sdata->u.mesh.mesh_cc_id;
  240. /* Synchronization protocol identifier */
  241. *pos++ = sdata->u.mesh.mesh_sp_id;
  242. /* Authentication Protocol identifier */
  243. *pos++ = sdata->u.mesh.mesh_auth_id;
  244. /* Mesh Formation Info - number of neighbors */
  245. neighbors = atomic_read(&sdata->u.mesh.mshstats.estab_plinks);
  246. /* Number of neighbor mesh STAs or 15 whichever is smaller */
  247. neighbors = (neighbors > 15) ? 15 : neighbors;
  248. *pos++ = neighbors << 1;
  249. /* Mesh capability */
  250. sdata->u.mesh.accepting_plinks = mesh_plink_availables(sdata);
  251. *pos = MESHCONF_CAPAB_FORWARDING;
  252. *pos++ |= sdata->u.mesh.accepting_plinks ?
  253. MESHCONF_CAPAB_ACCEPT_PLINKS : 0x00;
  254. *pos++ = 0x00;
  255. return;
  256. }
  257. u32 mesh_table_hash(u8 *addr, struct ieee80211_sub_if_data *sdata, struct mesh_table *tbl)
  258. {
  259. /* Use last four bytes of hw addr and interface index as hash index */
  260. return jhash_2words(*(u32 *)(addr+2), sdata->dev->ifindex, tbl->hash_rnd)
  261. & tbl->hash_mask;
  262. }
  263. struct mesh_table *mesh_table_alloc(int size_order)
  264. {
  265. int i;
  266. struct mesh_table *newtbl;
  267. newtbl = kmalloc(sizeof(struct mesh_table), GFP_KERNEL);
  268. if (!newtbl)
  269. return NULL;
  270. newtbl->hash_buckets = kzalloc(sizeof(struct hlist_head) *
  271. (1 << size_order), GFP_KERNEL);
  272. if (!newtbl->hash_buckets) {
  273. kfree(newtbl);
  274. return NULL;
  275. }
  276. newtbl->hashwlock = kmalloc(sizeof(spinlock_t) *
  277. (1 << size_order), GFP_KERNEL);
  278. if (!newtbl->hashwlock) {
  279. kfree(newtbl->hash_buckets);
  280. kfree(newtbl);
  281. return NULL;
  282. }
  283. newtbl->size_order = size_order;
  284. newtbl->hash_mask = (1 << size_order) - 1;
  285. atomic_set(&newtbl->entries, 0);
  286. get_random_bytes(&newtbl->hash_rnd,
  287. sizeof(newtbl->hash_rnd));
  288. for (i = 0; i <= newtbl->hash_mask; i++)
  289. spin_lock_init(&newtbl->hashwlock[i]);
  290. return newtbl;
  291. }
  292. static void ieee80211_mesh_path_timer(unsigned long data)
  293. {
  294. struct ieee80211_sub_if_data *sdata =
  295. (struct ieee80211_sub_if_data *) data;
  296. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  297. struct ieee80211_local *local = sdata->local;
  298. if (local->quiescing) {
  299. set_bit(TMR_RUNNING_MP, &ifmsh->timers_running);
  300. return;
  301. }
  302. ieee80211_queue_work(&local->hw, &ifmsh->work);
  303. }
  304. static void ieee80211_mesh_path_root_timer(unsigned long data)
  305. {
  306. struct ieee80211_sub_if_data *sdata =
  307. (struct ieee80211_sub_if_data *) data;
  308. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  309. struct ieee80211_local *local = sdata->local;
  310. set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
  311. if (local->quiescing) {
  312. set_bit(TMR_RUNNING_MPR, &ifmsh->timers_running);
  313. return;
  314. }
  315. ieee80211_queue_work(&local->hw, &ifmsh->work);
  316. }
  317. void ieee80211_mesh_root_setup(struct ieee80211_if_mesh *ifmsh)
  318. {
  319. if (ifmsh->mshcfg.dot11MeshHWMPRootMode)
  320. set_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
  321. else {
  322. clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags);
  323. /* stop running timer */
  324. del_timer_sync(&ifmsh->mesh_path_root_timer);
  325. }
  326. }
  327. /**
  328. * ieee80211_fill_mesh_addresses - fill addresses of a locally originated mesh frame
  329. * @hdr: 802.11 frame header
  330. * @fc: frame control field
  331. * @meshda: destination address in the mesh
  332. * @meshsa: source address address in the mesh. Same as TA, as frame is
  333. * locally originated.
  334. *
  335. * Return the length of the 802.11 (does not include a mesh control header)
  336. */
  337. int ieee80211_fill_mesh_addresses(struct ieee80211_hdr *hdr, __le16 *fc, char
  338. *meshda, char *meshsa) {
  339. if (is_multicast_ether_addr(meshda)) {
  340. *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS);
  341. /* DA TA SA */
  342. memcpy(hdr->addr1, meshda, ETH_ALEN);
  343. memcpy(hdr->addr2, meshsa, ETH_ALEN);
  344. memcpy(hdr->addr3, meshsa, ETH_ALEN);
  345. return 24;
  346. } else {
  347. *fc |= cpu_to_le16(IEEE80211_FCTL_FROMDS |
  348. IEEE80211_FCTL_TODS);
  349. /* RA TA DA SA */
  350. memset(hdr->addr1, 0, ETH_ALEN); /* RA is resolved later */
  351. memcpy(hdr->addr2, meshsa, ETH_ALEN);
  352. memcpy(hdr->addr3, meshda, ETH_ALEN);
  353. memcpy(hdr->addr4, meshsa, ETH_ALEN);
  354. return 30;
  355. }
  356. }
  357. /**
  358. * ieee80211_new_mesh_header - create a new mesh header
  359. * @meshhdr: uninitialized mesh header
  360. * @sdata: mesh interface to be used
  361. * @addr4: addr4 of the mesh frame (1st in ae header)
  362. * may be NULL
  363. * @addr5: addr5 of the mesh frame (1st or 2nd in ae header)
  364. * may be NULL unless addr6 is present
  365. * @addr6: addr6 of the mesh frame (2nd or 3rd in ae header)
  366. * may be NULL unless addr5 is present
  367. *
  368. * Return the header length.
  369. */
  370. int ieee80211_new_mesh_header(struct ieee80211s_hdr *meshhdr,
  371. struct ieee80211_sub_if_data *sdata, char *addr4,
  372. char *addr5, char *addr6)
  373. {
  374. int aelen = 0;
  375. memset(meshhdr, 0, sizeof(meshhdr));
  376. meshhdr->ttl = sdata->u.mesh.mshcfg.dot11MeshTTL;
  377. put_unaligned(cpu_to_le32(sdata->u.mesh.mesh_seqnum), &meshhdr->seqnum);
  378. sdata->u.mesh.mesh_seqnum++;
  379. if (addr4) {
  380. meshhdr->flags |= MESH_FLAGS_AE_A4;
  381. aelen += ETH_ALEN;
  382. memcpy(meshhdr->eaddr1, addr4, ETH_ALEN);
  383. }
  384. if (addr5 && addr6) {
  385. meshhdr->flags |= MESH_FLAGS_AE_A5_A6;
  386. aelen += 2 * ETH_ALEN;
  387. if (!addr4) {
  388. memcpy(meshhdr->eaddr1, addr5, ETH_ALEN);
  389. memcpy(meshhdr->eaddr2, addr6, ETH_ALEN);
  390. } else {
  391. memcpy(meshhdr->eaddr2, addr5, ETH_ALEN);
  392. memcpy(meshhdr->eaddr3, addr6, ETH_ALEN);
  393. }
  394. }
  395. return 6 + aelen;
  396. }
  397. static void ieee80211_mesh_housekeeping(struct ieee80211_sub_if_data *sdata,
  398. struct ieee80211_if_mesh *ifmsh)
  399. {
  400. bool free_plinks;
  401. #ifdef CONFIG_MAC80211_VERBOSE_DEBUG
  402. printk(KERN_DEBUG "%s: running mesh housekeeping\n",
  403. sdata->dev->name);
  404. #endif
  405. ieee80211_sta_expire(sdata, IEEE80211_MESH_PEER_INACTIVITY_LIMIT);
  406. mesh_path_expire(sdata);
  407. free_plinks = mesh_plink_availables(sdata);
  408. if (free_plinks != sdata->u.mesh.accepting_plinks)
  409. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON);
  410. mod_timer(&ifmsh->housekeeping_timer,
  411. round_jiffies(jiffies + IEEE80211_MESH_HOUSEKEEPING_INTERVAL));
  412. }
  413. static void ieee80211_mesh_rootpath(struct ieee80211_sub_if_data *sdata)
  414. {
  415. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  416. mesh_path_tx_root_frame(sdata);
  417. mod_timer(&ifmsh->mesh_path_root_timer,
  418. round_jiffies(jiffies + IEEE80211_MESH_RANN_INTERVAL));
  419. }
  420. #ifdef CONFIG_PM
  421. void ieee80211_mesh_quiesce(struct ieee80211_sub_if_data *sdata)
  422. {
  423. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  424. /* might restart the timer but that doesn't matter */
  425. cancel_work_sync(&ifmsh->work);
  426. /* use atomic bitops in case both timers fire at the same time */
  427. if (del_timer_sync(&ifmsh->housekeeping_timer))
  428. set_bit(TMR_RUNNING_HK, &ifmsh->timers_running);
  429. if (del_timer_sync(&ifmsh->mesh_path_timer))
  430. set_bit(TMR_RUNNING_MP, &ifmsh->timers_running);
  431. if (del_timer_sync(&ifmsh->mesh_path_root_timer))
  432. set_bit(TMR_RUNNING_MPR, &ifmsh->timers_running);
  433. }
  434. void ieee80211_mesh_restart(struct ieee80211_sub_if_data *sdata)
  435. {
  436. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  437. if (test_and_clear_bit(TMR_RUNNING_HK, &ifmsh->timers_running))
  438. add_timer(&ifmsh->housekeeping_timer);
  439. if (test_and_clear_bit(TMR_RUNNING_MP, &ifmsh->timers_running))
  440. add_timer(&ifmsh->mesh_path_timer);
  441. if (test_and_clear_bit(TMR_RUNNING_MPR, &ifmsh->timers_running))
  442. add_timer(&ifmsh->mesh_path_root_timer);
  443. ieee80211_mesh_root_setup(ifmsh);
  444. }
  445. #endif
  446. void ieee80211_start_mesh(struct ieee80211_sub_if_data *sdata)
  447. {
  448. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  449. struct ieee80211_local *local = sdata->local;
  450. set_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags);
  451. ieee80211_mesh_root_setup(ifmsh);
  452. ieee80211_queue_work(&local->hw, &ifmsh->work);
  453. sdata->vif.bss_conf.beacon_int = MESH_DEFAULT_BEACON_INTERVAL;
  454. ieee80211_bss_info_change_notify(sdata, BSS_CHANGED_BEACON |
  455. BSS_CHANGED_BEACON_ENABLED |
  456. BSS_CHANGED_BEACON_INT);
  457. }
  458. void ieee80211_stop_mesh(struct ieee80211_sub_if_data *sdata)
  459. {
  460. del_timer_sync(&sdata->u.mesh.housekeeping_timer);
  461. del_timer_sync(&sdata->u.mesh.mesh_path_root_timer);
  462. /*
  463. * If the timer fired while we waited for it, it will have
  464. * requeued the work. Now the work will be running again
  465. * but will not rearm the timer again because it checks
  466. * whether the interface is running, which, at this point,
  467. * it no longer is.
  468. */
  469. cancel_work_sync(&sdata->u.mesh.work);
  470. /*
  471. * When we get here, the interface is marked down.
  472. * Call synchronize_rcu() to wait for the RX path
  473. * should it be using the interface and enqueuing
  474. * frames at this very time on another CPU.
  475. */
  476. rcu_barrier(); /* Wait for RX path and call_rcu()'s */
  477. skb_queue_purge(&sdata->u.mesh.skb_queue);
  478. }
  479. static void ieee80211_mesh_rx_bcn_presp(struct ieee80211_sub_if_data *sdata,
  480. u16 stype,
  481. struct ieee80211_mgmt *mgmt,
  482. size_t len,
  483. struct ieee80211_rx_status *rx_status)
  484. {
  485. struct ieee80211_local *local = sdata->local;
  486. struct ieee802_11_elems elems;
  487. struct ieee80211_channel *channel;
  488. u32 supp_rates = 0;
  489. size_t baselen;
  490. int freq;
  491. enum ieee80211_band band = rx_status->band;
  492. /* ignore ProbeResp to foreign address */
  493. if (stype == IEEE80211_STYPE_PROBE_RESP &&
  494. compare_ether_addr(mgmt->da, sdata->dev->dev_addr))
  495. return;
  496. baselen = (u8 *) mgmt->u.probe_resp.variable - (u8 *) mgmt;
  497. if (baselen > len)
  498. return;
  499. ieee802_11_parse_elems(mgmt->u.probe_resp.variable, len - baselen,
  500. &elems);
  501. if (elems.ds_params && elems.ds_params_len == 1)
  502. freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
  503. else
  504. freq = rx_status->freq;
  505. channel = ieee80211_get_channel(local->hw.wiphy, freq);
  506. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  507. return;
  508. if (elems.mesh_id && elems.mesh_config &&
  509. mesh_matches_local(&elems, sdata)) {
  510. supp_rates = ieee80211_sta_get_rates(local, &elems, band);
  511. mesh_neighbour_update(mgmt->sa, supp_rates, sdata,
  512. mesh_peer_accepts_plinks(&elems));
  513. }
  514. }
  515. static void ieee80211_mesh_rx_mgmt_action(struct ieee80211_sub_if_data *sdata,
  516. struct ieee80211_mgmt *mgmt,
  517. size_t len,
  518. struct ieee80211_rx_status *rx_status)
  519. {
  520. switch (mgmt->u.action.category) {
  521. case MESH_PLINK_CATEGORY:
  522. mesh_rx_plink_frame(sdata, mgmt, len, rx_status);
  523. break;
  524. case MESH_PATH_SEL_CATEGORY:
  525. mesh_rx_path_sel_frame(sdata, mgmt, len);
  526. break;
  527. }
  528. }
  529. static void ieee80211_mesh_rx_queued_mgmt(struct ieee80211_sub_if_data *sdata,
  530. struct sk_buff *skb)
  531. {
  532. struct ieee80211_rx_status *rx_status;
  533. struct ieee80211_if_mesh *ifmsh;
  534. struct ieee80211_mgmt *mgmt;
  535. u16 stype;
  536. ifmsh = &sdata->u.mesh;
  537. rx_status = IEEE80211_SKB_RXCB(skb);
  538. mgmt = (struct ieee80211_mgmt *) skb->data;
  539. stype = le16_to_cpu(mgmt->frame_control) & IEEE80211_FCTL_STYPE;
  540. switch (stype) {
  541. case IEEE80211_STYPE_PROBE_RESP:
  542. case IEEE80211_STYPE_BEACON:
  543. ieee80211_mesh_rx_bcn_presp(sdata, stype, mgmt, skb->len,
  544. rx_status);
  545. break;
  546. case IEEE80211_STYPE_ACTION:
  547. ieee80211_mesh_rx_mgmt_action(sdata, mgmt, skb->len, rx_status);
  548. break;
  549. }
  550. kfree_skb(skb);
  551. }
  552. static void ieee80211_mesh_work(struct work_struct *work)
  553. {
  554. struct ieee80211_sub_if_data *sdata =
  555. container_of(work, struct ieee80211_sub_if_data, u.mesh.work);
  556. struct ieee80211_local *local = sdata->local;
  557. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  558. struct sk_buff *skb;
  559. if (!netif_running(sdata->dev))
  560. return;
  561. if (local->scanning)
  562. return;
  563. while ((skb = skb_dequeue(&ifmsh->skb_queue)))
  564. ieee80211_mesh_rx_queued_mgmt(sdata, skb);
  565. if (ifmsh->preq_queue_len &&
  566. time_after(jiffies,
  567. ifmsh->last_preq + msecs_to_jiffies(ifmsh->mshcfg.dot11MeshHWMPpreqMinInterval)))
  568. mesh_path_start_discovery(sdata);
  569. if (test_and_clear_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags))
  570. mesh_mpath_table_grow();
  571. if (test_and_clear_bit(MESH_WORK_GROW_MPATH_TABLE, &ifmsh->wrkq_flags))
  572. mesh_mpp_table_grow();
  573. if (test_and_clear_bit(MESH_WORK_HOUSEKEEPING, &ifmsh->wrkq_flags))
  574. ieee80211_mesh_housekeeping(sdata, ifmsh);
  575. if (test_and_clear_bit(MESH_WORK_ROOT, &ifmsh->wrkq_flags))
  576. ieee80211_mesh_rootpath(sdata);
  577. }
  578. void ieee80211_mesh_notify_scan_completed(struct ieee80211_local *local)
  579. {
  580. struct ieee80211_sub_if_data *sdata;
  581. rcu_read_lock();
  582. list_for_each_entry_rcu(sdata, &local->interfaces, list)
  583. if (ieee80211_vif_is_mesh(&sdata->vif))
  584. ieee80211_queue_work(&local->hw, &sdata->u.mesh.work);
  585. rcu_read_unlock();
  586. }
  587. void ieee80211_mesh_init_sdata(struct ieee80211_sub_if_data *sdata)
  588. {
  589. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  590. INIT_WORK(&ifmsh->work, ieee80211_mesh_work);
  591. setup_timer(&ifmsh->housekeeping_timer,
  592. ieee80211_mesh_housekeeping_timer,
  593. (unsigned long) sdata);
  594. skb_queue_head_init(&sdata->u.mesh.skb_queue);
  595. ifmsh->mshcfg.dot11MeshRetryTimeout = MESH_RET_T;
  596. ifmsh->mshcfg.dot11MeshConfirmTimeout = MESH_CONF_T;
  597. ifmsh->mshcfg.dot11MeshHoldingTimeout = MESH_HOLD_T;
  598. ifmsh->mshcfg.dot11MeshMaxRetries = MESH_MAX_RETR;
  599. ifmsh->mshcfg.dot11MeshTTL = MESH_TTL;
  600. ifmsh->mshcfg.auto_open_plinks = true;
  601. ifmsh->mshcfg.dot11MeshMaxPeerLinks =
  602. MESH_MAX_ESTAB_PLINKS;
  603. ifmsh->mshcfg.dot11MeshHWMPactivePathTimeout =
  604. MESH_PATH_TIMEOUT;
  605. ifmsh->mshcfg.dot11MeshHWMPpreqMinInterval =
  606. MESH_PREQ_MIN_INT;
  607. ifmsh->mshcfg.dot11MeshHWMPnetDiameterTraversalTime =
  608. MESH_DIAM_TRAVERSAL_TIME;
  609. ifmsh->mshcfg.dot11MeshHWMPmaxPREQretries =
  610. MESH_MAX_PREQ_RETRIES;
  611. ifmsh->mshcfg.path_refresh_time =
  612. MESH_PATH_REFRESH_TIME;
  613. ifmsh->mshcfg.min_discovery_timeout =
  614. MESH_MIN_DISCOVERY_TIMEOUT;
  615. ifmsh->accepting_plinks = true;
  616. ifmsh->preq_id = 0;
  617. ifmsh->sn = 0;
  618. atomic_set(&ifmsh->mpaths, 0);
  619. mesh_rmc_init(sdata);
  620. ifmsh->last_preq = jiffies;
  621. /* Allocate all mesh structures when creating the first mesh interface. */
  622. if (!mesh_allocated)
  623. ieee80211s_init();
  624. mesh_ids_set_default(ifmsh);
  625. setup_timer(&ifmsh->mesh_path_timer,
  626. ieee80211_mesh_path_timer,
  627. (unsigned long) sdata);
  628. setup_timer(&ifmsh->mesh_path_root_timer,
  629. ieee80211_mesh_path_root_timer,
  630. (unsigned long) sdata);
  631. INIT_LIST_HEAD(&ifmsh->preq_queue.list);
  632. spin_lock_init(&ifmsh->mesh_preq_queue_lock);
  633. }
  634. ieee80211_rx_result
  635. ieee80211_mesh_rx_mgmt(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
  636. {
  637. struct ieee80211_local *local = sdata->local;
  638. struct ieee80211_if_mesh *ifmsh = &sdata->u.mesh;
  639. struct ieee80211_mgmt *mgmt;
  640. u16 fc;
  641. if (skb->len < 24)
  642. return RX_DROP_MONITOR;
  643. mgmt = (struct ieee80211_mgmt *) skb->data;
  644. fc = le16_to_cpu(mgmt->frame_control);
  645. switch (fc & IEEE80211_FCTL_STYPE) {
  646. case IEEE80211_STYPE_ACTION:
  647. if (skb->len < IEEE80211_MIN_ACTION_SIZE)
  648. return RX_DROP_MONITOR;
  649. /* fall through */
  650. case IEEE80211_STYPE_PROBE_RESP:
  651. case IEEE80211_STYPE_BEACON:
  652. skb_queue_tail(&ifmsh->skb_queue, skb);
  653. ieee80211_queue_work(&local->hw, &ifmsh->work);
  654. return RX_QUEUED;
  655. }
  656. return RX_CONTINUE;
  657. }