translation-table.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889
  1. /*
  2. * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner, Simon Wunderlich
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA
  19. *
  20. */
  21. #include "main.h"
  22. #include "translation-table.h"
  23. #include "soft-interface.h"
  24. #include "hard-interface.h"
  25. #include "send.h"
  26. #include "hash.h"
  27. #include "originator.h"
  28. #include "routing.h"
  29. #include <linux/crc16.h>
  30. static void _tt_global_del(struct bat_priv *bat_priv,
  31. struct tt_global_entry *tt_global_entry,
  32. const char *message);
  33. static void tt_purge(struct work_struct *work);
  34. /* returns 1 if they are the same mac addr */
  35. static int compare_tt(const struct hlist_node *node, const void *data2)
  36. {
  37. const void *data1 = container_of(node, struct tt_common_entry,
  38. hash_entry);
  39. return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
  40. }
  41. static void tt_start_timer(struct bat_priv *bat_priv)
  42. {
  43. INIT_DELAYED_WORK(&bat_priv->tt_work, tt_purge);
  44. queue_delayed_work(bat_event_workqueue, &bat_priv->tt_work,
  45. msecs_to_jiffies(5000));
  46. }
  47. static struct tt_common_entry *tt_hash_find(struct hashtable_t *hash,
  48. const void *data)
  49. {
  50. struct hlist_head *head;
  51. struct hlist_node *node;
  52. struct tt_common_entry *tt_common_entry, *tt_common_entry_tmp = NULL;
  53. uint32_t index;
  54. if (!hash)
  55. return NULL;
  56. index = choose_orig(data, hash->size);
  57. head = &hash->table[index];
  58. rcu_read_lock();
  59. hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) {
  60. if (!compare_eth(tt_common_entry, data))
  61. continue;
  62. if (!atomic_inc_not_zero(&tt_common_entry->refcount))
  63. continue;
  64. tt_common_entry_tmp = tt_common_entry;
  65. break;
  66. }
  67. rcu_read_unlock();
  68. return tt_common_entry_tmp;
  69. }
  70. static struct tt_local_entry *tt_local_hash_find(struct bat_priv *bat_priv,
  71. const void *data)
  72. {
  73. struct tt_common_entry *tt_common_entry;
  74. struct tt_local_entry *tt_local_entry = NULL;
  75. tt_common_entry = tt_hash_find(bat_priv->tt_local_hash, data);
  76. if (tt_common_entry)
  77. tt_local_entry = container_of(tt_common_entry,
  78. struct tt_local_entry, common);
  79. return tt_local_entry;
  80. }
  81. static struct tt_global_entry *tt_global_hash_find(struct bat_priv *bat_priv,
  82. const void *data)
  83. {
  84. struct tt_common_entry *tt_common_entry;
  85. struct tt_global_entry *tt_global_entry = NULL;
  86. tt_common_entry = tt_hash_find(bat_priv->tt_global_hash, data);
  87. if (tt_common_entry)
  88. tt_global_entry = container_of(tt_common_entry,
  89. struct tt_global_entry, common);
  90. return tt_global_entry;
  91. }
  92. static void tt_local_entry_free_ref(struct tt_local_entry *tt_local_entry)
  93. {
  94. if (atomic_dec_and_test(&tt_local_entry->common.refcount))
  95. kfree_rcu(tt_local_entry, common.rcu);
  96. }
  97. static void tt_global_entry_free_rcu(struct rcu_head *rcu)
  98. {
  99. struct tt_common_entry *tt_common_entry;
  100. struct tt_global_entry *tt_global_entry;
  101. tt_common_entry = container_of(rcu, struct tt_common_entry, rcu);
  102. tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
  103. common);
  104. if (tt_global_entry->orig_node)
  105. orig_node_free_ref(tt_global_entry->orig_node);
  106. kfree(tt_global_entry);
  107. }
  108. static void tt_global_entry_free_ref(struct tt_global_entry *tt_global_entry)
  109. {
  110. if (atomic_dec_and_test(&tt_global_entry->common.refcount))
  111. call_rcu(&tt_global_entry->common.rcu,
  112. tt_global_entry_free_rcu);
  113. }
  114. static void tt_local_event(struct bat_priv *bat_priv, const uint8_t *addr,
  115. uint8_t flags)
  116. {
  117. struct tt_change_node *tt_change_node;
  118. tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
  119. if (!tt_change_node)
  120. return;
  121. tt_change_node->change.flags = flags;
  122. memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
  123. spin_lock_bh(&bat_priv->tt_changes_list_lock);
  124. /* track the change in the OGMinterval list */
  125. list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
  126. atomic_inc(&bat_priv->tt_local_changes);
  127. spin_unlock_bh(&bat_priv->tt_changes_list_lock);
  128. atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
  129. }
  130. int tt_len(int changes_num)
  131. {
  132. return changes_num * sizeof(struct tt_change);
  133. }
  134. static int tt_local_init(struct bat_priv *bat_priv)
  135. {
  136. if (bat_priv->tt_local_hash)
  137. return 1;
  138. bat_priv->tt_local_hash = hash_new(1024);
  139. if (!bat_priv->tt_local_hash)
  140. return 0;
  141. return 1;
  142. }
  143. void tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
  144. int ifindex)
  145. {
  146. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  147. struct tt_local_entry *tt_local_entry = NULL;
  148. struct tt_global_entry *tt_global_entry = NULL;
  149. int hash_added;
  150. tt_local_entry = tt_local_hash_find(bat_priv, addr);
  151. if (tt_local_entry) {
  152. tt_local_entry->last_seen = jiffies;
  153. goto out;
  154. }
  155. tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
  156. if (!tt_local_entry)
  157. goto out;
  158. bat_dbg(DBG_TT, bat_priv,
  159. "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
  160. (uint8_t)atomic_read(&bat_priv->ttvn));
  161. memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
  162. tt_local_entry->common.flags = NO_FLAGS;
  163. if (is_wifi_iface(ifindex))
  164. tt_local_entry->common.flags |= TT_CLIENT_WIFI;
  165. atomic_set(&tt_local_entry->common.refcount, 2);
  166. tt_local_entry->last_seen = jiffies;
  167. /* the batman interface mac address should never be purged */
  168. if (compare_eth(addr, soft_iface->dev_addr))
  169. tt_local_entry->common.flags |= TT_CLIENT_NOPURGE;
  170. /* The local entry has to be marked as NEW to avoid to send it in
  171. * a full table response going out before the next ttvn increment
  172. * (consistency check) */
  173. tt_local_entry->common.flags |= TT_CLIENT_NEW;
  174. hash_added = hash_add(bat_priv->tt_local_hash, compare_tt, choose_orig,
  175. &tt_local_entry->common,
  176. &tt_local_entry->common.hash_entry);
  177. if (unlikely(hash_added != 0)) {
  178. /* remove the reference for the hash */
  179. tt_local_entry_free_ref(tt_local_entry);
  180. goto out;
  181. }
  182. tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
  183. /* remove address from global hash if present */
  184. tt_global_entry = tt_global_hash_find(bat_priv, addr);
  185. /* Check whether it is a roaming! */
  186. if (tt_global_entry) {
  187. /* This node is probably going to update its tt table */
  188. tt_global_entry->orig_node->tt_poss_change = true;
  189. /* The global entry has to be marked as ROAMING and has to be
  190. * kept for consistency purpose */
  191. tt_global_entry->common.flags |= TT_CLIENT_ROAM;
  192. tt_global_entry->roam_at = jiffies;
  193. send_roam_adv(bat_priv, tt_global_entry->common.addr,
  194. tt_global_entry->orig_node);
  195. }
  196. out:
  197. if (tt_local_entry)
  198. tt_local_entry_free_ref(tt_local_entry);
  199. if (tt_global_entry)
  200. tt_global_entry_free_ref(tt_global_entry);
  201. }
  202. int tt_changes_fill_buffer(struct bat_priv *bat_priv,
  203. unsigned char *buff, int buff_len)
  204. {
  205. int count = 0, tot_changes = 0;
  206. struct tt_change_node *entry, *safe;
  207. if (buff_len > 0)
  208. tot_changes = buff_len / tt_len(1);
  209. spin_lock_bh(&bat_priv->tt_changes_list_lock);
  210. atomic_set(&bat_priv->tt_local_changes, 0);
  211. list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
  212. list) {
  213. if (count < tot_changes) {
  214. memcpy(buff + tt_len(count),
  215. &entry->change, sizeof(struct tt_change));
  216. count++;
  217. }
  218. list_del(&entry->list);
  219. kfree(entry);
  220. }
  221. spin_unlock_bh(&bat_priv->tt_changes_list_lock);
  222. /* Keep the buffer for possible tt_request */
  223. spin_lock_bh(&bat_priv->tt_buff_lock);
  224. kfree(bat_priv->tt_buff);
  225. bat_priv->tt_buff_len = 0;
  226. bat_priv->tt_buff = NULL;
  227. /* We check whether this new OGM has no changes due to size
  228. * problems */
  229. if (buff_len > 0) {
  230. /**
  231. * if kmalloc() fails we will reply with the full table
  232. * instead of providing the diff
  233. */
  234. bat_priv->tt_buff = kmalloc(buff_len, GFP_ATOMIC);
  235. if (bat_priv->tt_buff) {
  236. memcpy(bat_priv->tt_buff, buff, buff_len);
  237. bat_priv->tt_buff_len = buff_len;
  238. }
  239. }
  240. spin_unlock_bh(&bat_priv->tt_buff_lock);
  241. return tot_changes;
  242. }
  243. int tt_local_seq_print_text(struct seq_file *seq, void *offset)
  244. {
  245. struct net_device *net_dev = (struct net_device *)seq->private;
  246. struct bat_priv *bat_priv = netdev_priv(net_dev);
  247. struct hashtable_t *hash = bat_priv->tt_local_hash;
  248. struct tt_common_entry *tt_common_entry;
  249. struct hard_iface *primary_if;
  250. struct hlist_node *node;
  251. struct hlist_head *head;
  252. uint32_t i;
  253. int ret = 0;
  254. primary_if = primary_if_get_selected(bat_priv);
  255. if (!primary_if) {
  256. ret = seq_printf(seq,
  257. "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
  258. net_dev->name);
  259. goto out;
  260. }
  261. if (primary_if->if_status != IF_ACTIVE) {
  262. ret = seq_printf(seq,
  263. "BATMAN mesh %s disabled - primary interface not active\n",
  264. net_dev->name);
  265. goto out;
  266. }
  267. seq_printf(seq,
  268. "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
  269. net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
  270. for (i = 0; i < hash->size; i++) {
  271. head = &hash->table[i];
  272. rcu_read_lock();
  273. hlist_for_each_entry_rcu(tt_common_entry, node,
  274. head, hash_entry) {
  275. seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
  276. tt_common_entry->addr,
  277. (tt_common_entry->flags &
  278. TT_CLIENT_ROAM ? 'R' : '.'),
  279. (tt_common_entry->flags &
  280. TT_CLIENT_NOPURGE ? 'P' : '.'),
  281. (tt_common_entry->flags &
  282. TT_CLIENT_NEW ? 'N' : '.'),
  283. (tt_common_entry->flags &
  284. TT_CLIENT_PENDING ? 'X' : '.'),
  285. (tt_common_entry->flags &
  286. TT_CLIENT_WIFI ? 'W' : '.'));
  287. }
  288. rcu_read_unlock();
  289. }
  290. out:
  291. if (primary_if)
  292. hardif_free_ref(primary_if);
  293. return ret;
  294. }
  295. static void tt_local_set_pending(struct bat_priv *bat_priv,
  296. struct tt_local_entry *tt_local_entry,
  297. uint16_t flags, const char *message)
  298. {
  299. tt_local_event(bat_priv, tt_local_entry->common.addr,
  300. tt_local_entry->common.flags | flags);
  301. /* The local client has to be marked as "pending to be removed" but has
  302. * to be kept in the table in order to send it in a full table
  303. * response issued before the net ttvn increment (consistency check) */
  304. tt_local_entry->common.flags |= TT_CLIENT_PENDING;
  305. bat_dbg(DBG_TT, bat_priv,
  306. "Local tt entry (%pM) pending to be removed: %s\n",
  307. tt_local_entry->common.addr, message);
  308. }
  309. void tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
  310. const char *message, bool roaming)
  311. {
  312. struct tt_local_entry *tt_local_entry = NULL;
  313. tt_local_entry = tt_local_hash_find(bat_priv, addr);
  314. if (!tt_local_entry)
  315. goto out;
  316. tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL |
  317. (roaming ? TT_CLIENT_ROAM : NO_FLAGS), message);
  318. out:
  319. if (tt_local_entry)
  320. tt_local_entry_free_ref(tt_local_entry);
  321. }
  322. static void tt_local_purge(struct bat_priv *bat_priv)
  323. {
  324. struct hashtable_t *hash = bat_priv->tt_local_hash;
  325. struct tt_local_entry *tt_local_entry;
  326. struct tt_common_entry *tt_common_entry;
  327. struct hlist_node *node, *node_tmp;
  328. struct hlist_head *head;
  329. spinlock_t *list_lock; /* protects write access to the hash lists */
  330. uint32_t i;
  331. for (i = 0; i < hash->size; i++) {
  332. head = &hash->table[i];
  333. list_lock = &hash->list_locks[i];
  334. spin_lock_bh(list_lock);
  335. hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
  336. head, hash_entry) {
  337. tt_local_entry = container_of(tt_common_entry,
  338. struct tt_local_entry,
  339. common);
  340. if (tt_local_entry->common.flags & TT_CLIENT_NOPURGE)
  341. continue;
  342. /* entry already marked for deletion */
  343. if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
  344. continue;
  345. if (!has_timed_out(tt_local_entry->last_seen,
  346. TT_LOCAL_TIMEOUT))
  347. continue;
  348. tt_local_set_pending(bat_priv, tt_local_entry,
  349. TT_CLIENT_DEL, "timed out");
  350. }
  351. spin_unlock_bh(list_lock);
  352. }
  353. }
  354. static void tt_local_table_free(struct bat_priv *bat_priv)
  355. {
  356. struct hashtable_t *hash;
  357. spinlock_t *list_lock; /* protects write access to the hash lists */
  358. struct tt_common_entry *tt_common_entry;
  359. struct tt_local_entry *tt_local_entry;
  360. struct hlist_node *node, *node_tmp;
  361. struct hlist_head *head;
  362. uint32_t i;
  363. if (!bat_priv->tt_local_hash)
  364. return;
  365. hash = bat_priv->tt_local_hash;
  366. for (i = 0; i < hash->size; i++) {
  367. head = &hash->table[i];
  368. list_lock = &hash->list_locks[i];
  369. spin_lock_bh(list_lock);
  370. hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
  371. head, hash_entry) {
  372. hlist_del_rcu(node);
  373. tt_local_entry = container_of(tt_common_entry,
  374. struct tt_local_entry,
  375. common);
  376. tt_local_entry_free_ref(tt_local_entry);
  377. }
  378. spin_unlock_bh(list_lock);
  379. }
  380. hash_destroy(hash);
  381. bat_priv->tt_local_hash = NULL;
  382. }
  383. static int tt_global_init(struct bat_priv *bat_priv)
  384. {
  385. if (bat_priv->tt_global_hash)
  386. return 1;
  387. bat_priv->tt_global_hash = hash_new(1024);
  388. if (!bat_priv->tt_global_hash)
  389. return 0;
  390. return 1;
  391. }
  392. static void tt_changes_list_free(struct bat_priv *bat_priv)
  393. {
  394. struct tt_change_node *entry, *safe;
  395. spin_lock_bh(&bat_priv->tt_changes_list_lock);
  396. list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
  397. list) {
  398. list_del(&entry->list);
  399. kfree(entry);
  400. }
  401. atomic_set(&bat_priv->tt_local_changes, 0);
  402. spin_unlock_bh(&bat_priv->tt_changes_list_lock);
  403. }
  404. /* caller must hold orig_node refcount */
  405. int tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
  406. const unsigned char *tt_addr, uint8_t ttvn, bool roaming,
  407. bool wifi)
  408. {
  409. struct tt_global_entry *tt_global_entry;
  410. struct orig_node *orig_node_tmp;
  411. int ret = 0;
  412. int hash_added;
  413. tt_global_entry = tt_global_hash_find(bat_priv, tt_addr);
  414. if (!tt_global_entry) {
  415. tt_global_entry =
  416. kmalloc(sizeof(*tt_global_entry),
  417. GFP_ATOMIC);
  418. if (!tt_global_entry)
  419. goto out;
  420. memcpy(tt_global_entry->common.addr, tt_addr, ETH_ALEN);
  421. tt_global_entry->common.flags = NO_FLAGS;
  422. atomic_set(&tt_global_entry->common.refcount, 2);
  423. /* Assign the new orig_node */
  424. atomic_inc(&orig_node->refcount);
  425. tt_global_entry->orig_node = orig_node;
  426. tt_global_entry->ttvn = ttvn;
  427. tt_global_entry->roam_at = 0;
  428. hash_added = hash_add(bat_priv->tt_global_hash, compare_tt,
  429. choose_orig, &tt_global_entry->common,
  430. &tt_global_entry->common.hash_entry);
  431. if (unlikely(hash_added != 0)) {
  432. /* remove the reference for the hash */
  433. tt_global_entry_free_ref(tt_global_entry);
  434. goto out_remove;
  435. }
  436. atomic_inc(&orig_node->tt_size);
  437. } else {
  438. if (tt_global_entry->orig_node != orig_node) {
  439. atomic_dec(&tt_global_entry->orig_node->tt_size);
  440. orig_node_tmp = tt_global_entry->orig_node;
  441. atomic_inc(&orig_node->refcount);
  442. tt_global_entry->orig_node = orig_node;
  443. orig_node_free_ref(orig_node_tmp);
  444. atomic_inc(&orig_node->tt_size);
  445. }
  446. tt_global_entry->common.flags = NO_FLAGS;
  447. tt_global_entry->ttvn = ttvn;
  448. tt_global_entry->roam_at = 0;
  449. }
  450. if (wifi)
  451. tt_global_entry->common.flags |= TT_CLIENT_WIFI;
  452. bat_dbg(DBG_TT, bat_priv,
  453. "Creating new global tt entry: %pM (via %pM)\n",
  454. tt_global_entry->common.addr, orig_node->orig);
  455. out_remove:
  456. /* remove address from local hash if present */
  457. tt_local_remove(bat_priv, tt_global_entry->common.addr,
  458. "global tt received", roaming);
  459. ret = 1;
  460. out:
  461. if (tt_global_entry)
  462. tt_global_entry_free_ref(tt_global_entry);
  463. return ret;
  464. }
  465. int tt_global_seq_print_text(struct seq_file *seq, void *offset)
  466. {
  467. struct net_device *net_dev = (struct net_device *)seq->private;
  468. struct bat_priv *bat_priv = netdev_priv(net_dev);
  469. struct hashtable_t *hash = bat_priv->tt_global_hash;
  470. struct tt_common_entry *tt_common_entry;
  471. struct tt_global_entry *tt_global_entry;
  472. struct hard_iface *primary_if;
  473. struct hlist_node *node;
  474. struct hlist_head *head;
  475. uint32_t i;
  476. int ret = 0;
  477. primary_if = primary_if_get_selected(bat_priv);
  478. if (!primary_if) {
  479. ret = seq_printf(seq,
  480. "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
  481. net_dev->name);
  482. goto out;
  483. }
  484. if (primary_if->if_status != IF_ACTIVE) {
  485. ret = seq_printf(seq,
  486. "BATMAN mesh %s disabled - primary interface not active\n",
  487. net_dev->name);
  488. goto out;
  489. }
  490. seq_printf(seq,
  491. "Globally announced TT entries received via the mesh %s\n",
  492. net_dev->name);
  493. seq_printf(seq, " %-13s %s %-15s %s %s\n",
  494. "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
  495. for (i = 0; i < hash->size; i++) {
  496. head = &hash->table[i];
  497. rcu_read_lock();
  498. hlist_for_each_entry_rcu(tt_common_entry, node,
  499. head, hash_entry) {
  500. tt_global_entry = container_of(tt_common_entry,
  501. struct tt_global_entry,
  502. common);
  503. seq_printf(seq,
  504. " * %pM (%3u) via %pM (%3u) [%c%c]\n",
  505. tt_global_entry->common.addr,
  506. tt_global_entry->ttvn,
  507. tt_global_entry->orig_node->orig,
  508. (uint8_t) atomic_read(
  509. &tt_global_entry->orig_node->
  510. last_ttvn),
  511. (tt_global_entry->common.flags &
  512. TT_CLIENT_ROAM ? 'R' : '.'),
  513. (tt_global_entry->common.flags &
  514. TT_CLIENT_WIFI ? 'W' : '.'));
  515. }
  516. rcu_read_unlock();
  517. }
  518. out:
  519. if (primary_if)
  520. hardif_free_ref(primary_if);
  521. return ret;
  522. }
  523. static void _tt_global_del(struct bat_priv *bat_priv,
  524. struct tt_global_entry *tt_global_entry,
  525. const char *message)
  526. {
  527. if (!tt_global_entry)
  528. goto out;
  529. bat_dbg(DBG_TT, bat_priv,
  530. "Deleting global tt entry %pM (via %pM): %s\n",
  531. tt_global_entry->common.addr, tt_global_entry->orig_node->orig,
  532. message);
  533. atomic_dec(&tt_global_entry->orig_node->tt_size);
  534. hash_remove(bat_priv->tt_global_hash, compare_tt, choose_orig,
  535. tt_global_entry->common.addr);
  536. out:
  537. if (tt_global_entry)
  538. tt_global_entry_free_ref(tt_global_entry);
  539. }
  540. void tt_global_del(struct bat_priv *bat_priv,
  541. struct orig_node *orig_node, const unsigned char *addr,
  542. const char *message, bool roaming)
  543. {
  544. struct tt_global_entry *tt_global_entry = NULL;
  545. struct tt_local_entry *tt_local_entry = NULL;
  546. tt_global_entry = tt_global_hash_find(bat_priv, addr);
  547. if (!tt_global_entry || tt_global_entry->orig_node != orig_node)
  548. goto out;
  549. if (!roaming)
  550. goto out_del;
  551. /* if we are deleting a global entry due to a roam
  552. * event, there are two possibilities:
  553. * 1) the client roamed from node A to node B => we mark
  554. * it with TT_CLIENT_ROAM, we start a timer and we
  555. * wait for node B to claim it. In case of timeout
  556. * the entry is purged.
  557. * 2) the client roamed to us => we can directly delete
  558. * the global entry, since it is useless now. */
  559. tt_local_entry = tt_local_hash_find(bat_priv,
  560. tt_global_entry->common.addr);
  561. if (!tt_local_entry) {
  562. tt_global_entry->common.flags |= TT_CLIENT_ROAM;
  563. tt_global_entry->roam_at = jiffies;
  564. goto out;
  565. }
  566. out_del:
  567. _tt_global_del(bat_priv, tt_global_entry, message);
  568. out:
  569. if (tt_global_entry)
  570. tt_global_entry_free_ref(tt_global_entry);
  571. if (tt_local_entry)
  572. tt_local_entry_free_ref(tt_local_entry);
  573. }
  574. void tt_global_del_orig(struct bat_priv *bat_priv,
  575. struct orig_node *orig_node, const char *message)
  576. {
  577. struct tt_global_entry *tt_global_entry;
  578. struct tt_common_entry *tt_common_entry;
  579. uint32_t i;
  580. struct hashtable_t *hash = bat_priv->tt_global_hash;
  581. struct hlist_node *node, *safe;
  582. struct hlist_head *head;
  583. spinlock_t *list_lock; /* protects write access to the hash lists */
  584. if (!hash)
  585. return;
  586. for (i = 0; i < hash->size; i++) {
  587. head = &hash->table[i];
  588. list_lock = &hash->list_locks[i];
  589. spin_lock_bh(list_lock);
  590. hlist_for_each_entry_safe(tt_common_entry, node, safe,
  591. head, hash_entry) {
  592. tt_global_entry = container_of(tt_common_entry,
  593. struct tt_global_entry,
  594. common);
  595. if (tt_global_entry->orig_node == orig_node) {
  596. bat_dbg(DBG_TT, bat_priv,
  597. "Deleting global tt entry %pM (via %pM): %s\n",
  598. tt_global_entry->common.addr,
  599. tt_global_entry->orig_node->orig,
  600. message);
  601. hlist_del_rcu(node);
  602. tt_global_entry_free_ref(tt_global_entry);
  603. }
  604. }
  605. spin_unlock_bh(list_lock);
  606. }
  607. atomic_set(&orig_node->tt_size, 0);
  608. orig_node->tt_initialised = false;
  609. }
  610. static void tt_global_roam_purge(struct bat_priv *bat_priv)
  611. {
  612. struct hashtable_t *hash = bat_priv->tt_global_hash;
  613. struct tt_common_entry *tt_common_entry;
  614. struct tt_global_entry *tt_global_entry;
  615. struct hlist_node *node, *node_tmp;
  616. struct hlist_head *head;
  617. spinlock_t *list_lock; /* protects write access to the hash lists */
  618. uint32_t i;
  619. for (i = 0; i < hash->size; i++) {
  620. head = &hash->table[i];
  621. list_lock = &hash->list_locks[i];
  622. spin_lock_bh(list_lock);
  623. hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
  624. head, hash_entry) {
  625. tt_global_entry = container_of(tt_common_entry,
  626. struct tt_global_entry,
  627. common);
  628. if (!(tt_global_entry->common.flags & TT_CLIENT_ROAM))
  629. continue;
  630. if (!has_timed_out(tt_global_entry->roam_at,
  631. TT_CLIENT_ROAM_TIMEOUT))
  632. continue;
  633. bat_dbg(DBG_TT, bat_priv,
  634. "Deleting global tt entry (%pM): Roaming timeout\n",
  635. tt_global_entry->common.addr);
  636. atomic_dec(&tt_global_entry->orig_node->tt_size);
  637. hlist_del_rcu(node);
  638. tt_global_entry_free_ref(tt_global_entry);
  639. }
  640. spin_unlock_bh(list_lock);
  641. }
  642. }
  643. static void tt_global_table_free(struct bat_priv *bat_priv)
  644. {
  645. struct hashtable_t *hash;
  646. spinlock_t *list_lock; /* protects write access to the hash lists */
  647. struct tt_common_entry *tt_common_entry;
  648. struct tt_global_entry *tt_global_entry;
  649. struct hlist_node *node, *node_tmp;
  650. struct hlist_head *head;
  651. uint32_t i;
  652. if (!bat_priv->tt_global_hash)
  653. return;
  654. hash = bat_priv->tt_global_hash;
  655. for (i = 0; i < hash->size; i++) {
  656. head = &hash->table[i];
  657. list_lock = &hash->list_locks[i];
  658. spin_lock_bh(list_lock);
  659. hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
  660. head, hash_entry) {
  661. hlist_del_rcu(node);
  662. tt_global_entry = container_of(tt_common_entry,
  663. struct tt_global_entry,
  664. common);
  665. tt_global_entry_free_ref(tt_global_entry);
  666. }
  667. spin_unlock_bh(list_lock);
  668. }
  669. hash_destroy(hash);
  670. bat_priv->tt_global_hash = NULL;
  671. }
  672. static bool _is_ap_isolated(struct tt_local_entry *tt_local_entry,
  673. struct tt_global_entry *tt_global_entry)
  674. {
  675. bool ret = false;
  676. if (tt_local_entry->common.flags & TT_CLIENT_WIFI &&
  677. tt_global_entry->common.flags & TT_CLIENT_WIFI)
  678. ret = true;
  679. return ret;
  680. }
  681. struct orig_node *transtable_search(struct bat_priv *bat_priv,
  682. const uint8_t *src, const uint8_t *addr)
  683. {
  684. struct tt_local_entry *tt_local_entry = NULL;
  685. struct tt_global_entry *tt_global_entry = NULL;
  686. struct orig_node *orig_node = NULL;
  687. if (src && atomic_read(&bat_priv->ap_isolation)) {
  688. tt_local_entry = tt_local_hash_find(bat_priv, src);
  689. if (!tt_local_entry)
  690. goto out;
  691. }
  692. tt_global_entry = tt_global_hash_find(bat_priv, addr);
  693. if (!tt_global_entry)
  694. goto out;
  695. /* check whether the clients should not communicate due to AP
  696. * isolation */
  697. if (tt_local_entry && _is_ap_isolated(tt_local_entry, tt_global_entry))
  698. goto out;
  699. if (!atomic_inc_not_zero(&tt_global_entry->orig_node->refcount))
  700. goto out;
  701. orig_node = tt_global_entry->orig_node;
  702. out:
  703. if (tt_global_entry)
  704. tt_global_entry_free_ref(tt_global_entry);
  705. if (tt_local_entry)
  706. tt_local_entry_free_ref(tt_local_entry);
  707. return orig_node;
  708. }
  709. /* Calculates the checksum of the local table of a given orig_node */
  710. uint16_t tt_global_crc(struct bat_priv *bat_priv, struct orig_node *orig_node)
  711. {
  712. uint16_t total = 0, total_one;
  713. struct hashtable_t *hash = bat_priv->tt_global_hash;
  714. struct tt_common_entry *tt_common_entry;
  715. struct tt_global_entry *tt_global_entry;
  716. struct hlist_node *node;
  717. struct hlist_head *head;
  718. uint32_t i;
  719. int j;
  720. for (i = 0; i < hash->size; i++) {
  721. head = &hash->table[i];
  722. rcu_read_lock();
  723. hlist_for_each_entry_rcu(tt_common_entry, node,
  724. head, hash_entry) {
  725. tt_global_entry = container_of(tt_common_entry,
  726. struct tt_global_entry,
  727. common);
  728. if (compare_eth(tt_global_entry->orig_node,
  729. orig_node)) {
  730. /* Roaming clients are in the global table for
  731. * consistency only. They don't have to be
  732. * taken into account while computing the
  733. * global crc */
  734. if (tt_common_entry->flags & TT_CLIENT_ROAM)
  735. continue;
  736. total_one = 0;
  737. for (j = 0; j < ETH_ALEN; j++)
  738. total_one = crc16_byte(total_one,
  739. tt_common_entry->addr[j]);
  740. total ^= total_one;
  741. }
  742. }
  743. rcu_read_unlock();
  744. }
  745. return total;
  746. }
  747. /* Calculates the checksum of the local table */
  748. uint16_t tt_local_crc(struct bat_priv *bat_priv)
  749. {
  750. uint16_t total = 0, total_one;
  751. struct hashtable_t *hash = bat_priv->tt_local_hash;
  752. struct tt_common_entry *tt_common_entry;
  753. struct hlist_node *node;
  754. struct hlist_head *head;
  755. uint32_t i;
  756. int j;
  757. for (i = 0; i < hash->size; i++) {
  758. head = &hash->table[i];
  759. rcu_read_lock();
  760. hlist_for_each_entry_rcu(tt_common_entry, node,
  761. head, hash_entry) {
  762. /* not yet committed clients have not to be taken into
  763. * account while computing the CRC */
  764. if (tt_common_entry->flags & TT_CLIENT_NEW)
  765. continue;
  766. total_one = 0;
  767. for (j = 0; j < ETH_ALEN; j++)
  768. total_one = crc16_byte(total_one,
  769. tt_common_entry->addr[j]);
  770. total ^= total_one;
  771. }
  772. rcu_read_unlock();
  773. }
  774. return total;
  775. }
  776. static void tt_req_list_free(struct bat_priv *bat_priv)
  777. {
  778. struct tt_req_node *node, *safe;
  779. spin_lock_bh(&bat_priv->tt_req_list_lock);
  780. list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
  781. list_del(&node->list);
  782. kfree(node);
  783. }
  784. spin_unlock_bh(&bat_priv->tt_req_list_lock);
  785. }
  786. void tt_save_orig_buffer(struct bat_priv *bat_priv, struct orig_node *orig_node,
  787. const unsigned char *tt_buff, uint8_t tt_num_changes)
  788. {
  789. uint16_t tt_buff_len = tt_len(tt_num_changes);
  790. /* Replace the old buffer only if I received something in the
  791. * last OGM (the OGM could carry no changes) */
  792. spin_lock_bh(&orig_node->tt_buff_lock);
  793. if (tt_buff_len > 0) {
  794. kfree(orig_node->tt_buff);
  795. orig_node->tt_buff_len = 0;
  796. orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
  797. if (orig_node->tt_buff) {
  798. memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
  799. orig_node->tt_buff_len = tt_buff_len;
  800. }
  801. }
  802. spin_unlock_bh(&orig_node->tt_buff_lock);
  803. }
  804. static void tt_req_purge(struct bat_priv *bat_priv)
  805. {
  806. struct tt_req_node *node, *safe;
  807. spin_lock_bh(&bat_priv->tt_req_list_lock);
  808. list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
  809. if (has_timed_out(node->issued_at, TT_REQUEST_TIMEOUT)) {
  810. list_del(&node->list);
  811. kfree(node);
  812. }
  813. }
  814. spin_unlock_bh(&bat_priv->tt_req_list_lock);
  815. }
  816. /* returns the pointer to the new tt_req_node struct if no request
  817. * has already been issued for this orig_node, NULL otherwise */
  818. static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv,
  819. struct orig_node *orig_node)
  820. {
  821. struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
  822. spin_lock_bh(&bat_priv->tt_req_list_lock);
  823. list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
  824. if (compare_eth(tt_req_node_tmp, orig_node) &&
  825. !has_timed_out(tt_req_node_tmp->issued_at,
  826. TT_REQUEST_TIMEOUT))
  827. goto unlock;
  828. }
  829. tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
  830. if (!tt_req_node)
  831. goto unlock;
  832. memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
  833. tt_req_node->issued_at = jiffies;
  834. list_add(&tt_req_node->list, &bat_priv->tt_req_list);
  835. unlock:
  836. spin_unlock_bh(&bat_priv->tt_req_list_lock);
  837. return tt_req_node;
  838. }
  839. /* data_ptr is useless here, but has to be kept to respect the prototype */
  840. static int tt_local_valid_entry(const void *entry_ptr, const void *data_ptr)
  841. {
  842. const struct tt_common_entry *tt_common_entry = entry_ptr;
  843. if (tt_common_entry->flags & TT_CLIENT_NEW)
  844. return 0;
  845. return 1;
  846. }
  847. static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr)
  848. {
  849. const struct tt_common_entry *tt_common_entry = entry_ptr;
  850. const struct tt_global_entry *tt_global_entry;
  851. const struct orig_node *orig_node = data_ptr;
  852. if (tt_common_entry->flags & TT_CLIENT_ROAM)
  853. return 0;
  854. tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
  855. common);
  856. return (tt_global_entry->orig_node == orig_node);
  857. }
  858. static struct sk_buff *tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
  859. struct hashtable_t *hash,
  860. struct hard_iface *primary_if,
  861. int (*valid_cb)(const void *,
  862. const void *),
  863. void *cb_data)
  864. {
  865. struct tt_common_entry *tt_common_entry;
  866. struct tt_query_packet *tt_response;
  867. struct tt_change *tt_change;
  868. struct hlist_node *node;
  869. struct hlist_head *head;
  870. struct sk_buff *skb = NULL;
  871. uint16_t tt_tot, tt_count;
  872. ssize_t tt_query_size = sizeof(struct tt_query_packet);
  873. uint32_t i;
  874. if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
  875. tt_len = primary_if->soft_iface->mtu - tt_query_size;
  876. tt_len -= tt_len % sizeof(struct tt_change);
  877. }
  878. tt_tot = tt_len / sizeof(struct tt_change);
  879. skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
  880. if (!skb)
  881. goto out;
  882. skb_reserve(skb, ETH_HLEN);
  883. tt_response = (struct tt_query_packet *)skb_put(skb,
  884. tt_query_size + tt_len);
  885. tt_response->ttvn = ttvn;
  886. tt_change = (struct tt_change *)(skb->data + tt_query_size);
  887. tt_count = 0;
  888. rcu_read_lock();
  889. for (i = 0; i < hash->size; i++) {
  890. head = &hash->table[i];
  891. hlist_for_each_entry_rcu(tt_common_entry, node,
  892. head, hash_entry) {
  893. if (tt_count == tt_tot)
  894. break;
  895. if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
  896. continue;
  897. memcpy(tt_change->addr, tt_common_entry->addr,
  898. ETH_ALEN);
  899. tt_change->flags = NO_FLAGS;
  900. tt_count++;
  901. tt_change++;
  902. }
  903. }
  904. rcu_read_unlock();
  905. /* store in the message the number of entries we have successfully
  906. * copied */
  907. tt_response->tt_data = htons(tt_count);
  908. out:
  909. return skb;
  910. }
  911. static int send_tt_request(struct bat_priv *bat_priv,
  912. struct orig_node *dst_orig_node,
  913. uint8_t ttvn, uint16_t tt_crc, bool full_table)
  914. {
  915. struct sk_buff *skb = NULL;
  916. struct tt_query_packet *tt_request;
  917. struct neigh_node *neigh_node = NULL;
  918. struct hard_iface *primary_if;
  919. struct tt_req_node *tt_req_node = NULL;
  920. int ret = 1;
  921. primary_if = primary_if_get_selected(bat_priv);
  922. if (!primary_if)
  923. goto out;
  924. /* The new tt_req will be issued only if I'm not waiting for a
  925. * reply from the same orig_node yet */
  926. tt_req_node = new_tt_req_node(bat_priv, dst_orig_node);
  927. if (!tt_req_node)
  928. goto out;
  929. skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
  930. if (!skb)
  931. goto out;
  932. skb_reserve(skb, ETH_HLEN);
  933. tt_request = (struct tt_query_packet *)skb_put(skb,
  934. sizeof(struct tt_query_packet));
  935. tt_request->header.packet_type = BAT_TT_QUERY;
  936. tt_request->header.version = COMPAT_VERSION;
  937. memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
  938. memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
  939. tt_request->header.ttl = TTL;
  940. tt_request->ttvn = ttvn;
  941. tt_request->tt_data = tt_crc;
  942. tt_request->flags = TT_REQUEST;
  943. if (full_table)
  944. tt_request->flags |= TT_FULL_TABLE;
  945. neigh_node = orig_node_get_router(dst_orig_node);
  946. if (!neigh_node)
  947. goto out;
  948. bat_dbg(DBG_TT, bat_priv,
  949. "Sending TT_REQUEST to %pM via %pM [%c]\n",
  950. dst_orig_node->orig, neigh_node->addr,
  951. (full_table ? 'F' : '.'));
  952. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  953. ret = 0;
  954. out:
  955. if (neigh_node)
  956. neigh_node_free_ref(neigh_node);
  957. if (primary_if)
  958. hardif_free_ref(primary_if);
  959. if (ret)
  960. kfree_skb(skb);
  961. if (ret && tt_req_node) {
  962. spin_lock_bh(&bat_priv->tt_req_list_lock);
  963. list_del(&tt_req_node->list);
  964. spin_unlock_bh(&bat_priv->tt_req_list_lock);
  965. kfree(tt_req_node);
  966. }
  967. return ret;
  968. }
  969. static bool send_other_tt_response(struct bat_priv *bat_priv,
  970. struct tt_query_packet *tt_request)
  971. {
  972. struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
  973. struct neigh_node *neigh_node = NULL;
  974. struct hard_iface *primary_if = NULL;
  975. uint8_t orig_ttvn, req_ttvn, ttvn;
  976. int ret = false;
  977. unsigned char *tt_buff;
  978. bool full_table;
  979. uint16_t tt_len, tt_tot;
  980. struct sk_buff *skb = NULL;
  981. struct tt_query_packet *tt_response;
  982. bat_dbg(DBG_TT, bat_priv,
  983. "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
  984. tt_request->src, tt_request->ttvn, tt_request->dst,
  985. (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
  986. /* Let's get the orig node of the REAL destination */
  987. req_dst_orig_node = orig_hash_find(bat_priv, tt_request->dst);
  988. if (!req_dst_orig_node)
  989. goto out;
  990. res_dst_orig_node = orig_hash_find(bat_priv, tt_request->src);
  991. if (!res_dst_orig_node)
  992. goto out;
  993. neigh_node = orig_node_get_router(res_dst_orig_node);
  994. if (!neigh_node)
  995. goto out;
  996. primary_if = primary_if_get_selected(bat_priv);
  997. if (!primary_if)
  998. goto out;
  999. orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
  1000. req_ttvn = tt_request->ttvn;
  1001. /* I don't have the requested data */
  1002. if (orig_ttvn != req_ttvn ||
  1003. tt_request->tt_data != req_dst_orig_node->tt_crc)
  1004. goto out;
  1005. /* If the full table has been explicitly requested */
  1006. if (tt_request->flags & TT_FULL_TABLE ||
  1007. !req_dst_orig_node->tt_buff)
  1008. full_table = true;
  1009. else
  1010. full_table = false;
  1011. /* In this version, fragmentation is not implemented, then
  1012. * I'll send only one packet with as much TT entries as I can */
  1013. if (!full_table) {
  1014. spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
  1015. tt_len = req_dst_orig_node->tt_buff_len;
  1016. tt_tot = tt_len / sizeof(struct tt_change);
  1017. skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
  1018. tt_len + ETH_HLEN);
  1019. if (!skb)
  1020. goto unlock;
  1021. skb_reserve(skb, ETH_HLEN);
  1022. tt_response = (struct tt_query_packet *)skb_put(skb,
  1023. sizeof(struct tt_query_packet) + tt_len);
  1024. tt_response->ttvn = req_ttvn;
  1025. tt_response->tt_data = htons(tt_tot);
  1026. tt_buff = skb->data + sizeof(struct tt_query_packet);
  1027. /* Copy the last orig_node's OGM buffer */
  1028. memcpy(tt_buff, req_dst_orig_node->tt_buff,
  1029. req_dst_orig_node->tt_buff_len);
  1030. spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
  1031. } else {
  1032. tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
  1033. sizeof(struct tt_change);
  1034. ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
  1035. skb = tt_response_fill_table(tt_len, ttvn,
  1036. bat_priv->tt_global_hash,
  1037. primary_if, tt_global_valid_entry,
  1038. req_dst_orig_node);
  1039. if (!skb)
  1040. goto out;
  1041. tt_response = (struct tt_query_packet *)skb->data;
  1042. }
  1043. tt_response->header.packet_type = BAT_TT_QUERY;
  1044. tt_response->header.version = COMPAT_VERSION;
  1045. tt_response->header.ttl = TTL;
  1046. memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
  1047. memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
  1048. tt_response->flags = TT_RESPONSE;
  1049. if (full_table)
  1050. tt_response->flags |= TT_FULL_TABLE;
  1051. bat_dbg(DBG_TT, bat_priv,
  1052. "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
  1053. res_dst_orig_node->orig, neigh_node->addr,
  1054. req_dst_orig_node->orig, req_ttvn);
  1055. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  1056. ret = true;
  1057. goto out;
  1058. unlock:
  1059. spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
  1060. out:
  1061. if (res_dst_orig_node)
  1062. orig_node_free_ref(res_dst_orig_node);
  1063. if (req_dst_orig_node)
  1064. orig_node_free_ref(req_dst_orig_node);
  1065. if (neigh_node)
  1066. neigh_node_free_ref(neigh_node);
  1067. if (primary_if)
  1068. hardif_free_ref(primary_if);
  1069. if (!ret)
  1070. kfree_skb(skb);
  1071. return ret;
  1072. }
  1073. static bool send_my_tt_response(struct bat_priv *bat_priv,
  1074. struct tt_query_packet *tt_request)
  1075. {
  1076. struct orig_node *orig_node = NULL;
  1077. struct neigh_node *neigh_node = NULL;
  1078. struct hard_iface *primary_if = NULL;
  1079. uint8_t my_ttvn, req_ttvn, ttvn;
  1080. int ret = false;
  1081. unsigned char *tt_buff;
  1082. bool full_table;
  1083. uint16_t tt_len, tt_tot;
  1084. struct sk_buff *skb = NULL;
  1085. struct tt_query_packet *tt_response;
  1086. bat_dbg(DBG_TT, bat_priv,
  1087. "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
  1088. tt_request->src, tt_request->ttvn,
  1089. (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
  1090. my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
  1091. req_ttvn = tt_request->ttvn;
  1092. orig_node = orig_hash_find(bat_priv, tt_request->src);
  1093. if (!orig_node)
  1094. goto out;
  1095. neigh_node = orig_node_get_router(orig_node);
  1096. if (!neigh_node)
  1097. goto out;
  1098. primary_if = primary_if_get_selected(bat_priv);
  1099. if (!primary_if)
  1100. goto out;
  1101. /* If the full table has been explicitly requested or the gap
  1102. * is too big send the whole local translation table */
  1103. if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
  1104. !bat_priv->tt_buff)
  1105. full_table = true;
  1106. else
  1107. full_table = false;
  1108. /* In this version, fragmentation is not implemented, then
  1109. * I'll send only one packet with as much TT entries as I can */
  1110. if (!full_table) {
  1111. spin_lock_bh(&bat_priv->tt_buff_lock);
  1112. tt_len = bat_priv->tt_buff_len;
  1113. tt_tot = tt_len / sizeof(struct tt_change);
  1114. skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
  1115. tt_len + ETH_HLEN);
  1116. if (!skb)
  1117. goto unlock;
  1118. skb_reserve(skb, ETH_HLEN);
  1119. tt_response = (struct tt_query_packet *)skb_put(skb,
  1120. sizeof(struct tt_query_packet) + tt_len);
  1121. tt_response->ttvn = req_ttvn;
  1122. tt_response->tt_data = htons(tt_tot);
  1123. tt_buff = skb->data + sizeof(struct tt_query_packet);
  1124. memcpy(tt_buff, bat_priv->tt_buff,
  1125. bat_priv->tt_buff_len);
  1126. spin_unlock_bh(&bat_priv->tt_buff_lock);
  1127. } else {
  1128. tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
  1129. sizeof(struct tt_change);
  1130. ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
  1131. skb = tt_response_fill_table(tt_len, ttvn,
  1132. bat_priv->tt_local_hash,
  1133. primary_if, tt_local_valid_entry,
  1134. NULL);
  1135. if (!skb)
  1136. goto out;
  1137. tt_response = (struct tt_query_packet *)skb->data;
  1138. }
  1139. tt_response->header.packet_type = BAT_TT_QUERY;
  1140. tt_response->header.version = COMPAT_VERSION;
  1141. tt_response->header.ttl = TTL;
  1142. memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
  1143. memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
  1144. tt_response->flags = TT_RESPONSE;
  1145. if (full_table)
  1146. tt_response->flags |= TT_FULL_TABLE;
  1147. bat_dbg(DBG_TT, bat_priv,
  1148. "Sending TT_RESPONSE to %pM via %pM [%c]\n",
  1149. orig_node->orig, neigh_node->addr,
  1150. (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
  1151. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  1152. ret = true;
  1153. goto out;
  1154. unlock:
  1155. spin_unlock_bh(&bat_priv->tt_buff_lock);
  1156. out:
  1157. if (orig_node)
  1158. orig_node_free_ref(orig_node);
  1159. if (neigh_node)
  1160. neigh_node_free_ref(neigh_node);
  1161. if (primary_if)
  1162. hardif_free_ref(primary_if);
  1163. if (!ret)
  1164. kfree_skb(skb);
  1165. /* This packet was for me, so it doesn't need to be re-routed */
  1166. return true;
  1167. }
  1168. bool send_tt_response(struct bat_priv *bat_priv,
  1169. struct tt_query_packet *tt_request)
  1170. {
  1171. if (is_my_mac(tt_request->dst))
  1172. return send_my_tt_response(bat_priv, tt_request);
  1173. else
  1174. return send_other_tt_response(bat_priv, tt_request);
  1175. }
  1176. static void _tt_update_changes(struct bat_priv *bat_priv,
  1177. struct orig_node *orig_node,
  1178. struct tt_change *tt_change,
  1179. uint16_t tt_num_changes, uint8_t ttvn)
  1180. {
  1181. int i;
  1182. for (i = 0; i < tt_num_changes; i++) {
  1183. if ((tt_change + i)->flags & TT_CLIENT_DEL)
  1184. tt_global_del(bat_priv, orig_node,
  1185. (tt_change + i)->addr,
  1186. "tt removed by changes",
  1187. (tt_change + i)->flags & TT_CLIENT_ROAM);
  1188. else
  1189. if (!tt_global_add(bat_priv, orig_node,
  1190. (tt_change + i)->addr, ttvn, false,
  1191. (tt_change + i)->flags &
  1192. TT_CLIENT_WIFI))
  1193. /* In case of problem while storing a
  1194. * global_entry, we stop the updating
  1195. * procedure without committing the
  1196. * ttvn change. This will avoid to send
  1197. * corrupted data on tt_request
  1198. */
  1199. return;
  1200. }
  1201. orig_node->tt_initialised = true;
  1202. }
  1203. static void tt_fill_gtable(struct bat_priv *bat_priv,
  1204. struct tt_query_packet *tt_response)
  1205. {
  1206. struct orig_node *orig_node = NULL;
  1207. orig_node = orig_hash_find(bat_priv, tt_response->src);
  1208. if (!orig_node)
  1209. goto out;
  1210. /* Purge the old table first.. */
  1211. tt_global_del_orig(bat_priv, orig_node, "Received full table");
  1212. _tt_update_changes(bat_priv, orig_node,
  1213. (struct tt_change *)(tt_response + 1),
  1214. tt_response->tt_data, tt_response->ttvn);
  1215. spin_lock_bh(&orig_node->tt_buff_lock);
  1216. kfree(orig_node->tt_buff);
  1217. orig_node->tt_buff_len = 0;
  1218. orig_node->tt_buff = NULL;
  1219. spin_unlock_bh(&orig_node->tt_buff_lock);
  1220. atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
  1221. out:
  1222. if (orig_node)
  1223. orig_node_free_ref(orig_node);
  1224. }
  1225. static void tt_update_changes(struct bat_priv *bat_priv,
  1226. struct orig_node *orig_node,
  1227. uint16_t tt_num_changes, uint8_t ttvn,
  1228. struct tt_change *tt_change)
  1229. {
  1230. _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes,
  1231. ttvn);
  1232. tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change,
  1233. tt_num_changes);
  1234. atomic_set(&orig_node->last_ttvn, ttvn);
  1235. }
  1236. bool is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
  1237. {
  1238. struct tt_local_entry *tt_local_entry = NULL;
  1239. bool ret = false;
  1240. tt_local_entry = tt_local_hash_find(bat_priv, addr);
  1241. if (!tt_local_entry)
  1242. goto out;
  1243. /* Check if the client has been logically deleted (but is kept for
  1244. * consistency purpose) */
  1245. if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
  1246. goto out;
  1247. ret = true;
  1248. out:
  1249. if (tt_local_entry)
  1250. tt_local_entry_free_ref(tt_local_entry);
  1251. return ret;
  1252. }
  1253. void handle_tt_response(struct bat_priv *bat_priv,
  1254. struct tt_query_packet *tt_response)
  1255. {
  1256. struct tt_req_node *node, *safe;
  1257. struct orig_node *orig_node = NULL;
  1258. bat_dbg(DBG_TT, bat_priv,
  1259. "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
  1260. tt_response->src, tt_response->ttvn, tt_response->tt_data,
  1261. (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
  1262. orig_node = orig_hash_find(bat_priv, tt_response->src);
  1263. if (!orig_node)
  1264. goto out;
  1265. if (tt_response->flags & TT_FULL_TABLE)
  1266. tt_fill_gtable(bat_priv, tt_response);
  1267. else
  1268. tt_update_changes(bat_priv, orig_node, tt_response->tt_data,
  1269. tt_response->ttvn,
  1270. (struct tt_change *)(tt_response + 1));
  1271. /* Delete the tt_req_node from pending tt_requests list */
  1272. spin_lock_bh(&bat_priv->tt_req_list_lock);
  1273. list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
  1274. if (!compare_eth(node->addr, tt_response->src))
  1275. continue;
  1276. list_del(&node->list);
  1277. kfree(node);
  1278. }
  1279. spin_unlock_bh(&bat_priv->tt_req_list_lock);
  1280. /* Recalculate the CRC for this orig_node and store it */
  1281. orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
  1282. /* Roaming phase is over: tables are in sync again. I can
  1283. * unset the flag */
  1284. orig_node->tt_poss_change = false;
  1285. out:
  1286. if (orig_node)
  1287. orig_node_free_ref(orig_node);
  1288. }
  1289. int tt_init(struct bat_priv *bat_priv)
  1290. {
  1291. if (!tt_local_init(bat_priv))
  1292. return 0;
  1293. if (!tt_global_init(bat_priv))
  1294. return 0;
  1295. tt_start_timer(bat_priv);
  1296. return 1;
  1297. }
  1298. static void tt_roam_list_free(struct bat_priv *bat_priv)
  1299. {
  1300. struct tt_roam_node *node, *safe;
  1301. spin_lock_bh(&bat_priv->tt_roam_list_lock);
  1302. list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
  1303. list_del(&node->list);
  1304. kfree(node);
  1305. }
  1306. spin_unlock_bh(&bat_priv->tt_roam_list_lock);
  1307. }
  1308. static void tt_roam_purge(struct bat_priv *bat_priv)
  1309. {
  1310. struct tt_roam_node *node, *safe;
  1311. spin_lock_bh(&bat_priv->tt_roam_list_lock);
  1312. list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
  1313. if (!has_timed_out(node->first_time, ROAMING_MAX_TIME))
  1314. continue;
  1315. list_del(&node->list);
  1316. kfree(node);
  1317. }
  1318. spin_unlock_bh(&bat_priv->tt_roam_list_lock);
  1319. }
  1320. /* This function checks whether the client already reached the
  1321. * maximum number of possible roaming phases. In this case the ROAMING_ADV
  1322. * will not be sent.
  1323. *
  1324. * returns true if the ROAMING_ADV can be sent, false otherwise */
  1325. static bool tt_check_roam_count(struct bat_priv *bat_priv,
  1326. uint8_t *client)
  1327. {
  1328. struct tt_roam_node *tt_roam_node;
  1329. bool ret = false;
  1330. spin_lock_bh(&bat_priv->tt_roam_list_lock);
  1331. /* The new tt_req will be issued only if I'm not waiting for a
  1332. * reply from the same orig_node yet */
  1333. list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
  1334. if (!compare_eth(tt_roam_node->addr, client))
  1335. continue;
  1336. if (has_timed_out(tt_roam_node->first_time, ROAMING_MAX_TIME))
  1337. continue;
  1338. if (!atomic_dec_not_zero(&tt_roam_node->counter))
  1339. /* Sorry, you roamed too many times! */
  1340. goto unlock;
  1341. ret = true;
  1342. break;
  1343. }
  1344. if (!ret) {
  1345. tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
  1346. if (!tt_roam_node)
  1347. goto unlock;
  1348. tt_roam_node->first_time = jiffies;
  1349. atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
  1350. memcpy(tt_roam_node->addr, client, ETH_ALEN);
  1351. list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
  1352. ret = true;
  1353. }
  1354. unlock:
  1355. spin_unlock_bh(&bat_priv->tt_roam_list_lock);
  1356. return ret;
  1357. }
  1358. void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
  1359. struct orig_node *orig_node)
  1360. {
  1361. struct neigh_node *neigh_node = NULL;
  1362. struct sk_buff *skb = NULL;
  1363. struct roam_adv_packet *roam_adv_packet;
  1364. int ret = 1;
  1365. struct hard_iface *primary_if;
  1366. /* before going on we have to check whether the client has
  1367. * already roamed to us too many times */
  1368. if (!tt_check_roam_count(bat_priv, client))
  1369. goto out;
  1370. skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
  1371. if (!skb)
  1372. goto out;
  1373. skb_reserve(skb, ETH_HLEN);
  1374. roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
  1375. sizeof(struct roam_adv_packet));
  1376. roam_adv_packet->header.packet_type = BAT_ROAM_ADV;
  1377. roam_adv_packet->header.version = COMPAT_VERSION;
  1378. roam_adv_packet->header.ttl = TTL;
  1379. primary_if = primary_if_get_selected(bat_priv);
  1380. if (!primary_if)
  1381. goto out;
  1382. memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
  1383. hardif_free_ref(primary_if);
  1384. memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
  1385. memcpy(roam_adv_packet->client, client, ETH_ALEN);
  1386. neigh_node = orig_node_get_router(orig_node);
  1387. if (!neigh_node)
  1388. goto out;
  1389. bat_dbg(DBG_TT, bat_priv,
  1390. "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
  1391. orig_node->orig, client, neigh_node->addr);
  1392. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  1393. ret = 0;
  1394. out:
  1395. if (neigh_node)
  1396. neigh_node_free_ref(neigh_node);
  1397. if (ret)
  1398. kfree_skb(skb);
  1399. return;
  1400. }
  1401. static void tt_purge(struct work_struct *work)
  1402. {
  1403. struct delayed_work *delayed_work =
  1404. container_of(work, struct delayed_work, work);
  1405. struct bat_priv *bat_priv =
  1406. container_of(delayed_work, struct bat_priv, tt_work);
  1407. tt_local_purge(bat_priv);
  1408. tt_global_roam_purge(bat_priv);
  1409. tt_req_purge(bat_priv);
  1410. tt_roam_purge(bat_priv);
  1411. tt_start_timer(bat_priv);
  1412. }
  1413. void tt_free(struct bat_priv *bat_priv)
  1414. {
  1415. cancel_delayed_work_sync(&bat_priv->tt_work);
  1416. tt_local_table_free(bat_priv);
  1417. tt_global_table_free(bat_priv);
  1418. tt_req_list_free(bat_priv);
  1419. tt_changes_list_free(bat_priv);
  1420. tt_roam_list_free(bat_priv);
  1421. kfree(bat_priv->tt_buff);
  1422. }
  1423. /* This function will enable or disable the specified flags for all the entries
  1424. * in the given hash table and returns the number of modified entries */
  1425. static uint16_t tt_set_flags(struct hashtable_t *hash, uint16_t flags,
  1426. bool enable)
  1427. {
  1428. uint32_t i;
  1429. uint16_t changed_num = 0;
  1430. struct hlist_head *head;
  1431. struct hlist_node *node;
  1432. struct tt_common_entry *tt_common_entry;
  1433. if (!hash)
  1434. goto out;
  1435. for (i = 0; i < hash->size; i++) {
  1436. head = &hash->table[i];
  1437. rcu_read_lock();
  1438. hlist_for_each_entry_rcu(tt_common_entry, node,
  1439. head, hash_entry) {
  1440. if (enable) {
  1441. if ((tt_common_entry->flags & flags) == flags)
  1442. continue;
  1443. tt_common_entry->flags |= flags;
  1444. } else {
  1445. if (!(tt_common_entry->flags & flags))
  1446. continue;
  1447. tt_common_entry->flags &= ~flags;
  1448. }
  1449. changed_num++;
  1450. }
  1451. rcu_read_unlock();
  1452. }
  1453. out:
  1454. return changed_num;
  1455. }
  1456. /* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
  1457. static void tt_local_purge_pending_clients(struct bat_priv *bat_priv)
  1458. {
  1459. struct hashtable_t *hash = bat_priv->tt_local_hash;
  1460. struct tt_common_entry *tt_common_entry;
  1461. struct tt_local_entry *tt_local_entry;
  1462. struct hlist_node *node, *node_tmp;
  1463. struct hlist_head *head;
  1464. spinlock_t *list_lock; /* protects write access to the hash lists */
  1465. uint32_t i;
  1466. if (!hash)
  1467. return;
  1468. for (i = 0; i < hash->size; i++) {
  1469. head = &hash->table[i];
  1470. list_lock = &hash->list_locks[i];
  1471. spin_lock_bh(list_lock);
  1472. hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
  1473. head, hash_entry) {
  1474. if (!(tt_common_entry->flags & TT_CLIENT_PENDING))
  1475. continue;
  1476. bat_dbg(DBG_TT, bat_priv,
  1477. "Deleting local tt entry (%pM): pending\n",
  1478. tt_common_entry->addr);
  1479. atomic_dec(&bat_priv->num_local_tt);
  1480. hlist_del_rcu(node);
  1481. tt_local_entry = container_of(tt_common_entry,
  1482. struct tt_local_entry,
  1483. common);
  1484. tt_local_entry_free_ref(tt_local_entry);
  1485. }
  1486. spin_unlock_bh(list_lock);
  1487. }
  1488. }
  1489. void tt_commit_changes(struct bat_priv *bat_priv)
  1490. {
  1491. uint16_t changed_num = tt_set_flags(bat_priv->tt_local_hash,
  1492. TT_CLIENT_NEW, false);
  1493. /* all the reset entries have now to be effectively counted as local
  1494. * entries */
  1495. atomic_add(changed_num, &bat_priv->num_local_tt);
  1496. tt_local_purge_pending_clients(bat_priv);
  1497. /* Increment the TTVN only once per OGM interval */
  1498. atomic_inc(&bat_priv->ttvn);
  1499. bat_priv->tt_poss_change = false;
  1500. }
  1501. bool is_ap_isolated(struct bat_priv *bat_priv, uint8_t *src, uint8_t *dst)
  1502. {
  1503. struct tt_local_entry *tt_local_entry = NULL;
  1504. struct tt_global_entry *tt_global_entry = NULL;
  1505. bool ret = false;
  1506. if (!atomic_read(&bat_priv->ap_isolation))
  1507. goto out;
  1508. tt_local_entry = tt_local_hash_find(bat_priv, dst);
  1509. if (!tt_local_entry)
  1510. goto out;
  1511. tt_global_entry = tt_global_hash_find(bat_priv, src);
  1512. if (!tt_global_entry)
  1513. goto out;
  1514. if (!_is_ap_isolated(tt_local_entry, tt_global_entry))
  1515. goto out;
  1516. ret = true;
  1517. out:
  1518. if (tt_global_entry)
  1519. tt_global_entry_free_ref(tt_global_entry);
  1520. if (tt_local_entry)
  1521. tt_local_entry_free_ref(tt_local_entry);
  1522. return ret;
  1523. }
  1524. void tt_update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node,
  1525. const unsigned char *tt_buff, uint8_t tt_num_changes,
  1526. uint8_t ttvn, uint16_t tt_crc)
  1527. {
  1528. uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  1529. bool full_table = true;
  1530. /* orig table not initialised AND first diff is in the OGM OR the ttvn
  1531. * increased by one -> we can apply the attached changes */
  1532. if ((!orig_node->tt_initialised && ttvn == 1) ||
  1533. ttvn - orig_ttvn == 1) {
  1534. /* the OGM could not contain the changes due to their size or
  1535. * because they have already been sent TT_OGM_APPEND_MAX times.
  1536. * In this case send a tt request */
  1537. if (!tt_num_changes) {
  1538. full_table = false;
  1539. goto request_table;
  1540. }
  1541. tt_update_changes(bat_priv, orig_node, tt_num_changes, ttvn,
  1542. (struct tt_change *)tt_buff);
  1543. /* Even if we received the precomputed crc with the OGM, we
  1544. * prefer to recompute it to spot any possible inconsistency
  1545. * in the global table */
  1546. orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
  1547. /* The ttvn alone is not enough to guarantee consistency
  1548. * because a single value could represent different states
  1549. * (due to the wrap around). Thus a node has to check whether
  1550. * the resulting table (after applying the changes) is still
  1551. * consistent or not. E.g. a node could disconnect while its
  1552. * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
  1553. * checking the CRC value is mandatory to detect the
  1554. * inconsistency */
  1555. if (orig_node->tt_crc != tt_crc)
  1556. goto request_table;
  1557. /* Roaming phase is over: tables are in sync again. I can
  1558. * unset the flag */
  1559. orig_node->tt_poss_change = false;
  1560. } else {
  1561. /* if we missed more than one change or our tables are not
  1562. * in sync anymore -> request fresh tt data */
  1563. if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
  1564. orig_node->tt_crc != tt_crc) {
  1565. request_table:
  1566. bat_dbg(DBG_TT, bat_priv,
  1567. "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
  1568. orig_node->orig, ttvn, orig_ttvn, tt_crc,
  1569. orig_node->tt_crc, tt_num_changes);
  1570. send_tt_request(bat_priv, orig_node, ttvn, tt_crc,
  1571. full_table);
  1572. return;
  1573. }
  1574. }
  1575. }