translation-table.c 51 KB

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