translation-table.c 45 KB

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