translation-table.c 69 KB

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