translation-table.c 69 KB

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