translation-table.c 66 KB

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