translation-table.c 61 KB

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