translation-table.c 66 KB

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