translation-table.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238
  1. /*
  2. * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner, Simon Wunderlich, Antonio Quartulli
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA
  19. *
  20. */
  21. #include "main.h"
  22. #include "translation-table.h"
  23. #include "soft-interface.h"
  24. #include "hard-interface.h"
  25. #include "send.h"
  26. #include "hash.h"
  27. #include "originator.h"
  28. #include "routing.h"
  29. #include "bridge_loop_avoidance.h"
  30. #include <linux/crc16.h>
  31. static void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
  32. struct orig_node *orig_node);
  33. static void tt_purge(struct work_struct *work);
  34. static void tt_global_del_orig_list(struct tt_global_entry *tt_global_entry);
  35. /* returns 1 if they are the same mac addr */
  36. static int compare_tt(const struct hlist_node *node, const void *data2)
  37. {
  38. const void *data1 = container_of(node, struct tt_common_entry,
  39. hash_entry);
  40. return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
  41. }
  42. static void tt_start_timer(struct bat_priv *bat_priv)
  43. {
  44. INIT_DELAYED_WORK(&bat_priv->tt_work, tt_purge);
  45. queue_delayed_work(bat_event_workqueue, &bat_priv->tt_work,
  46. msecs_to_jiffies(5000));
  47. }
  48. static struct tt_common_entry *tt_hash_find(struct hashtable_t *hash,
  49. const void *data)
  50. {
  51. struct hlist_head *head;
  52. struct hlist_node *node;
  53. struct tt_common_entry *tt_common_entry, *tt_common_entry_tmp = NULL;
  54. uint32_t index;
  55. if (!hash)
  56. return NULL;
  57. index = choose_orig(data, hash->size);
  58. head = &hash->table[index];
  59. rcu_read_lock();
  60. hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) {
  61. if (!compare_eth(tt_common_entry, data))
  62. continue;
  63. if (!atomic_inc_not_zero(&tt_common_entry->refcount))
  64. continue;
  65. tt_common_entry_tmp = tt_common_entry;
  66. break;
  67. }
  68. rcu_read_unlock();
  69. return tt_common_entry_tmp;
  70. }
  71. static struct tt_local_entry *tt_local_hash_find(struct bat_priv *bat_priv,
  72. const void *data)
  73. {
  74. struct tt_common_entry *tt_common_entry;
  75. struct tt_local_entry *tt_local_entry = NULL;
  76. tt_common_entry = tt_hash_find(bat_priv->tt_local_hash, data);
  77. if (tt_common_entry)
  78. tt_local_entry = container_of(tt_common_entry,
  79. struct tt_local_entry, common);
  80. return tt_local_entry;
  81. }
  82. static struct tt_global_entry *tt_global_hash_find(struct bat_priv *bat_priv,
  83. const void *data)
  84. {
  85. struct tt_common_entry *tt_common_entry;
  86. struct tt_global_entry *tt_global_entry = NULL;
  87. tt_common_entry = tt_hash_find(bat_priv->tt_global_hash, data);
  88. if (tt_common_entry)
  89. tt_global_entry = container_of(tt_common_entry,
  90. struct tt_global_entry, common);
  91. return tt_global_entry;
  92. }
  93. static void tt_local_entry_free_ref(struct tt_local_entry *tt_local_entry)
  94. {
  95. if (atomic_dec_and_test(&tt_local_entry->common.refcount))
  96. kfree_rcu(tt_local_entry, common.rcu);
  97. }
  98. static void tt_global_entry_free_rcu(struct rcu_head *rcu)
  99. {
  100. struct tt_common_entry *tt_common_entry;
  101. struct tt_global_entry *tt_global_entry;
  102. tt_common_entry = container_of(rcu, struct tt_common_entry, rcu);
  103. tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
  104. common);
  105. kfree(tt_global_entry);
  106. }
  107. static void tt_global_entry_free_ref(struct tt_global_entry *tt_global_entry)
  108. {
  109. if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
  110. tt_global_del_orig_list(tt_global_entry);
  111. call_rcu(&tt_global_entry->common.rcu,
  112. tt_global_entry_free_rcu);
  113. }
  114. }
  115. static void tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
  116. {
  117. struct tt_orig_list_entry *orig_entry;
  118. orig_entry = container_of(rcu, struct tt_orig_list_entry, rcu);
  119. atomic_dec(&orig_entry->orig_node->tt_size);
  120. orig_node_free_ref(orig_entry->orig_node);
  121. kfree(orig_entry);
  122. }
  123. static void tt_orig_list_entry_free_ref(struct tt_orig_list_entry *orig_entry)
  124. {
  125. call_rcu(&orig_entry->rcu, tt_orig_list_entry_free_rcu);
  126. }
  127. static void tt_local_event(struct bat_priv *bat_priv, const uint8_t *addr,
  128. uint8_t flags)
  129. {
  130. struct tt_change_node *tt_change_node;
  131. tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
  132. if (!tt_change_node)
  133. return;
  134. tt_change_node->change.flags = flags;
  135. memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
  136. spin_lock_bh(&bat_priv->tt_changes_list_lock);
  137. /* track the change in the OGMinterval list */
  138. list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
  139. atomic_inc(&bat_priv->tt_local_changes);
  140. spin_unlock_bh(&bat_priv->tt_changes_list_lock);
  141. atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
  142. }
  143. int tt_len(int changes_num)
  144. {
  145. return changes_num * sizeof(struct tt_change);
  146. }
  147. static int tt_local_init(struct bat_priv *bat_priv)
  148. {
  149. if (bat_priv->tt_local_hash)
  150. return 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 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 += 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 / 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 + 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 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 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 tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
  506. const unsigned char *tt_addr, uint8_t ttvn, bool roaming,
  507. 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. 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 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 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 *transtable_search(struct bat_priv *bat_priv,
  874. const uint8_t *src, const uint8_t *addr)
  875. {
  876. struct tt_local_entry *tt_local_entry = NULL;
  877. struct tt_global_entry *tt_global_entry = NULL;
  878. struct orig_node *orig_node = NULL;
  879. struct neigh_node *router = NULL;
  880. struct hlist_head *head;
  881. struct hlist_node *node;
  882. struct tt_orig_list_entry *orig_entry;
  883. int best_tq;
  884. if (src && atomic_read(&bat_priv->ap_isolation)) {
  885. tt_local_entry = tt_local_hash_find(bat_priv, src);
  886. if (!tt_local_entry)
  887. goto out;
  888. }
  889. tt_global_entry = tt_global_hash_find(bat_priv, addr);
  890. if (!tt_global_entry)
  891. goto out;
  892. /* check whether the clients should not communicate due to AP
  893. * isolation */
  894. if (tt_local_entry && _is_ap_isolated(tt_local_entry, tt_global_entry))
  895. goto out;
  896. best_tq = 0;
  897. rcu_read_lock();
  898. head = &tt_global_entry->orig_list;
  899. hlist_for_each_entry_rcu(orig_entry, node, head, list) {
  900. router = orig_node_get_router(orig_entry->orig_node);
  901. if (!router)
  902. continue;
  903. if (router->tq_avg > best_tq) {
  904. orig_node = orig_entry->orig_node;
  905. best_tq = router->tq_avg;
  906. }
  907. neigh_node_free_ref(router);
  908. }
  909. /* found anything? */
  910. if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
  911. orig_node = NULL;
  912. rcu_read_unlock();
  913. out:
  914. if (tt_global_entry)
  915. tt_global_entry_free_ref(tt_global_entry);
  916. if (tt_local_entry)
  917. tt_local_entry_free_ref(tt_local_entry);
  918. return orig_node;
  919. }
  920. /* Calculates the checksum of the local table of a given orig_node */
  921. static uint16_t tt_global_crc(struct bat_priv *bat_priv,
  922. struct orig_node *orig_node)
  923. {
  924. uint16_t total = 0, total_one;
  925. struct hashtable_t *hash = bat_priv->tt_global_hash;
  926. struct tt_common_entry *tt_common_entry;
  927. struct tt_global_entry *tt_global_entry;
  928. struct hlist_node *node;
  929. struct hlist_head *head;
  930. uint32_t i;
  931. int j;
  932. for (i = 0; i < hash->size; i++) {
  933. head = &hash->table[i];
  934. rcu_read_lock();
  935. hlist_for_each_entry_rcu(tt_common_entry, node,
  936. head, hash_entry) {
  937. tt_global_entry = container_of(tt_common_entry,
  938. struct tt_global_entry,
  939. common);
  940. /* Roaming clients are in the global table for
  941. * consistency only. They don't have to be
  942. * taken into account while computing the
  943. * global crc
  944. */
  945. if (tt_global_entry->common.flags & TT_CLIENT_ROAM)
  946. continue;
  947. /* find out if this global entry is announced by this
  948. * originator
  949. */
  950. if (!tt_global_entry_has_orig(tt_global_entry,
  951. orig_node))
  952. continue;
  953. total_one = 0;
  954. for (j = 0; j < ETH_ALEN; j++)
  955. total_one = crc16_byte(total_one,
  956. tt_global_entry->common.addr[j]);
  957. total ^= total_one;
  958. }
  959. rcu_read_unlock();
  960. }
  961. return total;
  962. }
  963. /* Calculates the checksum of the local table */
  964. static uint16_t batadv_tt_local_crc(struct bat_priv *bat_priv)
  965. {
  966. uint16_t total = 0, total_one;
  967. struct hashtable_t *hash = bat_priv->tt_local_hash;
  968. struct tt_common_entry *tt_common_entry;
  969. struct hlist_node *node;
  970. struct hlist_head *head;
  971. uint32_t i;
  972. int j;
  973. for (i = 0; i < hash->size; i++) {
  974. head = &hash->table[i];
  975. rcu_read_lock();
  976. hlist_for_each_entry_rcu(tt_common_entry, node,
  977. head, hash_entry) {
  978. /* not yet committed clients have not to be taken into
  979. * account while computing the CRC */
  980. if (tt_common_entry->flags & TT_CLIENT_NEW)
  981. continue;
  982. total_one = 0;
  983. for (j = 0; j < ETH_ALEN; j++)
  984. total_one = crc16_byte(total_one,
  985. tt_common_entry->addr[j]);
  986. total ^= total_one;
  987. }
  988. rcu_read_unlock();
  989. }
  990. return total;
  991. }
  992. static void tt_req_list_free(struct bat_priv *bat_priv)
  993. {
  994. struct tt_req_node *node, *safe;
  995. spin_lock_bh(&bat_priv->tt_req_list_lock);
  996. list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
  997. list_del(&node->list);
  998. kfree(node);
  999. }
  1000. spin_unlock_bh(&bat_priv->tt_req_list_lock);
  1001. }
  1002. static void tt_save_orig_buffer(struct bat_priv *bat_priv,
  1003. struct orig_node *orig_node,
  1004. const unsigned char *tt_buff,
  1005. uint8_t tt_num_changes)
  1006. {
  1007. uint16_t tt_buff_len = tt_len(tt_num_changes);
  1008. /* Replace the old buffer only if I received something in the
  1009. * last OGM (the OGM could carry no changes) */
  1010. spin_lock_bh(&orig_node->tt_buff_lock);
  1011. if (tt_buff_len > 0) {
  1012. kfree(orig_node->tt_buff);
  1013. orig_node->tt_buff_len = 0;
  1014. orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
  1015. if (orig_node->tt_buff) {
  1016. memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
  1017. orig_node->tt_buff_len = tt_buff_len;
  1018. }
  1019. }
  1020. spin_unlock_bh(&orig_node->tt_buff_lock);
  1021. }
  1022. static void tt_req_purge(struct bat_priv *bat_priv)
  1023. {
  1024. struct tt_req_node *node, *safe;
  1025. spin_lock_bh(&bat_priv->tt_req_list_lock);
  1026. list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
  1027. if (has_timed_out(node->issued_at, TT_REQUEST_TIMEOUT)) {
  1028. list_del(&node->list);
  1029. kfree(node);
  1030. }
  1031. }
  1032. spin_unlock_bh(&bat_priv->tt_req_list_lock);
  1033. }
  1034. /* returns the pointer to the new tt_req_node struct if no request
  1035. * has already been issued for this orig_node, NULL otherwise */
  1036. static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv,
  1037. struct orig_node *orig_node)
  1038. {
  1039. struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
  1040. spin_lock_bh(&bat_priv->tt_req_list_lock);
  1041. list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
  1042. if (compare_eth(tt_req_node_tmp, orig_node) &&
  1043. !has_timed_out(tt_req_node_tmp->issued_at,
  1044. TT_REQUEST_TIMEOUT))
  1045. goto unlock;
  1046. }
  1047. tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
  1048. if (!tt_req_node)
  1049. goto unlock;
  1050. memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
  1051. tt_req_node->issued_at = jiffies;
  1052. list_add(&tt_req_node->list, &bat_priv->tt_req_list);
  1053. unlock:
  1054. spin_unlock_bh(&bat_priv->tt_req_list_lock);
  1055. return tt_req_node;
  1056. }
  1057. /* data_ptr is useless here, but has to be kept to respect the prototype */
  1058. static int tt_local_valid_entry(const void *entry_ptr, const void *data_ptr)
  1059. {
  1060. const struct tt_common_entry *tt_common_entry = entry_ptr;
  1061. if (tt_common_entry->flags & TT_CLIENT_NEW)
  1062. return 0;
  1063. return 1;
  1064. }
  1065. static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr)
  1066. {
  1067. const struct tt_common_entry *tt_common_entry = entry_ptr;
  1068. const struct tt_global_entry *tt_global_entry;
  1069. const struct orig_node *orig_node = data_ptr;
  1070. if (tt_common_entry->flags & TT_CLIENT_ROAM)
  1071. return 0;
  1072. tt_global_entry = container_of(tt_common_entry, struct tt_global_entry,
  1073. common);
  1074. return tt_global_entry_has_orig(tt_global_entry, orig_node);
  1075. }
  1076. static struct sk_buff *tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
  1077. struct hashtable_t *hash,
  1078. struct hard_iface *primary_if,
  1079. int (*valid_cb)(const void *,
  1080. const void *),
  1081. void *cb_data)
  1082. {
  1083. struct tt_common_entry *tt_common_entry;
  1084. struct tt_query_packet *tt_response;
  1085. struct tt_change *tt_change;
  1086. struct hlist_node *node;
  1087. struct hlist_head *head;
  1088. struct sk_buff *skb = NULL;
  1089. uint16_t tt_tot, tt_count;
  1090. ssize_t tt_query_size = sizeof(struct tt_query_packet);
  1091. uint32_t i;
  1092. if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
  1093. tt_len = primary_if->soft_iface->mtu - tt_query_size;
  1094. tt_len -= tt_len % sizeof(struct tt_change);
  1095. }
  1096. tt_tot = tt_len / sizeof(struct tt_change);
  1097. skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
  1098. if (!skb)
  1099. goto out;
  1100. skb_reserve(skb, ETH_HLEN);
  1101. tt_response = (struct tt_query_packet *)skb_put(skb,
  1102. tt_query_size + tt_len);
  1103. tt_response->ttvn = ttvn;
  1104. tt_change = (struct tt_change *)(skb->data + tt_query_size);
  1105. tt_count = 0;
  1106. rcu_read_lock();
  1107. for (i = 0; i < hash->size; i++) {
  1108. head = &hash->table[i];
  1109. hlist_for_each_entry_rcu(tt_common_entry, node,
  1110. head, hash_entry) {
  1111. if (tt_count == tt_tot)
  1112. break;
  1113. if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
  1114. continue;
  1115. memcpy(tt_change->addr, tt_common_entry->addr,
  1116. ETH_ALEN);
  1117. tt_change->flags = NO_FLAGS;
  1118. tt_count++;
  1119. tt_change++;
  1120. }
  1121. }
  1122. rcu_read_unlock();
  1123. /* store in the message the number of entries we have successfully
  1124. * copied */
  1125. tt_response->tt_data = htons(tt_count);
  1126. out:
  1127. return skb;
  1128. }
  1129. static int send_tt_request(struct bat_priv *bat_priv,
  1130. struct orig_node *dst_orig_node,
  1131. uint8_t ttvn, uint16_t tt_crc, bool full_table)
  1132. {
  1133. struct sk_buff *skb = NULL;
  1134. struct tt_query_packet *tt_request;
  1135. struct neigh_node *neigh_node = NULL;
  1136. struct hard_iface *primary_if;
  1137. struct tt_req_node *tt_req_node = NULL;
  1138. int ret = 1;
  1139. primary_if = primary_if_get_selected(bat_priv);
  1140. if (!primary_if)
  1141. goto out;
  1142. /* The new tt_req will be issued only if I'm not waiting for a
  1143. * reply from the same orig_node yet */
  1144. tt_req_node = new_tt_req_node(bat_priv, dst_orig_node);
  1145. if (!tt_req_node)
  1146. goto out;
  1147. skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
  1148. if (!skb)
  1149. goto out;
  1150. skb_reserve(skb, ETH_HLEN);
  1151. tt_request = (struct tt_query_packet *)skb_put(skb,
  1152. sizeof(struct tt_query_packet));
  1153. tt_request->header.packet_type = BAT_TT_QUERY;
  1154. tt_request->header.version = COMPAT_VERSION;
  1155. memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
  1156. memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
  1157. tt_request->header.ttl = TTL;
  1158. tt_request->ttvn = ttvn;
  1159. tt_request->tt_data = htons(tt_crc);
  1160. tt_request->flags = TT_REQUEST;
  1161. if (full_table)
  1162. tt_request->flags |= TT_FULL_TABLE;
  1163. neigh_node = orig_node_get_router(dst_orig_node);
  1164. if (!neigh_node)
  1165. goto out;
  1166. bat_dbg(DBG_TT, bat_priv,
  1167. "Sending TT_REQUEST to %pM via %pM [%c]\n",
  1168. dst_orig_node->orig, neigh_node->addr,
  1169. (full_table ? 'F' : '.'));
  1170. batadv_inc_counter(bat_priv, BAT_CNT_TT_REQUEST_TX);
  1171. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  1172. ret = 0;
  1173. out:
  1174. if (neigh_node)
  1175. neigh_node_free_ref(neigh_node);
  1176. if (primary_if)
  1177. hardif_free_ref(primary_if);
  1178. if (ret)
  1179. kfree_skb(skb);
  1180. if (ret && tt_req_node) {
  1181. spin_lock_bh(&bat_priv->tt_req_list_lock);
  1182. list_del(&tt_req_node->list);
  1183. spin_unlock_bh(&bat_priv->tt_req_list_lock);
  1184. kfree(tt_req_node);
  1185. }
  1186. return ret;
  1187. }
  1188. static bool send_other_tt_response(struct bat_priv *bat_priv,
  1189. struct tt_query_packet *tt_request)
  1190. {
  1191. struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
  1192. struct neigh_node *neigh_node = NULL;
  1193. struct hard_iface *primary_if = NULL;
  1194. uint8_t orig_ttvn, req_ttvn, ttvn;
  1195. int ret = false;
  1196. unsigned char *tt_buff;
  1197. bool full_table;
  1198. uint16_t tt_len, tt_tot;
  1199. struct sk_buff *skb = NULL;
  1200. struct tt_query_packet *tt_response;
  1201. bat_dbg(DBG_TT, bat_priv,
  1202. "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
  1203. tt_request->src, tt_request->ttvn, tt_request->dst,
  1204. (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
  1205. /* Let's get the orig node of the REAL destination */
  1206. req_dst_orig_node = orig_hash_find(bat_priv, tt_request->dst);
  1207. if (!req_dst_orig_node)
  1208. goto out;
  1209. res_dst_orig_node = orig_hash_find(bat_priv, tt_request->src);
  1210. if (!res_dst_orig_node)
  1211. goto out;
  1212. neigh_node = orig_node_get_router(res_dst_orig_node);
  1213. if (!neigh_node)
  1214. goto out;
  1215. primary_if = primary_if_get_selected(bat_priv);
  1216. if (!primary_if)
  1217. goto out;
  1218. orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
  1219. req_ttvn = tt_request->ttvn;
  1220. /* I don't have the requested data */
  1221. if (orig_ttvn != req_ttvn ||
  1222. tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
  1223. goto out;
  1224. /* If the full table has been explicitly requested */
  1225. if (tt_request->flags & TT_FULL_TABLE ||
  1226. !req_dst_orig_node->tt_buff)
  1227. full_table = true;
  1228. else
  1229. full_table = false;
  1230. /* In this version, fragmentation is not implemented, then
  1231. * I'll send only one packet with as much TT entries as I can */
  1232. if (!full_table) {
  1233. spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
  1234. tt_len = req_dst_orig_node->tt_buff_len;
  1235. tt_tot = tt_len / sizeof(struct tt_change);
  1236. skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
  1237. tt_len + ETH_HLEN);
  1238. if (!skb)
  1239. goto unlock;
  1240. skb_reserve(skb, ETH_HLEN);
  1241. tt_response = (struct tt_query_packet *)skb_put(skb,
  1242. sizeof(struct tt_query_packet) + tt_len);
  1243. tt_response->ttvn = req_ttvn;
  1244. tt_response->tt_data = htons(tt_tot);
  1245. tt_buff = skb->data + sizeof(struct tt_query_packet);
  1246. /* Copy the last orig_node's OGM buffer */
  1247. memcpy(tt_buff, req_dst_orig_node->tt_buff,
  1248. req_dst_orig_node->tt_buff_len);
  1249. spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
  1250. } else {
  1251. tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
  1252. sizeof(struct tt_change);
  1253. ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
  1254. skb = tt_response_fill_table(tt_len, ttvn,
  1255. bat_priv->tt_global_hash,
  1256. primary_if, tt_global_valid_entry,
  1257. req_dst_orig_node);
  1258. if (!skb)
  1259. goto out;
  1260. tt_response = (struct tt_query_packet *)skb->data;
  1261. }
  1262. tt_response->header.packet_type = BAT_TT_QUERY;
  1263. tt_response->header.version = COMPAT_VERSION;
  1264. tt_response->header.ttl = TTL;
  1265. memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
  1266. memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
  1267. tt_response->flags = TT_RESPONSE;
  1268. if (full_table)
  1269. tt_response->flags |= TT_FULL_TABLE;
  1270. bat_dbg(DBG_TT, bat_priv,
  1271. "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
  1272. res_dst_orig_node->orig, neigh_node->addr,
  1273. req_dst_orig_node->orig, req_ttvn);
  1274. batadv_inc_counter(bat_priv, BAT_CNT_TT_RESPONSE_TX);
  1275. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  1276. ret = true;
  1277. goto out;
  1278. unlock:
  1279. spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
  1280. out:
  1281. if (res_dst_orig_node)
  1282. orig_node_free_ref(res_dst_orig_node);
  1283. if (req_dst_orig_node)
  1284. orig_node_free_ref(req_dst_orig_node);
  1285. if (neigh_node)
  1286. neigh_node_free_ref(neigh_node);
  1287. if (primary_if)
  1288. hardif_free_ref(primary_if);
  1289. if (!ret)
  1290. kfree_skb(skb);
  1291. return ret;
  1292. }
  1293. static bool send_my_tt_response(struct bat_priv *bat_priv,
  1294. struct tt_query_packet *tt_request)
  1295. {
  1296. struct orig_node *orig_node = NULL;
  1297. struct neigh_node *neigh_node = NULL;
  1298. struct hard_iface *primary_if = NULL;
  1299. uint8_t my_ttvn, req_ttvn, ttvn;
  1300. int ret = false;
  1301. unsigned char *tt_buff;
  1302. bool full_table;
  1303. uint16_t tt_len, tt_tot;
  1304. struct sk_buff *skb = NULL;
  1305. struct tt_query_packet *tt_response;
  1306. bat_dbg(DBG_TT, bat_priv,
  1307. "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
  1308. tt_request->src, tt_request->ttvn,
  1309. (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
  1310. my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
  1311. req_ttvn = tt_request->ttvn;
  1312. orig_node = orig_hash_find(bat_priv, tt_request->src);
  1313. if (!orig_node)
  1314. goto out;
  1315. neigh_node = orig_node_get_router(orig_node);
  1316. if (!neigh_node)
  1317. goto out;
  1318. primary_if = primary_if_get_selected(bat_priv);
  1319. if (!primary_if)
  1320. goto out;
  1321. /* If the full table has been explicitly requested or the gap
  1322. * is too big send the whole local translation table */
  1323. if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
  1324. !bat_priv->tt_buff)
  1325. full_table = true;
  1326. else
  1327. full_table = false;
  1328. /* In this version, fragmentation is not implemented, then
  1329. * I'll send only one packet with as much TT entries as I can */
  1330. if (!full_table) {
  1331. spin_lock_bh(&bat_priv->tt_buff_lock);
  1332. tt_len = bat_priv->tt_buff_len;
  1333. tt_tot = tt_len / sizeof(struct tt_change);
  1334. skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
  1335. tt_len + ETH_HLEN);
  1336. if (!skb)
  1337. goto unlock;
  1338. skb_reserve(skb, ETH_HLEN);
  1339. tt_response = (struct tt_query_packet *)skb_put(skb,
  1340. sizeof(struct tt_query_packet) + tt_len);
  1341. tt_response->ttvn = req_ttvn;
  1342. tt_response->tt_data = htons(tt_tot);
  1343. tt_buff = skb->data + sizeof(struct tt_query_packet);
  1344. memcpy(tt_buff, bat_priv->tt_buff,
  1345. bat_priv->tt_buff_len);
  1346. spin_unlock_bh(&bat_priv->tt_buff_lock);
  1347. } else {
  1348. tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
  1349. sizeof(struct tt_change);
  1350. ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
  1351. skb = tt_response_fill_table(tt_len, ttvn,
  1352. bat_priv->tt_local_hash,
  1353. primary_if, tt_local_valid_entry,
  1354. NULL);
  1355. if (!skb)
  1356. goto out;
  1357. tt_response = (struct tt_query_packet *)skb->data;
  1358. }
  1359. tt_response->header.packet_type = BAT_TT_QUERY;
  1360. tt_response->header.version = COMPAT_VERSION;
  1361. tt_response->header.ttl = TTL;
  1362. memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
  1363. memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
  1364. tt_response->flags = TT_RESPONSE;
  1365. if (full_table)
  1366. tt_response->flags |= TT_FULL_TABLE;
  1367. bat_dbg(DBG_TT, bat_priv,
  1368. "Sending TT_RESPONSE to %pM via %pM [%c]\n",
  1369. orig_node->orig, neigh_node->addr,
  1370. (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
  1371. batadv_inc_counter(bat_priv, BAT_CNT_TT_RESPONSE_TX);
  1372. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  1373. ret = true;
  1374. goto out;
  1375. unlock:
  1376. spin_unlock_bh(&bat_priv->tt_buff_lock);
  1377. out:
  1378. if (orig_node)
  1379. orig_node_free_ref(orig_node);
  1380. if (neigh_node)
  1381. neigh_node_free_ref(neigh_node);
  1382. if (primary_if)
  1383. hardif_free_ref(primary_if);
  1384. if (!ret)
  1385. kfree_skb(skb);
  1386. /* This packet was for me, so it doesn't need to be re-routed */
  1387. return true;
  1388. }
  1389. bool send_tt_response(struct bat_priv *bat_priv,
  1390. struct tt_query_packet *tt_request)
  1391. {
  1392. if (is_my_mac(tt_request->dst)) {
  1393. /* don't answer backbone gws! */
  1394. if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
  1395. return true;
  1396. return send_my_tt_response(bat_priv, tt_request);
  1397. } else {
  1398. return send_other_tt_response(bat_priv, tt_request);
  1399. }
  1400. }
  1401. static void _tt_update_changes(struct bat_priv *bat_priv,
  1402. struct orig_node *orig_node,
  1403. struct tt_change *tt_change,
  1404. uint16_t tt_num_changes, uint8_t ttvn)
  1405. {
  1406. int i;
  1407. for (i = 0; i < tt_num_changes; i++) {
  1408. if ((tt_change + i)->flags & TT_CLIENT_DEL)
  1409. tt_global_del(bat_priv, orig_node,
  1410. (tt_change + i)->addr,
  1411. "tt removed by changes",
  1412. (tt_change + i)->flags & TT_CLIENT_ROAM);
  1413. else
  1414. if (!tt_global_add(bat_priv, orig_node,
  1415. (tt_change + i)->addr, ttvn, false,
  1416. (tt_change + i)->flags &
  1417. TT_CLIENT_WIFI))
  1418. /* In case of problem while storing a
  1419. * global_entry, we stop the updating
  1420. * procedure without committing the
  1421. * ttvn change. This will avoid to send
  1422. * corrupted data on tt_request
  1423. */
  1424. return;
  1425. }
  1426. orig_node->tt_initialised = true;
  1427. }
  1428. static void tt_fill_gtable(struct bat_priv *bat_priv,
  1429. struct tt_query_packet *tt_response)
  1430. {
  1431. struct orig_node *orig_node = NULL;
  1432. orig_node = orig_hash_find(bat_priv, tt_response->src);
  1433. if (!orig_node)
  1434. goto out;
  1435. /* Purge the old table first.. */
  1436. tt_global_del_orig(bat_priv, orig_node, "Received full table");
  1437. _tt_update_changes(bat_priv, orig_node,
  1438. (struct tt_change *)(tt_response + 1),
  1439. ntohs(tt_response->tt_data), tt_response->ttvn);
  1440. spin_lock_bh(&orig_node->tt_buff_lock);
  1441. kfree(orig_node->tt_buff);
  1442. orig_node->tt_buff_len = 0;
  1443. orig_node->tt_buff = NULL;
  1444. spin_unlock_bh(&orig_node->tt_buff_lock);
  1445. atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
  1446. out:
  1447. if (orig_node)
  1448. orig_node_free_ref(orig_node);
  1449. }
  1450. static void tt_update_changes(struct bat_priv *bat_priv,
  1451. struct orig_node *orig_node,
  1452. uint16_t tt_num_changes, uint8_t ttvn,
  1453. struct tt_change *tt_change)
  1454. {
  1455. _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes,
  1456. ttvn);
  1457. tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change,
  1458. tt_num_changes);
  1459. atomic_set(&orig_node->last_ttvn, ttvn);
  1460. }
  1461. bool is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
  1462. {
  1463. struct tt_local_entry *tt_local_entry = NULL;
  1464. bool ret = false;
  1465. tt_local_entry = tt_local_hash_find(bat_priv, addr);
  1466. if (!tt_local_entry)
  1467. goto out;
  1468. /* Check if the client has been logically deleted (but is kept for
  1469. * consistency purpose) */
  1470. if (tt_local_entry->common.flags & TT_CLIENT_PENDING)
  1471. goto out;
  1472. ret = true;
  1473. out:
  1474. if (tt_local_entry)
  1475. tt_local_entry_free_ref(tt_local_entry);
  1476. return ret;
  1477. }
  1478. void handle_tt_response(struct bat_priv *bat_priv,
  1479. struct tt_query_packet *tt_response)
  1480. {
  1481. struct tt_req_node *node, *safe;
  1482. struct orig_node *orig_node = NULL;
  1483. bat_dbg(DBG_TT, bat_priv,
  1484. "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
  1485. tt_response->src, tt_response->ttvn,
  1486. ntohs(tt_response->tt_data),
  1487. (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
  1488. /* we should have never asked a backbone gw */
  1489. if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
  1490. goto out;
  1491. orig_node = orig_hash_find(bat_priv, tt_response->src);
  1492. if (!orig_node)
  1493. goto out;
  1494. if (tt_response->flags & TT_FULL_TABLE)
  1495. tt_fill_gtable(bat_priv, tt_response);
  1496. else
  1497. tt_update_changes(bat_priv, orig_node,
  1498. ntohs(tt_response->tt_data),
  1499. tt_response->ttvn,
  1500. (struct tt_change *)(tt_response + 1));
  1501. /* Delete the tt_req_node from pending tt_requests list */
  1502. spin_lock_bh(&bat_priv->tt_req_list_lock);
  1503. list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
  1504. if (!compare_eth(node->addr, tt_response->src))
  1505. continue;
  1506. list_del(&node->list);
  1507. kfree(node);
  1508. }
  1509. spin_unlock_bh(&bat_priv->tt_req_list_lock);
  1510. /* Recalculate the CRC for this orig_node and store it */
  1511. orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
  1512. /* Roaming phase is over: tables are in sync again. I can
  1513. * unset the flag */
  1514. orig_node->tt_poss_change = false;
  1515. out:
  1516. if (orig_node)
  1517. orig_node_free_ref(orig_node);
  1518. }
  1519. int tt_init(struct bat_priv *bat_priv)
  1520. {
  1521. int ret;
  1522. ret = tt_local_init(bat_priv);
  1523. if (ret < 0)
  1524. return ret;
  1525. ret = tt_global_init(bat_priv);
  1526. if (ret < 0)
  1527. return ret;
  1528. tt_start_timer(bat_priv);
  1529. return 1;
  1530. }
  1531. static void tt_roam_list_free(struct bat_priv *bat_priv)
  1532. {
  1533. struct tt_roam_node *node, *safe;
  1534. spin_lock_bh(&bat_priv->tt_roam_list_lock);
  1535. list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
  1536. list_del(&node->list);
  1537. kfree(node);
  1538. }
  1539. spin_unlock_bh(&bat_priv->tt_roam_list_lock);
  1540. }
  1541. static void tt_roam_purge(struct bat_priv *bat_priv)
  1542. {
  1543. struct tt_roam_node *node, *safe;
  1544. spin_lock_bh(&bat_priv->tt_roam_list_lock);
  1545. list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
  1546. if (!has_timed_out(node->first_time, ROAMING_MAX_TIME))
  1547. continue;
  1548. list_del(&node->list);
  1549. kfree(node);
  1550. }
  1551. spin_unlock_bh(&bat_priv->tt_roam_list_lock);
  1552. }
  1553. /* This function checks whether the client already reached the
  1554. * maximum number of possible roaming phases. In this case the ROAMING_ADV
  1555. * will not be sent.
  1556. *
  1557. * returns true if the ROAMING_ADV can be sent, false otherwise */
  1558. static bool tt_check_roam_count(struct bat_priv *bat_priv,
  1559. uint8_t *client)
  1560. {
  1561. struct tt_roam_node *tt_roam_node;
  1562. bool ret = false;
  1563. spin_lock_bh(&bat_priv->tt_roam_list_lock);
  1564. /* The new tt_req will be issued only if I'm not waiting for a
  1565. * reply from the same orig_node yet */
  1566. list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
  1567. if (!compare_eth(tt_roam_node->addr, client))
  1568. continue;
  1569. if (has_timed_out(tt_roam_node->first_time, ROAMING_MAX_TIME))
  1570. continue;
  1571. if (!atomic_dec_not_zero(&tt_roam_node->counter))
  1572. /* Sorry, you roamed too many times! */
  1573. goto unlock;
  1574. ret = true;
  1575. break;
  1576. }
  1577. if (!ret) {
  1578. tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
  1579. if (!tt_roam_node)
  1580. goto unlock;
  1581. tt_roam_node->first_time = jiffies;
  1582. atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
  1583. memcpy(tt_roam_node->addr, client, ETH_ALEN);
  1584. list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
  1585. ret = true;
  1586. }
  1587. unlock:
  1588. spin_unlock_bh(&bat_priv->tt_roam_list_lock);
  1589. return ret;
  1590. }
  1591. static void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
  1592. struct orig_node *orig_node)
  1593. {
  1594. struct neigh_node *neigh_node = NULL;
  1595. struct sk_buff *skb = NULL;
  1596. struct roam_adv_packet *roam_adv_packet;
  1597. int ret = 1;
  1598. struct hard_iface *primary_if;
  1599. /* before going on we have to check whether the client has
  1600. * already roamed to us too many times */
  1601. if (!tt_check_roam_count(bat_priv, client))
  1602. goto out;
  1603. skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
  1604. if (!skb)
  1605. goto out;
  1606. skb_reserve(skb, ETH_HLEN);
  1607. roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
  1608. sizeof(struct roam_adv_packet));
  1609. roam_adv_packet->header.packet_type = BAT_ROAM_ADV;
  1610. roam_adv_packet->header.version = COMPAT_VERSION;
  1611. roam_adv_packet->header.ttl = TTL;
  1612. primary_if = primary_if_get_selected(bat_priv);
  1613. if (!primary_if)
  1614. goto out;
  1615. memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
  1616. hardif_free_ref(primary_if);
  1617. memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
  1618. memcpy(roam_adv_packet->client, client, ETH_ALEN);
  1619. neigh_node = orig_node_get_router(orig_node);
  1620. if (!neigh_node)
  1621. goto out;
  1622. bat_dbg(DBG_TT, bat_priv,
  1623. "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
  1624. orig_node->orig, client, neigh_node->addr);
  1625. batadv_inc_counter(bat_priv, BAT_CNT_TT_ROAM_ADV_TX);
  1626. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  1627. ret = 0;
  1628. out:
  1629. if (neigh_node)
  1630. neigh_node_free_ref(neigh_node);
  1631. if (ret)
  1632. kfree_skb(skb);
  1633. return;
  1634. }
  1635. static void tt_purge(struct work_struct *work)
  1636. {
  1637. struct delayed_work *delayed_work =
  1638. container_of(work, struct delayed_work, work);
  1639. struct bat_priv *bat_priv =
  1640. container_of(delayed_work, struct bat_priv, tt_work);
  1641. tt_local_purge(bat_priv);
  1642. tt_global_roam_purge(bat_priv);
  1643. tt_req_purge(bat_priv);
  1644. tt_roam_purge(bat_priv);
  1645. tt_start_timer(bat_priv);
  1646. }
  1647. void tt_free(struct bat_priv *bat_priv)
  1648. {
  1649. cancel_delayed_work_sync(&bat_priv->tt_work);
  1650. tt_local_table_free(bat_priv);
  1651. tt_global_table_free(bat_priv);
  1652. tt_req_list_free(bat_priv);
  1653. tt_changes_list_free(bat_priv);
  1654. tt_roam_list_free(bat_priv);
  1655. kfree(bat_priv->tt_buff);
  1656. }
  1657. /* This function will enable or disable the specified flags for all the entries
  1658. * in the given hash table and returns the number of modified entries */
  1659. static uint16_t tt_set_flags(struct hashtable_t *hash, uint16_t flags,
  1660. bool enable)
  1661. {
  1662. uint32_t i;
  1663. uint16_t changed_num = 0;
  1664. struct hlist_head *head;
  1665. struct hlist_node *node;
  1666. struct tt_common_entry *tt_common_entry;
  1667. if (!hash)
  1668. goto out;
  1669. for (i = 0; i < hash->size; i++) {
  1670. head = &hash->table[i];
  1671. rcu_read_lock();
  1672. hlist_for_each_entry_rcu(tt_common_entry, node,
  1673. head, hash_entry) {
  1674. if (enable) {
  1675. if ((tt_common_entry->flags & flags) == flags)
  1676. continue;
  1677. tt_common_entry->flags |= flags;
  1678. } else {
  1679. if (!(tt_common_entry->flags & flags))
  1680. continue;
  1681. tt_common_entry->flags &= ~flags;
  1682. }
  1683. changed_num++;
  1684. }
  1685. rcu_read_unlock();
  1686. }
  1687. out:
  1688. return changed_num;
  1689. }
  1690. /* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
  1691. static void tt_local_purge_pending_clients(struct bat_priv *bat_priv)
  1692. {
  1693. struct hashtable_t *hash = bat_priv->tt_local_hash;
  1694. struct tt_common_entry *tt_common_entry;
  1695. struct tt_local_entry *tt_local_entry;
  1696. struct hlist_node *node, *node_tmp;
  1697. struct hlist_head *head;
  1698. spinlock_t *list_lock; /* protects write access to the hash lists */
  1699. uint32_t i;
  1700. if (!hash)
  1701. return;
  1702. for (i = 0; i < hash->size; i++) {
  1703. head = &hash->table[i];
  1704. list_lock = &hash->list_locks[i];
  1705. spin_lock_bh(list_lock);
  1706. hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
  1707. head, hash_entry) {
  1708. if (!(tt_common_entry->flags & TT_CLIENT_PENDING))
  1709. continue;
  1710. bat_dbg(DBG_TT, bat_priv,
  1711. "Deleting local tt entry (%pM): pending\n",
  1712. tt_common_entry->addr);
  1713. atomic_dec(&bat_priv->num_local_tt);
  1714. hlist_del_rcu(node);
  1715. tt_local_entry = container_of(tt_common_entry,
  1716. struct tt_local_entry,
  1717. common);
  1718. tt_local_entry_free_ref(tt_local_entry);
  1719. }
  1720. spin_unlock_bh(list_lock);
  1721. }
  1722. }
  1723. static int tt_commit_changes(struct bat_priv *bat_priv,
  1724. unsigned char **packet_buff, int *packet_buff_len,
  1725. int packet_min_len)
  1726. {
  1727. uint16_t changed_num = 0;
  1728. if (atomic_read(&bat_priv->tt_local_changes) < 1)
  1729. return -ENOENT;
  1730. changed_num = tt_set_flags(bat_priv->tt_local_hash,
  1731. TT_CLIENT_NEW, false);
  1732. /* all reset entries have to be counted as local entries */
  1733. atomic_add(changed_num, &bat_priv->num_local_tt);
  1734. tt_local_purge_pending_clients(bat_priv);
  1735. bat_priv->tt_crc = batadv_tt_local_crc(bat_priv);
  1736. /* Increment the TTVN only once per OGM interval */
  1737. atomic_inc(&bat_priv->ttvn);
  1738. bat_dbg(DBG_TT, bat_priv, "Local changes committed, updating to ttvn %u\n",
  1739. (uint8_t)atomic_read(&bat_priv->ttvn));
  1740. bat_priv->tt_poss_change = false;
  1741. /* reset the sending counter */
  1742. atomic_set(&bat_priv->tt_ogm_append_cnt, TT_OGM_APPEND_MAX);
  1743. return tt_changes_fill_buff(bat_priv, packet_buff,
  1744. packet_buff_len, packet_min_len);
  1745. }
  1746. /* when calling this function (hard_iface == primary_if) has to be true */
  1747. int batadv_tt_append_diff(struct bat_priv *bat_priv,
  1748. unsigned char **packet_buff, int *packet_buff_len,
  1749. int packet_min_len)
  1750. {
  1751. int tt_num_changes;
  1752. /* if at least one change happened */
  1753. tt_num_changes = tt_commit_changes(bat_priv, packet_buff,
  1754. packet_buff_len, packet_min_len);
  1755. /* if the changes have been sent often enough */
  1756. if ((tt_num_changes < 0) &&
  1757. (!atomic_dec_not_zero(&bat_priv->tt_ogm_append_cnt))) {
  1758. tt_realloc_packet_buff(packet_buff, packet_buff_len,
  1759. packet_min_len, packet_min_len);
  1760. tt_num_changes = 0;
  1761. }
  1762. return tt_num_changes;
  1763. }
  1764. bool is_ap_isolated(struct bat_priv *bat_priv, uint8_t *src, uint8_t *dst)
  1765. {
  1766. struct tt_local_entry *tt_local_entry = NULL;
  1767. struct tt_global_entry *tt_global_entry = NULL;
  1768. bool ret = true;
  1769. if (!atomic_read(&bat_priv->ap_isolation))
  1770. return false;
  1771. tt_local_entry = tt_local_hash_find(bat_priv, dst);
  1772. if (!tt_local_entry)
  1773. goto out;
  1774. tt_global_entry = tt_global_hash_find(bat_priv, src);
  1775. if (!tt_global_entry)
  1776. goto out;
  1777. if (_is_ap_isolated(tt_local_entry, tt_global_entry))
  1778. goto out;
  1779. ret = false;
  1780. out:
  1781. if (tt_global_entry)
  1782. tt_global_entry_free_ref(tt_global_entry);
  1783. if (tt_local_entry)
  1784. tt_local_entry_free_ref(tt_local_entry);
  1785. return ret;
  1786. }
  1787. void tt_update_orig(struct bat_priv *bat_priv, struct orig_node *orig_node,
  1788. const unsigned char *tt_buff, uint8_t tt_num_changes,
  1789. uint8_t ttvn, uint16_t tt_crc)
  1790. {
  1791. uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  1792. bool full_table = true;
  1793. /* don't care about a backbone gateways updates. */
  1794. if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
  1795. return;
  1796. /* orig table not initialised AND first diff is in the OGM OR the ttvn
  1797. * increased by one -> we can apply the attached changes */
  1798. if ((!orig_node->tt_initialised && ttvn == 1) ||
  1799. ttvn - orig_ttvn == 1) {
  1800. /* the OGM could not contain the changes due to their size or
  1801. * because they have already been sent TT_OGM_APPEND_MAX times.
  1802. * In this case send a tt request */
  1803. if (!tt_num_changes) {
  1804. full_table = false;
  1805. goto request_table;
  1806. }
  1807. tt_update_changes(bat_priv, orig_node, tt_num_changes, ttvn,
  1808. (struct tt_change *)tt_buff);
  1809. /* Even if we received the precomputed crc with the OGM, we
  1810. * prefer to recompute it to spot any possible inconsistency
  1811. * in the global table */
  1812. orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
  1813. /* The ttvn alone is not enough to guarantee consistency
  1814. * because a single value could represent different states
  1815. * (due to the wrap around). Thus a node has to check whether
  1816. * the resulting table (after applying the changes) is still
  1817. * consistent or not. E.g. a node could disconnect while its
  1818. * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
  1819. * checking the CRC value is mandatory to detect the
  1820. * inconsistency */
  1821. if (orig_node->tt_crc != tt_crc)
  1822. goto request_table;
  1823. /* Roaming phase is over: tables are in sync again. I can
  1824. * unset the flag */
  1825. orig_node->tt_poss_change = false;
  1826. } else {
  1827. /* if we missed more than one change or our tables are not
  1828. * in sync anymore -> request fresh tt data */
  1829. if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
  1830. orig_node->tt_crc != tt_crc) {
  1831. request_table:
  1832. bat_dbg(DBG_TT, bat_priv,
  1833. "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
  1834. orig_node->orig, ttvn, orig_ttvn, tt_crc,
  1835. orig_node->tt_crc, tt_num_changes);
  1836. send_tt_request(bat_priv, orig_node, ttvn, tt_crc,
  1837. full_table);
  1838. return;
  1839. }
  1840. }
  1841. }
  1842. /* returns true whether we know that the client has moved from its old
  1843. * originator to another one. This entry is kept is still kept for consistency
  1844. * purposes
  1845. */
  1846. bool tt_global_client_is_roaming(struct bat_priv *bat_priv, uint8_t *addr)
  1847. {
  1848. struct tt_global_entry *tt_global_entry;
  1849. bool ret = false;
  1850. tt_global_entry = tt_global_hash_find(bat_priv, addr);
  1851. if (!tt_global_entry)
  1852. goto out;
  1853. ret = tt_global_entry->common.flags & TT_CLIENT_ROAM;
  1854. tt_global_entry_free_ref(tt_global_entry);
  1855. out:
  1856. return ret;
  1857. }