translation-table.c 58 KB

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