translation-table.c 57 KB

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