translation-table.c 45 KB

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