translation-table.c 49 KB

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