translation-table.c 51 KB

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