translation-table.c 73 KB

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