translation-table.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610
  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. /* the change can carry possible "attribute" flags like the
  728. * TT_CLIENT_WIFI, therefore they have to be copied in the
  729. * client entry
  730. */
  731. tt_global_entry->common.flags |= flags;
  732. /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
  733. * one originator left in the list and we previously received a
  734. * delete + roaming change for this originator.
  735. *
  736. * We should first delete the old originator before adding the
  737. * new one.
  738. */
  739. if (common->flags & BATADV_TT_CLIENT_ROAM) {
  740. batadv_tt_global_del_orig_list(tt_global_entry);
  741. common->flags &= ~BATADV_TT_CLIENT_ROAM;
  742. tt_global_entry->roam_at = 0;
  743. }
  744. }
  745. add_orig_entry:
  746. /* add the new orig_entry (if needed) or update it */
  747. batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
  748. batadv_dbg(BATADV_DBG_TT, bat_priv,
  749. "Creating new global tt entry: %pM (via %pM)\n",
  750. common->addr, orig_node->orig);
  751. ret = 1;
  752. out_remove:
  753. /* remove address from local hash if present */
  754. local_flags = batadv_tt_local_remove(bat_priv, tt_addr,
  755. "global tt received",
  756. !!(flags & BATADV_TT_CLIENT_ROAM));
  757. tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
  758. if (!(flags & BATADV_TT_CLIENT_ROAM))
  759. /* this is a normal global add. Therefore the client is not in a
  760. * roaming state anymore.
  761. */
  762. tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
  763. out:
  764. if (tt_global_entry)
  765. batadv_tt_global_entry_free_ref(tt_global_entry);
  766. if (tt_local_entry)
  767. batadv_tt_local_entry_free_ref(tt_local_entry);
  768. return ret;
  769. }
  770. /* batadv_transtable_best_orig - Get best originator list entry from tt entry
  771. * @tt_global_entry: global translation table entry to be analyzed
  772. *
  773. * This functon assumes the caller holds rcu_read_lock().
  774. * Returns best originator list entry or NULL on errors.
  775. */
  776. static struct batadv_tt_orig_list_entry *
  777. batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
  778. {
  779. struct batadv_neigh_node *router = NULL;
  780. struct hlist_head *head;
  781. struct hlist_node *node;
  782. struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
  783. int best_tq = 0;
  784. head = &tt_global_entry->orig_list;
  785. hlist_for_each_entry_rcu(orig_entry, node, head, list) {
  786. router = batadv_orig_node_get_router(orig_entry->orig_node);
  787. if (!router)
  788. continue;
  789. if (router->tq_avg > best_tq) {
  790. best_entry = orig_entry;
  791. best_tq = router->tq_avg;
  792. }
  793. batadv_neigh_node_free_ref(router);
  794. }
  795. return best_entry;
  796. }
  797. /* batadv_tt_global_print_entry - print all orig nodes who announce the address
  798. * for this global entry
  799. * @tt_global_entry: global translation table entry to be printed
  800. * @seq: debugfs table seq_file struct
  801. *
  802. * This functon assumes the caller holds rcu_read_lock().
  803. */
  804. static void
  805. batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
  806. struct seq_file *seq)
  807. {
  808. struct hlist_head *head;
  809. struct hlist_node *node;
  810. struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
  811. struct batadv_tt_common_entry *tt_common_entry;
  812. uint16_t flags;
  813. uint8_t last_ttvn;
  814. tt_common_entry = &tt_global_entry->common;
  815. flags = tt_common_entry->flags;
  816. best_entry = batadv_transtable_best_orig(tt_global_entry);
  817. if (best_entry) {
  818. last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
  819. seq_printf(seq, " %c %pM (%3u) via %pM (%3u) [%c%c%c]\n",
  820. '*', tt_global_entry->common.addr,
  821. best_entry->ttvn, best_entry->orig_node->orig,
  822. last_ttvn,
  823. (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
  824. (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
  825. (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
  826. }
  827. head = &tt_global_entry->orig_list;
  828. hlist_for_each_entry_rcu(orig_entry, node, head, list) {
  829. if (best_entry == orig_entry)
  830. continue;
  831. last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
  832. seq_printf(seq, " %c %pM (%3u) via %pM (%3u) [%c%c%c]\n",
  833. '+', tt_global_entry->common.addr,
  834. orig_entry->ttvn, orig_entry->orig_node->orig,
  835. last_ttvn,
  836. (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
  837. (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
  838. (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
  839. }
  840. }
  841. int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
  842. {
  843. struct net_device *net_dev = (struct net_device *)seq->private;
  844. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  845. struct batadv_hashtable *hash = bat_priv->tt.global_hash;
  846. struct batadv_tt_common_entry *tt_common_entry;
  847. struct batadv_tt_global_entry *tt_global;
  848. struct batadv_hard_iface *primary_if;
  849. struct hlist_node *node;
  850. struct hlist_head *head;
  851. uint32_t i;
  852. primary_if = batadv_seq_print_text_primary_if_get(seq);
  853. if (!primary_if)
  854. goto out;
  855. seq_printf(seq,
  856. "Globally announced TT entries received via the mesh %s\n",
  857. net_dev->name);
  858. seq_printf(seq, " %-13s %s %-15s %s %s\n",
  859. "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
  860. for (i = 0; i < hash->size; i++) {
  861. head = &hash->table[i];
  862. rcu_read_lock();
  863. hlist_for_each_entry_rcu(tt_common_entry, node,
  864. head, hash_entry) {
  865. tt_global = container_of(tt_common_entry,
  866. struct batadv_tt_global_entry,
  867. common);
  868. batadv_tt_global_print_entry(tt_global, seq);
  869. }
  870. rcu_read_unlock();
  871. }
  872. out:
  873. if (primary_if)
  874. batadv_hardif_free_ref(primary_if);
  875. return 0;
  876. }
  877. /* deletes the orig list of a tt_global_entry */
  878. static void
  879. batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
  880. {
  881. struct hlist_head *head;
  882. struct hlist_node *node, *safe;
  883. struct batadv_tt_orig_list_entry *orig_entry;
  884. spin_lock_bh(&tt_global_entry->list_lock);
  885. head = &tt_global_entry->orig_list;
  886. hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
  887. hlist_del_rcu(node);
  888. batadv_tt_orig_list_entry_free_ref(orig_entry);
  889. }
  890. spin_unlock_bh(&tt_global_entry->list_lock);
  891. }
  892. static void
  893. batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
  894. struct batadv_tt_global_entry *tt_global_entry,
  895. struct batadv_orig_node *orig_node,
  896. const char *message)
  897. {
  898. struct hlist_head *head;
  899. struct hlist_node *node, *safe;
  900. struct batadv_tt_orig_list_entry *orig_entry;
  901. spin_lock_bh(&tt_global_entry->list_lock);
  902. head = &tt_global_entry->orig_list;
  903. hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
  904. if (orig_entry->orig_node == orig_node) {
  905. batadv_dbg(BATADV_DBG_TT, bat_priv,
  906. "Deleting %pM from global tt entry %pM: %s\n",
  907. orig_node->orig,
  908. tt_global_entry->common.addr, message);
  909. hlist_del_rcu(node);
  910. batadv_tt_orig_list_entry_free_ref(orig_entry);
  911. }
  912. }
  913. spin_unlock_bh(&tt_global_entry->list_lock);
  914. }
  915. /* If the client is to be deleted, we check if it is the last origantor entry
  916. * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
  917. * timer, otherwise we simply remove the originator scheduled for deletion.
  918. */
  919. static void
  920. batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
  921. struct batadv_tt_global_entry *tt_global_entry,
  922. struct batadv_orig_node *orig_node,
  923. const char *message)
  924. {
  925. bool last_entry = true;
  926. struct hlist_head *head;
  927. struct hlist_node *node;
  928. struct batadv_tt_orig_list_entry *orig_entry;
  929. /* no local entry exists, case 1:
  930. * Check if this is the last one or if other entries exist.
  931. */
  932. rcu_read_lock();
  933. head = &tt_global_entry->orig_list;
  934. hlist_for_each_entry_rcu(orig_entry, node, head, list) {
  935. if (orig_entry->orig_node != orig_node) {
  936. last_entry = false;
  937. break;
  938. }
  939. }
  940. rcu_read_unlock();
  941. if (last_entry) {
  942. /* its the last one, mark for roaming. */
  943. tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
  944. tt_global_entry->roam_at = jiffies;
  945. } else
  946. /* there is another entry, we can simply delete this
  947. * one and can still use the other one.
  948. */
  949. batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
  950. orig_node, message);
  951. }
  952. static void batadv_tt_global_del(struct batadv_priv *bat_priv,
  953. struct batadv_orig_node *orig_node,
  954. const unsigned char *addr,
  955. const char *message, bool roaming)
  956. {
  957. struct batadv_tt_global_entry *tt_global_entry;
  958. struct batadv_tt_local_entry *local_entry = NULL;
  959. tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
  960. if (!tt_global_entry)
  961. goto out;
  962. if (!roaming) {
  963. batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
  964. orig_node, message);
  965. if (hlist_empty(&tt_global_entry->orig_list))
  966. batadv_tt_global_free(bat_priv, tt_global_entry,
  967. message);
  968. goto out;
  969. }
  970. /* if we are deleting a global entry due to a roam
  971. * event, there are two possibilities:
  972. * 1) the client roamed from node A to node B => if there
  973. * is only one originator left for this client, we mark
  974. * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
  975. * wait for node B to claim it. In case of timeout
  976. * the entry is purged.
  977. *
  978. * If there are other originators left, we directly delete
  979. * the originator.
  980. * 2) the client roamed to us => we can directly delete
  981. * the global entry, since it is useless now.
  982. */
  983. local_entry = batadv_tt_local_hash_find(bat_priv,
  984. tt_global_entry->common.addr);
  985. if (local_entry) {
  986. /* local entry exists, case 2: client roamed to us. */
  987. batadv_tt_global_del_orig_list(tt_global_entry);
  988. batadv_tt_global_free(bat_priv, tt_global_entry, message);
  989. } else
  990. /* no local entry exists, case 1: check for roaming */
  991. batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
  992. orig_node, message);
  993. out:
  994. if (tt_global_entry)
  995. batadv_tt_global_entry_free_ref(tt_global_entry);
  996. if (local_entry)
  997. batadv_tt_local_entry_free_ref(local_entry);
  998. }
  999. void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
  1000. struct batadv_orig_node *orig_node,
  1001. const char *message)
  1002. {
  1003. struct batadv_tt_global_entry *tt_global;
  1004. struct batadv_tt_common_entry *tt_common_entry;
  1005. uint32_t i;
  1006. struct batadv_hashtable *hash = bat_priv->tt.global_hash;
  1007. struct hlist_node *node, *safe;
  1008. struct hlist_head *head;
  1009. spinlock_t *list_lock; /* protects write access to the hash lists */
  1010. if (!hash)
  1011. return;
  1012. for (i = 0; i < hash->size; i++) {
  1013. head = &hash->table[i];
  1014. list_lock = &hash->list_locks[i];
  1015. spin_lock_bh(list_lock);
  1016. hlist_for_each_entry_safe(tt_common_entry, node, safe,
  1017. head, hash_entry) {
  1018. tt_global = container_of(tt_common_entry,
  1019. struct batadv_tt_global_entry,
  1020. common);
  1021. batadv_tt_global_del_orig_entry(bat_priv, tt_global,
  1022. orig_node, message);
  1023. if (hlist_empty(&tt_global->orig_list)) {
  1024. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1025. "Deleting global tt entry %pM: %s\n",
  1026. tt_global->common.addr, message);
  1027. hlist_del_rcu(node);
  1028. batadv_tt_global_entry_free_ref(tt_global);
  1029. }
  1030. }
  1031. spin_unlock_bh(list_lock);
  1032. }
  1033. orig_node->tt_initialised = false;
  1034. }
  1035. static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
  1036. char **msg)
  1037. {
  1038. bool purge = false;
  1039. unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
  1040. unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
  1041. if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
  1042. batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
  1043. purge = true;
  1044. *msg = "Roaming timeout\n";
  1045. }
  1046. if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
  1047. batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
  1048. purge = true;
  1049. *msg = "Temporary client timeout\n";
  1050. }
  1051. return purge;
  1052. }
  1053. static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
  1054. {
  1055. struct batadv_hashtable *hash = bat_priv->tt.global_hash;
  1056. struct hlist_head *head;
  1057. struct hlist_node *node, *node_tmp;
  1058. spinlock_t *list_lock; /* protects write access to the hash lists */
  1059. uint32_t i;
  1060. char *msg = NULL;
  1061. struct batadv_tt_common_entry *tt_common;
  1062. struct batadv_tt_global_entry *tt_global;
  1063. for (i = 0; i < hash->size; i++) {
  1064. head = &hash->table[i];
  1065. list_lock = &hash->list_locks[i];
  1066. spin_lock_bh(list_lock);
  1067. hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
  1068. hash_entry) {
  1069. tt_global = container_of(tt_common,
  1070. struct batadv_tt_global_entry,
  1071. common);
  1072. if (!batadv_tt_global_to_purge(tt_global, &msg))
  1073. continue;
  1074. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1075. "Deleting global tt entry (%pM): %s\n",
  1076. tt_global->common.addr, msg);
  1077. hlist_del_rcu(node);
  1078. batadv_tt_global_entry_free_ref(tt_global);
  1079. }
  1080. spin_unlock_bh(list_lock);
  1081. }
  1082. }
  1083. static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
  1084. {
  1085. struct batadv_hashtable *hash;
  1086. spinlock_t *list_lock; /* protects write access to the hash lists */
  1087. struct batadv_tt_common_entry *tt_common_entry;
  1088. struct batadv_tt_global_entry *tt_global;
  1089. struct hlist_node *node, *node_tmp;
  1090. struct hlist_head *head;
  1091. uint32_t i;
  1092. if (!bat_priv->tt.global_hash)
  1093. return;
  1094. hash = bat_priv->tt.global_hash;
  1095. for (i = 0; i < hash->size; i++) {
  1096. head = &hash->table[i];
  1097. list_lock = &hash->list_locks[i];
  1098. spin_lock_bh(list_lock);
  1099. hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
  1100. head, hash_entry) {
  1101. hlist_del_rcu(node);
  1102. tt_global = container_of(tt_common_entry,
  1103. struct batadv_tt_global_entry,
  1104. common);
  1105. batadv_tt_global_entry_free_ref(tt_global);
  1106. }
  1107. spin_unlock_bh(list_lock);
  1108. }
  1109. batadv_hash_destroy(hash);
  1110. bat_priv->tt.global_hash = NULL;
  1111. }
  1112. static bool
  1113. _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
  1114. struct batadv_tt_global_entry *tt_global_entry)
  1115. {
  1116. bool ret = false;
  1117. if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
  1118. tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
  1119. ret = true;
  1120. return ret;
  1121. }
  1122. struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
  1123. const uint8_t *src,
  1124. const uint8_t *addr)
  1125. {
  1126. struct batadv_tt_local_entry *tt_local_entry = NULL;
  1127. struct batadv_tt_global_entry *tt_global_entry = NULL;
  1128. struct batadv_orig_node *orig_node = NULL;
  1129. struct batadv_tt_orig_list_entry *best_entry;
  1130. if (src && atomic_read(&bat_priv->ap_isolation)) {
  1131. tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
  1132. if (!tt_local_entry ||
  1133. (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
  1134. goto out;
  1135. }
  1136. tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
  1137. if (!tt_global_entry)
  1138. goto out;
  1139. /* check whether the clients should not communicate due to AP
  1140. * isolation
  1141. */
  1142. if (tt_local_entry &&
  1143. _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
  1144. goto out;
  1145. rcu_read_lock();
  1146. best_entry = batadv_transtable_best_orig(tt_global_entry);
  1147. /* found anything? */
  1148. if (best_entry)
  1149. orig_node = best_entry->orig_node;
  1150. if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
  1151. orig_node = NULL;
  1152. rcu_read_unlock();
  1153. out:
  1154. if (tt_global_entry)
  1155. batadv_tt_global_entry_free_ref(tt_global_entry);
  1156. if (tt_local_entry)
  1157. batadv_tt_local_entry_free_ref(tt_local_entry);
  1158. return orig_node;
  1159. }
  1160. /* Calculates the checksum of the local table of a given orig_node */
  1161. static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
  1162. struct batadv_orig_node *orig_node)
  1163. {
  1164. uint16_t total = 0, total_one;
  1165. struct batadv_hashtable *hash = bat_priv->tt.global_hash;
  1166. struct batadv_tt_common_entry *tt_common;
  1167. struct batadv_tt_global_entry *tt_global;
  1168. struct hlist_node *node;
  1169. struct hlist_head *head;
  1170. uint32_t i;
  1171. int j;
  1172. for (i = 0; i < hash->size; i++) {
  1173. head = &hash->table[i];
  1174. rcu_read_lock();
  1175. hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
  1176. tt_global = container_of(tt_common,
  1177. struct batadv_tt_global_entry,
  1178. common);
  1179. /* Roaming clients are in the global table for
  1180. * consistency only. They don't have to be
  1181. * taken into account while computing the
  1182. * global crc
  1183. */
  1184. if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
  1185. continue;
  1186. /* Temporary clients have not been announced yet, so
  1187. * they have to be skipped while computing the global
  1188. * crc
  1189. */
  1190. if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
  1191. continue;
  1192. /* find out if this global entry is announced by this
  1193. * originator
  1194. */
  1195. if (!batadv_tt_global_entry_has_orig(tt_global,
  1196. orig_node))
  1197. continue;
  1198. total_one = 0;
  1199. for (j = 0; j < ETH_ALEN; j++)
  1200. total_one = crc16_byte(total_one,
  1201. tt_common->addr[j]);
  1202. total ^= total_one;
  1203. }
  1204. rcu_read_unlock();
  1205. }
  1206. return total;
  1207. }
  1208. /* Calculates the checksum of the local table */
  1209. static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
  1210. {
  1211. uint16_t total = 0, total_one;
  1212. struct batadv_hashtable *hash = bat_priv->tt.local_hash;
  1213. struct batadv_tt_common_entry *tt_common;
  1214. struct hlist_node *node;
  1215. struct hlist_head *head;
  1216. uint32_t i;
  1217. int j;
  1218. for (i = 0; i < hash->size; i++) {
  1219. head = &hash->table[i];
  1220. rcu_read_lock();
  1221. hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
  1222. /* not yet committed clients have not to be taken into
  1223. * account while computing the CRC
  1224. */
  1225. if (tt_common->flags & BATADV_TT_CLIENT_NEW)
  1226. continue;
  1227. total_one = 0;
  1228. for (j = 0; j < ETH_ALEN; j++)
  1229. total_one = crc16_byte(total_one,
  1230. tt_common->addr[j]);
  1231. total ^= total_one;
  1232. }
  1233. rcu_read_unlock();
  1234. }
  1235. return total;
  1236. }
  1237. static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
  1238. {
  1239. struct batadv_tt_req_node *node, *safe;
  1240. spin_lock_bh(&bat_priv->tt.req_list_lock);
  1241. list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
  1242. list_del(&node->list);
  1243. kfree(node);
  1244. }
  1245. spin_unlock_bh(&bat_priv->tt.req_list_lock);
  1246. }
  1247. static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
  1248. struct batadv_orig_node *orig_node,
  1249. const unsigned char *tt_buff,
  1250. uint8_t tt_num_changes)
  1251. {
  1252. uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
  1253. /* Replace the old buffer only if I received something in the
  1254. * last OGM (the OGM could carry no changes)
  1255. */
  1256. spin_lock_bh(&orig_node->tt_buff_lock);
  1257. if (tt_buff_len > 0) {
  1258. kfree(orig_node->tt_buff);
  1259. orig_node->tt_buff_len = 0;
  1260. orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
  1261. if (orig_node->tt_buff) {
  1262. memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
  1263. orig_node->tt_buff_len = tt_buff_len;
  1264. }
  1265. }
  1266. spin_unlock_bh(&orig_node->tt_buff_lock);
  1267. }
  1268. static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
  1269. {
  1270. struct batadv_tt_req_node *node, *safe;
  1271. spin_lock_bh(&bat_priv->tt.req_list_lock);
  1272. list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
  1273. if (batadv_has_timed_out(node->issued_at,
  1274. BATADV_TT_REQUEST_TIMEOUT)) {
  1275. list_del(&node->list);
  1276. kfree(node);
  1277. }
  1278. }
  1279. spin_unlock_bh(&bat_priv->tt.req_list_lock);
  1280. }
  1281. /* returns the pointer to the new tt_req_node struct if no request
  1282. * has already been issued for this orig_node, NULL otherwise
  1283. */
  1284. static struct batadv_tt_req_node *
  1285. batadv_new_tt_req_node(struct batadv_priv *bat_priv,
  1286. struct batadv_orig_node *orig_node)
  1287. {
  1288. struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
  1289. spin_lock_bh(&bat_priv->tt.req_list_lock);
  1290. list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
  1291. if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
  1292. !batadv_has_timed_out(tt_req_node_tmp->issued_at,
  1293. BATADV_TT_REQUEST_TIMEOUT))
  1294. goto unlock;
  1295. }
  1296. tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
  1297. if (!tt_req_node)
  1298. goto unlock;
  1299. memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
  1300. tt_req_node->issued_at = jiffies;
  1301. list_add(&tt_req_node->list, &bat_priv->tt.req_list);
  1302. unlock:
  1303. spin_unlock_bh(&bat_priv->tt.req_list_lock);
  1304. return tt_req_node;
  1305. }
  1306. /* data_ptr is useless here, but has to be kept to respect the prototype */
  1307. static int batadv_tt_local_valid_entry(const void *entry_ptr,
  1308. const void *data_ptr)
  1309. {
  1310. const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
  1311. if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
  1312. return 0;
  1313. return 1;
  1314. }
  1315. static int batadv_tt_global_valid(const void *entry_ptr,
  1316. const void *data_ptr)
  1317. {
  1318. const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
  1319. const struct batadv_tt_global_entry *tt_global_entry;
  1320. const struct batadv_orig_node *orig_node = data_ptr;
  1321. if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
  1322. tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
  1323. return 0;
  1324. tt_global_entry = container_of(tt_common_entry,
  1325. struct batadv_tt_global_entry,
  1326. common);
  1327. return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
  1328. }
  1329. static struct sk_buff *
  1330. batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
  1331. struct batadv_hashtable *hash,
  1332. struct batadv_hard_iface *primary_if,
  1333. int (*valid_cb)(const void *, const void *),
  1334. void *cb_data)
  1335. {
  1336. struct batadv_tt_common_entry *tt_common_entry;
  1337. struct batadv_tt_query_packet *tt_response;
  1338. struct batadv_tt_change *tt_change;
  1339. struct hlist_node *node;
  1340. struct hlist_head *head;
  1341. struct sk_buff *skb = NULL;
  1342. uint16_t tt_tot, tt_count;
  1343. ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
  1344. uint32_t i;
  1345. size_t len;
  1346. if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
  1347. tt_len = primary_if->soft_iface->mtu - tt_query_size;
  1348. tt_len -= tt_len % sizeof(struct batadv_tt_change);
  1349. }
  1350. tt_tot = tt_len / sizeof(struct batadv_tt_change);
  1351. len = tt_query_size + tt_len;
  1352. skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
  1353. if (!skb)
  1354. goto out;
  1355. skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
  1356. tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
  1357. tt_response->ttvn = ttvn;
  1358. tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
  1359. tt_count = 0;
  1360. rcu_read_lock();
  1361. for (i = 0; i < hash->size; i++) {
  1362. head = &hash->table[i];
  1363. hlist_for_each_entry_rcu(tt_common_entry, node,
  1364. head, hash_entry) {
  1365. if (tt_count == tt_tot)
  1366. break;
  1367. if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
  1368. continue;
  1369. memcpy(tt_change->addr, tt_common_entry->addr,
  1370. ETH_ALEN);
  1371. tt_change->flags = tt_common_entry->flags;
  1372. tt_count++;
  1373. tt_change++;
  1374. }
  1375. }
  1376. rcu_read_unlock();
  1377. /* store in the message the number of entries we have successfully
  1378. * copied
  1379. */
  1380. tt_response->tt_data = htons(tt_count);
  1381. out:
  1382. return skb;
  1383. }
  1384. static int batadv_send_tt_request(struct batadv_priv *bat_priv,
  1385. struct batadv_orig_node *dst_orig_node,
  1386. uint8_t ttvn, uint16_t tt_crc,
  1387. bool full_table)
  1388. {
  1389. struct sk_buff *skb = NULL;
  1390. struct batadv_tt_query_packet *tt_request;
  1391. struct batadv_neigh_node *neigh_node = NULL;
  1392. struct batadv_hard_iface *primary_if;
  1393. struct batadv_tt_req_node *tt_req_node = NULL;
  1394. int ret = 1;
  1395. size_t tt_req_len;
  1396. primary_if = batadv_primary_if_get_selected(bat_priv);
  1397. if (!primary_if)
  1398. goto out;
  1399. /* The new tt_req will be issued only if I'm not waiting for a
  1400. * reply from the same orig_node yet
  1401. */
  1402. tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
  1403. if (!tt_req_node)
  1404. goto out;
  1405. skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN + NET_IP_ALIGN);
  1406. if (!skb)
  1407. goto out;
  1408. skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
  1409. tt_req_len = sizeof(*tt_request);
  1410. tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
  1411. tt_request->header.packet_type = BATADV_TT_QUERY;
  1412. tt_request->header.version = BATADV_COMPAT_VERSION;
  1413. memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
  1414. memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
  1415. tt_request->header.ttl = BATADV_TTL;
  1416. tt_request->ttvn = ttvn;
  1417. tt_request->tt_data = htons(tt_crc);
  1418. tt_request->flags = BATADV_TT_REQUEST;
  1419. if (full_table)
  1420. tt_request->flags |= BATADV_TT_FULL_TABLE;
  1421. neigh_node = batadv_orig_node_get_router(dst_orig_node);
  1422. if (!neigh_node)
  1423. goto out;
  1424. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1425. "Sending TT_REQUEST to %pM via %pM [%c]\n",
  1426. dst_orig_node->orig, neigh_node->addr,
  1427. (full_table ? 'F' : '.'));
  1428. batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
  1429. batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  1430. ret = 0;
  1431. out:
  1432. if (neigh_node)
  1433. batadv_neigh_node_free_ref(neigh_node);
  1434. if (primary_if)
  1435. batadv_hardif_free_ref(primary_if);
  1436. if (ret)
  1437. kfree_skb(skb);
  1438. if (ret && tt_req_node) {
  1439. spin_lock_bh(&bat_priv->tt.req_list_lock);
  1440. list_del(&tt_req_node->list);
  1441. spin_unlock_bh(&bat_priv->tt.req_list_lock);
  1442. kfree(tt_req_node);
  1443. }
  1444. return ret;
  1445. }
  1446. static bool
  1447. batadv_send_other_tt_response(struct batadv_priv *bat_priv,
  1448. struct batadv_tt_query_packet *tt_request)
  1449. {
  1450. struct batadv_orig_node *req_dst_orig_node;
  1451. struct batadv_orig_node *res_dst_orig_node = NULL;
  1452. struct batadv_neigh_node *neigh_node = NULL;
  1453. struct batadv_hard_iface *primary_if = NULL;
  1454. uint8_t orig_ttvn, req_ttvn, ttvn;
  1455. int ret = false;
  1456. unsigned char *tt_buff;
  1457. bool full_table;
  1458. uint16_t tt_len, tt_tot;
  1459. struct sk_buff *skb = NULL;
  1460. struct batadv_tt_query_packet *tt_response;
  1461. uint8_t *packet_pos;
  1462. size_t len;
  1463. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1464. "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
  1465. tt_request->src, tt_request->ttvn, tt_request->dst,
  1466. (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
  1467. /* Let's get the orig node of the REAL destination */
  1468. req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
  1469. if (!req_dst_orig_node)
  1470. goto out;
  1471. res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
  1472. if (!res_dst_orig_node)
  1473. goto out;
  1474. neigh_node = batadv_orig_node_get_router(res_dst_orig_node);
  1475. if (!neigh_node)
  1476. goto out;
  1477. primary_if = batadv_primary_if_get_selected(bat_priv);
  1478. if (!primary_if)
  1479. goto out;
  1480. orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
  1481. req_ttvn = tt_request->ttvn;
  1482. /* I don't have the requested data */
  1483. if (orig_ttvn != req_ttvn ||
  1484. tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
  1485. goto out;
  1486. /* If the full table has been explicitly requested */
  1487. if (tt_request->flags & BATADV_TT_FULL_TABLE ||
  1488. !req_dst_orig_node->tt_buff)
  1489. full_table = true;
  1490. else
  1491. full_table = false;
  1492. /* In this version, fragmentation is not implemented, then
  1493. * I'll send only one packet with as much TT entries as I can
  1494. */
  1495. if (!full_table) {
  1496. spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
  1497. tt_len = req_dst_orig_node->tt_buff_len;
  1498. tt_tot = tt_len / sizeof(struct batadv_tt_change);
  1499. len = sizeof(*tt_response) + tt_len;
  1500. skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
  1501. if (!skb)
  1502. goto unlock;
  1503. skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
  1504. packet_pos = skb_put(skb, len);
  1505. tt_response = (struct batadv_tt_query_packet *)packet_pos;
  1506. tt_response->ttvn = req_ttvn;
  1507. tt_response->tt_data = htons(tt_tot);
  1508. tt_buff = skb->data + sizeof(*tt_response);
  1509. /* Copy the last orig_node's OGM buffer */
  1510. memcpy(tt_buff, req_dst_orig_node->tt_buff,
  1511. req_dst_orig_node->tt_buff_len);
  1512. spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
  1513. } else {
  1514. tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
  1515. tt_len *= sizeof(struct batadv_tt_change);
  1516. ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
  1517. skb = batadv_tt_response_fill_table(tt_len, ttvn,
  1518. bat_priv->tt.global_hash,
  1519. primary_if,
  1520. batadv_tt_global_valid,
  1521. req_dst_orig_node);
  1522. if (!skb)
  1523. goto out;
  1524. tt_response = (struct batadv_tt_query_packet *)skb->data;
  1525. }
  1526. tt_response->header.packet_type = BATADV_TT_QUERY;
  1527. tt_response->header.version = BATADV_COMPAT_VERSION;
  1528. tt_response->header.ttl = BATADV_TTL;
  1529. memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
  1530. memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
  1531. tt_response->flags = BATADV_TT_RESPONSE;
  1532. if (full_table)
  1533. tt_response->flags |= BATADV_TT_FULL_TABLE;
  1534. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1535. "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
  1536. res_dst_orig_node->orig, neigh_node->addr,
  1537. req_dst_orig_node->orig, req_ttvn);
  1538. batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
  1539. batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  1540. ret = true;
  1541. goto out;
  1542. unlock:
  1543. spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
  1544. out:
  1545. if (res_dst_orig_node)
  1546. batadv_orig_node_free_ref(res_dst_orig_node);
  1547. if (req_dst_orig_node)
  1548. batadv_orig_node_free_ref(req_dst_orig_node);
  1549. if (neigh_node)
  1550. batadv_neigh_node_free_ref(neigh_node);
  1551. if (primary_if)
  1552. batadv_hardif_free_ref(primary_if);
  1553. if (!ret)
  1554. kfree_skb(skb);
  1555. return ret;
  1556. }
  1557. static bool
  1558. batadv_send_my_tt_response(struct batadv_priv *bat_priv,
  1559. struct batadv_tt_query_packet *tt_request)
  1560. {
  1561. struct batadv_orig_node *orig_node;
  1562. struct batadv_neigh_node *neigh_node = NULL;
  1563. struct batadv_hard_iface *primary_if = NULL;
  1564. uint8_t my_ttvn, req_ttvn, ttvn;
  1565. int ret = false;
  1566. unsigned char *tt_buff;
  1567. bool full_table;
  1568. uint16_t tt_len, tt_tot;
  1569. struct sk_buff *skb = NULL;
  1570. struct batadv_tt_query_packet *tt_response;
  1571. uint8_t *packet_pos;
  1572. size_t len;
  1573. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1574. "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
  1575. tt_request->src, tt_request->ttvn,
  1576. (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
  1577. my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
  1578. req_ttvn = tt_request->ttvn;
  1579. orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
  1580. if (!orig_node)
  1581. goto out;
  1582. neigh_node = batadv_orig_node_get_router(orig_node);
  1583. if (!neigh_node)
  1584. goto out;
  1585. primary_if = batadv_primary_if_get_selected(bat_priv);
  1586. if (!primary_if)
  1587. goto out;
  1588. /* If the full table has been explicitly requested or the gap
  1589. * is too big send the whole local translation table
  1590. */
  1591. if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
  1592. !bat_priv->tt.last_changeset)
  1593. full_table = true;
  1594. else
  1595. full_table = false;
  1596. /* In this version, fragmentation is not implemented, then
  1597. * I'll send only one packet with as much TT entries as I can
  1598. */
  1599. if (!full_table) {
  1600. spin_lock_bh(&bat_priv->tt.last_changeset_lock);
  1601. tt_len = bat_priv->tt.last_changeset_len;
  1602. tt_tot = tt_len / sizeof(struct batadv_tt_change);
  1603. len = sizeof(*tt_response) + tt_len;
  1604. skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
  1605. if (!skb)
  1606. goto unlock;
  1607. skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
  1608. packet_pos = skb_put(skb, len);
  1609. tt_response = (struct batadv_tt_query_packet *)packet_pos;
  1610. tt_response->ttvn = req_ttvn;
  1611. tt_response->tt_data = htons(tt_tot);
  1612. tt_buff = skb->data + sizeof(*tt_response);
  1613. memcpy(tt_buff, bat_priv->tt.last_changeset,
  1614. bat_priv->tt.last_changeset_len);
  1615. spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
  1616. } else {
  1617. tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
  1618. tt_len *= sizeof(struct batadv_tt_change);
  1619. ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
  1620. skb = batadv_tt_response_fill_table(tt_len, ttvn,
  1621. bat_priv->tt.local_hash,
  1622. primary_if,
  1623. batadv_tt_local_valid_entry,
  1624. NULL);
  1625. if (!skb)
  1626. goto out;
  1627. tt_response = (struct batadv_tt_query_packet *)skb->data;
  1628. }
  1629. tt_response->header.packet_type = BATADV_TT_QUERY;
  1630. tt_response->header.version = BATADV_COMPAT_VERSION;
  1631. tt_response->header.ttl = BATADV_TTL;
  1632. memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
  1633. memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
  1634. tt_response->flags = BATADV_TT_RESPONSE;
  1635. if (full_table)
  1636. tt_response->flags |= BATADV_TT_FULL_TABLE;
  1637. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1638. "Sending TT_RESPONSE to %pM via %pM [%c]\n",
  1639. orig_node->orig, neigh_node->addr,
  1640. (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
  1641. batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
  1642. batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  1643. ret = true;
  1644. goto out;
  1645. unlock:
  1646. spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
  1647. out:
  1648. if (orig_node)
  1649. batadv_orig_node_free_ref(orig_node);
  1650. if (neigh_node)
  1651. batadv_neigh_node_free_ref(neigh_node);
  1652. if (primary_if)
  1653. batadv_hardif_free_ref(primary_if);
  1654. if (!ret)
  1655. kfree_skb(skb);
  1656. /* This packet was for me, so it doesn't need to be re-routed */
  1657. return true;
  1658. }
  1659. bool batadv_send_tt_response(struct batadv_priv *bat_priv,
  1660. struct batadv_tt_query_packet *tt_request)
  1661. {
  1662. if (batadv_is_my_mac(tt_request->dst)) {
  1663. /* don't answer backbone gws! */
  1664. if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
  1665. return true;
  1666. return batadv_send_my_tt_response(bat_priv, tt_request);
  1667. } else {
  1668. return batadv_send_other_tt_response(bat_priv, tt_request);
  1669. }
  1670. }
  1671. static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
  1672. struct batadv_orig_node *orig_node,
  1673. struct batadv_tt_change *tt_change,
  1674. uint16_t tt_num_changes, uint8_t ttvn)
  1675. {
  1676. int i;
  1677. int roams;
  1678. for (i = 0; i < tt_num_changes; i++) {
  1679. if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
  1680. roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
  1681. batadv_tt_global_del(bat_priv, orig_node,
  1682. (tt_change + i)->addr,
  1683. "tt removed by changes",
  1684. roams);
  1685. } else {
  1686. if (!batadv_tt_global_add(bat_priv, orig_node,
  1687. (tt_change + i)->addr,
  1688. (tt_change + i)->flags, ttvn))
  1689. /* In case of problem while storing a
  1690. * global_entry, we stop the updating
  1691. * procedure without committing the
  1692. * ttvn change. This will avoid to send
  1693. * corrupted data on tt_request
  1694. */
  1695. return;
  1696. }
  1697. }
  1698. orig_node->tt_initialised = true;
  1699. }
  1700. static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
  1701. struct batadv_tt_query_packet *tt_response)
  1702. {
  1703. struct batadv_orig_node *orig_node;
  1704. orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
  1705. if (!orig_node)
  1706. goto out;
  1707. /* Purge the old table first.. */
  1708. batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
  1709. _batadv_tt_update_changes(bat_priv, orig_node,
  1710. (struct batadv_tt_change *)(tt_response + 1),
  1711. ntohs(tt_response->tt_data),
  1712. tt_response->ttvn);
  1713. spin_lock_bh(&orig_node->tt_buff_lock);
  1714. kfree(orig_node->tt_buff);
  1715. orig_node->tt_buff_len = 0;
  1716. orig_node->tt_buff = NULL;
  1717. spin_unlock_bh(&orig_node->tt_buff_lock);
  1718. atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
  1719. out:
  1720. if (orig_node)
  1721. batadv_orig_node_free_ref(orig_node);
  1722. }
  1723. static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
  1724. struct batadv_orig_node *orig_node,
  1725. uint16_t tt_num_changes, uint8_t ttvn,
  1726. struct batadv_tt_change *tt_change)
  1727. {
  1728. _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
  1729. tt_num_changes, ttvn);
  1730. batadv_tt_save_orig_buffer(bat_priv, orig_node,
  1731. (unsigned char *)tt_change, tt_num_changes);
  1732. atomic_set(&orig_node->last_ttvn, ttvn);
  1733. }
  1734. bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
  1735. {
  1736. struct batadv_tt_local_entry *tt_local_entry;
  1737. bool ret = false;
  1738. tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
  1739. if (!tt_local_entry)
  1740. goto out;
  1741. /* Check if the client has been logically deleted (but is kept for
  1742. * consistency purpose)
  1743. */
  1744. if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
  1745. (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
  1746. goto out;
  1747. ret = true;
  1748. out:
  1749. if (tt_local_entry)
  1750. batadv_tt_local_entry_free_ref(tt_local_entry);
  1751. return ret;
  1752. }
  1753. void batadv_handle_tt_response(struct batadv_priv *bat_priv,
  1754. struct batadv_tt_query_packet *tt_response)
  1755. {
  1756. struct batadv_tt_req_node *node, *safe;
  1757. struct batadv_orig_node *orig_node = NULL;
  1758. struct batadv_tt_change *tt_change;
  1759. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1760. "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
  1761. tt_response->src, tt_response->ttvn,
  1762. ntohs(tt_response->tt_data),
  1763. (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
  1764. /* we should have never asked a backbone gw */
  1765. if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
  1766. goto out;
  1767. orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
  1768. if (!orig_node)
  1769. goto out;
  1770. if (tt_response->flags & BATADV_TT_FULL_TABLE) {
  1771. batadv_tt_fill_gtable(bat_priv, tt_response);
  1772. } else {
  1773. tt_change = (struct batadv_tt_change *)(tt_response + 1);
  1774. batadv_tt_update_changes(bat_priv, orig_node,
  1775. ntohs(tt_response->tt_data),
  1776. tt_response->ttvn, tt_change);
  1777. }
  1778. /* Delete the tt_req_node from pending tt_requests list */
  1779. spin_lock_bh(&bat_priv->tt.req_list_lock);
  1780. list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
  1781. if (!batadv_compare_eth(node->addr, tt_response->src))
  1782. continue;
  1783. list_del(&node->list);
  1784. kfree(node);
  1785. }
  1786. spin_unlock_bh(&bat_priv->tt.req_list_lock);
  1787. /* Recalculate the CRC for this orig_node and store it */
  1788. orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
  1789. out:
  1790. if (orig_node)
  1791. batadv_orig_node_free_ref(orig_node);
  1792. }
  1793. int batadv_tt_init(struct batadv_priv *bat_priv)
  1794. {
  1795. int ret;
  1796. ret = batadv_tt_local_init(bat_priv);
  1797. if (ret < 0)
  1798. return ret;
  1799. ret = batadv_tt_global_init(bat_priv);
  1800. if (ret < 0)
  1801. return ret;
  1802. batadv_tt_start_timer(bat_priv);
  1803. return 1;
  1804. }
  1805. static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
  1806. {
  1807. struct batadv_tt_roam_node *node, *safe;
  1808. spin_lock_bh(&bat_priv->tt.roam_list_lock);
  1809. list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
  1810. list_del(&node->list);
  1811. kfree(node);
  1812. }
  1813. spin_unlock_bh(&bat_priv->tt.roam_list_lock);
  1814. }
  1815. static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
  1816. {
  1817. struct batadv_tt_roam_node *node, *safe;
  1818. spin_lock_bh(&bat_priv->tt.roam_list_lock);
  1819. list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
  1820. if (!batadv_has_timed_out(node->first_time,
  1821. BATADV_ROAMING_MAX_TIME))
  1822. continue;
  1823. list_del(&node->list);
  1824. kfree(node);
  1825. }
  1826. spin_unlock_bh(&bat_priv->tt.roam_list_lock);
  1827. }
  1828. /* This function checks whether the client already reached the
  1829. * maximum number of possible roaming phases. In this case the ROAMING_ADV
  1830. * will not be sent.
  1831. *
  1832. * returns true if the ROAMING_ADV can be sent, false otherwise
  1833. */
  1834. static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
  1835. uint8_t *client)
  1836. {
  1837. struct batadv_tt_roam_node *tt_roam_node;
  1838. bool ret = false;
  1839. spin_lock_bh(&bat_priv->tt.roam_list_lock);
  1840. /* The new tt_req will be issued only if I'm not waiting for a
  1841. * reply from the same orig_node yet
  1842. */
  1843. list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
  1844. if (!batadv_compare_eth(tt_roam_node->addr, client))
  1845. continue;
  1846. if (batadv_has_timed_out(tt_roam_node->first_time,
  1847. BATADV_ROAMING_MAX_TIME))
  1848. continue;
  1849. if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
  1850. /* Sorry, you roamed too many times! */
  1851. goto unlock;
  1852. ret = true;
  1853. break;
  1854. }
  1855. if (!ret) {
  1856. tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
  1857. if (!tt_roam_node)
  1858. goto unlock;
  1859. tt_roam_node->first_time = jiffies;
  1860. atomic_set(&tt_roam_node->counter,
  1861. BATADV_ROAMING_MAX_COUNT - 1);
  1862. memcpy(tt_roam_node->addr, client, ETH_ALEN);
  1863. list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
  1864. ret = true;
  1865. }
  1866. unlock:
  1867. spin_unlock_bh(&bat_priv->tt.roam_list_lock);
  1868. return ret;
  1869. }
  1870. static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
  1871. struct batadv_orig_node *orig_node)
  1872. {
  1873. struct batadv_neigh_node *neigh_node = NULL;
  1874. struct sk_buff *skb = NULL;
  1875. struct batadv_roam_adv_packet *roam_adv_packet;
  1876. int ret = 1;
  1877. struct batadv_hard_iface *primary_if;
  1878. size_t len = sizeof(*roam_adv_packet);
  1879. /* before going on we have to check whether the client has
  1880. * already roamed to us too many times
  1881. */
  1882. if (!batadv_tt_check_roam_count(bat_priv, client))
  1883. goto out;
  1884. skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN + NET_IP_ALIGN);
  1885. if (!skb)
  1886. goto out;
  1887. skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
  1888. roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
  1889. roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
  1890. roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
  1891. roam_adv_packet->header.ttl = BATADV_TTL;
  1892. roam_adv_packet->reserved = 0;
  1893. primary_if = batadv_primary_if_get_selected(bat_priv);
  1894. if (!primary_if)
  1895. goto out;
  1896. memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
  1897. batadv_hardif_free_ref(primary_if);
  1898. memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
  1899. memcpy(roam_adv_packet->client, client, ETH_ALEN);
  1900. neigh_node = batadv_orig_node_get_router(orig_node);
  1901. if (!neigh_node)
  1902. goto out;
  1903. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1904. "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
  1905. orig_node->orig, client, neigh_node->addr);
  1906. batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
  1907. batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  1908. ret = 0;
  1909. out:
  1910. if (neigh_node)
  1911. batadv_neigh_node_free_ref(neigh_node);
  1912. if (ret)
  1913. kfree_skb(skb);
  1914. return;
  1915. }
  1916. static void batadv_tt_purge(struct work_struct *work)
  1917. {
  1918. struct delayed_work *delayed_work;
  1919. struct batadv_priv_tt *priv_tt;
  1920. struct batadv_priv *bat_priv;
  1921. delayed_work = container_of(work, struct delayed_work, work);
  1922. priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
  1923. bat_priv = container_of(priv_tt, struct batadv_priv, tt);
  1924. batadv_tt_local_purge(bat_priv);
  1925. batadv_tt_global_purge(bat_priv);
  1926. batadv_tt_req_purge(bat_priv);
  1927. batadv_tt_roam_purge(bat_priv);
  1928. batadv_tt_start_timer(bat_priv);
  1929. }
  1930. void batadv_tt_free(struct batadv_priv *bat_priv)
  1931. {
  1932. cancel_delayed_work_sync(&bat_priv->tt.work);
  1933. batadv_tt_local_table_free(bat_priv);
  1934. batadv_tt_global_table_free(bat_priv);
  1935. batadv_tt_req_list_free(bat_priv);
  1936. batadv_tt_changes_list_free(bat_priv);
  1937. batadv_tt_roam_list_free(bat_priv);
  1938. kfree(bat_priv->tt.last_changeset);
  1939. }
  1940. /* This function will enable or disable the specified flags for all the entries
  1941. * in the given hash table and returns the number of modified entries
  1942. */
  1943. static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
  1944. uint16_t flags, bool enable)
  1945. {
  1946. uint32_t i;
  1947. uint16_t changed_num = 0;
  1948. struct hlist_head *head;
  1949. struct hlist_node *node;
  1950. struct batadv_tt_common_entry *tt_common_entry;
  1951. if (!hash)
  1952. goto out;
  1953. for (i = 0; i < hash->size; i++) {
  1954. head = &hash->table[i];
  1955. rcu_read_lock();
  1956. hlist_for_each_entry_rcu(tt_common_entry, node,
  1957. head, hash_entry) {
  1958. if (enable) {
  1959. if ((tt_common_entry->flags & flags) == flags)
  1960. continue;
  1961. tt_common_entry->flags |= flags;
  1962. } else {
  1963. if (!(tt_common_entry->flags & flags))
  1964. continue;
  1965. tt_common_entry->flags &= ~flags;
  1966. }
  1967. changed_num++;
  1968. }
  1969. rcu_read_unlock();
  1970. }
  1971. out:
  1972. return changed_num;
  1973. }
  1974. /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
  1975. static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
  1976. {
  1977. struct batadv_hashtable *hash = bat_priv->tt.local_hash;
  1978. struct batadv_tt_common_entry *tt_common;
  1979. struct batadv_tt_local_entry *tt_local;
  1980. struct hlist_node *node, *node_tmp;
  1981. struct hlist_head *head;
  1982. spinlock_t *list_lock; /* protects write access to the hash lists */
  1983. uint32_t i;
  1984. if (!hash)
  1985. return;
  1986. for (i = 0; i < hash->size; i++) {
  1987. head = &hash->table[i];
  1988. list_lock = &hash->list_locks[i];
  1989. spin_lock_bh(list_lock);
  1990. hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
  1991. hash_entry) {
  1992. if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
  1993. continue;
  1994. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1995. "Deleting local tt entry (%pM): pending\n",
  1996. tt_common->addr);
  1997. atomic_dec(&bat_priv->tt.local_entry_num);
  1998. hlist_del_rcu(node);
  1999. tt_local = container_of(tt_common,
  2000. struct batadv_tt_local_entry,
  2001. common);
  2002. batadv_tt_local_entry_free_ref(tt_local);
  2003. }
  2004. spin_unlock_bh(list_lock);
  2005. }
  2006. }
  2007. static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
  2008. unsigned char **packet_buff,
  2009. int *packet_buff_len, int packet_min_len)
  2010. {
  2011. uint16_t changed_num = 0;
  2012. if (atomic_read(&bat_priv->tt.local_changes) < 1)
  2013. return -ENOENT;
  2014. changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
  2015. BATADV_TT_CLIENT_NEW, false);
  2016. /* all reset entries have to be counted as local entries */
  2017. atomic_add(changed_num, &bat_priv->tt.local_entry_num);
  2018. batadv_tt_local_purge_pending_clients(bat_priv);
  2019. bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
  2020. /* Increment the TTVN only once per OGM interval */
  2021. atomic_inc(&bat_priv->tt.vn);
  2022. batadv_dbg(BATADV_DBG_TT, bat_priv,
  2023. "Local changes committed, updating to ttvn %u\n",
  2024. (uint8_t)atomic_read(&bat_priv->tt.vn));
  2025. /* reset the sending counter */
  2026. atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
  2027. return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
  2028. packet_buff_len, packet_min_len);
  2029. }
  2030. /* when calling this function (hard_iface == primary_if) has to be true */
  2031. int batadv_tt_append_diff(struct batadv_priv *bat_priv,
  2032. unsigned char **packet_buff, int *packet_buff_len,
  2033. int packet_min_len)
  2034. {
  2035. int tt_num_changes;
  2036. /* if at least one change happened */
  2037. tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
  2038. packet_buff_len,
  2039. packet_min_len);
  2040. /* if the changes have been sent often enough */
  2041. if ((tt_num_changes < 0) &&
  2042. (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) {
  2043. batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
  2044. packet_min_len, packet_min_len);
  2045. tt_num_changes = 0;
  2046. }
  2047. return tt_num_changes;
  2048. }
  2049. bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
  2050. uint8_t *dst)
  2051. {
  2052. struct batadv_tt_local_entry *tt_local_entry = NULL;
  2053. struct batadv_tt_global_entry *tt_global_entry = NULL;
  2054. bool ret = false;
  2055. if (!atomic_read(&bat_priv->ap_isolation))
  2056. goto out;
  2057. tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
  2058. if (!tt_local_entry)
  2059. goto out;
  2060. tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
  2061. if (!tt_global_entry)
  2062. goto out;
  2063. if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
  2064. goto out;
  2065. ret = true;
  2066. out:
  2067. if (tt_global_entry)
  2068. batadv_tt_global_entry_free_ref(tt_global_entry);
  2069. if (tt_local_entry)
  2070. batadv_tt_local_entry_free_ref(tt_local_entry);
  2071. return ret;
  2072. }
  2073. void batadv_tt_update_orig(struct batadv_priv *bat_priv,
  2074. struct batadv_orig_node *orig_node,
  2075. const unsigned char *tt_buff, uint8_t tt_num_changes,
  2076. uint8_t ttvn, uint16_t tt_crc)
  2077. {
  2078. uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  2079. bool full_table = true;
  2080. struct batadv_tt_change *tt_change;
  2081. /* don't care about a backbone gateways updates. */
  2082. if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
  2083. return;
  2084. /* orig table not initialised AND first diff is in the OGM OR the ttvn
  2085. * increased by one -> we can apply the attached changes
  2086. */
  2087. if ((!orig_node->tt_initialised && ttvn == 1) ||
  2088. ttvn - orig_ttvn == 1) {
  2089. /* the OGM could not contain the changes due to their size or
  2090. * because they have already been sent BATADV_TT_OGM_APPEND_MAX
  2091. * times.
  2092. * In this case send a tt request
  2093. */
  2094. if (!tt_num_changes) {
  2095. full_table = false;
  2096. goto request_table;
  2097. }
  2098. tt_change = (struct batadv_tt_change *)tt_buff;
  2099. batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
  2100. ttvn, tt_change);
  2101. /* Even if we received the precomputed crc with the OGM, we
  2102. * prefer to recompute it to spot any possible inconsistency
  2103. * in the global table
  2104. */
  2105. orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
  2106. /* The ttvn alone is not enough to guarantee consistency
  2107. * because a single value could represent different states
  2108. * (due to the wrap around). Thus a node has to check whether
  2109. * the resulting table (after applying the changes) is still
  2110. * consistent or not. E.g. a node could disconnect while its
  2111. * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
  2112. * checking the CRC value is mandatory to detect the
  2113. * inconsistency
  2114. */
  2115. if (orig_node->tt_crc != tt_crc)
  2116. goto request_table;
  2117. } else {
  2118. /* if we missed more than one change or our tables are not
  2119. * in sync anymore -> request fresh tt data
  2120. */
  2121. if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
  2122. orig_node->tt_crc != tt_crc) {
  2123. request_table:
  2124. batadv_dbg(BATADV_DBG_TT, bat_priv,
  2125. "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
  2126. orig_node->orig, ttvn, orig_ttvn, tt_crc,
  2127. orig_node->tt_crc, tt_num_changes);
  2128. batadv_send_tt_request(bat_priv, orig_node, ttvn,
  2129. tt_crc, full_table);
  2130. return;
  2131. }
  2132. }
  2133. }
  2134. /* returns true whether we know that the client has moved from its old
  2135. * originator to another one. This entry is kept is still kept for consistency
  2136. * purposes
  2137. */
  2138. bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
  2139. uint8_t *addr)
  2140. {
  2141. struct batadv_tt_global_entry *tt_global_entry;
  2142. bool ret = false;
  2143. tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
  2144. if (!tt_global_entry)
  2145. goto out;
  2146. ret = !!(tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM);
  2147. batadv_tt_global_entry_free_ref(tt_global_entry);
  2148. out:
  2149. return ret;
  2150. }
  2151. /**
  2152. * batadv_tt_local_client_is_roaming - tells whether the client is roaming
  2153. * @bat_priv: the bat priv with all the soft interface information
  2154. * @addr: the MAC address of the local client to query
  2155. *
  2156. * Returns true if the local client is known to be roaming (it is not served by
  2157. * this node anymore) or not. If yes, the client is still present in the table
  2158. * to keep the latter consistent with the node TTVN
  2159. */
  2160. bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
  2161. uint8_t *addr)
  2162. {
  2163. struct batadv_tt_local_entry *tt_local_entry;
  2164. bool ret = false;
  2165. tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
  2166. if (!tt_local_entry)
  2167. goto out;
  2168. ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
  2169. batadv_tt_local_entry_free_ref(tt_local_entry);
  2170. out:
  2171. return ret;
  2172. }
  2173. bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
  2174. struct batadv_orig_node *orig_node,
  2175. const unsigned char *addr)
  2176. {
  2177. bool ret = false;
  2178. /* if the originator is a backbone node (meaning it belongs to the same
  2179. * LAN of this node) the temporary client must not be added because to
  2180. * reach such destination the node must use the LAN instead of the mesh
  2181. */
  2182. if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
  2183. goto out;
  2184. if (!batadv_tt_global_add(bat_priv, orig_node, addr,
  2185. BATADV_TT_CLIENT_TEMP,
  2186. atomic_read(&orig_node->last_ttvn)))
  2187. goto out;
  2188. batadv_dbg(BATADV_DBG_TT, bat_priv,
  2189. "Added temporary global client (addr: %pM orig: %pM)\n",
  2190. addr, orig_node->orig);
  2191. ret = true;
  2192. out:
  2193. return ret;
  2194. }