translation-table.c 74 KB

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