translation-table.c 61 KB

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