translation-table.c 46 KB

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