translation-table.c 64 KB

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