translation-table.c 52 KB

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