translation-table.c 64 KB

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