translation-table.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742
  1. /*
  2. * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner, Simon Wunderlich
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA
  19. *
  20. */
  21. #include "main.h"
  22. #include "translation-table.h"
  23. #include "soft-interface.h"
  24. #include "hard-interface.h"
  25. #include "send.h"
  26. #include "hash.h"
  27. #include "originator.h"
  28. #include "routing.h"
  29. #include <linux/crc16.h>
  30. static void _tt_global_del(struct bat_priv *bat_priv,
  31. struct tt_global_entry *tt_global_entry,
  32. const char *message);
  33. static void tt_purge(struct work_struct *work);
  34. /* returns 1 if they are the same mac addr */
  35. static int compare_ltt(const struct hlist_node *node, const void *data2)
  36. {
  37. const void *data1 = container_of(node, struct tt_local_entry,
  38. hash_entry);
  39. return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
  40. }
  41. /* returns 1 if they are the same mac addr */
  42. static int compare_gtt(const struct hlist_node *node, const void *data2)
  43. {
  44. const void *data1 = container_of(node, struct tt_global_entry,
  45. hash_entry);
  46. return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
  47. }
  48. static void tt_start_timer(struct bat_priv *bat_priv)
  49. {
  50. INIT_DELAYED_WORK(&bat_priv->tt_work, tt_purge);
  51. queue_delayed_work(bat_event_workqueue, &bat_priv->tt_work,
  52. msecs_to_jiffies(5000));
  53. }
  54. static struct tt_local_entry *tt_local_hash_find(struct bat_priv *bat_priv,
  55. const void *data)
  56. {
  57. struct hashtable_t *hash = bat_priv->tt_local_hash;
  58. struct hlist_head *head;
  59. struct hlist_node *node;
  60. struct tt_local_entry *tt_local_entry, *tt_local_entry_tmp = NULL;
  61. int index;
  62. if (!hash)
  63. return NULL;
  64. index = choose_orig(data, hash->size);
  65. head = &hash->table[index];
  66. rcu_read_lock();
  67. hlist_for_each_entry_rcu(tt_local_entry, node, head, hash_entry) {
  68. if (!compare_eth(tt_local_entry, data))
  69. continue;
  70. if (!atomic_inc_not_zero(&tt_local_entry->refcount))
  71. continue;
  72. tt_local_entry_tmp = tt_local_entry;
  73. break;
  74. }
  75. rcu_read_unlock();
  76. return tt_local_entry_tmp;
  77. }
  78. static struct tt_global_entry *tt_global_hash_find(struct bat_priv *bat_priv,
  79. const void *data)
  80. {
  81. struct hashtable_t *hash = bat_priv->tt_global_hash;
  82. struct hlist_head *head;
  83. struct hlist_node *node;
  84. struct tt_global_entry *tt_global_entry;
  85. struct tt_global_entry *tt_global_entry_tmp = NULL;
  86. int index;
  87. if (!hash)
  88. return NULL;
  89. index = choose_orig(data, hash->size);
  90. head = &hash->table[index];
  91. rcu_read_lock();
  92. hlist_for_each_entry_rcu(tt_global_entry, node, head, hash_entry) {
  93. if (!compare_eth(tt_global_entry, data))
  94. continue;
  95. if (!atomic_inc_not_zero(&tt_global_entry->refcount))
  96. continue;
  97. tt_global_entry_tmp = tt_global_entry;
  98. break;
  99. }
  100. rcu_read_unlock();
  101. return tt_global_entry_tmp;
  102. }
  103. static bool is_out_of_time(unsigned long starting_time, unsigned long timeout)
  104. {
  105. unsigned long deadline;
  106. deadline = starting_time + msecs_to_jiffies(timeout);
  107. return time_after(jiffies, deadline);
  108. }
  109. static void tt_local_entry_free_ref(struct tt_local_entry *tt_local_entry)
  110. {
  111. if (atomic_dec_and_test(&tt_local_entry->refcount))
  112. kfree_rcu(tt_local_entry, rcu);
  113. }
  114. static void tt_global_entry_free_rcu(struct rcu_head *rcu)
  115. {
  116. struct tt_global_entry *tt_global_entry;
  117. tt_global_entry = container_of(rcu, struct tt_global_entry, rcu);
  118. if (tt_global_entry->orig_node)
  119. orig_node_free_ref(tt_global_entry->orig_node);
  120. kfree(tt_global_entry);
  121. }
  122. static void tt_global_entry_free_ref(struct tt_global_entry *tt_global_entry)
  123. {
  124. if (atomic_dec_and_test(&tt_global_entry->refcount))
  125. call_rcu(&tt_global_entry->rcu, tt_global_entry_free_rcu);
  126. }
  127. static void tt_local_event(struct bat_priv *bat_priv, const uint8_t *addr,
  128. uint8_t flags)
  129. {
  130. struct tt_change_node *tt_change_node;
  131. tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
  132. if (!tt_change_node)
  133. return;
  134. tt_change_node->change.flags = flags;
  135. memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
  136. spin_lock_bh(&bat_priv->tt_changes_list_lock);
  137. /* track the change in the OGMinterval list */
  138. list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
  139. atomic_inc(&bat_priv->tt_local_changes);
  140. spin_unlock_bh(&bat_priv->tt_changes_list_lock);
  141. atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
  142. }
  143. int tt_len(int changes_num)
  144. {
  145. return changes_num * sizeof(struct tt_change);
  146. }
  147. static int tt_local_init(struct bat_priv *bat_priv)
  148. {
  149. if (bat_priv->tt_local_hash)
  150. return 1;
  151. bat_priv->tt_local_hash = hash_new(1024);
  152. if (!bat_priv->tt_local_hash)
  153. return 0;
  154. return 1;
  155. }
  156. void tt_local_add(struct net_device *soft_iface, const uint8_t *addr)
  157. {
  158. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  159. struct tt_local_entry *tt_local_entry = NULL;
  160. struct tt_global_entry *tt_global_entry = NULL;
  161. tt_local_entry = tt_local_hash_find(bat_priv, addr);
  162. if (tt_local_entry) {
  163. tt_local_entry->last_seen = jiffies;
  164. goto out;
  165. }
  166. tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
  167. if (!tt_local_entry)
  168. goto out;
  169. bat_dbg(DBG_TT, bat_priv,
  170. "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
  171. (uint8_t)atomic_read(&bat_priv->ttvn));
  172. memcpy(tt_local_entry->addr, addr, ETH_ALEN);
  173. tt_local_entry->last_seen = jiffies;
  174. tt_local_entry->flags = NO_FLAGS;
  175. atomic_set(&tt_local_entry->refcount, 2);
  176. /* the batman interface mac address should never be purged */
  177. if (compare_eth(addr, soft_iface->dev_addr))
  178. tt_local_entry->flags |= TT_CLIENT_NOPURGE;
  179. tt_local_event(bat_priv, addr, tt_local_entry->flags);
  180. /* The local entry has to be marked as NEW to avoid to send it in
  181. * a full table response going out before the next ttvn increment
  182. * (consistency check) */
  183. tt_local_entry->flags |= TT_CLIENT_NEW;
  184. hash_add(bat_priv->tt_local_hash, compare_ltt, choose_orig,
  185. tt_local_entry, &tt_local_entry->hash_entry);
  186. /* remove address from global hash if present */
  187. tt_global_entry = tt_global_hash_find(bat_priv, addr);
  188. /* Check whether it is a roaming! */
  189. if (tt_global_entry) {
  190. /* This node is probably going to update its tt table */
  191. tt_global_entry->orig_node->tt_poss_change = true;
  192. /* The global entry has to be marked as PENDING and has to be
  193. * kept for consistency purpose */
  194. tt_global_entry->flags |= TT_CLIENT_PENDING;
  195. send_roam_adv(bat_priv, tt_global_entry->addr,
  196. tt_global_entry->orig_node);
  197. }
  198. out:
  199. if (tt_local_entry)
  200. tt_local_entry_free_ref(tt_local_entry);
  201. if (tt_global_entry)
  202. tt_global_entry_free_ref(tt_global_entry);
  203. }
  204. int tt_changes_fill_buffer(struct bat_priv *bat_priv,
  205. unsigned char *buff, int buff_len)
  206. {
  207. int count = 0, tot_changes = 0;
  208. struct tt_change_node *entry, *safe;
  209. if (buff_len > 0)
  210. tot_changes = buff_len / tt_len(1);
  211. spin_lock_bh(&bat_priv->tt_changes_list_lock);
  212. atomic_set(&bat_priv->tt_local_changes, 0);
  213. list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
  214. list) {
  215. if (count < tot_changes) {
  216. memcpy(buff + tt_len(count),
  217. &entry->change, sizeof(struct tt_change));
  218. count++;
  219. }
  220. list_del(&entry->list);
  221. kfree(entry);
  222. }
  223. spin_unlock_bh(&bat_priv->tt_changes_list_lock);
  224. /* Keep the buffer for possible tt_request */
  225. spin_lock_bh(&bat_priv->tt_buff_lock);
  226. kfree(bat_priv->tt_buff);
  227. bat_priv->tt_buff_len = 0;
  228. bat_priv->tt_buff = NULL;
  229. /* We check whether this new OGM has no changes due to size
  230. * problems */
  231. if (buff_len > 0) {
  232. /**
  233. * if kmalloc() fails we will reply with the full table
  234. * instead of providing the diff
  235. */
  236. bat_priv->tt_buff = kmalloc(buff_len, GFP_ATOMIC);
  237. if (bat_priv->tt_buff) {
  238. memcpy(bat_priv->tt_buff, buff, buff_len);
  239. bat_priv->tt_buff_len = buff_len;
  240. }
  241. }
  242. spin_unlock_bh(&bat_priv->tt_buff_lock);
  243. return tot_changes;
  244. }
  245. int tt_local_seq_print_text(struct seq_file *seq, void *offset)
  246. {
  247. struct net_device *net_dev = (struct net_device *)seq->private;
  248. struct bat_priv *bat_priv = netdev_priv(net_dev);
  249. struct hashtable_t *hash = bat_priv->tt_local_hash;
  250. struct tt_local_entry *tt_local_entry;
  251. struct hard_iface *primary_if;
  252. struct hlist_node *node;
  253. struct hlist_head *head;
  254. size_t buf_size, pos;
  255. char *buff;
  256. int i, ret = 0;
  257. primary_if = primary_if_get_selected(bat_priv);
  258. if (!primary_if) {
  259. ret = seq_printf(seq, "BATMAN mesh %s disabled - "
  260. "please specify interfaces to enable it\n",
  261. net_dev->name);
  262. goto out;
  263. }
  264. if (primary_if->if_status != IF_ACTIVE) {
  265. ret = seq_printf(seq, "BATMAN mesh %s disabled - "
  266. "primary interface not active\n",
  267. net_dev->name);
  268. goto out;
  269. }
  270. seq_printf(seq, "Locally retrieved addresses (from %s) "
  271. "announced via TT (TTVN: %u):\n",
  272. net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
  273. buf_size = 1;
  274. /* Estimate length for: " * xx:xx:xx:xx:xx:xx\n" */
  275. for (i = 0; i < hash->size; i++) {
  276. head = &hash->table[i];
  277. rcu_read_lock();
  278. __hlist_for_each_rcu(node, head)
  279. buf_size += 21;
  280. rcu_read_unlock();
  281. }
  282. buff = kmalloc(buf_size, GFP_ATOMIC);
  283. if (!buff) {
  284. ret = -ENOMEM;
  285. goto out;
  286. }
  287. buff[0] = '\0';
  288. pos = 0;
  289. for (i = 0; i < hash->size; i++) {
  290. head = &hash->table[i];
  291. rcu_read_lock();
  292. hlist_for_each_entry_rcu(tt_local_entry, node,
  293. head, hash_entry) {
  294. pos += snprintf(buff + pos, 22, " * %pM\n",
  295. tt_local_entry->addr);
  296. }
  297. rcu_read_unlock();
  298. }
  299. seq_printf(seq, "%s", buff);
  300. kfree(buff);
  301. out:
  302. if (primary_if)
  303. hardif_free_ref(primary_if);
  304. return ret;
  305. }
  306. static void tt_local_set_pending(struct bat_priv *bat_priv,
  307. struct tt_local_entry *tt_local_entry,
  308. uint16_t flags)
  309. {
  310. tt_local_event(bat_priv, tt_local_entry->addr,
  311. tt_local_entry->flags | flags);
  312. /* The local client has to be merked as "pending to be removed" but has
  313. * to be kept in the table in order to send it in an full tables
  314. * response issued before the net ttvn increment (consistency check) */
  315. tt_local_entry->flags |= TT_CLIENT_PENDING;
  316. }
  317. void tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
  318. const char *message, bool roaming)
  319. {
  320. struct tt_local_entry *tt_local_entry = NULL;
  321. tt_local_entry = tt_local_hash_find(bat_priv, addr);
  322. if (!tt_local_entry)
  323. goto out;
  324. tt_local_set_pending(bat_priv, tt_local_entry, TT_CLIENT_DEL |
  325. (roaming ? TT_CLIENT_ROAM : NO_FLAGS));
  326. bat_dbg(DBG_TT, bat_priv, "Local tt entry (%pM) pending to be removed: "
  327. "%s\n", tt_local_entry->addr, message);
  328. out:
  329. if (tt_local_entry)
  330. tt_local_entry_free_ref(tt_local_entry);
  331. }
  332. static void tt_local_purge(struct bat_priv *bat_priv)
  333. {
  334. struct hashtable_t *hash = bat_priv->tt_local_hash;
  335. struct tt_local_entry *tt_local_entry;
  336. struct hlist_node *node, *node_tmp;
  337. struct hlist_head *head;
  338. spinlock_t *list_lock; /* protects write access to the hash lists */
  339. int i;
  340. for (i = 0; i < hash->size; i++) {
  341. head = &hash->table[i];
  342. list_lock = &hash->list_locks[i];
  343. spin_lock_bh(list_lock);
  344. hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
  345. head, hash_entry) {
  346. if (tt_local_entry->flags & TT_CLIENT_NOPURGE)
  347. continue;
  348. /* entry already marked for deletion */
  349. if (tt_local_entry->flags & TT_CLIENT_PENDING)
  350. continue;
  351. if (!is_out_of_time(tt_local_entry->last_seen,
  352. TT_LOCAL_TIMEOUT * 1000))
  353. continue;
  354. tt_local_set_pending(bat_priv, tt_local_entry,
  355. TT_CLIENT_DEL);
  356. bat_dbg(DBG_TT, bat_priv, "Local tt entry (%pM) "
  357. "pending to be removed: timed out\n",
  358. tt_local_entry->addr);
  359. }
  360. spin_unlock_bh(list_lock);
  361. }
  362. }
  363. static void tt_local_table_free(struct bat_priv *bat_priv)
  364. {
  365. struct hashtable_t *hash;
  366. spinlock_t *list_lock; /* protects write access to the hash lists */
  367. struct tt_local_entry *tt_local_entry;
  368. struct hlist_node *node, *node_tmp;
  369. struct hlist_head *head;
  370. int i;
  371. if (!bat_priv->tt_local_hash)
  372. return;
  373. hash = bat_priv->tt_local_hash;
  374. for (i = 0; i < hash->size; i++) {
  375. head = &hash->table[i];
  376. list_lock = &hash->list_locks[i];
  377. spin_lock_bh(list_lock);
  378. hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
  379. head, hash_entry) {
  380. hlist_del_rcu(node);
  381. tt_local_entry_free_ref(tt_local_entry);
  382. }
  383. spin_unlock_bh(list_lock);
  384. }
  385. hash_destroy(hash);
  386. bat_priv->tt_local_hash = NULL;
  387. }
  388. static int tt_global_init(struct bat_priv *bat_priv)
  389. {
  390. if (bat_priv->tt_global_hash)
  391. return 1;
  392. bat_priv->tt_global_hash = hash_new(1024);
  393. if (!bat_priv->tt_global_hash)
  394. return 0;
  395. return 1;
  396. }
  397. static void tt_changes_list_free(struct bat_priv *bat_priv)
  398. {
  399. struct tt_change_node *entry, *safe;
  400. spin_lock_bh(&bat_priv->tt_changes_list_lock);
  401. list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
  402. list) {
  403. list_del(&entry->list);
  404. kfree(entry);
  405. }
  406. atomic_set(&bat_priv->tt_local_changes, 0);
  407. spin_unlock_bh(&bat_priv->tt_changes_list_lock);
  408. }
  409. /* caller must hold orig_node refcount */
  410. int tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
  411. const unsigned char *tt_addr, uint8_t ttvn, bool roaming)
  412. {
  413. struct tt_global_entry *tt_global_entry;
  414. struct orig_node *orig_node_tmp;
  415. int ret = 0;
  416. tt_global_entry = tt_global_hash_find(bat_priv, tt_addr);
  417. if (!tt_global_entry) {
  418. tt_global_entry =
  419. kmalloc(sizeof(*tt_global_entry),
  420. GFP_ATOMIC);
  421. if (!tt_global_entry)
  422. goto out;
  423. memcpy(tt_global_entry->addr, tt_addr, ETH_ALEN);
  424. /* Assign the new orig_node */
  425. atomic_inc(&orig_node->refcount);
  426. tt_global_entry->orig_node = orig_node;
  427. tt_global_entry->ttvn = ttvn;
  428. tt_global_entry->flags = NO_FLAGS;
  429. tt_global_entry->roam_at = 0;
  430. atomic_set(&tt_global_entry->refcount, 2);
  431. hash_add(bat_priv->tt_global_hash, compare_gtt,
  432. choose_orig, tt_global_entry,
  433. &tt_global_entry->hash_entry);
  434. atomic_inc(&orig_node->tt_size);
  435. } else {
  436. if (tt_global_entry->orig_node != orig_node) {
  437. atomic_dec(&tt_global_entry->orig_node->tt_size);
  438. orig_node_tmp = tt_global_entry->orig_node;
  439. atomic_inc(&orig_node->refcount);
  440. tt_global_entry->orig_node = orig_node;
  441. orig_node_free_ref(orig_node_tmp);
  442. atomic_inc(&orig_node->tt_size);
  443. }
  444. tt_global_entry->ttvn = ttvn;
  445. tt_global_entry->flags = NO_FLAGS;
  446. tt_global_entry->roam_at = 0;
  447. }
  448. bat_dbg(DBG_TT, bat_priv,
  449. "Creating new global tt entry: %pM (via %pM)\n",
  450. tt_global_entry->addr, orig_node->orig);
  451. /* remove address from local hash if present */
  452. tt_local_remove(bat_priv, tt_global_entry->addr,
  453. "global tt received", roaming);
  454. ret = 1;
  455. out:
  456. if (tt_global_entry)
  457. tt_global_entry_free_ref(tt_global_entry);
  458. return ret;
  459. }
  460. int tt_global_seq_print_text(struct seq_file *seq, void *offset)
  461. {
  462. struct net_device *net_dev = (struct net_device *)seq->private;
  463. struct bat_priv *bat_priv = netdev_priv(net_dev);
  464. struct hashtable_t *hash = bat_priv->tt_global_hash;
  465. struct tt_global_entry *tt_global_entry;
  466. struct hard_iface *primary_if;
  467. struct hlist_node *node;
  468. struct hlist_head *head;
  469. size_t buf_size, pos;
  470. char *buff;
  471. int i, ret = 0;
  472. primary_if = primary_if_get_selected(bat_priv);
  473. if (!primary_if) {
  474. ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
  475. "specify interfaces to enable it\n",
  476. net_dev->name);
  477. goto out;
  478. }
  479. if (primary_if->if_status != IF_ACTIVE) {
  480. ret = seq_printf(seq, "BATMAN mesh %s disabled - "
  481. "primary interface not active\n",
  482. net_dev->name);
  483. goto out;
  484. }
  485. seq_printf(seq,
  486. "Globally announced TT entries received via the mesh %s\n",
  487. net_dev->name);
  488. seq_printf(seq, " %-13s %s %-15s %s\n",
  489. "Client", "(TTVN)", "Originator", "(Curr TTVN)");
  490. buf_size = 1;
  491. /* Estimate length for: " * xx:xx:xx:xx:xx:xx (ttvn) via
  492. * xx:xx:xx:xx:xx:xx (cur_ttvn)\n"*/
  493. for (i = 0; i < hash->size; i++) {
  494. head = &hash->table[i];
  495. rcu_read_lock();
  496. __hlist_for_each_rcu(node, head)
  497. buf_size += 59;
  498. rcu_read_unlock();
  499. }
  500. buff = kmalloc(buf_size, GFP_ATOMIC);
  501. if (!buff) {
  502. ret = -ENOMEM;
  503. goto out;
  504. }
  505. buff[0] = '\0';
  506. pos = 0;
  507. for (i = 0; i < hash->size; i++) {
  508. head = &hash->table[i];
  509. rcu_read_lock();
  510. hlist_for_each_entry_rcu(tt_global_entry, node,
  511. head, hash_entry) {
  512. pos += snprintf(buff + pos, 61,
  513. " * %pM (%3u) via %pM (%3u)\n",
  514. tt_global_entry->addr,
  515. tt_global_entry->ttvn,
  516. tt_global_entry->orig_node->orig,
  517. (uint8_t) atomic_read(
  518. &tt_global_entry->orig_node->
  519. last_ttvn));
  520. }
  521. rcu_read_unlock();
  522. }
  523. seq_printf(seq, "%s", buff);
  524. kfree(buff);
  525. out:
  526. if (primary_if)
  527. hardif_free_ref(primary_if);
  528. return ret;
  529. }
  530. static void _tt_global_del(struct bat_priv *bat_priv,
  531. struct tt_global_entry *tt_global_entry,
  532. const char *message)
  533. {
  534. if (!tt_global_entry)
  535. goto out;
  536. bat_dbg(DBG_TT, bat_priv,
  537. "Deleting global tt entry %pM (via %pM): %s\n",
  538. tt_global_entry->addr, tt_global_entry->orig_node->orig,
  539. message);
  540. atomic_dec(&tt_global_entry->orig_node->tt_size);
  541. hash_remove(bat_priv->tt_global_hash, compare_gtt, choose_orig,
  542. tt_global_entry->addr);
  543. out:
  544. if (tt_global_entry)
  545. tt_global_entry_free_ref(tt_global_entry);
  546. }
  547. void tt_global_del(struct bat_priv *bat_priv,
  548. struct orig_node *orig_node, const unsigned char *addr,
  549. const char *message, bool roaming)
  550. {
  551. struct tt_global_entry *tt_global_entry = NULL;
  552. tt_global_entry = tt_global_hash_find(bat_priv, addr);
  553. if (!tt_global_entry)
  554. goto out;
  555. if (tt_global_entry->orig_node == orig_node) {
  556. if (roaming) {
  557. tt_global_entry->flags |= TT_CLIENT_ROAM;
  558. tt_global_entry->roam_at = jiffies;
  559. goto out;
  560. }
  561. _tt_global_del(bat_priv, tt_global_entry, message);
  562. }
  563. out:
  564. if (tt_global_entry)
  565. tt_global_entry_free_ref(tt_global_entry);
  566. }
  567. void tt_global_del_orig(struct bat_priv *bat_priv,
  568. struct orig_node *orig_node, const char *message)
  569. {
  570. struct tt_global_entry *tt_global_entry;
  571. int i;
  572. struct hashtable_t *hash = bat_priv->tt_global_hash;
  573. struct hlist_node *node, *safe;
  574. struct hlist_head *head;
  575. spinlock_t *list_lock; /* protects write access to the hash lists */
  576. if (!hash)
  577. return;
  578. for (i = 0; i < hash->size; i++) {
  579. head = &hash->table[i];
  580. list_lock = &hash->list_locks[i];
  581. spin_lock_bh(list_lock);
  582. hlist_for_each_entry_safe(tt_global_entry, node, safe,
  583. head, hash_entry) {
  584. if (tt_global_entry->orig_node == orig_node) {
  585. bat_dbg(DBG_TT, bat_priv,
  586. "Deleting global tt entry %pM "
  587. "(via %pM): originator time out\n",
  588. tt_global_entry->addr,
  589. tt_global_entry->orig_node->orig);
  590. hlist_del_rcu(node);
  591. tt_global_entry_free_ref(tt_global_entry);
  592. }
  593. }
  594. spin_unlock_bh(list_lock);
  595. }
  596. atomic_set(&orig_node->tt_size, 0);
  597. }
  598. static void tt_global_roam_purge(struct bat_priv *bat_priv)
  599. {
  600. struct hashtable_t *hash = bat_priv->tt_global_hash;
  601. struct tt_global_entry *tt_global_entry;
  602. struct hlist_node *node, *node_tmp;
  603. struct hlist_head *head;
  604. spinlock_t *list_lock; /* protects write access to the hash lists */
  605. int i;
  606. for (i = 0; i < hash->size; i++) {
  607. head = &hash->table[i];
  608. list_lock = &hash->list_locks[i];
  609. spin_lock_bh(list_lock);
  610. hlist_for_each_entry_safe(tt_global_entry, node, node_tmp,
  611. head, hash_entry) {
  612. if (!(tt_global_entry->flags & TT_CLIENT_ROAM))
  613. continue;
  614. if (!is_out_of_time(tt_global_entry->roam_at,
  615. TT_CLIENT_ROAM_TIMEOUT * 1000))
  616. continue;
  617. bat_dbg(DBG_TT, bat_priv, "Deleting global "
  618. "tt entry (%pM): Roaming timeout\n",
  619. tt_global_entry->addr);
  620. atomic_dec(&tt_global_entry->orig_node->tt_size);
  621. hlist_del_rcu(node);
  622. tt_global_entry_free_ref(tt_global_entry);
  623. }
  624. spin_unlock_bh(list_lock);
  625. }
  626. }
  627. static void tt_global_table_free(struct bat_priv *bat_priv)
  628. {
  629. struct hashtable_t *hash;
  630. spinlock_t *list_lock; /* protects write access to the hash lists */
  631. struct tt_global_entry *tt_global_entry;
  632. struct hlist_node *node, *node_tmp;
  633. struct hlist_head *head;
  634. int i;
  635. if (!bat_priv->tt_global_hash)
  636. return;
  637. hash = bat_priv->tt_global_hash;
  638. for (i = 0; i < hash->size; i++) {
  639. head = &hash->table[i];
  640. list_lock = &hash->list_locks[i];
  641. spin_lock_bh(list_lock);
  642. hlist_for_each_entry_safe(tt_global_entry, node, node_tmp,
  643. head, hash_entry) {
  644. hlist_del_rcu(node);
  645. tt_global_entry_free_ref(tt_global_entry);
  646. }
  647. spin_unlock_bh(list_lock);
  648. }
  649. hash_destroy(hash);
  650. bat_priv->tt_global_hash = NULL;
  651. }
  652. struct orig_node *transtable_search(struct bat_priv *bat_priv,
  653. const uint8_t *addr)
  654. {
  655. struct tt_global_entry *tt_global_entry;
  656. struct orig_node *orig_node = NULL;
  657. tt_global_entry = tt_global_hash_find(bat_priv, addr);
  658. if (!tt_global_entry)
  659. goto out;
  660. if (!atomic_inc_not_zero(&tt_global_entry->orig_node->refcount))
  661. goto free_tt;
  662. /* A global client marked as PENDING has already moved from that
  663. * originator */
  664. if (tt_global_entry->flags & TT_CLIENT_PENDING)
  665. goto free_tt;
  666. orig_node = tt_global_entry->orig_node;
  667. free_tt:
  668. tt_global_entry_free_ref(tt_global_entry);
  669. out:
  670. return orig_node;
  671. }
  672. /* Calculates the checksum of the local table of a given orig_node */
  673. uint16_t tt_global_crc(struct bat_priv *bat_priv, struct orig_node *orig_node)
  674. {
  675. uint16_t total = 0, total_one;
  676. struct hashtable_t *hash = bat_priv->tt_global_hash;
  677. struct tt_global_entry *tt_global_entry;
  678. struct hlist_node *node;
  679. struct hlist_head *head;
  680. int i, j;
  681. for (i = 0; i < hash->size; i++) {
  682. head = &hash->table[i];
  683. rcu_read_lock();
  684. hlist_for_each_entry_rcu(tt_global_entry, node,
  685. head, hash_entry) {
  686. if (compare_eth(tt_global_entry->orig_node,
  687. orig_node)) {
  688. /* Roaming clients are in the global table for
  689. * consistency only. They don't have to be
  690. * taken into account while computing the
  691. * global crc */
  692. if (tt_global_entry->flags & TT_CLIENT_ROAM)
  693. continue;
  694. total_one = 0;
  695. for (j = 0; j < ETH_ALEN; j++)
  696. total_one = crc16_byte(total_one,
  697. tt_global_entry->addr[j]);
  698. total ^= total_one;
  699. }
  700. }
  701. rcu_read_unlock();
  702. }
  703. return total;
  704. }
  705. /* Calculates the checksum of the local table */
  706. uint16_t tt_local_crc(struct bat_priv *bat_priv)
  707. {
  708. uint16_t total = 0, total_one;
  709. struct hashtable_t *hash = bat_priv->tt_local_hash;
  710. struct tt_local_entry *tt_local_entry;
  711. struct hlist_node *node;
  712. struct hlist_head *head;
  713. int i, j;
  714. for (i = 0; i < hash->size; i++) {
  715. head = &hash->table[i];
  716. rcu_read_lock();
  717. hlist_for_each_entry_rcu(tt_local_entry, node,
  718. head, hash_entry) {
  719. /* not yet committed clients have not to be taken into
  720. * account while computing the CRC */
  721. if (tt_local_entry->flags & TT_CLIENT_NEW)
  722. continue;
  723. total_one = 0;
  724. for (j = 0; j < ETH_ALEN; j++)
  725. total_one = crc16_byte(total_one,
  726. tt_local_entry->addr[j]);
  727. total ^= total_one;
  728. }
  729. rcu_read_unlock();
  730. }
  731. return total;
  732. }
  733. static void tt_req_list_free(struct bat_priv *bat_priv)
  734. {
  735. struct tt_req_node *node, *safe;
  736. spin_lock_bh(&bat_priv->tt_req_list_lock);
  737. list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
  738. list_del(&node->list);
  739. kfree(node);
  740. }
  741. spin_unlock_bh(&bat_priv->tt_req_list_lock);
  742. }
  743. void tt_save_orig_buffer(struct bat_priv *bat_priv, struct orig_node *orig_node,
  744. const unsigned char *tt_buff, uint8_t tt_num_changes)
  745. {
  746. uint16_t tt_buff_len = tt_len(tt_num_changes);
  747. /* Replace the old buffer only if I received something in the
  748. * last OGM (the OGM could carry no changes) */
  749. spin_lock_bh(&orig_node->tt_buff_lock);
  750. if (tt_buff_len > 0) {
  751. kfree(orig_node->tt_buff);
  752. orig_node->tt_buff_len = 0;
  753. orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
  754. if (orig_node->tt_buff) {
  755. memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
  756. orig_node->tt_buff_len = tt_buff_len;
  757. }
  758. }
  759. spin_unlock_bh(&orig_node->tt_buff_lock);
  760. }
  761. static void tt_req_purge(struct bat_priv *bat_priv)
  762. {
  763. struct tt_req_node *node, *safe;
  764. spin_lock_bh(&bat_priv->tt_req_list_lock);
  765. list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
  766. if (is_out_of_time(node->issued_at,
  767. TT_REQUEST_TIMEOUT * 1000)) {
  768. list_del(&node->list);
  769. kfree(node);
  770. }
  771. }
  772. spin_unlock_bh(&bat_priv->tt_req_list_lock);
  773. }
  774. /* returns the pointer to the new tt_req_node struct if no request
  775. * has already been issued for this orig_node, NULL otherwise */
  776. static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv,
  777. struct orig_node *orig_node)
  778. {
  779. struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
  780. spin_lock_bh(&bat_priv->tt_req_list_lock);
  781. list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
  782. if (compare_eth(tt_req_node_tmp, orig_node) &&
  783. !is_out_of_time(tt_req_node_tmp->issued_at,
  784. TT_REQUEST_TIMEOUT * 1000))
  785. goto unlock;
  786. }
  787. tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
  788. if (!tt_req_node)
  789. goto unlock;
  790. memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
  791. tt_req_node->issued_at = jiffies;
  792. list_add(&tt_req_node->list, &bat_priv->tt_req_list);
  793. unlock:
  794. spin_unlock_bh(&bat_priv->tt_req_list_lock);
  795. return tt_req_node;
  796. }
  797. /* data_ptr is useless here, but has to be kept to respect the prototype */
  798. static int tt_local_valid_entry(const void *entry_ptr, const void *data_ptr)
  799. {
  800. const struct tt_local_entry *tt_local_entry = entry_ptr;
  801. if (tt_local_entry->flags & TT_CLIENT_NEW)
  802. return 0;
  803. return 1;
  804. }
  805. static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr)
  806. {
  807. const struct tt_global_entry *tt_global_entry = entry_ptr;
  808. const struct orig_node *orig_node = data_ptr;
  809. if (tt_global_entry->flags & TT_CLIENT_ROAM)
  810. return 0;
  811. return (tt_global_entry->orig_node == orig_node);
  812. }
  813. static struct sk_buff *tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
  814. struct hashtable_t *hash,
  815. struct hard_iface *primary_if,
  816. int (*valid_cb)(const void *,
  817. const void *),
  818. void *cb_data)
  819. {
  820. struct tt_local_entry *tt_local_entry;
  821. struct tt_query_packet *tt_response;
  822. struct tt_change *tt_change;
  823. struct hlist_node *node;
  824. struct hlist_head *head;
  825. struct sk_buff *skb = NULL;
  826. uint16_t tt_tot, tt_count;
  827. ssize_t tt_query_size = sizeof(struct tt_query_packet);
  828. int i;
  829. if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
  830. tt_len = primary_if->soft_iface->mtu - tt_query_size;
  831. tt_len -= tt_len % sizeof(struct tt_change);
  832. }
  833. tt_tot = tt_len / sizeof(struct tt_change);
  834. skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
  835. if (!skb)
  836. goto out;
  837. skb_reserve(skb, ETH_HLEN);
  838. tt_response = (struct tt_query_packet *)skb_put(skb,
  839. tt_query_size + tt_len);
  840. tt_response->ttvn = ttvn;
  841. tt_change = (struct tt_change *)(skb->data + tt_query_size);
  842. tt_count = 0;
  843. rcu_read_lock();
  844. for (i = 0; i < hash->size; i++) {
  845. head = &hash->table[i];
  846. hlist_for_each_entry_rcu(tt_local_entry, node,
  847. head, hash_entry) {
  848. if (tt_count == tt_tot)
  849. break;
  850. if ((valid_cb) && (!valid_cb(tt_local_entry, cb_data)))
  851. continue;
  852. memcpy(tt_change->addr, tt_local_entry->addr, ETH_ALEN);
  853. tt_change->flags = NO_FLAGS;
  854. tt_count++;
  855. tt_change++;
  856. }
  857. }
  858. rcu_read_unlock();
  859. /* store in the message the number of entries we have successfully
  860. * copied */
  861. tt_response->tt_data = htons(tt_count);
  862. out:
  863. return skb;
  864. }
  865. int send_tt_request(struct bat_priv *bat_priv, struct orig_node *dst_orig_node,
  866. uint8_t ttvn, uint16_t tt_crc, bool full_table)
  867. {
  868. struct sk_buff *skb = NULL;
  869. struct tt_query_packet *tt_request;
  870. struct neigh_node *neigh_node = NULL;
  871. struct hard_iface *primary_if;
  872. struct tt_req_node *tt_req_node = NULL;
  873. int ret = 1;
  874. primary_if = primary_if_get_selected(bat_priv);
  875. if (!primary_if)
  876. goto out;
  877. /* The new tt_req will be issued only if I'm not waiting for a
  878. * reply from the same orig_node yet */
  879. tt_req_node = new_tt_req_node(bat_priv, dst_orig_node);
  880. if (!tt_req_node)
  881. goto out;
  882. skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
  883. if (!skb)
  884. goto out;
  885. skb_reserve(skb, ETH_HLEN);
  886. tt_request = (struct tt_query_packet *)skb_put(skb,
  887. sizeof(struct tt_query_packet));
  888. tt_request->packet_type = BAT_TT_QUERY;
  889. tt_request->version = COMPAT_VERSION;
  890. memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
  891. memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
  892. tt_request->ttl = TTL;
  893. tt_request->ttvn = ttvn;
  894. tt_request->tt_data = tt_crc;
  895. tt_request->flags = TT_REQUEST;
  896. if (full_table)
  897. tt_request->flags |= TT_FULL_TABLE;
  898. neigh_node = orig_node_get_router(dst_orig_node);
  899. if (!neigh_node)
  900. goto out;
  901. bat_dbg(DBG_TT, bat_priv, "Sending TT_REQUEST to %pM via %pM "
  902. "[%c]\n", dst_orig_node->orig, neigh_node->addr,
  903. (full_table ? 'F' : '.'));
  904. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  905. ret = 0;
  906. out:
  907. if (neigh_node)
  908. neigh_node_free_ref(neigh_node);
  909. if (primary_if)
  910. hardif_free_ref(primary_if);
  911. if (ret)
  912. kfree_skb(skb);
  913. if (ret && tt_req_node) {
  914. spin_lock_bh(&bat_priv->tt_req_list_lock);
  915. list_del(&tt_req_node->list);
  916. spin_unlock_bh(&bat_priv->tt_req_list_lock);
  917. kfree(tt_req_node);
  918. }
  919. return ret;
  920. }
  921. static bool send_other_tt_response(struct bat_priv *bat_priv,
  922. struct tt_query_packet *tt_request)
  923. {
  924. struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
  925. struct neigh_node *neigh_node = NULL;
  926. struct hard_iface *primary_if = NULL;
  927. uint8_t orig_ttvn, req_ttvn, ttvn;
  928. int ret = false;
  929. unsigned char *tt_buff;
  930. bool full_table;
  931. uint16_t tt_len, tt_tot;
  932. struct sk_buff *skb = NULL;
  933. struct tt_query_packet *tt_response;
  934. bat_dbg(DBG_TT, bat_priv,
  935. "Received TT_REQUEST from %pM for "
  936. "ttvn: %u (%pM) [%c]\n", tt_request->src,
  937. tt_request->ttvn, tt_request->dst,
  938. (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
  939. /* Let's get the orig node of the REAL destination */
  940. req_dst_orig_node = get_orig_node(bat_priv, tt_request->dst);
  941. if (!req_dst_orig_node)
  942. goto out;
  943. res_dst_orig_node = get_orig_node(bat_priv, tt_request->src);
  944. if (!res_dst_orig_node)
  945. goto out;
  946. neigh_node = orig_node_get_router(res_dst_orig_node);
  947. if (!neigh_node)
  948. goto out;
  949. primary_if = primary_if_get_selected(bat_priv);
  950. if (!primary_if)
  951. goto out;
  952. orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
  953. req_ttvn = tt_request->ttvn;
  954. /* I have not the requested data */
  955. if (orig_ttvn != req_ttvn ||
  956. tt_request->tt_data != req_dst_orig_node->tt_crc)
  957. goto out;
  958. /* If it has explicitly been requested the full table */
  959. if (tt_request->flags & TT_FULL_TABLE ||
  960. !req_dst_orig_node->tt_buff)
  961. full_table = true;
  962. else
  963. full_table = false;
  964. /* In this version, fragmentation is not implemented, then
  965. * I'll send only one packet with as much TT entries as I can */
  966. if (!full_table) {
  967. spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
  968. tt_len = req_dst_orig_node->tt_buff_len;
  969. tt_tot = tt_len / sizeof(struct tt_change);
  970. skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
  971. tt_len + ETH_HLEN);
  972. if (!skb)
  973. goto unlock;
  974. skb_reserve(skb, ETH_HLEN);
  975. tt_response = (struct tt_query_packet *)skb_put(skb,
  976. sizeof(struct tt_query_packet) + tt_len);
  977. tt_response->ttvn = req_ttvn;
  978. tt_response->tt_data = htons(tt_tot);
  979. tt_buff = skb->data + sizeof(struct tt_query_packet);
  980. /* Copy the last orig_node's OGM buffer */
  981. memcpy(tt_buff, req_dst_orig_node->tt_buff,
  982. req_dst_orig_node->tt_buff_len);
  983. spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
  984. } else {
  985. tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
  986. sizeof(struct tt_change);
  987. ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
  988. skb = tt_response_fill_table(tt_len, ttvn,
  989. bat_priv->tt_global_hash,
  990. primary_if, tt_global_valid_entry,
  991. req_dst_orig_node);
  992. if (!skb)
  993. goto out;
  994. tt_response = (struct tt_query_packet *)skb->data;
  995. }
  996. tt_response->packet_type = BAT_TT_QUERY;
  997. tt_response->version = COMPAT_VERSION;
  998. tt_response->ttl = TTL;
  999. memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
  1000. memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
  1001. tt_response->flags = TT_RESPONSE;
  1002. if (full_table)
  1003. tt_response->flags |= TT_FULL_TABLE;
  1004. bat_dbg(DBG_TT, bat_priv,
  1005. "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
  1006. res_dst_orig_node->orig, neigh_node->addr,
  1007. req_dst_orig_node->orig, req_ttvn);
  1008. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  1009. ret = true;
  1010. goto out;
  1011. unlock:
  1012. spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
  1013. out:
  1014. if (res_dst_orig_node)
  1015. orig_node_free_ref(res_dst_orig_node);
  1016. if (req_dst_orig_node)
  1017. orig_node_free_ref(req_dst_orig_node);
  1018. if (neigh_node)
  1019. neigh_node_free_ref(neigh_node);
  1020. if (primary_if)
  1021. hardif_free_ref(primary_if);
  1022. if (!ret)
  1023. kfree_skb(skb);
  1024. return ret;
  1025. }
  1026. static bool send_my_tt_response(struct bat_priv *bat_priv,
  1027. struct tt_query_packet *tt_request)
  1028. {
  1029. struct orig_node *orig_node = NULL;
  1030. struct neigh_node *neigh_node = NULL;
  1031. struct hard_iface *primary_if = NULL;
  1032. uint8_t my_ttvn, req_ttvn, ttvn;
  1033. int ret = false;
  1034. unsigned char *tt_buff;
  1035. bool full_table;
  1036. uint16_t tt_len, tt_tot;
  1037. struct sk_buff *skb = NULL;
  1038. struct tt_query_packet *tt_response;
  1039. bat_dbg(DBG_TT, bat_priv,
  1040. "Received TT_REQUEST from %pM for "
  1041. "ttvn: %u (me) [%c]\n", tt_request->src,
  1042. tt_request->ttvn,
  1043. (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
  1044. my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
  1045. req_ttvn = tt_request->ttvn;
  1046. orig_node = get_orig_node(bat_priv, tt_request->src);
  1047. if (!orig_node)
  1048. goto out;
  1049. neigh_node = orig_node_get_router(orig_node);
  1050. if (!neigh_node)
  1051. goto out;
  1052. primary_if = primary_if_get_selected(bat_priv);
  1053. if (!primary_if)
  1054. goto out;
  1055. /* If the full table has been explicitly requested or the gap
  1056. * is too big send the whole local translation table */
  1057. if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
  1058. !bat_priv->tt_buff)
  1059. full_table = true;
  1060. else
  1061. full_table = false;
  1062. /* In this version, fragmentation is not implemented, then
  1063. * I'll send only one packet with as much TT entries as I can */
  1064. if (!full_table) {
  1065. spin_lock_bh(&bat_priv->tt_buff_lock);
  1066. tt_len = bat_priv->tt_buff_len;
  1067. tt_tot = tt_len / sizeof(struct tt_change);
  1068. skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
  1069. tt_len + ETH_HLEN);
  1070. if (!skb)
  1071. goto unlock;
  1072. skb_reserve(skb, ETH_HLEN);
  1073. tt_response = (struct tt_query_packet *)skb_put(skb,
  1074. sizeof(struct tt_query_packet) + tt_len);
  1075. tt_response->ttvn = req_ttvn;
  1076. tt_response->tt_data = htons(tt_tot);
  1077. tt_buff = skb->data + sizeof(struct tt_query_packet);
  1078. memcpy(tt_buff, bat_priv->tt_buff,
  1079. bat_priv->tt_buff_len);
  1080. spin_unlock_bh(&bat_priv->tt_buff_lock);
  1081. } else {
  1082. tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
  1083. sizeof(struct tt_change);
  1084. ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
  1085. skb = tt_response_fill_table(tt_len, ttvn,
  1086. bat_priv->tt_local_hash,
  1087. primary_if, tt_local_valid_entry,
  1088. NULL);
  1089. if (!skb)
  1090. goto out;
  1091. tt_response = (struct tt_query_packet *)skb->data;
  1092. }
  1093. tt_response->packet_type = BAT_TT_QUERY;
  1094. tt_response->version = COMPAT_VERSION;
  1095. tt_response->ttl = TTL;
  1096. memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
  1097. memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
  1098. tt_response->flags = TT_RESPONSE;
  1099. if (full_table)
  1100. tt_response->flags |= TT_FULL_TABLE;
  1101. bat_dbg(DBG_TT, bat_priv,
  1102. "Sending TT_RESPONSE to %pM via %pM [%c]\n",
  1103. orig_node->orig, neigh_node->addr,
  1104. (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
  1105. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  1106. ret = true;
  1107. goto out;
  1108. unlock:
  1109. spin_unlock_bh(&bat_priv->tt_buff_lock);
  1110. out:
  1111. if (orig_node)
  1112. orig_node_free_ref(orig_node);
  1113. if (neigh_node)
  1114. neigh_node_free_ref(neigh_node);
  1115. if (primary_if)
  1116. hardif_free_ref(primary_if);
  1117. if (!ret)
  1118. kfree_skb(skb);
  1119. /* This packet was for me, so it doesn't need to be re-routed */
  1120. return true;
  1121. }
  1122. bool send_tt_response(struct bat_priv *bat_priv,
  1123. struct tt_query_packet *tt_request)
  1124. {
  1125. if (is_my_mac(tt_request->dst))
  1126. return send_my_tt_response(bat_priv, tt_request);
  1127. else
  1128. return send_other_tt_response(bat_priv, tt_request);
  1129. }
  1130. static void _tt_update_changes(struct bat_priv *bat_priv,
  1131. struct orig_node *orig_node,
  1132. struct tt_change *tt_change,
  1133. uint16_t tt_num_changes, uint8_t ttvn)
  1134. {
  1135. int i;
  1136. for (i = 0; i < tt_num_changes; i++) {
  1137. if ((tt_change + i)->flags & TT_CLIENT_DEL)
  1138. tt_global_del(bat_priv, orig_node,
  1139. (tt_change + i)->addr,
  1140. "tt removed by changes",
  1141. (tt_change + i)->flags & TT_CLIENT_ROAM);
  1142. else
  1143. if (!tt_global_add(bat_priv, orig_node,
  1144. (tt_change + i)->addr, ttvn, false))
  1145. /* In case of problem while storing a
  1146. * global_entry, we stop the updating
  1147. * procedure without committing the
  1148. * ttvn change. This will avoid to send
  1149. * corrupted data on tt_request
  1150. */
  1151. return;
  1152. }
  1153. }
  1154. static void tt_fill_gtable(struct bat_priv *bat_priv,
  1155. struct tt_query_packet *tt_response)
  1156. {
  1157. struct orig_node *orig_node = NULL;
  1158. orig_node = orig_hash_find(bat_priv, tt_response->src);
  1159. if (!orig_node)
  1160. goto out;
  1161. /* Purge the old table first.. */
  1162. tt_global_del_orig(bat_priv, orig_node, "Received full table");
  1163. _tt_update_changes(bat_priv, orig_node,
  1164. (struct tt_change *)(tt_response + 1),
  1165. tt_response->tt_data, tt_response->ttvn);
  1166. spin_lock_bh(&orig_node->tt_buff_lock);
  1167. kfree(orig_node->tt_buff);
  1168. orig_node->tt_buff_len = 0;
  1169. orig_node->tt_buff = NULL;
  1170. spin_unlock_bh(&orig_node->tt_buff_lock);
  1171. atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
  1172. out:
  1173. if (orig_node)
  1174. orig_node_free_ref(orig_node);
  1175. }
  1176. void tt_update_changes(struct bat_priv *bat_priv, struct orig_node *orig_node,
  1177. uint16_t tt_num_changes, uint8_t ttvn,
  1178. struct tt_change *tt_change)
  1179. {
  1180. _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes,
  1181. ttvn);
  1182. tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change,
  1183. tt_num_changes);
  1184. atomic_set(&orig_node->last_ttvn, ttvn);
  1185. }
  1186. bool is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
  1187. {
  1188. struct tt_local_entry *tt_local_entry = NULL;
  1189. bool ret = false;
  1190. tt_local_entry = tt_local_hash_find(bat_priv, addr);
  1191. if (!tt_local_entry)
  1192. goto out;
  1193. /* Check if the client has been logically deleted (but is kept for
  1194. * consistency purpose) */
  1195. if (tt_local_entry->flags & TT_CLIENT_PENDING)
  1196. goto out;
  1197. ret = true;
  1198. out:
  1199. if (tt_local_entry)
  1200. tt_local_entry_free_ref(tt_local_entry);
  1201. return ret;
  1202. }
  1203. void handle_tt_response(struct bat_priv *bat_priv,
  1204. struct tt_query_packet *tt_response)
  1205. {
  1206. struct tt_req_node *node, *safe;
  1207. struct orig_node *orig_node = NULL;
  1208. bat_dbg(DBG_TT, bat_priv, "Received TT_RESPONSE from %pM for "
  1209. "ttvn %d t_size: %d [%c]\n",
  1210. tt_response->src, tt_response->ttvn,
  1211. tt_response->tt_data,
  1212. (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
  1213. orig_node = orig_hash_find(bat_priv, tt_response->src);
  1214. if (!orig_node)
  1215. goto out;
  1216. if (tt_response->flags & TT_FULL_TABLE)
  1217. tt_fill_gtable(bat_priv, tt_response);
  1218. else
  1219. tt_update_changes(bat_priv, orig_node, tt_response->tt_data,
  1220. tt_response->ttvn,
  1221. (struct tt_change *)(tt_response + 1));
  1222. /* Delete the tt_req_node from pending tt_requests list */
  1223. spin_lock_bh(&bat_priv->tt_req_list_lock);
  1224. list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
  1225. if (!compare_eth(node->addr, tt_response->src))
  1226. continue;
  1227. list_del(&node->list);
  1228. kfree(node);
  1229. }
  1230. spin_unlock_bh(&bat_priv->tt_req_list_lock);
  1231. /* Recalculate the CRC for this orig_node and store it */
  1232. orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
  1233. /* Roaming phase is over: tables are in sync again. I can
  1234. * unset the flag */
  1235. orig_node->tt_poss_change = false;
  1236. out:
  1237. if (orig_node)
  1238. orig_node_free_ref(orig_node);
  1239. }
  1240. int tt_init(struct bat_priv *bat_priv)
  1241. {
  1242. if (!tt_local_init(bat_priv))
  1243. return 0;
  1244. if (!tt_global_init(bat_priv))
  1245. return 0;
  1246. tt_start_timer(bat_priv);
  1247. return 1;
  1248. }
  1249. static void tt_roam_list_free(struct bat_priv *bat_priv)
  1250. {
  1251. struct tt_roam_node *node, *safe;
  1252. spin_lock_bh(&bat_priv->tt_roam_list_lock);
  1253. list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
  1254. list_del(&node->list);
  1255. kfree(node);
  1256. }
  1257. spin_unlock_bh(&bat_priv->tt_roam_list_lock);
  1258. }
  1259. static void tt_roam_purge(struct bat_priv *bat_priv)
  1260. {
  1261. struct tt_roam_node *node, *safe;
  1262. spin_lock_bh(&bat_priv->tt_roam_list_lock);
  1263. list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
  1264. if (!is_out_of_time(node->first_time,
  1265. ROAMING_MAX_TIME * 1000))
  1266. continue;
  1267. list_del(&node->list);
  1268. kfree(node);
  1269. }
  1270. spin_unlock_bh(&bat_priv->tt_roam_list_lock);
  1271. }
  1272. /* This function checks whether the client already reached the
  1273. * maximum number of possible roaming phases. In this case the ROAMING_ADV
  1274. * will not be sent.
  1275. *
  1276. * returns true if the ROAMING_ADV can be sent, false otherwise */
  1277. static bool tt_check_roam_count(struct bat_priv *bat_priv,
  1278. uint8_t *client)
  1279. {
  1280. struct tt_roam_node *tt_roam_node;
  1281. bool ret = false;
  1282. spin_lock_bh(&bat_priv->tt_roam_list_lock);
  1283. /* The new tt_req will be issued only if I'm not waiting for a
  1284. * reply from the same orig_node yet */
  1285. list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
  1286. if (!compare_eth(tt_roam_node->addr, client))
  1287. continue;
  1288. if (is_out_of_time(tt_roam_node->first_time,
  1289. ROAMING_MAX_TIME * 1000))
  1290. continue;
  1291. if (!atomic_dec_not_zero(&tt_roam_node->counter))
  1292. /* Sorry, you roamed too many times! */
  1293. goto unlock;
  1294. ret = true;
  1295. break;
  1296. }
  1297. if (!ret) {
  1298. tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
  1299. if (!tt_roam_node)
  1300. goto unlock;
  1301. tt_roam_node->first_time = jiffies;
  1302. atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
  1303. memcpy(tt_roam_node->addr, client, ETH_ALEN);
  1304. list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
  1305. ret = true;
  1306. }
  1307. unlock:
  1308. spin_unlock_bh(&bat_priv->tt_roam_list_lock);
  1309. return ret;
  1310. }
  1311. void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
  1312. struct orig_node *orig_node)
  1313. {
  1314. struct neigh_node *neigh_node = NULL;
  1315. struct sk_buff *skb = NULL;
  1316. struct roam_adv_packet *roam_adv_packet;
  1317. int ret = 1;
  1318. struct hard_iface *primary_if;
  1319. /* before going on we have to check whether the client has
  1320. * already roamed to us too many times */
  1321. if (!tt_check_roam_count(bat_priv, client))
  1322. goto out;
  1323. skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
  1324. if (!skb)
  1325. goto out;
  1326. skb_reserve(skb, ETH_HLEN);
  1327. roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
  1328. sizeof(struct roam_adv_packet));
  1329. roam_adv_packet->packet_type = BAT_ROAM_ADV;
  1330. roam_adv_packet->version = COMPAT_VERSION;
  1331. roam_adv_packet->ttl = TTL;
  1332. primary_if = primary_if_get_selected(bat_priv);
  1333. if (!primary_if)
  1334. goto out;
  1335. memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
  1336. hardif_free_ref(primary_if);
  1337. memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
  1338. memcpy(roam_adv_packet->client, client, ETH_ALEN);
  1339. neigh_node = orig_node_get_router(orig_node);
  1340. if (!neigh_node)
  1341. goto out;
  1342. bat_dbg(DBG_TT, bat_priv,
  1343. "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
  1344. orig_node->orig, client, neigh_node->addr);
  1345. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  1346. ret = 0;
  1347. out:
  1348. if (neigh_node)
  1349. neigh_node_free_ref(neigh_node);
  1350. if (ret)
  1351. kfree_skb(skb);
  1352. return;
  1353. }
  1354. static void tt_purge(struct work_struct *work)
  1355. {
  1356. struct delayed_work *delayed_work =
  1357. container_of(work, struct delayed_work, work);
  1358. struct bat_priv *bat_priv =
  1359. container_of(delayed_work, struct bat_priv, tt_work);
  1360. tt_local_purge(bat_priv);
  1361. tt_global_roam_purge(bat_priv);
  1362. tt_req_purge(bat_priv);
  1363. tt_roam_purge(bat_priv);
  1364. tt_start_timer(bat_priv);
  1365. }
  1366. void tt_free(struct bat_priv *bat_priv)
  1367. {
  1368. cancel_delayed_work_sync(&bat_priv->tt_work);
  1369. tt_local_table_free(bat_priv);
  1370. tt_global_table_free(bat_priv);
  1371. tt_req_list_free(bat_priv);
  1372. tt_changes_list_free(bat_priv);
  1373. tt_roam_list_free(bat_priv);
  1374. kfree(bat_priv->tt_buff);
  1375. }
  1376. /* This function will reset the specified flags from all the entries in
  1377. * the given hash table and will increment num_local_tt for each involved
  1378. * entry */
  1379. static void tt_local_reset_flags(struct bat_priv *bat_priv, uint16_t flags)
  1380. {
  1381. int i;
  1382. struct hashtable_t *hash = bat_priv->tt_local_hash;
  1383. struct hlist_head *head;
  1384. struct hlist_node *node;
  1385. struct tt_local_entry *tt_local_entry;
  1386. if (!hash)
  1387. return;
  1388. for (i = 0; i < hash->size; i++) {
  1389. head = &hash->table[i];
  1390. rcu_read_lock();
  1391. hlist_for_each_entry_rcu(tt_local_entry, node,
  1392. head, hash_entry) {
  1393. if (!(tt_local_entry->flags & flags))
  1394. continue;
  1395. tt_local_entry->flags &= ~flags;
  1396. atomic_inc(&bat_priv->num_local_tt);
  1397. }
  1398. rcu_read_unlock();
  1399. }
  1400. }
  1401. /* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
  1402. static void tt_local_purge_pending_clients(struct bat_priv *bat_priv)
  1403. {
  1404. struct hashtable_t *hash = bat_priv->tt_local_hash;
  1405. struct tt_local_entry *tt_local_entry;
  1406. struct hlist_node *node, *node_tmp;
  1407. struct hlist_head *head;
  1408. spinlock_t *list_lock; /* protects write access to the hash lists */
  1409. int i;
  1410. if (!hash)
  1411. return;
  1412. for (i = 0; i < hash->size; i++) {
  1413. head = &hash->table[i];
  1414. list_lock = &hash->list_locks[i];
  1415. spin_lock_bh(list_lock);
  1416. hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
  1417. head, hash_entry) {
  1418. if (!(tt_local_entry->flags & TT_CLIENT_PENDING))
  1419. continue;
  1420. bat_dbg(DBG_TT, bat_priv, "Deleting local tt entry "
  1421. "(%pM): pending\n", tt_local_entry->addr);
  1422. atomic_dec(&bat_priv->num_local_tt);
  1423. hlist_del_rcu(node);
  1424. tt_local_entry_free_ref(tt_local_entry);
  1425. }
  1426. spin_unlock_bh(list_lock);
  1427. }
  1428. }
  1429. void tt_commit_changes(struct bat_priv *bat_priv)
  1430. {
  1431. tt_local_reset_flags(bat_priv, TT_CLIENT_NEW);
  1432. tt_local_purge_pending_clients(bat_priv);
  1433. /* Increment the TTVN only once per OGM interval */
  1434. atomic_inc(&bat_priv->ttvn);
  1435. bat_priv->tt_poss_change = false;
  1436. }