translation-table.c 75 KB

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