translation-table.c 63 KB

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