translation-table.c 67 KB

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