translation-table.c 74 KB

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