translation-table.c 49 KB

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