translation-table.c 62 KB

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