translation-table.c 65 KB

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