translation-table.c 73 KB

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