translation-table.c 74 KB

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