translation-table.c 74 KB

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