translation-table.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889
  1. /*
  2. * Copyright (C) 2007-2011 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 bool is_out_of_time(unsigned long starting_time, unsigned long timeout)
  93. {
  94. unsigned long deadline;
  95. deadline = starting_time + msecs_to_jiffies(timeout);
  96. return time_after(jiffies, deadline);
  97. }
  98. static void tt_local_entry_free_ref(struct tt_local_entry *tt_local_entry)
  99. {
  100. if (atomic_dec_and_test(&tt_local_entry->common.refcount))
  101. kfree_rcu(tt_local_entry, common.rcu);
  102. }
  103. static void tt_global_entry_free_rcu(struct rcu_head *rcu)
  104. {
  105. struct tt_common_entry *tt_common_entry;
  106. struct tt_global_entry *tt_global_entry;
  107. tt_common_entry = container_of(rcu, struct tt_common_entry, rcu);
  108. tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
  109. common);
  110. if (tt_global_entry->orig_node)
  111. orig_node_free_ref(tt_global_entry->orig_node);
  112. kfree(tt_global_entry);
  113. }
  114. static void tt_global_entry_free_ref(struct tt_global_entry *tt_global_entry)
  115. {
  116. if (atomic_dec_and_test(&tt_global_entry->common.refcount))
  117. call_rcu(&tt_global_entry->common.rcu,
  118. tt_global_entry_free_rcu);
  119. }
  120. static void tt_local_event(struct bat_priv *bat_priv, const uint8_t *addr,
  121. uint8_t flags)
  122. {
  123. struct tt_change_node *tt_change_node;
  124. tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
  125. if (!tt_change_node)
  126. return;
  127. tt_change_node->change.flags = flags;
  128. memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
  129. spin_lock_bh(&bat_priv->tt_changes_list_lock);
  130. /* track the change in the OGMinterval list */
  131. list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
  132. atomic_inc(&bat_priv->tt_local_changes);
  133. spin_unlock_bh(&bat_priv->tt_changes_list_lock);
  134. atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
  135. }
  136. int tt_len(int changes_num)
  137. {
  138. return changes_num * sizeof(struct tt_change);
  139. }
  140. static int tt_local_init(struct bat_priv *bat_priv)
  141. {
  142. if (bat_priv->tt_local_hash)
  143. return 1;
  144. bat_priv->tt_local_hash = hash_new(1024);
  145. if (!bat_priv->tt_local_hash)
  146. return 0;
  147. return 1;
  148. }
  149. void tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
  150. int ifindex)
  151. {
  152. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  153. struct tt_local_entry *tt_local_entry = NULL;
  154. struct tt_global_entry *tt_global_entry = NULL;
  155. int hash_added;
  156. tt_local_entry = tt_local_hash_find(bat_priv, addr);
  157. if (tt_local_entry) {
  158. tt_local_entry->last_seen = jiffies;
  159. goto out;
  160. }
  161. tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
  162. if (!tt_local_entry)
  163. goto out;
  164. bat_dbg(DBG_TT, bat_priv,
  165. "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
  166. (uint8_t)atomic_read(&bat_priv->ttvn));
  167. memcpy(tt_local_entry->common.addr, addr, ETH_ALEN);
  168. tt_local_entry->common.flags = NO_FLAGS;
  169. if (is_wifi_iface(ifindex))
  170. tt_local_entry->common.flags |= TT_CLIENT_WIFI;
  171. atomic_set(&tt_local_entry->common.refcount, 2);
  172. tt_local_entry->last_seen = jiffies;
  173. /* the batman interface mac address should never be purged */
  174. if (compare_eth(addr, soft_iface->dev_addr))
  175. tt_local_entry->common.flags |= TT_CLIENT_NOPURGE;
  176. hash_added = hash_add(bat_priv->tt_local_hash, compare_tt, choose_orig,
  177. &tt_local_entry->common,
  178. &tt_local_entry->common.hash_entry);
  179. if (unlikely(hash_added != 0)) {
  180. /* remove the reference for the hash */
  181. tt_local_entry_free_ref(tt_local_entry);
  182. goto out;
  183. }
  184. tt_local_event(bat_priv, addr, tt_local_entry->common.flags);
  185. /* The local entry has to be marked as NEW to avoid to send it in
  186. * a full table response going out before the next ttvn increment
  187. * (consistency check) */
  188. tt_local_entry->common.flags |= TT_CLIENT_NEW;
  189. /* remove address from global hash if present */
  190. tt_global_entry = tt_global_hash_find(bat_priv, addr);
  191. /* Check whether it is a roaming! */
  192. if (tt_global_entry) {
  193. /* This node is probably going to update its tt table */
  194. tt_global_entry->orig_node->tt_poss_change = true;
  195. /* The global entry has to be marked as PENDING and has to be
  196. * kept for consistency purpose */
  197. tt_global_entry->common.flags |= TT_CLIENT_PENDING;
  198. send_roam_adv(bat_priv, tt_global_entry->common.addr,
  199. tt_global_entry->orig_node);
  200. }
  201. out:
  202. if (tt_local_entry)
  203. tt_local_entry_free_ref(tt_local_entry);
  204. if (tt_global_entry)
  205. tt_global_entry_free_ref(tt_global_entry);
  206. }
  207. int tt_changes_fill_buffer(struct bat_priv *bat_priv,
  208. unsigned char *buff, int buff_len)
  209. {
  210. int count = 0, tot_changes = 0;
  211. struct tt_change_node *entry, *safe;
  212. if (buff_len > 0)
  213. tot_changes = buff_len / tt_len(1);
  214. spin_lock_bh(&bat_priv->tt_changes_list_lock);
  215. atomic_set(&bat_priv->tt_local_changes, 0);
  216. list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
  217. list) {
  218. if (count < tot_changes) {
  219. memcpy(buff + tt_len(count),
  220. &entry->change, sizeof(struct tt_change));
  221. count++;
  222. }
  223. list_del(&entry->list);
  224. kfree(entry);
  225. }
  226. spin_unlock_bh(&bat_priv->tt_changes_list_lock);
  227. /* Keep the buffer for possible tt_request */
  228. spin_lock_bh(&bat_priv->tt_buff_lock);
  229. kfree(bat_priv->tt_buff);
  230. bat_priv->tt_buff_len = 0;
  231. bat_priv->tt_buff = NULL;
  232. /* We check whether this new OGM has no changes due to size
  233. * problems */
  234. if (buff_len > 0) {
  235. /**
  236. * if kmalloc() fails we will reply with the full table
  237. * instead of providing the diff
  238. */
  239. bat_priv->tt_buff = kmalloc(buff_len, GFP_ATOMIC);
  240. if (bat_priv->tt_buff) {
  241. memcpy(bat_priv->tt_buff, buff, buff_len);
  242. bat_priv->tt_buff_len = buff_len;
  243. }
  244. }
  245. spin_unlock_bh(&bat_priv->tt_buff_lock);
  246. return tot_changes;
  247. }
  248. int tt_local_seq_print_text(struct seq_file *seq, void *offset)
  249. {
  250. struct net_device *net_dev = (struct net_device *)seq->private;
  251. struct bat_priv *bat_priv = netdev_priv(net_dev);
  252. struct hashtable_t *hash = bat_priv->tt_local_hash;
  253. struct tt_common_entry *tt_common_entry;
  254. struct hard_iface *primary_if;
  255. struct hlist_node *node;
  256. struct hlist_head *head;
  257. uint32_t i;
  258. int ret = 0;
  259. primary_if = primary_if_get_selected(bat_priv);
  260. if (!primary_if) {
  261. ret = seq_printf(seq, "BATMAN mesh %s disabled - "
  262. "please specify interfaces to enable it\n",
  263. net_dev->name);
  264. goto out;
  265. }
  266. if (primary_if->if_status != IF_ACTIVE) {
  267. ret = seq_printf(seq, "BATMAN mesh %s disabled - "
  268. "primary interface not active\n",
  269. net_dev->name);
  270. goto out;
  271. }
  272. seq_printf(seq, "Locally retrieved addresses (from %s) "
  273. "announced via TT (TTVN: %u):\n",
  274. net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
  275. for (i = 0; i < hash->size; i++) {
  276. head = &hash->table[i];
  277. rcu_read_lock();
  278. hlist_for_each_entry_rcu(tt_common_entry, node,
  279. head, hash_entry) {
  280. seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
  281. tt_common_entry->addr,
  282. (tt_common_entry->flags &
  283. TT_CLIENT_ROAM ? 'R' : '.'),
  284. (tt_common_entry->flags &
  285. TT_CLIENT_NOPURGE ? 'P' : '.'),
  286. (tt_common_entry->flags &
  287. TT_CLIENT_NEW ? 'N' : '.'),
  288. (tt_common_entry->flags &
  289. TT_CLIENT_PENDING ? 'X' : '.'),
  290. (tt_common_entry->flags &
  291. TT_CLIENT_WIFI ? 'W' : '.'));
  292. }
  293. rcu_read_unlock();
  294. }
  295. out:
  296. if (primary_if)
  297. hardif_free_ref(primary_if);
  298. return ret;
  299. }
  300. static void tt_local_set_pending(struct bat_priv *bat_priv,
  301. struct tt_local_entry *tt_local_entry,
  302. uint16_t flags)
  303. {
  304. tt_local_event(bat_priv, tt_local_entry->common.addr,
  305. tt_local_entry->common.flags | flags);
  306. /* The local client has to be marked as "pending to be removed" but has
  307. * to be kept in the table in order to send it in a full table
  308. * response issued before the net ttvn increment (consistency check) */
  309. tt_local_entry->common.flags |= TT_CLIENT_PENDING;
  310. }
  311. void tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
  312. const char *message, bool roaming)
  313. {
  314. struct tt_local_entry *tt_local_entry = NULL;
  315. tt_local_entry = tt_local_hash_find(bat_priv, addr);
  316. if (!tt_local_entry)
  317. goto out;
  318. tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL |
  319. (roaming ? TT_CLIENT_ROAM : NO_FLAGS));
  320. bat_dbg(DBG_TT, bat_priv, "Local tt entry (%pM) pending to be removed: "
  321. "%s\n", tt_local_entry->common.addr, message);
  322. out:
  323. if (tt_local_entry)
  324. tt_local_entry_free_ref(tt_local_entry);
  325. }
  326. static void tt_local_purge(struct bat_priv *bat_priv)
  327. {
  328. struct hashtable_t *hash = bat_priv->tt_local_hash;
  329. struct tt_local_entry *tt_local_entry;
  330. struct tt_common_entry *tt_common_entry;
  331. struct hlist_node *node, *node_tmp;
  332. struct hlist_head *head;
  333. spinlock_t *list_lock; /* protects write access to the hash lists */
  334. uint32_t i;
  335. for (i = 0; i < hash->size; i++) {
  336. head = &hash->table[i];
  337. list_lock = &hash->list_locks[i];
  338. spin_lock_bh(list_lock);
  339. hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
  340. head, hash_entry) {
  341. tt_local_entry = container_of(tt_common_entry,
  342. struct tt_local_entry,
  343. common);
  344. if (tt_local_entry->common.flags & TT_CLIENT_NOPURGE)
  345. continue;
  346. /* entry already marked for deletion */
  347. if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
  348. continue;
  349. if (!is_out_of_time(tt_local_entry->last_seen,
  350. TT_LOCAL_TIMEOUT * 1000))
  351. continue;
  352. tt_local_set_pending(bat_priv, tt_local_entry,
  353. TT_CLIENT_DEL);
  354. bat_dbg(DBG_TT, bat_priv, "Local tt entry (%pM) "
  355. "pending to be removed: timed out\n",
  356. tt_local_entry->common.addr);
  357. }
  358. spin_unlock_bh(list_lock);
  359. }
  360. }
  361. static void tt_local_table_free(struct bat_priv *bat_priv)
  362. {
  363. struct hashtable_t *hash;
  364. spinlock_t *list_lock; /* protects write access to the hash lists */
  365. struct tt_common_entry *tt_common_entry;
  366. struct tt_local_entry *tt_local_entry;
  367. struct hlist_node *node, *node_tmp;
  368. struct hlist_head *head;
  369. uint32_t i;
  370. if (!bat_priv->tt_local_hash)
  371. return;
  372. hash = bat_priv->tt_local_hash;
  373. for (i = 0; i < hash->size; i++) {
  374. head = &hash->table[i];
  375. list_lock = &hash->list_locks[i];
  376. spin_lock_bh(list_lock);
  377. hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
  378. head, hash_entry) {
  379. hlist_del_rcu(node);
  380. tt_local_entry = container_of(tt_common_entry,
  381. struct tt_local_entry,
  382. common);
  383. tt_local_entry_free_ref(tt_local_entry);
  384. }
  385. spin_unlock_bh(list_lock);
  386. }
  387. hash_destroy(hash);
  388. bat_priv->tt_local_hash = NULL;
  389. }
  390. static int tt_global_init(struct bat_priv *bat_priv)
  391. {
  392. if (bat_priv->tt_global_hash)
  393. return 1;
  394. bat_priv->tt_global_hash = hash_new(1024);
  395. if (!bat_priv->tt_global_hash)
  396. return 0;
  397. return 1;
  398. }
  399. static void tt_changes_list_free(struct bat_priv *bat_priv)
  400. {
  401. struct tt_change_node *entry, *safe;
  402. spin_lock_bh(&bat_priv->tt_changes_list_lock);
  403. list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
  404. list) {
  405. list_del(&entry->list);
  406. kfree(entry);
  407. }
  408. atomic_set(&bat_priv->tt_local_changes, 0);
  409. spin_unlock_bh(&bat_priv->tt_changes_list_lock);
  410. }
  411. /* caller must hold orig_node refcount */
  412. int tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
  413. const unsigned char *tt_addr, uint8_t ttvn, bool roaming,
  414. bool wifi)
  415. {
  416. struct tt_global_entry *tt_global_entry;
  417. struct orig_node *orig_node_tmp;
  418. int ret = 0;
  419. int hash_added;
  420. tt_global_entry = tt_global_hash_find(bat_priv, tt_addr);
  421. if (!tt_global_entry) {
  422. tt_global_entry =
  423. kmalloc(sizeof(*tt_global_entry),
  424. GFP_ATOMIC);
  425. if (!tt_global_entry)
  426. goto out;
  427. memcpy(tt_global_entry->common.addr, tt_addr, ETH_ALEN);
  428. tt_global_entry->common.flags = NO_FLAGS;
  429. atomic_set(&tt_global_entry->common.refcount, 2);
  430. /* Assign the new orig_node */
  431. atomic_inc(&orig_node->refcount);
  432. tt_global_entry->orig_node = orig_node;
  433. tt_global_entry->ttvn = ttvn;
  434. tt_global_entry->roam_at = 0;
  435. hash_added = hash_add(bat_priv->tt_global_hash, compare_tt,
  436. choose_orig, &tt_global_entry->common,
  437. &tt_global_entry->common.hash_entry);
  438. if (unlikely(hash_added != 0)) {
  439. /* remove the reference for the hash */
  440. tt_global_entry_free_ref(tt_global_entry);
  441. goto out_remove;
  442. }
  443. atomic_inc(&orig_node->tt_size);
  444. } else {
  445. if (tt_global_entry->orig_node != orig_node) {
  446. atomic_dec(&tt_global_entry->orig_node->tt_size);
  447. orig_node_tmp = tt_global_entry->orig_node;
  448. atomic_inc(&orig_node->refcount);
  449. tt_global_entry->orig_node = orig_node;
  450. orig_node_free_ref(orig_node_tmp);
  451. atomic_inc(&orig_node->tt_size);
  452. }
  453. tt_global_entry->common.flags = NO_FLAGS;
  454. tt_global_entry->ttvn = ttvn;
  455. tt_global_entry->roam_at = 0;
  456. }
  457. if (wifi)
  458. tt_global_entry->common.flags |= TT_CLIENT_WIFI;
  459. bat_dbg(DBG_TT, bat_priv,
  460. "Creating new global tt entry: %pM (via %pM)\n",
  461. tt_global_entry->common.addr, orig_node->orig);
  462. out_remove:
  463. /* remove address from local hash if present */
  464. tt_local_remove(bat_priv, tt_global_entry->common.addr,
  465. "global tt received", roaming);
  466. ret = 1;
  467. out:
  468. if (tt_global_entry)
  469. tt_global_entry_free_ref(tt_global_entry);
  470. return ret;
  471. }
  472. int tt_global_seq_print_text(struct seq_file *seq, void *offset)
  473. {
  474. struct net_device *net_dev = (struct net_device *)seq->private;
  475. struct bat_priv *bat_priv = netdev_priv(net_dev);
  476. struct hashtable_t *hash = bat_priv->tt_global_hash;
  477. struct tt_common_entry *tt_common_entry;
  478. struct tt_global_entry *tt_global_entry;
  479. struct hard_iface *primary_if;
  480. struct hlist_node *node;
  481. struct hlist_head *head;
  482. uint32_t i;
  483. int ret = 0;
  484. primary_if = primary_if_get_selected(bat_priv);
  485. if (!primary_if) {
  486. ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
  487. "specify interfaces to enable it\n",
  488. net_dev->name);
  489. goto out;
  490. }
  491. if (primary_if->if_status != IF_ACTIVE) {
  492. ret = seq_printf(seq, "BATMAN mesh %s disabled - "
  493. "primary interface not active\n",
  494. net_dev->name);
  495. goto out;
  496. }
  497. seq_printf(seq,
  498. "Globally announced TT entries received via the mesh %s\n",
  499. net_dev->name);
  500. seq_printf(seq, " %-13s %s %-15s %s %s\n",
  501. "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
  502. for (i = 0; i < hash->size; i++) {
  503. head = &hash->table[i];
  504. rcu_read_lock();
  505. hlist_for_each_entry_rcu(tt_common_entry, node,
  506. head, hash_entry) {
  507. tt_global_entry = container_of(tt_common_entry,
  508. struct tt_global_entry,
  509. common);
  510. seq_printf(seq, " * %pM (%3u) via %pM (%3u) "
  511. "[%c%c%c]\n",
  512. tt_global_entry->common.addr,
  513. tt_global_entry->ttvn,
  514. tt_global_entry->orig_node->orig,
  515. (uint8_t) atomic_read(
  516. &tt_global_entry->orig_node->
  517. last_ttvn),
  518. (tt_global_entry->common.flags &
  519. TT_CLIENT_ROAM ? 'R' : '.'),
  520. (tt_global_entry->common.flags &
  521. TT_CLIENT_PENDING ? 'X' : '.'),
  522. (tt_global_entry->common.flags &
  523. TT_CLIENT_WIFI ? 'W' : '.'));
  524. }
  525. rcu_read_unlock();
  526. }
  527. out:
  528. if (primary_if)
  529. hardif_free_ref(primary_if);
  530. return ret;
  531. }
  532. static void _tt_global_del(struct bat_priv *bat_priv,
  533. struct tt_global_entry *tt_global_entry,
  534. const char *message)
  535. {
  536. if (!tt_global_entry)
  537. goto out;
  538. bat_dbg(DBG_TT, bat_priv,
  539. "Deleting global tt entry %pM (via %pM): %s\n",
  540. tt_global_entry->common.addr, tt_global_entry->orig_node->orig,
  541. message);
  542. atomic_dec(&tt_global_entry->orig_node->tt_size);
  543. hash_remove(bat_priv->tt_global_hash, compare_tt, choose_orig,
  544. tt_global_entry->common.addr);
  545. out:
  546. if (tt_global_entry)
  547. tt_global_entry_free_ref(tt_global_entry);
  548. }
  549. void tt_global_del(struct bat_priv *bat_priv,
  550. struct orig_node *orig_node, const unsigned char *addr,
  551. const char *message, bool roaming)
  552. {
  553. struct tt_global_entry *tt_global_entry = NULL;
  554. tt_global_entry = tt_global_hash_find(bat_priv, addr);
  555. if (!tt_global_entry)
  556. goto out;
  557. if (tt_global_entry->orig_node == orig_node) {
  558. if (roaming) {
  559. tt_global_entry->common.flags |= TT_CLIENT_ROAM;
  560. tt_global_entry->roam_at = jiffies;
  561. goto out;
  562. }
  563. _tt_global_del(bat_priv, tt_global_entry, message);
  564. }
  565. out:
  566. if (tt_global_entry)
  567. tt_global_entry_free_ref(tt_global_entry);
  568. }
  569. void tt_global_del_orig(struct bat_priv *bat_priv,
  570. struct orig_node *orig_node, const char *message)
  571. {
  572. struct tt_global_entry *tt_global_entry;
  573. struct tt_common_entry *tt_common_entry;
  574. uint32_t i;
  575. struct hashtable_t *hash = bat_priv->tt_global_hash;
  576. struct hlist_node *node, *safe;
  577. struct hlist_head *head;
  578. spinlock_t *list_lock; /* protects write access to the hash lists */
  579. if (!hash)
  580. return;
  581. for (i = 0; i < hash->size; i++) {
  582. head = &hash->table[i];
  583. list_lock = &hash->list_locks[i];
  584. spin_lock_bh(list_lock);
  585. hlist_for_each_entry_safe(tt_common_entry, node, safe,
  586. head, hash_entry) {
  587. tt_global_entry = container_of(tt_common_entry,
  588. struct tt_global_entry,
  589. common);
  590. if (tt_global_entry->orig_node == orig_node) {
  591. bat_dbg(DBG_TT, bat_priv,
  592. "Deleting global tt entry %pM "
  593. "(via %pM): %s\n",
  594. tt_global_entry->common.addr,
  595. tt_global_entry->orig_node->orig,
  596. message);
  597. hlist_del_rcu(node);
  598. tt_global_entry_free_ref(tt_global_entry);
  599. }
  600. }
  601. spin_unlock_bh(list_lock);
  602. }
  603. atomic_set(&orig_node->tt_size, 0);
  604. }
  605. static void tt_global_roam_purge(struct bat_priv *bat_priv)
  606. {
  607. struct hashtable_t *hash = bat_priv->tt_global_hash;
  608. struct tt_common_entry *tt_common_entry;
  609. struct tt_global_entry *tt_global_entry;
  610. struct hlist_node *node, *node_tmp;
  611. struct hlist_head *head;
  612. spinlock_t *list_lock; /* protects write access to the hash lists */
  613. uint32_t i;
  614. for (i = 0; i < hash->size; i++) {
  615. head = &hash->table[i];
  616. list_lock = &hash->list_locks[i];
  617. spin_lock_bh(list_lock);
  618. hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
  619. head, hash_entry) {
  620. tt_global_entry = container_of(tt_common_entry,
  621. struct tt_global_entry,
  622. common);
  623. if (!(tt_global_entry->common.flags & TT_CLIENT_ROAM))
  624. continue;
  625. if (!is_out_of_time(tt_global_entry->roam_at,
  626. TT_CLIENT_ROAM_TIMEOUT * 1000))
  627. continue;
  628. bat_dbg(DBG_TT, bat_priv, "Deleting global "
  629. "tt entry (%pM): Roaming timeout\n",
  630. tt_global_entry->common.addr);
  631. atomic_dec(&tt_global_entry->orig_node->tt_size);
  632. hlist_del_rcu(node);
  633. tt_global_entry_free_ref(tt_global_entry);
  634. }
  635. spin_unlock_bh(list_lock);
  636. }
  637. }
  638. static void tt_global_table_free(struct bat_priv *bat_priv)
  639. {
  640. struct hashtable_t *hash;
  641. spinlock_t *list_lock; /* protects write access to the hash lists */
  642. struct tt_common_entry *tt_common_entry;
  643. struct tt_global_entry *tt_global_entry;
  644. struct hlist_node *node, *node_tmp;
  645. struct hlist_head *head;
  646. uint32_t i;
  647. if (!bat_priv->tt_global_hash)
  648. return;
  649. hash = bat_priv->tt_global_hash;
  650. for (i = 0; i < hash->size; i++) {
  651. head = &hash->table[i];
  652. list_lock = &hash->list_locks[i];
  653. spin_lock_bh(list_lock);
  654. hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
  655. head, hash_entry) {
  656. hlist_del_rcu(node);
  657. tt_global_entry = container_of(tt_common_entry,
  658. struct tt_global_entry,
  659. common);
  660. tt_global_entry_free_ref(tt_global_entry);
  661. }
  662. spin_unlock_bh(list_lock);
  663. }
  664. hash_destroy(hash);
  665. bat_priv->tt_global_hash = NULL;
  666. }
  667. static bool _is_ap_isolated(struct tt_local_entry *tt_local_entry,
  668. struct tt_global_entry *tt_global_entry)
  669. {
  670. bool ret = false;
  671. if (tt_local_entry->common.flags & TT_CLIENT_WIFI &&
  672. tt_global_entry->common.flags & TT_CLIENT_WIFI)
  673. ret = true;
  674. return ret;
  675. }
  676. struct orig_node *transtable_search(struct bat_priv *bat_priv,
  677. const uint8_t *src, const uint8_t *addr)
  678. {
  679. struct tt_local_entry *tt_local_entry = NULL;
  680. struct tt_global_entry *tt_global_entry = NULL;
  681. struct orig_node *orig_node = NULL;
  682. if (src && atomic_read(&bat_priv->ap_isolation)) {
  683. tt_local_entry = tt_local_hash_find(bat_priv, src);
  684. if (!tt_local_entry)
  685. goto out;
  686. }
  687. tt_global_entry = tt_global_hash_find(bat_priv, addr);
  688. if (!tt_global_entry)
  689. goto out;
  690. /* check whether the clients should not communicate due to AP
  691. * isolation */
  692. if (tt_local_entry && _is_ap_isolated(tt_local_entry, tt_global_entry))
  693. goto out;
  694. if (!atomic_inc_not_zero(&tt_global_entry->orig_node->refcount))
  695. goto out;
  696. /* A global client marked as PENDING has already moved from that
  697. * originator */
  698. if (tt_global_entry->common.flags & TT_CLIENT_PENDING)
  699. goto out;
  700. orig_node = tt_global_entry->orig_node;
  701. out:
  702. if (tt_global_entry)
  703. tt_global_entry_free_ref(tt_global_entry);
  704. if (tt_local_entry)
  705. tt_local_entry_free_ref(tt_local_entry);
  706. return orig_node;
  707. }
  708. /* Calculates the checksum of the local table of a given orig_node */
  709. uint16_t tt_global_crc(struct bat_priv *bat_priv, struct orig_node *orig_node)
  710. {
  711. uint16_t total = 0, total_one;
  712. struct hashtable_t *hash = bat_priv->tt_global_hash;
  713. struct tt_common_entry *tt_common_entry;
  714. struct tt_global_entry *tt_global_entry;
  715. struct hlist_node *node;
  716. struct hlist_head *head;
  717. uint32_t i;
  718. int j;
  719. for (i = 0; i < hash->size; i++) {
  720. head = &hash->table[i];
  721. rcu_read_lock();
  722. hlist_for_each_entry_rcu(tt_common_entry, node,
  723. head, hash_entry) {
  724. tt_global_entry = container_of(tt_common_entry,
  725. struct tt_global_entry,
  726. common);
  727. if (compare_eth(tt_global_entry->orig_node,
  728. orig_node)) {
  729. /* Roaming clients are in the global table for
  730. * consistency only. They don't have to be
  731. * taken into account while computing the
  732. * global crc */
  733. if (tt_common_entry->flags & TT_CLIENT_ROAM)
  734. continue;
  735. total_one = 0;
  736. for (j = 0; j < ETH_ALEN; j++)
  737. total_one = crc16_byte(total_one,
  738. tt_common_entry->addr[j]);
  739. total ^= total_one;
  740. }
  741. }
  742. rcu_read_unlock();
  743. }
  744. return total;
  745. }
  746. /* Calculates the checksum of the local table */
  747. uint16_t tt_local_crc(struct bat_priv *bat_priv)
  748. {
  749. uint16_t total = 0, total_one;
  750. struct hashtable_t *hash = bat_priv->tt_local_hash;
  751. struct tt_common_entry *tt_common_entry;
  752. struct hlist_node *node;
  753. struct hlist_head *head;
  754. uint32_t i;
  755. int j;
  756. for (i = 0; i < hash->size; i++) {
  757. head = &hash->table[i];
  758. rcu_read_lock();
  759. hlist_for_each_entry_rcu(tt_common_entry, node,
  760. head, hash_entry) {
  761. /* not yet committed clients have not to be taken into
  762. * account while computing the CRC */
  763. if (tt_common_entry->flags & TT_CLIENT_NEW)
  764. continue;
  765. total_one = 0;
  766. for (j = 0; j < ETH_ALEN; j++)
  767. total_one = crc16_byte(total_one,
  768. tt_common_entry->addr[j]);
  769. total ^= total_one;
  770. }
  771. rcu_read_unlock();
  772. }
  773. return total;
  774. }
  775. static void tt_req_list_free(struct bat_priv *bat_priv)
  776. {
  777. struct tt_req_node *node, *safe;
  778. spin_lock_bh(&bat_priv->tt_req_list_lock);
  779. list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
  780. list_del(&node->list);
  781. kfree(node);
  782. }
  783. spin_unlock_bh(&bat_priv->tt_req_list_lock);
  784. }
  785. void tt_save_orig_buffer(struct bat_priv *bat_priv, struct orig_node *orig_node,
  786. const unsigned char *tt_buff, uint8_t tt_num_changes)
  787. {
  788. uint16_t tt_buff_len = tt_len(tt_num_changes);
  789. /* Replace the old buffer only if I received something in the
  790. * last OGM (the OGM could carry no changes) */
  791. spin_lock_bh(&orig_node->tt_buff_lock);
  792. if (tt_buff_len > 0) {
  793. kfree(orig_node->tt_buff);
  794. orig_node->tt_buff_len = 0;
  795. orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
  796. if (orig_node->tt_buff) {
  797. memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
  798. orig_node->tt_buff_len = tt_buff_len;
  799. }
  800. }
  801. spin_unlock_bh(&orig_node->tt_buff_lock);
  802. }
  803. static void tt_req_purge(struct bat_priv *bat_priv)
  804. {
  805. struct tt_req_node *node, *safe;
  806. spin_lock_bh(&bat_priv->tt_req_list_lock);
  807. list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
  808. if (is_out_of_time(node->issued_at,
  809. TT_REQUEST_TIMEOUT * 1000)) {
  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. !is_out_of_time(tt_req_node_tmp->issued_at,
  826. TT_REQUEST_TIMEOUT * 1000))
  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->packet_type = BAT_TT_QUERY;
  936. tt_request->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->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, "Sending TT_REQUEST to %pM via %pM "
  949. "[%c]\n", dst_orig_node->orig, neigh_node->addr,
  950. (full_table ? 'F' : '.'));
  951. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  952. ret = 0;
  953. out:
  954. if (neigh_node)
  955. neigh_node_free_ref(neigh_node);
  956. if (primary_if)
  957. hardif_free_ref(primary_if);
  958. if (ret)
  959. kfree_skb(skb);
  960. if (ret && tt_req_node) {
  961. spin_lock_bh(&bat_priv->tt_req_list_lock);
  962. list_del(&tt_req_node->list);
  963. spin_unlock_bh(&bat_priv->tt_req_list_lock);
  964. kfree(tt_req_node);
  965. }
  966. return ret;
  967. }
  968. static bool send_other_tt_response(struct bat_priv *bat_priv,
  969. struct tt_query_packet *tt_request)
  970. {
  971. struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
  972. struct neigh_node *neigh_node = NULL;
  973. struct hard_iface *primary_if = NULL;
  974. uint8_t orig_ttvn, req_ttvn, ttvn;
  975. int ret = false;
  976. unsigned char *tt_buff;
  977. bool full_table;
  978. uint16_t tt_len, tt_tot;
  979. struct sk_buff *skb = NULL;
  980. struct tt_query_packet *tt_response;
  981. bat_dbg(DBG_TT, bat_priv,
  982. "Received TT_REQUEST from %pM for "
  983. "ttvn: %u (%pM) [%c]\n", tt_request->src,
  984. 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->packet_type = BAT_TT_QUERY;
  1044. tt_response->version = COMPAT_VERSION;
  1045. tt_response->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 "
  1088. "ttvn: %u (me) [%c]\n", tt_request->src,
  1089. tt_request->ttvn,
  1090. (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
  1091. my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
  1092. req_ttvn = tt_request->ttvn;
  1093. orig_node = orig_hash_find(bat_priv, tt_request->src);
  1094. if (!orig_node)
  1095. goto out;
  1096. neigh_node = orig_node_get_router(orig_node);
  1097. if (!neigh_node)
  1098. goto out;
  1099. primary_if = primary_if_get_selected(bat_priv);
  1100. if (!primary_if)
  1101. goto out;
  1102. /* If the full table has been explicitly requested or the gap
  1103. * is too big send the whole local translation table */
  1104. if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
  1105. !bat_priv->tt_buff)
  1106. full_table = true;
  1107. else
  1108. full_table = false;
  1109. /* In this version, fragmentation is not implemented, then
  1110. * I'll send only one packet with as much TT entries as I can */
  1111. if (!full_table) {
  1112. spin_lock_bh(&bat_priv->tt_buff_lock);
  1113. tt_len = bat_priv->tt_buff_len;
  1114. tt_tot = tt_len / sizeof(struct tt_change);
  1115. skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
  1116. tt_len + ETH_HLEN);
  1117. if (!skb)
  1118. goto unlock;
  1119. skb_reserve(skb, ETH_HLEN);
  1120. tt_response = (struct tt_query_packet *)skb_put(skb,
  1121. sizeof(struct tt_query_packet) + tt_len);
  1122. tt_response->ttvn = req_ttvn;
  1123. tt_response->tt_data = htons(tt_tot);
  1124. tt_buff = skb->data + sizeof(struct tt_query_packet);
  1125. memcpy(tt_buff, bat_priv->tt_buff,
  1126. bat_priv->tt_buff_len);
  1127. spin_unlock_bh(&bat_priv->tt_buff_lock);
  1128. } else {
  1129. tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
  1130. sizeof(struct tt_change);
  1131. ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
  1132. skb = tt_response_fill_table(tt_len, ttvn,
  1133. bat_priv->tt_local_hash,
  1134. primary_if, tt_local_valid_entry,
  1135. NULL);
  1136. if (!skb)
  1137. goto out;
  1138. tt_response = (struct tt_query_packet *)skb->data;
  1139. }
  1140. tt_response->packet_type = BAT_TT_QUERY;
  1141. tt_response->version = COMPAT_VERSION;
  1142. tt_response->ttl = TTL;
  1143. memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
  1144. memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
  1145. tt_response->flags = TT_RESPONSE;
  1146. if (full_table)
  1147. tt_response->flags |= TT_FULL_TABLE;
  1148. bat_dbg(DBG_TT, bat_priv,
  1149. "Sending TT_RESPONSE to %pM via %pM [%c]\n",
  1150. orig_node->orig, neigh_node->addr,
  1151. (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
  1152. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  1153. ret = true;
  1154. goto out;
  1155. unlock:
  1156. spin_unlock_bh(&bat_priv->tt_buff_lock);
  1157. out:
  1158. if (orig_node)
  1159. orig_node_free_ref(orig_node);
  1160. if (neigh_node)
  1161. neigh_node_free_ref(neigh_node);
  1162. if (primary_if)
  1163. hardif_free_ref(primary_if);
  1164. if (!ret)
  1165. kfree_skb(skb);
  1166. /* This packet was for me, so it doesn't need to be re-routed */
  1167. return true;
  1168. }
  1169. bool send_tt_response(struct bat_priv *bat_priv,
  1170. struct tt_query_packet *tt_request)
  1171. {
  1172. if (is_my_mac(tt_request->dst))
  1173. return send_my_tt_response(bat_priv, tt_request);
  1174. else
  1175. return send_other_tt_response(bat_priv, tt_request);
  1176. }
  1177. static void _tt_update_changes(struct bat_priv *bat_priv,
  1178. struct orig_node *orig_node,
  1179. struct tt_change *tt_change,
  1180. uint16_t tt_num_changes, uint8_t ttvn)
  1181. {
  1182. int i;
  1183. for (i = 0; i < tt_num_changes; i++) {
  1184. if ((tt_change + i)->flags & TT_CLIENT_DEL)
  1185. tt_global_del(bat_priv, orig_node,
  1186. (tt_change + i)->addr,
  1187. "tt removed by changes",
  1188. (tt_change + i)->flags & TT_CLIENT_ROAM);
  1189. else
  1190. if (!tt_global_add(bat_priv, orig_node,
  1191. (tt_change + i)->addr, ttvn, false,
  1192. (tt_change + i)->flags &
  1193. TT_CLIENT_WIFI))
  1194. /* In case of problem while storing a
  1195. * global_entry, we stop the updating
  1196. * procedure without committing the
  1197. * ttvn change. This will avoid to send
  1198. * corrupted data on tt_request
  1199. */
  1200. return;
  1201. }
  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, "Received TT_RESPONSE from %pM for "
  1259. "ttvn %d t_size: %d [%c]\n",
  1260. tt_response->src, tt_response->ttvn,
  1261. tt_response->tt_data,
  1262. (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
  1263. orig_node = orig_hash_find(bat_priv, tt_response->src);
  1264. if (!orig_node)
  1265. goto out;
  1266. if (tt_response->flags & TT_FULL_TABLE)
  1267. tt_fill_gtable(bat_priv, tt_response);
  1268. else
  1269. tt_update_changes(bat_priv, orig_node, tt_response->tt_data,
  1270. tt_response->ttvn,
  1271. (struct tt_change *)(tt_response + 1));
  1272. /* Delete the tt_req_node from pending tt_requests list */
  1273. spin_lock_bh(&bat_priv->tt_req_list_lock);
  1274. list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
  1275. if (!compare_eth(node->addr, tt_response->src))
  1276. continue;
  1277. list_del(&node->list);
  1278. kfree(node);
  1279. }
  1280. spin_unlock_bh(&bat_priv->tt_req_list_lock);
  1281. /* Recalculate the CRC for this orig_node and store it */
  1282. orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
  1283. /* Roaming phase is over: tables are in sync again. I can
  1284. * unset the flag */
  1285. orig_node->tt_poss_change = false;
  1286. out:
  1287. if (orig_node)
  1288. orig_node_free_ref(orig_node);
  1289. }
  1290. int tt_init(struct bat_priv *bat_priv)
  1291. {
  1292. if (!tt_local_init(bat_priv))
  1293. return 0;
  1294. if (!tt_global_init(bat_priv))
  1295. return 0;
  1296. tt_start_timer(bat_priv);
  1297. return 1;
  1298. }
  1299. static void tt_roam_list_free(struct bat_priv *bat_priv)
  1300. {
  1301. struct tt_roam_node *node, *safe;
  1302. spin_lock_bh(&bat_priv->tt_roam_list_lock);
  1303. list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
  1304. list_del(&node->list);
  1305. kfree(node);
  1306. }
  1307. spin_unlock_bh(&bat_priv->tt_roam_list_lock);
  1308. }
  1309. static void tt_roam_purge(struct bat_priv *bat_priv)
  1310. {
  1311. struct tt_roam_node *node, *safe;
  1312. spin_lock_bh(&bat_priv->tt_roam_list_lock);
  1313. list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
  1314. if (!is_out_of_time(node->first_time,
  1315. ROAMING_MAX_TIME * 1000))
  1316. continue;
  1317. list_del(&node->list);
  1318. kfree(node);
  1319. }
  1320. spin_unlock_bh(&bat_priv->tt_roam_list_lock);
  1321. }
  1322. /* This function checks whether the client already reached the
  1323. * maximum number of possible roaming phases. In this case the ROAMING_ADV
  1324. * will not be sent.
  1325. *
  1326. * returns true if the ROAMING_ADV can be sent, false otherwise */
  1327. static bool tt_check_roam_count(struct bat_priv *bat_priv,
  1328. uint8_t *client)
  1329. {
  1330. struct tt_roam_node *tt_roam_node;
  1331. bool ret = false;
  1332. spin_lock_bh(&bat_priv->tt_roam_list_lock);
  1333. /* The new tt_req will be issued only if I'm not waiting for a
  1334. * reply from the same orig_node yet */
  1335. list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
  1336. if (!compare_eth(tt_roam_node->addr, client))
  1337. continue;
  1338. if (is_out_of_time(tt_roam_node->first_time,
  1339. ROAMING_MAX_TIME * 1000))
  1340. continue;
  1341. if (!atomic_dec_not_zero(&tt_roam_node->counter))
  1342. /* Sorry, you roamed too many times! */
  1343. goto unlock;
  1344. ret = true;
  1345. break;
  1346. }
  1347. if (!ret) {
  1348. tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
  1349. if (!tt_roam_node)
  1350. goto unlock;
  1351. tt_roam_node->first_time = jiffies;
  1352. atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
  1353. memcpy(tt_roam_node->addr, client, ETH_ALEN);
  1354. list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
  1355. ret = true;
  1356. }
  1357. unlock:
  1358. spin_unlock_bh(&bat_priv->tt_roam_list_lock);
  1359. return ret;
  1360. }
  1361. void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
  1362. struct orig_node *orig_node)
  1363. {
  1364. struct neigh_node *neigh_node = NULL;
  1365. struct sk_buff *skb = NULL;
  1366. struct roam_adv_packet *roam_adv_packet;
  1367. int ret = 1;
  1368. struct hard_iface *primary_if;
  1369. /* before going on we have to check whether the client has
  1370. * already roamed to us too many times */
  1371. if (!tt_check_roam_count(bat_priv, client))
  1372. goto out;
  1373. skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
  1374. if (!skb)
  1375. goto out;
  1376. skb_reserve(skb, ETH_HLEN);
  1377. roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
  1378. sizeof(struct roam_adv_packet));
  1379. roam_adv_packet->packet_type = BAT_ROAM_ADV;
  1380. roam_adv_packet->version = COMPAT_VERSION;
  1381. roam_adv_packet->ttl = TTL;
  1382. primary_if = primary_if_get_selected(bat_priv);
  1383. if (!primary_if)
  1384. goto out;
  1385. memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
  1386. hardif_free_ref(primary_if);
  1387. memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
  1388. memcpy(roam_adv_packet->client, client, ETH_ALEN);
  1389. neigh_node = orig_node_get_router(orig_node);
  1390. if (!neigh_node)
  1391. goto out;
  1392. bat_dbg(DBG_TT, bat_priv,
  1393. "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
  1394. orig_node->orig, client, neigh_node->addr);
  1395. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  1396. ret = 0;
  1397. out:
  1398. if (neigh_node)
  1399. neigh_node_free_ref(neigh_node);
  1400. if (ret)
  1401. kfree_skb(skb);
  1402. return;
  1403. }
  1404. static void tt_purge(struct work_struct *work)
  1405. {
  1406. struct delayed_work *delayed_work =
  1407. container_of(work, struct delayed_work, work);
  1408. struct bat_priv *bat_priv =
  1409. container_of(delayed_work, struct bat_priv, tt_work);
  1410. tt_local_purge(bat_priv);
  1411. tt_global_roam_purge(bat_priv);
  1412. tt_req_purge(bat_priv);
  1413. tt_roam_purge(bat_priv);
  1414. tt_start_timer(bat_priv);
  1415. }
  1416. void tt_free(struct bat_priv *bat_priv)
  1417. {
  1418. cancel_delayed_work_sync(&bat_priv->tt_work);
  1419. tt_local_table_free(bat_priv);
  1420. tt_global_table_free(bat_priv);
  1421. tt_req_list_free(bat_priv);
  1422. tt_changes_list_free(bat_priv);
  1423. tt_roam_list_free(bat_priv);
  1424. kfree(bat_priv->tt_buff);
  1425. }
  1426. /* This function will enable or disable the specified flags for all the entries
  1427. * in the given hash table and returns the number of modified entries */
  1428. static uint16_t tt_set_flags(struct hashtable_t *hash, uint16_t flags,
  1429. bool enable)
  1430. {
  1431. uint32_t i;
  1432. uint16_t changed_num = 0;
  1433. struct hlist_head *head;
  1434. struct hlist_node *node;
  1435. struct tt_common_entry *tt_common_entry;
  1436. if (!hash)
  1437. goto out;
  1438. for (i = 0; i < hash->size; i++) {
  1439. head = &hash->table[i];
  1440. rcu_read_lock();
  1441. hlist_for_each_entry_rcu(tt_common_entry, node,
  1442. head, hash_entry) {
  1443. if (enable) {
  1444. if ((tt_common_entry->flags & flags) == flags)
  1445. continue;
  1446. tt_common_entry->flags |= flags;
  1447. } else {
  1448. if (!(tt_common_entry->flags & flags))
  1449. continue;
  1450. tt_common_entry->flags &= ~flags;
  1451. }
  1452. changed_num++;
  1453. }
  1454. rcu_read_unlock();
  1455. }
  1456. out:
  1457. return changed_num;
  1458. }
  1459. /* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
  1460. static void tt_local_purge_pending_clients(struct bat_priv *bat_priv)
  1461. {
  1462. struct hashtable_t *hash = bat_priv->tt_local_hash;
  1463. struct tt_common_entry *tt_common_entry;
  1464. struct tt_local_entry *tt_local_entry;
  1465. struct hlist_node *node, *node_tmp;
  1466. struct hlist_head *head;
  1467. spinlock_t *list_lock; /* protects write access to the hash lists */
  1468. uint32_t i;
  1469. if (!hash)
  1470. return;
  1471. for (i = 0; i < hash->size; i++) {
  1472. head = &hash->table[i];
  1473. list_lock = &hash->list_locks[i];
  1474. spin_lock_bh(list_lock);
  1475. hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
  1476. head, hash_entry) {
  1477. if (!(tt_common_entry->flags & TT_CLIENT_PENDING))
  1478. continue;
  1479. bat_dbg(DBG_TT, bat_priv, "Deleting local tt entry "
  1480. "(%pM): pending\n", tt_common_entry->addr);
  1481. atomic_dec(&bat_priv->num_local_tt);
  1482. hlist_del_rcu(node);
  1483. tt_local_entry = container_of(tt_common_entry,
  1484. struct tt_local_entry,
  1485. common);
  1486. tt_local_entry_free_ref(tt_local_entry);
  1487. }
  1488. spin_unlock_bh(list_lock);
  1489. }
  1490. }
  1491. void tt_commit_changes(struct bat_priv *bat_priv)
  1492. {
  1493. uint16_t changed_num = tt_set_flags(bat_priv->tt_local_hash,
  1494. TT_CLIENT_NEW, false);
  1495. /* all the reset entries have now to be effectively counted as local
  1496. * entries */
  1497. atomic_add(changed_num, &bat_priv->num_local_tt);
  1498. tt_local_purge_pending_clients(bat_priv);
  1499. /* Increment the TTVN only once per OGM interval */
  1500. atomic_inc(&bat_priv->ttvn);
  1501. bat_priv->tt_poss_change = false;
  1502. }
  1503. bool is_ap_isolated(struct bat_priv *bat_priv, uint8_t *src, uint8_t *dst)
  1504. {
  1505. struct tt_local_entry *tt_local_entry = NULL;
  1506. struct tt_global_entry *tt_global_entry = NULL;
  1507. bool ret = true;
  1508. if (!atomic_read(&bat_priv->ap_isolation))
  1509. return false;
  1510. tt_local_entry = tt_local_hash_find(bat_priv, dst);
  1511. if (!tt_local_entry)
  1512. goto out;
  1513. tt_global_entry = tt_global_hash_find(bat_priv, src);
  1514. if (!tt_global_entry)
  1515. goto out;
  1516. if (_is_ap_isolated(tt_local_entry, tt_global_entry))
  1517. goto out;
  1518. ret = false;
  1519. out:
  1520. if (tt_global_entry)
  1521. tt_global_entry_free_ref(tt_global_entry);
  1522. if (tt_local_entry)
  1523. tt_local_entry_free_ref(tt_local_entry);
  1524. return ret;
  1525. }
  1526. void tt_update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node,
  1527. const unsigned char *tt_buff, uint8_t tt_num_changes,
  1528. uint8_t ttvn, uint16_t tt_crc)
  1529. {
  1530. uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  1531. bool full_table = true;
  1532. /* the ttvn increased by one -> we can apply the attached changes */
  1533. if (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 (ttvn != orig_ttvn || orig_node->tt_crc != tt_crc) {
  1564. request_table:
  1565. bat_dbg(DBG_TT, bat_priv, "TT inconsistency for %pM. "
  1566. "Need to retrieve the correct information "
  1567. "(ttvn: %u last_ttvn: %u crc: %u last_crc: "
  1568. "%u num_changes: %u)\n", orig_node->orig, ttvn,
  1569. orig_ttvn, tt_crc, orig_node->tt_crc,
  1570. tt_num_changes);
  1571. send_tt_request(bat_priv, orig_node, ttvn, tt_crc,
  1572. full_table);
  1573. return;
  1574. }
  1575. }
  1576. }