translation-table.c 75 KB

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