translation-table.c 49 KB

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