translation-table.c 52 KB

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