translation-table.c 58 KB

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