translation-table.c 70 KB

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