translation-table.c 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960
  1. /* Copyright (C) 2007-2013 B.A.T.M.A.N. contributors:
  2. *
  3. * Marek Lindner, Simon Wunderlich, Antonio Quartulli
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA
  18. */
  19. #include "main.h"
  20. #include "translation-table.h"
  21. #include "soft-interface.h"
  22. #include "hard-interface.h"
  23. #include "send.h"
  24. #include "hash.h"
  25. #include "originator.h"
  26. #include "routing.h"
  27. #include "bridge_loop_avoidance.h"
  28. #include <linux/crc32c.h>
  29. /* hash class keys */
  30. static struct lock_class_key batadv_tt_local_hash_lock_class_key;
  31. static struct lock_class_key batadv_tt_global_hash_lock_class_key;
  32. static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
  33. unsigned short vid,
  34. struct batadv_orig_node *orig_node);
  35. static void batadv_tt_purge(struct work_struct *work);
  36. static void
  37. batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
  38. static void batadv_tt_global_del(struct batadv_priv *bat_priv,
  39. struct batadv_orig_node *orig_node,
  40. const unsigned char *addr,
  41. unsigned short vid, const char *message,
  42. bool roaming);
  43. /* returns 1 if they are the same mac addr */
  44. static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
  45. {
  46. const void *data1 = container_of(node, struct batadv_tt_common_entry,
  47. hash_entry);
  48. return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
  49. }
  50. /**
  51. * batadv_choose_tt - return the index of the tt entry in the hash table
  52. * @data: pointer to the tt_common_entry object to map
  53. * @size: the size of the hash table
  54. *
  55. * Returns the hash index where the object represented by 'data' should be
  56. * stored at.
  57. */
  58. static inline uint32_t batadv_choose_tt(const void *data, uint32_t size)
  59. {
  60. struct batadv_tt_common_entry *tt;
  61. uint32_t hash = 0;
  62. tt = (struct batadv_tt_common_entry *)data;
  63. hash = batadv_hash_bytes(hash, &tt->addr, ETH_ALEN);
  64. hash = batadv_hash_bytes(hash, &tt->vid, sizeof(tt->vid));
  65. hash += (hash << 3);
  66. hash ^= (hash >> 11);
  67. hash += (hash << 15);
  68. return hash % size;
  69. }
  70. /**
  71. * batadv_tt_hash_find - look for a client in the given hash table
  72. * @hash: the hash table to search
  73. * @addr: the mac address of the client to look for
  74. * @vid: VLAN identifier
  75. *
  76. * Returns a pointer to the tt_common struct belonging to the searched client if
  77. * found, NULL otherwise.
  78. */
  79. static struct batadv_tt_common_entry *
  80. batadv_tt_hash_find(struct batadv_hashtable *hash, const uint8_t *addr,
  81. unsigned short vid)
  82. {
  83. struct hlist_head *head;
  84. struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
  85. uint32_t index;
  86. if (!hash)
  87. return NULL;
  88. memcpy(to_search.addr, addr, ETH_ALEN);
  89. to_search.vid = vid;
  90. index = batadv_choose_tt(&to_search, hash->size);
  91. head = &hash->table[index];
  92. rcu_read_lock();
  93. hlist_for_each_entry_rcu(tt, head, hash_entry) {
  94. if (!batadv_compare_eth(tt, addr))
  95. continue;
  96. if (tt->vid != vid)
  97. continue;
  98. if (!atomic_inc_not_zero(&tt->refcount))
  99. continue;
  100. tt_tmp = tt;
  101. break;
  102. }
  103. rcu_read_unlock();
  104. return tt_tmp;
  105. }
  106. /**
  107. * batadv_tt_local_hash_find - search the local table for a given client
  108. * @bat_priv: the bat priv with all the soft interface information
  109. * @addr: the mac address of the client to look for
  110. * @vid: VLAN identifier
  111. *
  112. * Returns a pointer to the corresponding tt_local_entry struct if the client is
  113. * found, NULL otherwise.
  114. */
  115. static struct batadv_tt_local_entry *
  116. batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const uint8_t *addr,
  117. unsigned short vid)
  118. {
  119. struct batadv_tt_common_entry *tt_common_entry;
  120. struct batadv_tt_local_entry *tt_local_entry = NULL;
  121. tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
  122. vid);
  123. if (tt_common_entry)
  124. tt_local_entry = container_of(tt_common_entry,
  125. struct batadv_tt_local_entry,
  126. common);
  127. return tt_local_entry;
  128. }
  129. /**
  130. * batadv_tt_global_hash_find - search the global table for a given client
  131. * @bat_priv: the bat priv with all the soft interface information
  132. * @addr: the mac address of the client to look for
  133. * @vid: VLAN identifier
  134. *
  135. * Returns a pointer to the corresponding tt_global_entry struct if the client
  136. * is found, NULL otherwise.
  137. */
  138. static struct batadv_tt_global_entry *
  139. batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const uint8_t *addr,
  140. unsigned short vid)
  141. {
  142. struct batadv_tt_common_entry *tt_common_entry;
  143. struct batadv_tt_global_entry *tt_global_entry = NULL;
  144. tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
  145. vid);
  146. if (tt_common_entry)
  147. tt_global_entry = container_of(tt_common_entry,
  148. struct batadv_tt_global_entry,
  149. common);
  150. return tt_global_entry;
  151. }
  152. static void
  153. batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
  154. {
  155. if (atomic_dec_and_test(&tt_local_entry->common.refcount))
  156. kfree_rcu(tt_local_entry, common.rcu);
  157. }
  158. /**
  159. * batadv_tt_global_entry_free_ref - decrement the refcounter for a
  160. * tt_global_entry and possibly free it
  161. * @tt_global_entry: the object to free
  162. */
  163. static void
  164. batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
  165. {
  166. if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
  167. batadv_tt_global_del_orig_list(tt_global_entry);
  168. kfree_rcu(tt_global_entry, common.rcu);
  169. }
  170. }
  171. static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
  172. {
  173. struct batadv_tt_orig_list_entry *orig_entry;
  174. orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
  175. /* We are in an rcu callback here, therefore we cannot use
  176. * batadv_orig_node_free_ref() and its call_rcu():
  177. * An rcu_barrier() wouldn't wait for that to finish
  178. */
  179. batadv_orig_node_free_ref_now(orig_entry->orig_node);
  180. kfree(orig_entry);
  181. }
  182. static void
  183. batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
  184. {
  185. if (!atomic_dec_and_test(&orig_entry->refcount))
  186. return;
  187. /* to avoid race conditions, immediately decrease the tt counter */
  188. atomic_dec(&orig_entry->orig_node->tt_size);
  189. call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
  190. }
  191. /**
  192. * batadv_tt_local_event - store a local TT event (ADD/DEL)
  193. * @bat_priv: the bat priv with all the soft interface information
  194. * @tt_local_entry: the TT entry involved in the event
  195. * @event_flags: flags to store in the event structure
  196. */
  197. static void batadv_tt_local_event(struct batadv_priv *bat_priv,
  198. struct batadv_tt_local_entry *tt_local_entry,
  199. uint8_t event_flags)
  200. {
  201. struct batadv_tt_change_node *tt_change_node, *entry, *safe;
  202. struct batadv_tt_common_entry *common = &tt_local_entry->common;
  203. uint8_t flags = common->flags | event_flags;
  204. bool event_removed = false;
  205. bool del_op_requested, del_op_entry;
  206. tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
  207. if (!tt_change_node)
  208. return;
  209. tt_change_node->change.flags = flags;
  210. tt_change_node->change.reserved = 0;
  211. memcpy(tt_change_node->change.addr, common->addr, ETH_ALEN);
  212. tt_change_node->change.vid = htons(common->vid);
  213. del_op_requested = flags & BATADV_TT_CLIENT_DEL;
  214. /* check for ADD+DEL or DEL+ADD events */
  215. spin_lock_bh(&bat_priv->tt.changes_list_lock);
  216. list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
  217. list) {
  218. if (!batadv_compare_eth(entry->change.addr, common->addr))
  219. continue;
  220. /* DEL+ADD in the same orig interval have no effect and can be
  221. * removed to avoid silly behaviour on the receiver side. The
  222. * other way around (ADD+DEL) can happen in case of roaming of
  223. * a client still in the NEW state. Roaming of NEW clients is
  224. * now possible due to automatically recognition of "temporary"
  225. * clients
  226. */
  227. del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
  228. if (!del_op_requested && del_op_entry)
  229. goto del;
  230. if (del_op_requested && !del_op_entry)
  231. goto del;
  232. continue;
  233. del:
  234. list_del(&entry->list);
  235. kfree(entry);
  236. kfree(tt_change_node);
  237. event_removed = true;
  238. goto unlock;
  239. }
  240. /* track the change in the OGMinterval list */
  241. list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
  242. unlock:
  243. spin_unlock_bh(&bat_priv->tt.changes_list_lock);
  244. if (event_removed)
  245. atomic_dec(&bat_priv->tt.local_changes);
  246. else
  247. atomic_inc(&bat_priv->tt.local_changes);
  248. }
  249. /**
  250. * batadv_tt_len - compute length in bytes of given number of tt changes
  251. * @changes_num: number of tt changes
  252. *
  253. * Returns computed length in bytes.
  254. */
  255. static int batadv_tt_len(int changes_num)
  256. {
  257. return changes_num * sizeof(struct batadv_tvlv_tt_change);
  258. }
  259. /**
  260. * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
  261. * @tt_len: available space
  262. *
  263. * Returns the number of entries.
  264. */
  265. static uint16_t batadv_tt_entries(uint16_t tt_len)
  266. {
  267. return tt_len / batadv_tt_len(1);
  268. }
  269. static int batadv_tt_local_init(struct batadv_priv *bat_priv)
  270. {
  271. if (bat_priv->tt.local_hash)
  272. return 0;
  273. bat_priv->tt.local_hash = batadv_hash_new(1024);
  274. if (!bat_priv->tt.local_hash)
  275. return -ENOMEM;
  276. batadv_hash_set_lock_class(bat_priv->tt.local_hash,
  277. &batadv_tt_local_hash_lock_class_key);
  278. return 0;
  279. }
  280. static void batadv_tt_global_free(struct batadv_priv *bat_priv,
  281. struct batadv_tt_global_entry *tt_global,
  282. const char *message)
  283. {
  284. batadv_dbg(BATADV_DBG_TT, bat_priv,
  285. "Deleting global tt entry %pM (vid: %d): %s\n",
  286. tt_global->common.addr,
  287. BATADV_PRINT_VID(tt_global->common.vid), message);
  288. batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
  289. batadv_choose_tt, &tt_global->common);
  290. batadv_tt_global_entry_free_ref(tt_global);
  291. }
  292. /**
  293. * batadv_tt_local_add - add a new client to the local table or update an
  294. * existing client
  295. * @soft_iface: netdev struct of the mesh interface
  296. * @addr: the mac address of the client to add
  297. * @vid: VLAN identifier
  298. * @ifindex: index of the interface where the client is connected to (useful to
  299. * identify wireless clients)
  300. */
  301. void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
  302. unsigned short vid, int ifindex)
  303. {
  304. struct batadv_priv *bat_priv = netdev_priv(soft_iface);
  305. struct batadv_tt_local_entry *tt_local;
  306. struct batadv_tt_global_entry *tt_global;
  307. struct hlist_head *head;
  308. struct batadv_tt_orig_list_entry *orig_entry;
  309. int hash_added;
  310. bool roamed_back = false;
  311. tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
  312. tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
  313. if (tt_local) {
  314. tt_local->last_seen = jiffies;
  315. if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
  316. batadv_dbg(BATADV_DBG_TT, bat_priv,
  317. "Re-adding pending client %pM (vid: %d)\n",
  318. addr, BATADV_PRINT_VID(vid));
  319. /* whatever the reason why the PENDING flag was set,
  320. * this is a client which was enqueued to be removed in
  321. * this orig_interval. Since it popped up again, the
  322. * flag can be reset like it was never enqueued
  323. */
  324. tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
  325. goto add_event;
  326. }
  327. if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
  328. batadv_dbg(BATADV_DBG_TT, bat_priv,
  329. "Roaming client %pM (vid: %d) came back to its original location\n",
  330. addr, BATADV_PRINT_VID(vid));
  331. /* the ROAM flag is set because this client roamed away
  332. * and the node got a roaming_advertisement message. Now
  333. * that the client popped up again at its original
  334. * location such flag can be unset
  335. */
  336. tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
  337. roamed_back = true;
  338. }
  339. goto check_roaming;
  340. }
  341. tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
  342. if (!tt_local)
  343. goto out;
  344. batadv_dbg(BATADV_DBG_TT, bat_priv,
  345. "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
  346. addr, BATADV_PRINT_VID(vid),
  347. (uint8_t)atomic_read(&bat_priv->tt.vn));
  348. memcpy(tt_local->common.addr, addr, ETH_ALEN);
  349. /* The local entry has to be marked as NEW to avoid to send it in
  350. * a full table response going out before the next ttvn increment
  351. * (consistency check)
  352. */
  353. tt_local->common.flags = BATADV_TT_CLIENT_NEW;
  354. tt_local->common.vid = vid;
  355. if (batadv_is_wifi_iface(ifindex))
  356. tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
  357. atomic_set(&tt_local->common.refcount, 2);
  358. tt_local->last_seen = jiffies;
  359. tt_local->common.added_at = tt_local->last_seen;
  360. /* the batman interface mac address should never be purged */
  361. if (batadv_compare_eth(addr, soft_iface->dev_addr))
  362. tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
  363. hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
  364. batadv_choose_tt, &tt_local->common,
  365. &tt_local->common.hash_entry);
  366. if (unlikely(hash_added != 0)) {
  367. /* remove the reference for the hash */
  368. batadv_tt_local_entry_free_ref(tt_local);
  369. goto out;
  370. }
  371. add_event:
  372. batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
  373. check_roaming:
  374. /* Check whether it is a roaming, but don't do anything if the roaming
  375. * process has already been handled
  376. */
  377. if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
  378. /* These node are probably going to update their tt table */
  379. head = &tt_global->orig_list;
  380. rcu_read_lock();
  381. hlist_for_each_entry_rcu(orig_entry, head, list) {
  382. batadv_send_roam_adv(bat_priv, tt_global->common.addr,
  383. tt_global->common.vid,
  384. orig_entry->orig_node);
  385. }
  386. rcu_read_unlock();
  387. if (roamed_back) {
  388. batadv_tt_global_free(bat_priv, tt_global,
  389. "Roaming canceled");
  390. tt_global = NULL;
  391. } else {
  392. /* The global entry has to be marked as ROAMING and
  393. * has to be kept for consistency purpose
  394. */
  395. tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
  396. tt_global->roam_at = jiffies;
  397. }
  398. }
  399. out:
  400. if (tt_local)
  401. batadv_tt_local_entry_free_ref(tt_local);
  402. if (tt_global)
  403. batadv_tt_global_entry_free_ref(tt_global);
  404. }
  405. /**
  406. * batadv_tt_tvlv_container_update - update the translation table tvlv container
  407. * after local tt changes have been committed
  408. * @bat_priv: the bat priv with all the soft interface information
  409. */
  410. static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
  411. {
  412. struct batadv_tt_change_node *entry, *safe;
  413. struct batadv_tvlv_tt_data *tt_data;
  414. struct batadv_tvlv_tt_change *tt_change;
  415. int tt_diff_len = 0, tt_change_len = 0;
  416. int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
  417. tt_diff_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
  418. /* if we have too many changes for one packet don't send any
  419. * and wait for the tt table request which will be fragmented
  420. */
  421. if (tt_diff_len > bat_priv->soft_iface->mtu)
  422. tt_diff_len = 0;
  423. tt_data = kzalloc(sizeof(*tt_data) + tt_diff_len, GFP_ATOMIC);
  424. if (!tt_data)
  425. return;
  426. tt_data->flags = BATADV_TT_OGM_DIFF;
  427. tt_data->ttvn = atomic_read(&bat_priv->tt.vn);
  428. tt_data->crc = htonl(bat_priv->tt.local_crc);
  429. if (tt_diff_len == 0)
  430. goto container_register;
  431. tt_diff_entries_num = batadv_tt_entries(tt_diff_len);
  432. spin_lock_bh(&bat_priv->tt.changes_list_lock);
  433. atomic_set(&bat_priv->tt.local_changes, 0);
  434. tt_change = (struct batadv_tvlv_tt_change *)(tt_data + 1);
  435. list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
  436. list) {
  437. if (tt_diff_entries_count < tt_diff_entries_num) {
  438. memcpy(tt_change + tt_diff_entries_count,
  439. &entry->change,
  440. sizeof(struct batadv_tvlv_tt_change));
  441. tt_diff_entries_count++;
  442. }
  443. list_del(&entry->list);
  444. kfree(entry);
  445. }
  446. spin_unlock_bh(&bat_priv->tt.changes_list_lock);
  447. /* Keep the buffer for possible tt_request */
  448. spin_lock_bh(&bat_priv->tt.last_changeset_lock);
  449. kfree(bat_priv->tt.last_changeset);
  450. bat_priv->tt.last_changeset_len = 0;
  451. bat_priv->tt.last_changeset = NULL;
  452. tt_change_len = batadv_tt_len(tt_diff_entries_count);
  453. /* check whether this new OGM has no changes due to size problems */
  454. if (tt_diff_entries_count > 0) {
  455. /* if kmalloc() fails we will reply with the full table
  456. * instead of providing the diff
  457. */
  458. bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
  459. if (bat_priv->tt.last_changeset) {
  460. memcpy(bat_priv->tt.last_changeset,
  461. tt_change, tt_change_len);
  462. bat_priv->tt.last_changeset_len = tt_diff_len;
  463. }
  464. }
  465. spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
  466. container_register:
  467. batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
  468. sizeof(*tt_data) + tt_change_len);
  469. kfree(tt_data);
  470. }
  471. int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
  472. {
  473. struct net_device *net_dev = (struct net_device *)seq->private;
  474. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  475. struct batadv_hashtable *hash = bat_priv->tt.local_hash;
  476. struct batadv_tt_common_entry *tt_common_entry;
  477. struct batadv_tt_local_entry *tt_local;
  478. struct batadv_hard_iface *primary_if;
  479. struct hlist_head *head;
  480. uint32_t i;
  481. int last_seen_secs;
  482. int last_seen_msecs;
  483. unsigned long last_seen_jiffies;
  484. bool no_purge;
  485. uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
  486. primary_if = batadv_seq_print_text_primary_if_get(seq);
  487. if (!primary_if)
  488. goto out;
  489. seq_printf(seq,
  490. "Locally retrieved addresses (from %s) announced via TT (TTVN: %u CRC: %#.8x):\n",
  491. net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn),
  492. bat_priv->tt.local_crc);
  493. seq_printf(seq, " %-13s %s %-7s %-10s\n", "Client", "VID",
  494. "Flags", "Last seen");
  495. for (i = 0; i < hash->size; i++) {
  496. head = &hash->table[i];
  497. rcu_read_lock();
  498. hlist_for_each_entry_rcu(tt_common_entry,
  499. head, hash_entry) {
  500. tt_local = container_of(tt_common_entry,
  501. struct batadv_tt_local_entry,
  502. common);
  503. last_seen_jiffies = jiffies - tt_local->last_seen;
  504. last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
  505. last_seen_secs = last_seen_msecs / 1000;
  506. last_seen_msecs = last_seen_msecs % 1000;
  507. no_purge = tt_common_entry->flags & np_flag;
  508. seq_printf(seq, " * %pM %4i [%c%c%c%c%c] %3u.%03u\n",
  509. tt_common_entry->addr,
  510. BATADV_PRINT_VID(tt_common_entry->vid),
  511. (tt_common_entry->flags &
  512. BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
  513. no_purge ? 'P' : '.',
  514. (tt_common_entry->flags &
  515. BATADV_TT_CLIENT_NEW ? 'N' : '.'),
  516. (tt_common_entry->flags &
  517. BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
  518. (tt_common_entry->flags &
  519. BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
  520. no_purge ? 0 : last_seen_secs,
  521. no_purge ? 0 : last_seen_msecs);
  522. }
  523. rcu_read_unlock();
  524. }
  525. out:
  526. if (primary_if)
  527. batadv_hardif_free_ref(primary_if);
  528. return 0;
  529. }
  530. static void
  531. batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
  532. struct batadv_tt_local_entry *tt_local_entry,
  533. uint16_t flags, const char *message)
  534. {
  535. batadv_tt_local_event(bat_priv, tt_local_entry, flags);
  536. /* The local client has to be marked as "pending to be removed" but has
  537. * to be kept in the table in order to send it in a full table
  538. * response issued before the net ttvn increment (consistency check)
  539. */
  540. tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
  541. batadv_dbg(BATADV_DBG_TT, bat_priv,
  542. "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
  543. tt_local_entry->common.addr,
  544. BATADV_PRINT_VID(tt_local_entry->common.vid), message);
  545. }
  546. /**
  547. * batadv_tt_local_remove - logically remove an entry from the local table
  548. * @bat_priv: the bat priv with all the soft interface information
  549. * @addr: the MAC address of the client to remove
  550. * @vid: VLAN identifier
  551. * @message: message to append to the log on deletion
  552. * @roaming: true if the deletion is due to a roaming event
  553. *
  554. * Returns the flags assigned to the local entry before being deleted
  555. */
  556. uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
  557. const uint8_t *addr, unsigned short vid,
  558. const char *message, bool roaming)
  559. {
  560. struct batadv_tt_local_entry *tt_local_entry;
  561. uint16_t flags, curr_flags = BATADV_NO_FLAGS;
  562. tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
  563. if (!tt_local_entry)
  564. goto out;
  565. curr_flags = tt_local_entry->common.flags;
  566. flags = BATADV_TT_CLIENT_DEL;
  567. /* if this global entry addition is due to a roaming, the node has to
  568. * mark the local entry as "roamed" in order to correctly reroute
  569. * packets later
  570. */
  571. if (roaming) {
  572. flags |= BATADV_TT_CLIENT_ROAM;
  573. /* mark the local client as ROAMed */
  574. tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
  575. }
  576. if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
  577. batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
  578. message);
  579. goto out;
  580. }
  581. /* if this client has been added right now, it is possible to
  582. * immediately purge it
  583. */
  584. batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
  585. hlist_del_rcu(&tt_local_entry->common.hash_entry);
  586. batadv_tt_local_entry_free_ref(tt_local_entry);
  587. out:
  588. if (tt_local_entry)
  589. batadv_tt_local_entry_free_ref(tt_local_entry);
  590. return curr_flags;
  591. }
  592. static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
  593. struct hlist_head *head)
  594. {
  595. struct batadv_tt_local_entry *tt_local_entry;
  596. struct batadv_tt_common_entry *tt_common_entry;
  597. struct hlist_node *node_tmp;
  598. hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
  599. hash_entry) {
  600. tt_local_entry = container_of(tt_common_entry,
  601. struct batadv_tt_local_entry,
  602. common);
  603. if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
  604. continue;
  605. /* entry already marked for deletion */
  606. if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
  607. continue;
  608. if (!batadv_has_timed_out(tt_local_entry->last_seen,
  609. BATADV_TT_LOCAL_TIMEOUT))
  610. continue;
  611. batadv_tt_local_set_pending(bat_priv, tt_local_entry,
  612. BATADV_TT_CLIENT_DEL, "timed out");
  613. }
  614. }
  615. static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
  616. {
  617. struct batadv_hashtable *hash = bat_priv->tt.local_hash;
  618. struct hlist_head *head;
  619. spinlock_t *list_lock; /* protects write access to the hash lists */
  620. uint32_t i;
  621. for (i = 0; i < hash->size; i++) {
  622. head = &hash->table[i];
  623. list_lock = &hash->list_locks[i];
  624. spin_lock_bh(list_lock);
  625. batadv_tt_local_purge_list(bat_priv, head);
  626. spin_unlock_bh(list_lock);
  627. }
  628. }
  629. static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
  630. {
  631. struct batadv_hashtable *hash;
  632. spinlock_t *list_lock; /* protects write access to the hash lists */
  633. struct batadv_tt_common_entry *tt_common_entry;
  634. struct batadv_tt_local_entry *tt_local;
  635. struct hlist_node *node_tmp;
  636. struct hlist_head *head;
  637. uint32_t i;
  638. if (!bat_priv->tt.local_hash)
  639. return;
  640. hash = bat_priv->tt.local_hash;
  641. for (i = 0; i < hash->size; i++) {
  642. head = &hash->table[i];
  643. list_lock = &hash->list_locks[i];
  644. spin_lock_bh(list_lock);
  645. hlist_for_each_entry_safe(tt_common_entry, node_tmp,
  646. head, hash_entry) {
  647. hlist_del_rcu(&tt_common_entry->hash_entry);
  648. tt_local = container_of(tt_common_entry,
  649. struct batadv_tt_local_entry,
  650. common);
  651. batadv_tt_local_entry_free_ref(tt_local);
  652. }
  653. spin_unlock_bh(list_lock);
  654. }
  655. batadv_hash_destroy(hash);
  656. bat_priv->tt.local_hash = NULL;
  657. }
  658. static int batadv_tt_global_init(struct batadv_priv *bat_priv)
  659. {
  660. if (bat_priv->tt.global_hash)
  661. return 0;
  662. bat_priv->tt.global_hash = batadv_hash_new(1024);
  663. if (!bat_priv->tt.global_hash)
  664. return -ENOMEM;
  665. batadv_hash_set_lock_class(bat_priv->tt.global_hash,
  666. &batadv_tt_global_hash_lock_class_key);
  667. return 0;
  668. }
  669. static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
  670. {
  671. struct batadv_tt_change_node *entry, *safe;
  672. spin_lock_bh(&bat_priv->tt.changes_list_lock);
  673. list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
  674. list) {
  675. list_del(&entry->list);
  676. kfree(entry);
  677. }
  678. atomic_set(&bat_priv->tt.local_changes, 0);
  679. spin_unlock_bh(&bat_priv->tt.changes_list_lock);
  680. }
  681. /* retrieves the orig_tt_list_entry belonging to orig_node from the
  682. * batadv_tt_global_entry list
  683. *
  684. * returns it with an increased refcounter, NULL if not found
  685. */
  686. static struct batadv_tt_orig_list_entry *
  687. batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
  688. const struct batadv_orig_node *orig_node)
  689. {
  690. struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
  691. const struct hlist_head *head;
  692. rcu_read_lock();
  693. head = &entry->orig_list;
  694. hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
  695. if (tmp_orig_entry->orig_node != orig_node)
  696. continue;
  697. if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
  698. continue;
  699. orig_entry = tmp_orig_entry;
  700. break;
  701. }
  702. rcu_read_unlock();
  703. return orig_entry;
  704. }
  705. /* find out if an orig_node is already in the list of a tt_global_entry.
  706. * returns true if found, false otherwise
  707. */
  708. static bool
  709. batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
  710. const struct batadv_orig_node *orig_node)
  711. {
  712. struct batadv_tt_orig_list_entry *orig_entry;
  713. bool found = false;
  714. orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
  715. if (orig_entry) {
  716. found = true;
  717. batadv_tt_orig_list_entry_free_ref(orig_entry);
  718. }
  719. return found;
  720. }
  721. static void
  722. batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
  723. struct batadv_orig_node *orig_node, int ttvn)
  724. {
  725. struct batadv_tt_orig_list_entry *orig_entry;
  726. orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
  727. if (orig_entry) {
  728. /* refresh the ttvn: the current value could be a bogus one that
  729. * was added during a "temporary client detection"
  730. */
  731. orig_entry->ttvn = ttvn;
  732. goto out;
  733. }
  734. orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
  735. if (!orig_entry)
  736. goto out;
  737. INIT_HLIST_NODE(&orig_entry->list);
  738. atomic_inc(&orig_node->refcount);
  739. atomic_inc(&orig_node->tt_size);
  740. orig_entry->orig_node = orig_node;
  741. orig_entry->ttvn = ttvn;
  742. atomic_set(&orig_entry->refcount, 2);
  743. spin_lock_bh(&tt_global->list_lock);
  744. hlist_add_head_rcu(&orig_entry->list,
  745. &tt_global->orig_list);
  746. spin_unlock_bh(&tt_global->list_lock);
  747. out:
  748. if (orig_entry)
  749. batadv_tt_orig_list_entry_free_ref(orig_entry);
  750. }
  751. /**
  752. * batadv_tt_global_add - add a new TT global entry or update an existing one
  753. * @bat_priv: the bat priv with all the soft interface information
  754. * @orig_node: the originator announcing the client
  755. * @tt_addr: the mac address of the non-mesh client
  756. * @vid: VLAN identifier
  757. * @flags: TT flags that have to be set for this non-mesh client
  758. * @ttvn: the tt version number ever announcing this non-mesh client
  759. *
  760. * Add a new TT global entry for the given originator. If the entry already
  761. * exists add a new reference to the given originator (a global entry can have
  762. * references to multiple originators) and adjust the flags attribute to reflect
  763. * the function argument.
  764. * If a TT local entry exists for this non-mesh client remove it.
  765. *
  766. * The caller must hold orig_node refcount.
  767. *
  768. * Return true if the new entry has been added, false otherwise
  769. */
  770. static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
  771. struct batadv_orig_node *orig_node,
  772. const unsigned char *tt_addr,
  773. unsigned short vid, uint16_t flags,
  774. uint8_t ttvn)
  775. {
  776. struct batadv_tt_global_entry *tt_global_entry;
  777. struct batadv_tt_local_entry *tt_local_entry;
  778. bool ret = false;
  779. int hash_added;
  780. struct batadv_tt_common_entry *common;
  781. uint16_t local_flags;
  782. tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
  783. tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
  784. /* if the node already has a local client for this entry, it has to wait
  785. * for a roaming advertisement instead of manually messing up the global
  786. * table
  787. */
  788. if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
  789. !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
  790. goto out;
  791. if (!tt_global_entry) {
  792. tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
  793. if (!tt_global_entry)
  794. goto out;
  795. common = &tt_global_entry->common;
  796. memcpy(common->addr, tt_addr, ETH_ALEN);
  797. common->vid = vid;
  798. common->flags = flags;
  799. tt_global_entry->roam_at = 0;
  800. /* node must store current time in case of roaming. This is
  801. * needed to purge this entry out on timeout (if nobody claims
  802. * it)
  803. */
  804. if (flags & BATADV_TT_CLIENT_ROAM)
  805. tt_global_entry->roam_at = jiffies;
  806. atomic_set(&common->refcount, 2);
  807. common->added_at = jiffies;
  808. INIT_HLIST_HEAD(&tt_global_entry->orig_list);
  809. spin_lock_init(&tt_global_entry->list_lock);
  810. hash_added = batadv_hash_add(bat_priv->tt.global_hash,
  811. batadv_compare_tt,
  812. batadv_choose_tt, common,
  813. &common->hash_entry);
  814. if (unlikely(hash_added != 0)) {
  815. /* remove the reference for the hash */
  816. batadv_tt_global_entry_free_ref(tt_global_entry);
  817. goto out_remove;
  818. }
  819. } else {
  820. common = &tt_global_entry->common;
  821. /* If there is already a global entry, we can use this one for
  822. * our processing.
  823. * But if we are trying to add a temporary client then here are
  824. * two options at this point:
  825. * 1) the global client is not a temporary client: the global
  826. * client has to be left as it is, temporary information
  827. * should never override any already known client state
  828. * 2) the global client is a temporary client: purge the
  829. * originator list and add the new one orig_entry
  830. */
  831. if (flags & BATADV_TT_CLIENT_TEMP) {
  832. if (!(common->flags & BATADV_TT_CLIENT_TEMP))
  833. goto out;
  834. if (batadv_tt_global_entry_has_orig(tt_global_entry,
  835. orig_node))
  836. goto out_remove;
  837. batadv_tt_global_del_orig_list(tt_global_entry);
  838. goto add_orig_entry;
  839. }
  840. /* if the client was temporary added before receiving the first
  841. * OGM announcing it, we have to clear the TEMP flag
  842. */
  843. common->flags &= ~BATADV_TT_CLIENT_TEMP;
  844. /* the change can carry possible "attribute" flags like the
  845. * TT_CLIENT_WIFI, therefore they have to be copied in the
  846. * client entry
  847. */
  848. tt_global_entry->common.flags |= flags;
  849. /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
  850. * one originator left in the list and we previously received a
  851. * delete + roaming change for this originator.
  852. *
  853. * We should first delete the old originator before adding the
  854. * new one.
  855. */
  856. if (common->flags & BATADV_TT_CLIENT_ROAM) {
  857. batadv_tt_global_del_orig_list(tt_global_entry);
  858. common->flags &= ~BATADV_TT_CLIENT_ROAM;
  859. tt_global_entry->roam_at = 0;
  860. }
  861. }
  862. add_orig_entry:
  863. /* add the new orig_entry (if needed) or update it */
  864. batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
  865. batadv_dbg(BATADV_DBG_TT, bat_priv,
  866. "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
  867. common->addr, BATADV_PRINT_VID(common->vid),
  868. orig_node->orig);
  869. ret = true;
  870. out_remove:
  871. /* remove address from local hash if present */
  872. local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
  873. "global tt received",
  874. flags & BATADV_TT_CLIENT_ROAM);
  875. tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
  876. if (!(flags & BATADV_TT_CLIENT_ROAM))
  877. /* this is a normal global add. Therefore the client is not in a
  878. * roaming state anymore.
  879. */
  880. tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
  881. out:
  882. if (tt_global_entry)
  883. batadv_tt_global_entry_free_ref(tt_global_entry);
  884. if (tt_local_entry)
  885. batadv_tt_local_entry_free_ref(tt_local_entry);
  886. return ret;
  887. }
  888. /* batadv_transtable_best_orig - Get best originator list entry from tt entry
  889. * @tt_global_entry: global translation table entry to be analyzed
  890. *
  891. * This functon assumes the caller holds rcu_read_lock().
  892. * Returns best originator list entry or NULL on errors.
  893. */
  894. static struct batadv_tt_orig_list_entry *
  895. batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
  896. {
  897. struct batadv_neigh_node *router = NULL;
  898. struct hlist_head *head;
  899. struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
  900. int best_tq = 0;
  901. head = &tt_global_entry->orig_list;
  902. hlist_for_each_entry_rcu(orig_entry, head, list) {
  903. router = batadv_orig_node_get_router(orig_entry->orig_node);
  904. if (!router)
  905. continue;
  906. if (router->tq_avg > best_tq) {
  907. best_entry = orig_entry;
  908. best_tq = router->tq_avg;
  909. }
  910. batadv_neigh_node_free_ref(router);
  911. }
  912. return best_entry;
  913. }
  914. /* batadv_tt_global_print_entry - print all orig nodes who announce the address
  915. * for this global entry
  916. * @tt_global_entry: global translation table entry to be printed
  917. * @seq: debugfs table seq_file struct
  918. *
  919. * This functon assumes the caller holds rcu_read_lock().
  920. */
  921. static void
  922. batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
  923. struct seq_file *seq)
  924. {
  925. struct hlist_head *head;
  926. struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
  927. struct batadv_tt_common_entry *tt_common_entry;
  928. uint16_t flags;
  929. uint8_t last_ttvn;
  930. tt_common_entry = &tt_global_entry->common;
  931. flags = tt_common_entry->flags;
  932. best_entry = batadv_transtable_best_orig(tt_global_entry);
  933. if (best_entry) {
  934. last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
  935. seq_printf(seq,
  936. " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
  937. '*', tt_global_entry->common.addr,
  938. BATADV_PRINT_VID(tt_global_entry->common.vid),
  939. best_entry->ttvn, best_entry->orig_node->orig,
  940. last_ttvn, best_entry->orig_node->tt_crc,
  941. (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
  942. (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
  943. (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
  944. }
  945. head = &tt_global_entry->orig_list;
  946. hlist_for_each_entry_rcu(orig_entry, head, list) {
  947. if (best_entry == orig_entry)
  948. continue;
  949. last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
  950. seq_printf(seq,
  951. " %c %pM %4d (%3u) via %pM (%3u) [%c%c%c]\n",
  952. '+', tt_global_entry->common.addr,
  953. BATADV_PRINT_VID(tt_global_entry->common.vid),
  954. orig_entry->ttvn, orig_entry->orig_node->orig,
  955. last_ttvn,
  956. (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
  957. (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
  958. (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
  959. }
  960. }
  961. int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
  962. {
  963. struct net_device *net_dev = (struct net_device *)seq->private;
  964. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  965. struct batadv_hashtable *hash = bat_priv->tt.global_hash;
  966. struct batadv_tt_common_entry *tt_common_entry;
  967. struct batadv_tt_global_entry *tt_global;
  968. struct batadv_hard_iface *primary_if;
  969. struct hlist_head *head;
  970. uint32_t i;
  971. primary_if = batadv_seq_print_text_primary_if_get(seq);
  972. if (!primary_if)
  973. goto out;
  974. seq_printf(seq,
  975. "Globally announced TT entries received via the mesh %s\n",
  976. net_dev->name);
  977. seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
  978. "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
  979. "CRC", "Flags");
  980. for (i = 0; i < hash->size; i++) {
  981. head = &hash->table[i];
  982. rcu_read_lock();
  983. hlist_for_each_entry_rcu(tt_common_entry,
  984. head, hash_entry) {
  985. tt_global = container_of(tt_common_entry,
  986. struct batadv_tt_global_entry,
  987. common);
  988. batadv_tt_global_print_entry(tt_global, seq);
  989. }
  990. rcu_read_unlock();
  991. }
  992. out:
  993. if (primary_if)
  994. batadv_hardif_free_ref(primary_if);
  995. return 0;
  996. }
  997. /* deletes the orig list of a tt_global_entry */
  998. static void
  999. batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
  1000. {
  1001. struct hlist_head *head;
  1002. struct hlist_node *safe;
  1003. struct batadv_tt_orig_list_entry *orig_entry;
  1004. spin_lock_bh(&tt_global_entry->list_lock);
  1005. head = &tt_global_entry->orig_list;
  1006. hlist_for_each_entry_safe(orig_entry, safe, head, list) {
  1007. hlist_del_rcu(&orig_entry->list);
  1008. batadv_tt_orig_list_entry_free_ref(orig_entry);
  1009. }
  1010. spin_unlock_bh(&tt_global_entry->list_lock);
  1011. }
  1012. static void
  1013. batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
  1014. struct batadv_tt_global_entry *tt_global_entry,
  1015. struct batadv_orig_node *orig_node,
  1016. const char *message)
  1017. {
  1018. struct hlist_head *head;
  1019. struct hlist_node *safe;
  1020. struct batadv_tt_orig_list_entry *orig_entry;
  1021. unsigned short vid;
  1022. spin_lock_bh(&tt_global_entry->list_lock);
  1023. head = &tt_global_entry->orig_list;
  1024. hlist_for_each_entry_safe(orig_entry, safe, head, list) {
  1025. if (orig_entry->orig_node == orig_node) {
  1026. vid = tt_global_entry->common.vid;
  1027. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1028. "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
  1029. orig_node->orig,
  1030. tt_global_entry->common.addr,
  1031. BATADV_PRINT_VID(vid), message);
  1032. hlist_del_rcu(&orig_entry->list);
  1033. batadv_tt_orig_list_entry_free_ref(orig_entry);
  1034. }
  1035. }
  1036. spin_unlock_bh(&tt_global_entry->list_lock);
  1037. }
  1038. /* If the client is to be deleted, we check if it is the last origantor entry
  1039. * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
  1040. * timer, otherwise we simply remove the originator scheduled for deletion.
  1041. */
  1042. static void
  1043. batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
  1044. struct batadv_tt_global_entry *tt_global_entry,
  1045. struct batadv_orig_node *orig_node,
  1046. const char *message)
  1047. {
  1048. bool last_entry = true;
  1049. struct hlist_head *head;
  1050. struct batadv_tt_orig_list_entry *orig_entry;
  1051. /* no local entry exists, case 1:
  1052. * Check if this is the last one or if other entries exist.
  1053. */
  1054. rcu_read_lock();
  1055. head = &tt_global_entry->orig_list;
  1056. hlist_for_each_entry_rcu(orig_entry, head, list) {
  1057. if (orig_entry->orig_node != orig_node) {
  1058. last_entry = false;
  1059. break;
  1060. }
  1061. }
  1062. rcu_read_unlock();
  1063. if (last_entry) {
  1064. /* its the last one, mark for roaming. */
  1065. tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
  1066. tt_global_entry->roam_at = jiffies;
  1067. } else
  1068. /* there is another entry, we can simply delete this
  1069. * one and can still use the other one.
  1070. */
  1071. batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
  1072. orig_node, message);
  1073. }
  1074. /**
  1075. * batadv_tt_global_del - remove a client from the global table
  1076. * @bat_priv: the bat priv with all the soft interface information
  1077. * @orig_node: an originator serving this client
  1078. * @addr: the mac address of the client
  1079. * @vid: VLAN identifier
  1080. * @message: a message explaining the reason for deleting the client to print
  1081. * for debugging purpose
  1082. * @roaming: true if the deletion has been triggered by a roaming event
  1083. */
  1084. static void batadv_tt_global_del(struct batadv_priv *bat_priv,
  1085. struct batadv_orig_node *orig_node,
  1086. const unsigned char *addr, unsigned short vid,
  1087. const char *message, bool roaming)
  1088. {
  1089. struct batadv_tt_global_entry *tt_global_entry;
  1090. struct batadv_tt_local_entry *local_entry = NULL;
  1091. tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
  1092. if (!tt_global_entry)
  1093. goto out;
  1094. if (!roaming) {
  1095. batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
  1096. orig_node, message);
  1097. if (hlist_empty(&tt_global_entry->orig_list))
  1098. batadv_tt_global_free(bat_priv, tt_global_entry,
  1099. message);
  1100. goto out;
  1101. }
  1102. /* if we are deleting a global entry due to a roam
  1103. * event, there are two possibilities:
  1104. * 1) the client roamed from node A to node B => if there
  1105. * is only one originator left for this client, we mark
  1106. * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
  1107. * wait for node B to claim it. In case of timeout
  1108. * the entry is purged.
  1109. *
  1110. * If there are other originators left, we directly delete
  1111. * the originator.
  1112. * 2) the client roamed to us => we can directly delete
  1113. * the global entry, since it is useless now.
  1114. */
  1115. local_entry = batadv_tt_local_hash_find(bat_priv,
  1116. tt_global_entry->common.addr,
  1117. vid);
  1118. if (local_entry) {
  1119. /* local entry exists, case 2: client roamed to us. */
  1120. batadv_tt_global_del_orig_list(tt_global_entry);
  1121. batadv_tt_global_free(bat_priv, tt_global_entry, message);
  1122. } else
  1123. /* no local entry exists, case 1: check for roaming */
  1124. batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
  1125. orig_node, message);
  1126. out:
  1127. if (tt_global_entry)
  1128. batadv_tt_global_entry_free_ref(tt_global_entry);
  1129. if (local_entry)
  1130. batadv_tt_local_entry_free_ref(local_entry);
  1131. }
  1132. void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
  1133. struct batadv_orig_node *orig_node,
  1134. const char *message)
  1135. {
  1136. struct batadv_tt_global_entry *tt_global;
  1137. struct batadv_tt_common_entry *tt_common_entry;
  1138. uint32_t i;
  1139. struct batadv_hashtable *hash = bat_priv->tt.global_hash;
  1140. struct hlist_node *safe;
  1141. struct hlist_head *head;
  1142. spinlock_t *list_lock; /* protects write access to the hash lists */
  1143. unsigned short vid;
  1144. if (!hash)
  1145. return;
  1146. for (i = 0; i < hash->size; i++) {
  1147. head = &hash->table[i];
  1148. list_lock = &hash->list_locks[i];
  1149. spin_lock_bh(list_lock);
  1150. hlist_for_each_entry_safe(tt_common_entry, safe,
  1151. head, hash_entry) {
  1152. tt_global = container_of(tt_common_entry,
  1153. struct batadv_tt_global_entry,
  1154. common);
  1155. batadv_tt_global_del_orig_entry(bat_priv, tt_global,
  1156. orig_node, message);
  1157. if (hlist_empty(&tt_global->orig_list)) {
  1158. vid = tt_global->common.vid;
  1159. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1160. "Deleting global tt entry %pM (vid: %d): %s\n",
  1161. tt_global->common.addr,
  1162. BATADV_PRINT_VID(vid), message);
  1163. hlist_del_rcu(&tt_common_entry->hash_entry);
  1164. batadv_tt_global_entry_free_ref(tt_global);
  1165. }
  1166. }
  1167. spin_unlock_bh(list_lock);
  1168. }
  1169. orig_node->tt_initialised = false;
  1170. }
  1171. static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
  1172. char **msg)
  1173. {
  1174. bool purge = false;
  1175. unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
  1176. unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
  1177. if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
  1178. batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
  1179. purge = true;
  1180. *msg = "Roaming timeout\n";
  1181. }
  1182. if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
  1183. batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
  1184. purge = true;
  1185. *msg = "Temporary client timeout\n";
  1186. }
  1187. return purge;
  1188. }
  1189. static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
  1190. {
  1191. struct batadv_hashtable *hash = bat_priv->tt.global_hash;
  1192. struct hlist_head *head;
  1193. struct hlist_node *node_tmp;
  1194. spinlock_t *list_lock; /* protects write access to the hash lists */
  1195. uint32_t i;
  1196. char *msg = NULL;
  1197. struct batadv_tt_common_entry *tt_common;
  1198. struct batadv_tt_global_entry *tt_global;
  1199. for (i = 0; i < hash->size; i++) {
  1200. head = &hash->table[i];
  1201. list_lock = &hash->list_locks[i];
  1202. spin_lock_bh(list_lock);
  1203. hlist_for_each_entry_safe(tt_common, node_tmp, head,
  1204. hash_entry) {
  1205. tt_global = container_of(tt_common,
  1206. struct batadv_tt_global_entry,
  1207. common);
  1208. if (!batadv_tt_global_to_purge(tt_global, &msg))
  1209. continue;
  1210. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1211. "Deleting global tt entry %pM (vid: %d): %s\n",
  1212. tt_global->common.addr,
  1213. BATADV_PRINT_VID(tt_global->common.vid),
  1214. msg);
  1215. hlist_del_rcu(&tt_common->hash_entry);
  1216. batadv_tt_global_entry_free_ref(tt_global);
  1217. }
  1218. spin_unlock_bh(list_lock);
  1219. }
  1220. }
  1221. static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
  1222. {
  1223. struct batadv_hashtable *hash;
  1224. spinlock_t *list_lock; /* protects write access to the hash lists */
  1225. struct batadv_tt_common_entry *tt_common_entry;
  1226. struct batadv_tt_global_entry *tt_global;
  1227. struct hlist_node *node_tmp;
  1228. struct hlist_head *head;
  1229. uint32_t i;
  1230. if (!bat_priv->tt.global_hash)
  1231. return;
  1232. hash = bat_priv->tt.global_hash;
  1233. for (i = 0; i < hash->size; i++) {
  1234. head = &hash->table[i];
  1235. list_lock = &hash->list_locks[i];
  1236. spin_lock_bh(list_lock);
  1237. hlist_for_each_entry_safe(tt_common_entry, node_tmp,
  1238. head, hash_entry) {
  1239. hlist_del_rcu(&tt_common_entry->hash_entry);
  1240. tt_global = container_of(tt_common_entry,
  1241. struct batadv_tt_global_entry,
  1242. common);
  1243. batadv_tt_global_entry_free_ref(tt_global);
  1244. }
  1245. spin_unlock_bh(list_lock);
  1246. }
  1247. batadv_hash_destroy(hash);
  1248. bat_priv->tt.global_hash = NULL;
  1249. }
  1250. static bool
  1251. _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
  1252. struct batadv_tt_global_entry *tt_global_entry)
  1253. {
  1254. bool ret = false;
  1255. if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
  1256. tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
  1257. ret = true;
  1258. return ret;
  1259. }
  1260. /**
  1261. * batadv_transtable_search - get the mesh destination for a given client
  1262. * @bat_priv: the bat priv with all the soft interface information
  1263. * @src: mac address of the source client
  1264. * @addr: mac address of the destination client
  1265. * @vid: VLAN identifier
  1266. *
  1267. * Returns a pointer to the originator that was selected as destination in the
  1268. * mesh for contacting the client 'addr', NULL otherwise.
  1269. * In case of multiple originators serving the same client, the function returns
  1270. * the best one (best in terms of metric towards the destination node).
  1271. *
  1272. * If the two clients are AP isolated the function returns NULL.
  1273. */
  1274. struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
  1275. const uint8_t *src,
  1276. const uint8_t *addr,
  1277. unsigned short vid)
  1278. {
  1279. struct batadv_tt_local_entry *tt_local_entry = NULL;
  1280. struct batadv_tt_global_entry *tt_global_entry = NULL;
  1281. struct batadv_orig_node *orig_node = NULL;
  1282. struct batadv_tt_orig_list_entry *best_entry;
  1283. bool ap_isolation_enabled = false;
  1284. struct batadv_softif_vlan *vlan;
  1285. /* if the AP isolation is requested on a VLAN, then check for its
  1286. * setting in the proper VLAN private data structure
  1287. */
  1288. vlan = batadv_softif_vlan_get(bat_priv, vid);
  1289. if (vlan) {
  1290. ap_isolation_enabled = atomic_read(&vlan->ap_isolation);
  1291. batadv_softif_vlan_free_ref(vlan);
  1292. }
  1293. if (src && ap_isolation_enabled) {
  1294. tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
  1295. if (!tt_local_entry ||
  1296. (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
  1297. goto out;
  1298. }
  1299. tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
  1300. if (!tt_global_entry)
  1301. goto out;
  1302. /* check whether the clients should not communicate due to AP
  1303. * isolation
  1304. */
  1305. if (tt_local_entry &&
  1306. _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
  1307. goto out;
  1308. rcu_read_lock();
  1309. best_entry = batadv_transtable_best_orig(tt_global_entry);
  1310. /* found anything? */
  1311. if (best_entry)
  1312. orig_node = best_entry->orig_node;
  1313. if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
  1314. orig_node = NULL;
  1315. rcu_read_unlock();
  1316. out:
  1317. if (tt_global_entry)
  1318. batadv_tt_global_entry_free_ref(tt_global_entry);
  1319. if (tt_local_entry)
  1320. batadv_tt_local_entry_free_ref(tt_local_entry);
  1321. return orig_node;
  1322. }
  1323. /**
  1324. * batadv_tt_global_crc - calculates the checksum of the local table belonging
  1325. * to the given orig_node
  1326. * @bat_priv: the bat priv with all the soft interface information
  1327. * @orig_node: originator for which the CRC should be computed
  1328. *
  1329. * This function computes the checksum for the global table corresponding to a
  1330. * specific originator. In particular, the checksum is computed as follows: For
  1331. * each client connected to the originator the CRC32C of the MAC address and the
  1332. * VID is computed and then all the CRC32Cs of the various clients are xor'ed
  1333. * together.
  1334. *
  1335. * The idea behind is that CRC32C should be used as much as possible in order to
  1336. * produce a unique hash of the table, but since the order which is used to feed
  1337. * the CRC32C function affects the result and since every node in the network
  1338. * probably sorts the clients differently, the hash function cannot be directly
  1339. * computed over the entire table. Hence the CRC32C is used only on
  1340. * the single client entry, while all the results are then xor'ed together
  1341. * because the XOR operation can combine them all while trying to reduce the
  1342. * noise as much as possible.
  1343. *
  1344. * Returns the checksum of the global table of a given originator.
  1345. */
  1346. static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
  1347. struct batadv_orig_node *orig_node)
  1348. {
  1349. struct batadv_hashtable *hash = bat_priv->tt.global_hash;
  1350. struct batadv_tt_common_entry *tt_common;
  1351. struct batadv_tt_global_entry *tt_global;
  1352. struct hlist_head *head;
  1353. uint32_t i, crc_tmp, crc = 0;
  1354. for (i = 0; i < hash->size; i++) {
  1355. head = &hash->table[i];
  1356. rcu_read_lock();
  1357. hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
  1358. tt_global = container_of(tt_common,
  1359. struct batadv_tt_global_entry,
  1360. common);
  1361. /* Roaming clients are in the global table for
  1362. * consistency only. They don't have to be
  1363. * taken into account while computing the
  1364. * global crc
  1365. */
  1366. if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
  1367. continue;
  1368. /* Temporary clients have not been announced yet, so
  1369. * they have to be skipped while computing the global
  1370. * crc
  1371. */
  1372. if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
  1373. continue;
  1374. /* find out if this global entry is announced by this
  1375. * originator
  1376. */
  1377. if (!batadv_tt_global_entry_has_orig(tt_global,
  1378. orig_node))
  1379. continue;
  1380. crc_tmp = crc32c(0, &tt_common->vid,
  1381. sizeof(tt_common->vid));
  1382. crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
  1383. }
  1384. rcu_read_unlock();
  1385. }
  1386. return crc;
  1387. }
  1388. /**
  1389. * batadv_tt_local_crc - calculates the checksum of the local table
  1390. * @bat_priv: the bat priv with all the soft interface information
  1391. *
  1392. * For details about the computation, please refer to the documentation for
  1393. * batadv_tt_global_crc().
  1394. *
  1395. * Returns the checksum of the local table
  1396. */
  1397. static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
  1398. {
  1399. struct batadv_hashtable *hash = bat_priv->tt.local_hash;
  1400. struct batadv_tt_common_entry *tt_common;
  1401. struct hlist_head *head;
  1402. uint32_t i, crc_tmp, crc = 0;
  1403. for (i = 0; i < hash->size; i++) {
  1404. head = &hash->table[i];
  1405. rcu_read_lock();
  1406. hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
  1407. /* not yet committed clients have not to be taken into
  1408. * account while computing the CRC
  1409. */
  1410. if (tt_common->flags & BATADV_TT_CLIENT_NEW)
  1411. continue;
  1412. crc_tmp = crc32c(0, &tt_common->vid,
  1413. sizeof(tt_common->vid));
  1414. crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
  1415. }
  1416. rcu_read_unlock();
  1417. }
  1418. return crc;
  1419. }
  1420. static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
  1421. {
  1422. struct batadv_tt_req_node *node, *safe;
  1423. spin_lock_bh(&bat_priv->tt.req_list_lock);
  1424. list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
  1425. list_del(&node->list);
  1426. kfree(node);
  1427. }
  1428. spin_unlock_bh(&bat_priv->tt.req_list_lock);
  1429. }
  1430. static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
  1431. struct batadv_orig_node *orig_node,
  1432. const void *tt_buff,
  1433. uint16_t tt_buff_len)
  1434. {
  1435. /* Replace the old buffer only if I received something in the
  1436. * last OGM (the OGM could carry no changes)
  1437. */
  1438. spin_lock_bh(&orig_node->tt_buff_lock);
  1439. if (tt_buff_len > 0) {
  1440. kfree(orig_node->tt_buff);
  1441. orig_node->tt_buff_len = 0;
  1442. orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
  1443. if (orig_node->tt_buff) {
  1444. memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
  1445. orig_node->tt_buff_len = tt_buff_len;
  1446. }
  1447. }
  1448. spin_unlock_bh(&orig_node->tt_buff_lock);
  1449. }
  1450. static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
  1451. {
  1452. struct batadv_tt_req_node *node, *safe;
  1453. spin_lock_bh(&bat_priv->tt.req_list_lock);
  1454. list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
  1455. if (batadv_has_timed_out(node->issued_at,
  1456. BATADV_TT_REQUEST_TIMEOUT)) {
  1457. list_del(&node->list);
  1458. kfree(node);
  1459. }
  1460. }
  1461. spin_unlock_bh(&bat_priv->tt.req_list_lock);
  1462. }
  1463. /* returns the pointer to the new tt_req_node struct if no request
  1464. * has already been issued for this orig_node, NULL otherwise
  1465. */
  1466. static struct batadv_tt_req_node *
  1467. batadv_new_tt_req_node(struct batadv_priv *bat_priv,
  1468. struct batadv_orig_node *orig_node)
  1469. {
  1470. struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
  1471. spin_lock_bh(&bat_priv->tt.req_list_lock);
  1472. list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
  1473. if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
  1474. !batadv_has_timed_out(tt_req_node_tmp->issued_at,
  1475. BATADV_TT_REQUEST_TIMEOUT))
  1476. goto unlock;
  1477. }
  1478. tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
  1479. if (!tt_req_node)
  1480. goto unlock;
  1481. memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
  1482. tt_req_node->issued_at = jiffies;
  1483. list_add(&tt_req_node->list, &bat_priv->tt.req_list);
  1484. unlock:
  1485. spin_unlock_bh(&bat_priv->tt.req_list_lock);
  1486. return tt_req_node;
  1487. }
  1488. /**
  1489. * batadv_tt_local_valid - verify that given tt entry is a valid one
  1490. * @entry_ptr: to be checked local tt entry
  1491. * @data_ptr: not used but definition required to satisfy the callback prototype
  1492. *
  1493. * Returns 1 if the entry is a valid, 0 otherwise.
  1494. */
  1495. static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
  1496. {
  1497. const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
  1498. if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
  1499. return 0;
  1500. return 1;
  1501. }
  1502. static int batadv_tt_global_valid(const void *entry_ptr,
  1503. const void *data_ptr)
  1504. {
  1505. const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
  1506. const struct batadv_tt_global_entry *tt_global_entry;
  1507. const struct batadv_orig_node *orig_node = data_ptr;
  1508. if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
  1509. tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
  1510. return 0;
  1511. tt_global_entry = container_of(tt_common_entry,
  1512. struct batadv_tt_global_entry,
  1513. common);
  1514. return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
  1515. }
  1516. /**
  1517. * batadv_tt_tvlv_generate - creates tvlv tt data buffer to fill it with the
  1518. * tt entries from the specified tt hash
  1519. * @bat_priv: the bat priv with all the soft interface information
  1520. * @hash: hash table containing the tt entries
  1521. * @tt_len: expected tvlv tt data buffer length in number of bytes
  1522. * @valid_cb: function to filter tt change entries
  1523. * @cb_data: data passed to the filter function as argument
  1524. *
  1525. * Returns pointer to allocated tvlv tt data buffer if operation was
  1526. * successful or NULL otherwise.
  1527. */
  1528. static struct batadv_tvlv_tt_data *
  1529. batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
  1530. struct batadv_hashtable *hash, uint16_t tt_len,
  1531. int (*valid_cb)(const void *, const void *),
  1532. void *cb_data)
  1533. {
  1534. struct batadv_tt_common_entry *tt_common_entry;
  1535. struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
  1536. struct batadv_tvlv_tt_change *tt_change;
  1537. struct hlist_head *head;
  1538. uint16_t tt_tot, tt_num_entries = 0;
  1539. ssize_t tvlv_tt_size = sizeof(struct batadv_tvlv_tt_data);
  1540. uint32_t i;
  1541. if (tvlv_tt_size + tt_len > bat_priv->soft_iface->mtu) {
  1542. tt_len = bat_priv->soft_iface->mtu - tvlv_tt_size;
  1543. tt_len -= tt_len % sizeof(struct batadv_tvlv_tt_change);
  1544. }
  1545. tt_tot = batadv_tt_entries(tt_len);
  1546. tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
  1547. GFP_ATOMIC);
  1548. if (!tvlv_tt_data)
  1549. goto out;
  1550. tt_change = (struct batadv_tvlv_tt_change *)(tvlv_tt_data + 1);
  1551. rcu_read_lock();
  1552. for (i = 0; i < hash->size; i++) {
  1553. head = &hash->table[i];
  1554. hlist_for_each_entry_rcu(tt_common_entry,
  1555. head, hash_entry) {
  1556. if (tt_tot == tt_num_entries)
  1557. break;
  1558. if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
  1559. continue;
  1560. memcpy(tt_change->addr, tt_common_entry->addr,
  1561. ETH_ALEN);
  1562. tt_change->flags = tt_common_entry->flags;
  1563. tt_change->vid = htons(tt_common_entry->vid);
  1564. tt_change->reserved = 0;
  1565. tt_num_entries++;
  1566. tt_change++;
  1567. }
  1568. }
  1569. rcu_read_unlock();
  1570. out:
  1571. return tvlv_tt_data;
  1572. }
  1573. /**
  1574. * batadv_send_tt_request - send a TT Request message to a given node
  1575. * @bat_priv: the bat priv with all the soft interface information
  1576. * @dst_orig_node: the destination of the message
  1577. * @ttvn: the version number that the source of the message is looking for
  1578. * @tt_crc: the CRC associated with the version number
  1579. * @full_table: ask for the entire translation table if true, while only for the
  1580. * last TT diff otherwise
  1581. */
  1582. static int batadv_send_tt_request(struct batadv_priv *bat_priv,
  1583. struct batadv_orig_node *dst_orig_node,
  1584. uint8_t ttvn, uint32_t tt_crc,
  1585. bool full_table)
  1586. {
  1587. struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
  1588. struct batadv_hard_iface *primary_if;
  1589. struct batadv_tt_req_node *tt_req_node = NULL;
  1590. bool ret = false;
  1591. primary_if = batadv_primary_if_get_selected(bat_priv);
  1592. if (!primary_if)
  1593. goto out;
  1594. /* The new tt_req will be issued only if I'm not waiting for a
  1595. * reply from the same orig_node yet
  1596. */
  1597. tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
  1598. if (!tt_req_node)
  1599. goto out;
  1600. tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data), GFP_ATOMIC);
  1601. if (!tvlv_tt_data)
  1602. goto out;
  1603. tvlv_tt_data->flags = BATADV_TT_REQUEST;
  1604. tvlv_tt_data->ttvn = ttvn;
  1605. tvlv_tt_data->crc = htonl(tt_crc);
  1606. if (full_table)
  1607. tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
  1608. batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
  1609. dst_orig_node->orig, full_table ? 'F' : '.');
  1610. batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
  1611. batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
  1612. dst_orig_node->orig, BATADV_TVLV_TT, 1,
  1613. tvlv_tt_data, sizeof(*tvlv_tt_data));
  1614. ret = true;
  1615. out:
  1616. if (primary_if)
  1617. batadv_hardif_free_ref(primary_if);
  1618. if (ret && tt_req_node) {
  1619. spin_lock_bh(&bat_priv->tt.req_list_lock);
  1620. list_del(&tt_req_node->list);
  1621. spin_unlock_bh(&bat_priv->tt.req_list_lock);
  1622. kfree(tt_req_node);
  1623. }
  1624. kfree(tvlv_tt_data);
  1625. return ret;
  1626. }
  1627. /**
  1628. * batadv_send_other_tt_response - send reply to tt request concerning another
  1629. * node's translation table
  1630. * @bat_priv: the bat priv with all the soft interface information
  1631. * @tt_data: tt data containing the tt request information
  1632. * @req_src: mac address of tt request sender
  1633. * @req_dst: mac address of tt request recipient
  1634. *
  1635. * Returns true if tt request reply was sent, false otherwise.
  1636. */
  1637. static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
  1638. struct batadv_tvlv_tt_data *tt_data,
  1639. uint8_t *req_src, uint8_t *req_dst)
  1640. {
  1641. struct batadv_orig_node *req_dst_orig_node;
  1642. struct batadv_orig_node *res_dst_orig_node = NULL;
  1643. struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
  1644. uint8_t orig_ttvn, req_ttvn;
  1645. uint16_t tt_len;
  1646. bool ret = false, full_table;
  1647. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1648. "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
  1649. req_src, tt_data->ttvn, req_dst,
  1650. (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
  1651. /* Let's get the orig node of the REAL destination */
  1652. req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
  1653. if (!req_dst_orig_node)
  1654. goto out;
  1655. res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
  1656. if (!res_dst_orig_node)
  1657. goto out;
  1658. orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
  1659. req_ttvn = tt_data->ttvn;
  1660. /* this node doesn't have the requested data */
  1661. if (orig_ttvn != req_ttvn ||
  1662. tt_data->crc != htonl(req_dst_orig_node->tt_crc))
  1663. goto out;
  1664. /* If the full table has been explicitly requested */
  1665. if (tt_data->flags & BATADV_TT_FULL_TABLE ||
  1666. !req_dst_orig_node->tt_buff)
  1667. full_table = true;
  1668. else
  1669. full_table = false;
  1670. /* TT fragmentation hasn't been implemented yet, so send as many
  1671. * TT entries fit a single packet as possible only
  1672. */
  1673. if (!full_table) {
  1674. spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
  1675. tt_len = req_dst_orig_node->tt_buff_len;
  1676. tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
  1677. GFP_ATOMIC);
  1678. if (!tvlv_tt_data)
  1679. goto unlock;
  1680. /* Copy the last orig_node's OGM buffer */
  1681. memcpy(tvlv_tt_data + 1, req_dst_orig_node->tt_buff,
  1682. req_dst_orig_node->tt_buff_len);
  1683. spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
  1684. } else {
  1685. tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
  1686. tt_len = batadv_tt_len(tt_len);
  1687. tvlv_tt_data = batadv_tt_tvlv_generate(bat_priv,
  1688. bat_priv->tt.global_hash,
  1689. tt_len,
  1690. batadv_tt_global_valid,
  1691. req_dst_orig_node);
  1692. if (!tvlv_tt_data)
  1693. goto out;
  1694. }
  1695. tvlv_tt_data->flags = BATADV_TT_RESPONSE;
  1696. tvlv_tt_data->ttvn = req_ttvn;
  1697. if (full_table)
  1698. tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
  1699. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1700. "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
  1701. res_dst_orig_node->orig, req_dst_orig_node->orig,
  1702. full_table ? 'F' : '.', req_ttvn);
  1703. batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
  1704. batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
  1705. req_src, BATADV_TVLV_TT, 1,
  1706. tvlv_tt_data, sizeof(*tvlv_tt_data) + tt_len);
  1707. ret = true;
  1708. goto out;
  1709. unlock:
  1710. spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
  1711. out:
  1712. if (res_dst_orig_node)
  1713. batadv_orig_node_free_ref(res_dst_orig_node);
  1714. if (req_dst_orig_node)
  1715. batadv_orig_node_free_ref(req_dst_orig_node);
  1716. kfree(tvlv_tt_data);
  1717. return ret;
  1718. }
  1719. /**
  1720. * batadv_send_my_tt_response - send reply to tt request concerning this node's
  1721. * translation table
  1722. * @bat_priv: the bat priv with all the soft interface information
  1723. * @tt_data: tt data containing the tt request information
  1724. * @req_src: mac address of tt request sender
  1725. *
  1726. * Returns true if tt request reply was sent, false otherwise.
  1727. */
  1728. static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
  1729. struct batadv_tvlv_tt_data *tt_data,
  1730. uint8_t *req_src)
  1731. {
  1732. struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
  1733. struct batadv_orig_node *orig_node;
  1734. struct batadv_hard_iface *primary_if = NULL;
  1735. uint8_t my_ttvn, req_ttvn;
  1736. bool full_table;
  1737. uint16_t tt_len;
  1738. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1739. "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
  1740. req_src, tt_data->ttvn,
  1741. (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
  1742. spin_lock_bh(&bat_priv->tt.commit_lock);
  1743. my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
  1744. req_ttvn = tt_data->ttvn;
  1745. orig_node = batadv_orig_hash_find(bat_priv, req_src);
  1746. if (!orig_node)
  1747. goto out;
  1748. primary_if = batadv_primary_if_get_selected(bat_priv);
  1749. if (!primary_if)
  1750. goto out;
  1751. /* If the full table has been explicitly requested or the gap
  1752. * is too big send the whole local translation table
  1753. */
  1754. if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
  1755. !bat_priv->tt.last_changeset)
  1756. full_table = true;
  1757. else
  1758. full_table = false;
  1759. /* TT fragmentation hasn't been implemented yet, so send as many
  1760. * TT entries fit a single packet as possible only
  1761. */
  1762. if (!full_table) {
  1763. spin_lock_bh(&bat_priv->tt.last_changeset_lock);
  1764. tt_len = bat_priv->tt.last_changeset_len;
  1765. tvlv_tt_data = kzalloc(sizeof(*tvlv_tt_data) + tt_len,
  1766. GFP_ATOMIC);
  1767. if (!tvlv_tt_data)
  1768. goto unlock;
  1769. /* Copy the last orig_node's OGM buffer */
  1770. memcpy(tvlv_tt_data + 1, bat_priv->tt.last_changeset,
  1771. bat_priv->tt.last_changeset_len);
  1772. spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
  1773. } else {
  1774. tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
  1775. tt_len = batadv_tt_len(tt_len);
  1776. req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
  1777. tvlv_tt_data = batadv_tt_tvlv_generate(bat_priv,
  1778. bat_priv->tt.local_hash,
  1779. tt_len,
  1780. batadv_tt_local_valid,
  1781. NULL);
  1782. if (!tvlv_tt_data)
  1783. goto out;
  1784. }
  1785. tvlv_tt_data->flags = BATADV_TT_RESPONSE;
  1786. tvlv_tt_data->ttvn = req_ttvn;
  1787. if (full_table)
  1788. tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
  1789. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1790. "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
  1791. orig_node->orig, full_table ? 'F' : '.', req_ttvn);
  1792. batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
  1793. batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
  1794. req_src, BATADV_TVLV_TT, 1,
  1795. tvlv_tt_data, sizeof(*tvlv_tt_data) + tt_len);
  1796. goto out;
  1797. unlock:
  1798. spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
  1799. out:
  1800. spin_unlock_bh(&bat_priv->tt.commit_lock);
  1801. if (orig_node)
  1802. batadv_orig_node_free_ref(orig_node);
  1803. if (primary_if)
  1804. batadv_hardif_free_ref(primary_if);
  1805. kfree(tvlv_tt_data);
  1806. /* The packet was for this host, so it doesn't need to be re-routed */
  1807. return true;
  1808. }
  1809. /**
  1810. * batadv_send_tt_response - send reply to tt request
  1811. * @bat_priv: the bat priv with all the soft interface information
  1812. * @tt_data: tt data containing the tt request information
  1813. * @req_src: mac address of tt request sender
  1814. * @req_dst: mac address of tt request recipient
  1815. *
  1816. * Returns true if tt request reply was sent, false otherwise.
  1817. */
  1818. static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
  1819. struct batadv_tvlv_tt_data *tt_data,
  1820. uint8_t *req_src, uint8_t *req_dst)
  1821. {
  1822. if (batadv_is_my_mac(bat_priv, req_dst)) {
  1823. /* don't answer backbone gws! */
  1824. if (batadv_bla_is_backbone_gw_orig(bat_priv, req_src))
  1825. return true;
  1826. return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
  1827. } else {
  1828. return batadv_send_other_tt_response(bat_priv, tt_data,
  1829. req_src, req_dst);
  1830. }
  1831. }
  1832. static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
  1833. struct batadv_orig_node *orig_node,
  1834. struct batadv_tvlv_tt_change *tt_change,
  1835. uint16_t tt_num_changes, uint8_t ttvn)
  1836. {
  1837. int i;
  1838. int roams;
  1839. for (i = 0; i < tt_num_changes; i++) {
  1840. if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
  1841. roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
  1842. batadv_tt_global_del(bat_priv, orig_node,
  1843. (tt_change + i)->addr,
  1844. ntohs((tt_change + i)->vid),
  1845. "tt removed by changes",
  1846. roams);
  1847. } else {
  1848. if (!batadv_tt_global_add(bat_priv, orig_node,
  1849. (tt_change + i)->addr,
  1850. ntohs((tt_change + i)->vid),
  1851. (tt_change + i)->flags, ttvn))
  1852. /* In case of problem while storing a
  1853. * global_entry, we stop the updating
  1854. * procedure without committing the
  1855. * ttvn change. This will avoid to send
  1856. * corrupted data on tt_request
  1857. */
  1858. return;
  1859. }
  1860. }
  1861. orig_node->tt_initialised = true;
  1862. }
  1863. static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
  1864. struct batadv_tvlv_tt_data *tt_data,
  1865. uint8_t *resp_src, uint16_t num_entries)
  1866. {
  1867. struct batadv_orig_node *orig_node;
  1868. orig_node = batadv_orig_hash_find(bat_priv, resp_src);
  1869. if (!orig_node)
  1870. goto out;
  1871. /* Purge the old table first.. */
  1872. batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
  1873. _batadv_tt_update_changes(bat_priv, orig_node,
  1874. (struct batadv_tvlv_tt_change *)(tt_data + 1),
  1875. num_entries, tt_data->ttvn);
  1876. spin_lock_bh(&orig_node->tt_buff_lock);
  1877. kfree(orig_node->tt_buff);
  1878. orig_node->tt_buff_len = 0;
  1879. orig_node->tt_buff = NULL;
  1880. spin_unlock_bh(&orig_node->tt_buff_lock);
  1881. atomic_set(&orig_node->last_ttvn, tt_data->ttvn);
  1882. out:
  1883. if (orig_node)
  1884. batadv_orig_node_free_ref(orig_node);
  1885. }
  1886. static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
  1887. struct batadv_orig_node *orig_node,
  1888. uint16_t tt_num_changes, uint8_t ttvn,
  1889. struct batadv_tvlv_tt_change *tt_change)
  1890. {
  1891. _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
  1892. tt_num_changes, ttvn);
  1893. batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
  1894. batadv_tt_len(tt_num_changes));
  1895. atomic_set(&orig_node->last_ttvn, ttvn);
  1896. }
  1897. /**
  1898. * batadv_is_my_client - check if a client is served by the local node
  1899. * @bat_priv: the bat priv with all the soft interface information
  1900. * @addr: the mac adress of the client to check
  1901. * @vid: VLAN identifier
  1902. *
  1903. * Returns true if the client is served by this node, false otherwise.
  1904. */
  1905. bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr,
  1906. unsigned short vid)
  1907. {
  1908. struct batadv_tt_local_entry *tt_local_entry;
  1909. bool ret = false;
  1910. tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
  1911. if (!tt_local_entry)
  1912. goto out;
  1913. /* Check if the client has been logically deleted (but is kept for
  1914. * consistency purpose)
  1915. */
  1916. if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
  1917. (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
  1918. goto out;
  1919. ret = true;
  1920. out:
  1921. if (tt_local_entry)
  1922. batadv_tt_local_entry_free_ref(tt_local_entry);
  1923. return ret;
  1924. }
  1925. /**
  1926. * batadv_handle_tt_response - process incoming tt reply
  1927. * @bat_priv: the bat priv with all the soft interface information
  1928. * @tt_data: tt data containing the tt request information
  1929. * @resp_src: mac address of tt reply sender
  1930. * @num_entries: number of tt change entries appended to the tt data
  1931. */
  1932. static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
  1933. struct batadv_tvlv_tt_data *tt_data,
  1934. uint8_t *resp_src, uint16_t num_entries)
  1935. {
  1936. struct batadv_tt_req_node *node, *safe;
  1937. struct batadv_orig_node *orig_node = NULL;
  1938. struct batadv_tvlv_tt_change *tt_change;
  1939. batadv_dbg(BATADV_DBG_TT, bat_priv,
  1940. "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
  1941. resp_src, tt_data->ttvn, num_entries,
  1942. (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
  1943. /* we should have never asked a backbone gw */
  1944. if (batadv_bla_is_backbone_gw_orig(bat_priv, resp_src))
  1945. goto out;
  1946. orig_node = batadv_orig_hash_find(bat_priv, resp_src);
  1947. if (!orig_node)
  1948. goto out;
  1949. spin_lock_bh(&orig_node->tt_lock);
  1950. if (tt_data->flags & BATADV_TT_FULL_TABLE) {
  1951. batadv_tt_fill_gtable(bat_priv, tt_data, resp_src, num_entries);
  1952. } else {
  1953. tt_change = (struct batadv_tvlv_tt_change *)(tt_data + 1);
  1954. batadv_tt_update_changes(bat_priv, orig_node, num_entries,
  1955. tt_data->ttvn, tt_change);
  1956. }
  1957. /* Recalculate the CRC for this orig_node and store it */
  1958. orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
  1959. spin_unlock_bh(&orig_node->tt_lock);
  1960. /* Delete the tt_req_node from pending tt_requests list */
  1961. spin_lock_bh(&bat_priv->tt.req_list_lock);
  1962. list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
  1963. if (!batadv_compare_eth(node->addr, resp_src))
  1964. continue;
  1965. list_del(&node->list);
  1966. kfree(node);
  1967. }
  1968. spin_unlock_bh(&bat_priv->tt.req_list_lock);
  1969. out:
  1970. if (orig_node)
  1971. batadv_orig_node_free_ref(orig_node);
  1972. }
  1973. static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
  1974. {
  1975. struct batadv_tt_roam_node *node, *safe;
  1976. spin_lock_bh(&bat_priv->tt.roam_list_lock);
  1977. list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
  1978. list_del(&node->list);
  1979. kfree(node);
  1980. }
  1981. spin_unlock_bh(&bat_priv->tt.roam_list_lock);
  1982. }
  1983. static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
  1984. {
  1985. struct batadv_tt_roam_node *node, *safe;
  1986. spin_lock_bh(&bat_priv->tt.roam_list_lock);
  1987. list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
  1988. if (!batadv_has_timed_out(node->first_time,
  1989. BATADV_ROAMING_MAX_TIME))
  1990. continue;
  1991. list_del(&node->list);
  1992. kfree(node);
  1993. }
  1994. spin_unlock_bh(&bat_priv->tt.roam_list_lock);
  1995. }
  1996. /* This function checks whether the client already reached the
  1997. * maximum number of possible roaming phases. In this case the ROAMING_ADV
  1998. * will not be sent.
  1999. *
  2000. * returns true if the ROAMING_ADV can be sent, false otherwise
  2001. */
  2002. static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
  2003. uint8_t *client)
  2004. {
  2005. struct batadv_tt_roam_node *tt_roam_node;
  2006. bool ret = false;
  2007. spin_lock_bh(&bat_priv->tt.roam_list_lock);
  2008. /* The new tt_req will be issued only if I'm not waiting for a
  2009. * reply from the same orig_node yet
  2010. */
  2011. list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
  2012. if (!batadv_compare_eth(tt_roam_node->addr, client))
  2013. continue;
  2014. if (batadv_has_timed_out(tt_roam_node->first_time,
  2015. BATADV_ROAMING_MAX_TIME))
  2016. continue;
  2017. if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
  2018. /* Sorry, you roamed too many times! */
  2019. goto unlock;
  2020. ret = true;
  2021. break;
  2022. }
  2023. if (!ret) {
  2024. tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
  2025. if (!tt_roam_node)
  2026. goto unlock;
  2027. tt_roam_node->first_time = jiffies;
  2028. atomic_set(&tt_roam_node->counter,
  2029. BATADV_ROAMING_MAX_COUNT - 1);
  2030. memcpy(tt_roam_node->addr, client, ETH_ALEN);
  2031. list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
  2032. ret = true;
  2033. }
  2034. unlock:
  2035. spin_unlock_bh(&bat_priv->tt.roam_list_lock);
  2036. return ret;
  2037. }
  2038. /**
  2039. * batadv_send_roam_adv - send a roaming advertisement message
  2040. * @bat_priv: the bat priv with all the soft interface information
  2041. * @client: mac address of the roaming client
  2042. * @vid: VLAN identifier
  2043. * @orig_node: message destination
  2044. *
  2045. * Send a ROAMING_ADV message to the node which was previously serving this
  2046. * client. This is done to inform the node that from now on all traffic destined
  2047. * for this particular roamed client has to be forwarded to the sender of the
  2048. * roaming message.
  2049. */
  2050. static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
  2051. unsigned short vid,
  2052. struct batadv_orig_node *orig_node)
  2053. {
  2054. struct batadv_hard_iface *primary_if;
  2055. struct batadv_tvlv_roam_adv tvlv_roam;
  2056. primary_if = batadv_primary_if_get_selected(bat_priv);
  2057. if (!primary_if)
  2058. goto out;
  2059. /* before going on we have to check whether the client has
  2060. * already roamed to us too many times
  2061. */
  2062. if (!batadv_tt_check_roam_count(bat_priv, client))
  2063. goto out;
  2064. batadv_dbg(BATADV_DBG_TT, bat_priv,
  2065. "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
  2066. orig_node->orig, client, BATADV_PRINT_VID(vid));
  2067. batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
  2068. memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
  2069. tvlv_roam.vid = htons(vid);
  2070. batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
  2071. orig_node->orig, BATADV_TVLV_ROAM, 1,
  2072. &tvlv_roam, sizeof(tvlv_roam));
  2073. out:
  2074. if (primary_if)
  2075. batadv_hardif_free_ref(primary_if);
  2076. }
  2077. static void batadv_tt_purge(struct work_struct *work)
  2078. {
  2079. struct delayed_work *delayed_work;
  2080. struct batadv_priv_tt *priv_tt;
  2081. struct batadv_priv *bat_priv;
  2082. delayed_work = container_of(work, struct delayed_work, work);
  2083. priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
  2084. bat_priv = container_of(priv_tt, struct batadv_priv, tt);
  2085. batadv_tt_local_purge(bat_priv);
  2086. batadv_tt_global_purge(bat_priv);
  2087. batadv_tt_req_purge(bat_priv);
  2088. batadv_tt_roam_purge(bat_priv);
  2089. queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
  2090. msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
  2091. }
  2092. void batadv_tt_free(struct batadv_priv *bat_priv)
  2093. {
  2094. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
  2095. batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
  2096. cancel_delayed_work_sync(&bat_priv->tt.work);
  2097. batadv_tt_local_table_free(bat_priv);
  2098. batadv_tt_global_table_free(bat_priv);
  2099. batadv_tt_req_list_free(bat_priv);
  2100. batadv_tt_changes_list_free(bat_priv);
  2101. batadv_tt_roam_list_free(bat_priv);
  2102. kfree(bat_priv->tt.last_changeset);
  2103. }
  2104. /* This function will enable or disable the specified flags for all the entries
  2105. * in the given hash table and returns the number of modified entries
  2106. */
  2107. static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
  2108. uint16_t flags, bool enable)
  2109. {
  2110. uint32_t i;
  2111. uint16_t changed_num = 0;
  2112. struct hlist_head *head;
  2113. struct batadv_tt_common_entry *tt_common_entry;
  2114. if (!hash)
  2115. goto out;
  2116. for (i = 0; i < hash->size; i++) {
  2117. head = &hash->table[i];
  2118. rcu_read_lock();
  2119. hlist_for_each_entry_rcu(tt_common_entry,
  2120. head, hash_entry) {
  2121. if (enable) {
  2122. if ((tt_common_entry->flags & flags) == flags)
  2123. continue;
  2124. tt_common_entry->flags |= flags;
  2125. } else {
  2126. if (!(tt_common_entry->flags & flags))
  2127. continue;
  2128. tt_common_entry->flags &= ~flags;
  2129. }
  2130. changed_num++;
  2131. }
  2132. rcu_read_unlock();
  2133. }
  2134. out:
  2135. return changed_num;
  2136. }
  2137. /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
  2138. static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
  2139. {
  2140. struct batadv_hashtable *hash = bat_priv->tt.local_hash;
  2141. struct batadv_tt_common_entry *tt_common;
  2142. struct batadv_tt_local_entry *tt_local;
  2143. struct hlist_node *node_tmp;
  2144. struct hlist_head *head;
  2145. spinlock_t *list_lock; /* protects write access to the hash lists */
  2146. uint32_t i;
  2147. if (!hash)
  2148. return;
  2149. for (i = 0; i < hash->size; i++) {
  2150. head = &hash->table[i];
  2151. list_lock = &hash->list_locks[i];
  2152. spin_lock_bh(list_lock);
  2153. hlist_for_each_entry_safe(tt_common, node_tmp, head,
  2154. hash_entry) {
  2155. if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
  2156. continue;
  2157. batadv_dbg(BATADV_DBG_TT, bat_priv,
  2158. "Deleting local tt entry (%pM, vid: %d): pending\n",
  2159. tt_common->addr,
  2160. BATADV_PRINT_VID(tt_common->vid));
  2161. atomic_dec(&bat_priv->tt.local_entry_num);
  2162. hlist_del_rcu(&tt_common->hash_entry);
  2163. tt_local = container_of(tt_common,
  2164. struct batadv_tt_local_entry,
  2165. common);
  2166. batadv_tt_local_entry_free_ref(tt_local);
  2167. }
  2168. spin_unlock_bh(list_lock);
  2169. }
  2170. }
  2171. /**
  2172. * batadv_tt_local_commit_changes - commit all pending local tt changes which
  2173. * have been queued in the time since the last commit
  2174. * @bat_priv: the bat priv with all the soft interface information
  2175. */
  2176. void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
  2177. {
  2178. uint16_t changed_num = 0;
  2179. spin_lock_bh(&bat_priv->tt.commit_lock);
  2180. if (atomic_read(&bat_priv->tt.local_changes) < 1) {
  2181. if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
  2182. batadv_tt_tvlv_container_update(bat_priv);
  2183. goto out;
  2184. }
  2185. changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
  2186. BATADV_TT_CLIENT_NEW, false);
  2187. /* all reset entries have to be counted as local entries */
  2188. atomic_add(changed_num, &bat_priv->tt.local_entry_num);
  2189. batadv_tt_local_purge_pending_clients(bat_priv);
  2190. bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
  2191. /* Increment the TTVN only once per OGM interval */
  2192. atomic_inc(&bat_priv->tt.vn);
  2193. batadv_dbg(BATADV_DBG_TT, bat_priv,
  2194. "Local changes committed, updating to ttvn %u\n",
  2195. (uint8_t)atomic_read(&bat_priv->tt.vn));
  2196. /* reset the sending counter */
  2197. atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
  2198. batadv_tt_tvlv_container_update(bat_priv);
  2199. out:
  2200. spin_unlock_bh(&bat_priv->tt.commit_lock);
  2201. }
  2202. bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
  2203. uint8_t *dst, unsigned short vid)
  2204. {
  2205. struct batadv_tt_local_entry *tt_local_entry = NULL;
  2206. struct batadv_tt_global_entry *tt_global_entry = NULL;
  2207. struct batadv_softif_vlan *vlan;
  2208. bool ret = false;
  2209. vlan = batadv_softif_vlan_get(bat_priv, vid);
  2210. if (!vlan || !atomic_read(&vlan->ap_isolation))
  2211. goto out;
  2212. tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
  2213. if (!tt_local_entry)
  2214. goto out;
  2215. tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
  2216. if (!tt_global_entry)
  2217. goto out;
  2218. if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
  2219. goto out;
  2220. ret = true;
  2221. out:
  2222. if (vlan)
  2223. batadv_softif_vlan_free_ref(vlan);
  2224. if (tt_global_entry)
  2225. batadv_tt_global_entry_free_ref(tt_global_entry);
  2226. if (tt_local_entry)
  2227. batadv_tt_local_entry_free_ref(tt_local_entry);
  2228. return ret;
  2229. }
  2230. /**
  2231. * batadv_tt_update_orig - update global translation table with new tt
  2232. * information received via ogms
  2233. * @bat_priv: the bat priv with all the soft interface information
  2234. * @orig: the orig_node of the ogm
  2235. * @tt_buff: buffer holding the tt information
  2236. * @tt_num_changes: number of tt changes inside the tt buffer
  2237. * @ttvn: translation table version number of this changeset
  2238. * @tt_crc: crc32 checksum of orig node's translation table
  2239. */
  2240. static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
  2241. struct batadv_orig_node *orig_node,
  2242. const unsigned char *tt_buff,
  2243. uint16_t tt_num_changes, uint8_t ttvn,
  2244. uint32_t tt_crc)
  2245. {
  2246. uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  2247. bool full_table = true;
  2248. struct batadv_tvlv_tt_change *tt_change;
  2249. /* don't care about a backbone gateways updates. */
  2250. if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
  2251. return;
  2252. /* orig table not initialised AND first diff is in the OGM OR the ttvn
  2253. * increased by one -> we can apply the attached changes
  2254. */
  2255. if ((!orig_node->tt_initialised && ttvn == 1) ||
  2256. ttvn - orig_ttvn == 1) {
  2257. /* the OGM could not contain the changes due to their size or
  2258. * because they have already been sent BATADV_TT_OGM_APPEND_MAX
  2259. * times.
  2260. * In this case send a tt request
  2261. */
  2262. if (!tt_num_changes) {
  2263. full_table = false;
  2264. goto request_table;
  2265. }
  2266. spin_lock_bh(&orig_node->tt_lock);
  2267. tt_change = (struct batadv_tvlv_tt_change *)tt_buff;
  2268. batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
  2269. ttvn, tt_change);
  2270. /* Even if we received the precomputed crc with the OGM, we
  2271. * prefer to recompute it to spot any possible inconsistency
  2272. * in the global table
  2273. */
  2274. orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
  2275. spin_unlock_bh(&orig_node->tt_lock);
  2276. /* The ttvn alone is not enough to guarantee consistency
  2277. * because a single value could represent different states
  2278. * (due to the wrap around). Thus a node has to check whether
  2279. * the resulting table (after applying the changes) is still
  2280. * consistent or not. E.g. a node could disconnect while its
  2281. * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
  2282. * checking the CRC value is mandatory to detect the
  2283. * inconsistency
  2284. */
  2285. if (orig_node->tt_crc != tt_crc)
  2286. goto request_table;
  2287. } else {
  2288. /* if we missed more than one change or our tables are not
  2289. * in sync anymore -> request fresh tt data
  2290. */
  2291. if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
  2292. orig_node->tt_crc != tt_crc) {
  2293. request_table:
  2294. batadv_dbg(BATADV_DBG_TT, bat_priv,
  2295. "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %#.8x last_crc: %#.8x num_changes: %u)\n",
  2296. orig_node->orig, ttvn, orig_ttvn, tt_crc,
  2297. orig_node->tt_crc, tt_num_changes);
  2298. batadv_send_tt_request(bat_priv, orig_node, ttvn,
  2299. tt_crc, full_table);
  2300. return;
  2301. }
  2302. }
  2303. }
  2304. /**
  2305. * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
  2306. * @bat_priv: the bat priv with all the soft interface information
  2307. * @addr: the mac address of the client to check
  2308. * @vid: VLAN identifier
  2309. *
  2310. * Returns true if we know that the client has moved from its old originator
  2311. * to another one. This entry is still kept for consistency purposes and will be
  2312. * deleted later by a DEL or because of timeout
  2313. */
  2314. bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
  2315. uint8_t *addr, unsigned short vid)
  2316. {
  2317. struct batadv_tt_global_entry *tt_global_entry;
  2318. bool ret = false;
  2319. tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
  2320. if (!tt_global_entry)
  2321. goto out;
  2322. ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
  2323. batadv_tt_global_entry_free_ref(tt_global_entry);
  2324. out:
  2325. return ret;
  2326. }
  2327. /**
  2328. * batadv_tt_local_client_is_roaming - tells whether the client is roaming
  2329. * @bat_priv: the bat priv with all the soft interface information
  2330. * @addr: the mac address of the local client to query
  2331. * @vid: VLAN identifier
  2332. *
  2333. * Returns true if the local client is known to be roaming (it is not served by
  2334. * this node anymore) or not. If yes, the client is still present in the table
  2335. * to keep the latter consistent with the node TTVN
  2336. */
  2337. bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
  2338. uint8_t *addr, unsigned short vid)
  2339. {
  2340. struct batadv_tt_local_entry *tt_local_entry;
  2341. bool ret = false;
  2342. tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
  2343. if (!tt_local_entry)
  2344. goto out;
  2345. ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
  2346. batadv_tt_local_entry_free_ref(tt_local_entry);
  2347. out:
  2348. return ret;
  2349. }
  2350. bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
  2351. struct batadv_orig_node *orig_node,
  2352. const unsigned char *addr,
  2353. unsigned short vid)
  2354. {
  2355. bool ret = false;
  2356. /* if the originator is a backbone node (meaning it belongs to the same
  2357. * LAN of this node) the temporary client must not be added because to
  2358. * reach such destination the node must use the LAN instead of the mesh
  2359. */
  2360. if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
  2361. goto out;
  2362. if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
  2363. BATADV_TT_CLIENT_TEMP,
  2364. atomic_read(&orig_node->last_ttvn)))
  2365. goto out;
  2366. batadv_dbg(BATADV_DBG_TT, bat_priv,
  2367. "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
  2368. addr, BATADV_PRINT_VID(vid), orig_node->orig);
  2369. ret = true;
  2370. out:
  2371. return ret;
  2372. }
  2373. /**
  2374. * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
  2375. * @bat_priv: the bat priv with all the soft interface information
  2376. * @orig: the orig_node of the ogm
  2377. * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
  2378. * @tvlv_value: tvlv buffer containing the gateway data
  2379. * @tvlv_value_len: tvlv buffer length
  2380. */
  2381. static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
  2382. struct batadv_orig_node *orig,
  2383. uint8_t flags,
  2384. void *tvlv_value,
  2385. uint16_t tvlv_value_len)
  2386. {
  2387. struct batadv_tvlv_tt_data *tt_data;
  2388. uint16_t num_entries;
  2389. if (tvlv_value_len < sizeof(*tt_data))
  2390. return;
  2391. tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
  2392. tvlv_value_len -= sizeof(*tt_data);
  2393. num_entries = batadv_tt_entries(tvlv_value_len);
  2394. batadv_tt_update_orig(bat_priv, orig,
  2395. (unsigned char *)(tt_data + 1),
  2396. num_entries, tt_data->ttvn, ntohl(tt_data->crc));
  2397. }
  2398. /**
  2399. * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
  2400. * container
  2401. * @bat_priv: the bat priv with all the soft interface information
  2402. * @src: mac address of tt tvlv sender
  2403. * @dst: mac address of tt tvlv recipient
  2404. * @tvlv_value: tvlv buffer containing the tt data
  2405. * @tvlv_value_len: tvlv buffer length
  2406. *
  2407. * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
  2408. * otherwise.
  2409. */
  2410. static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
  2411. uint8_t *src, uint8_t *dst,
  2412. void *tvlv_value,
  2413. uint16_t tvlv_value_len)
  2414. {
  2415. struct batadv_tvlv_tt_data *tt_data;
  2416. uint16_t num_entries;
  2417. char tt_flag;
  2418. bool ret;
  2419. if (tvlv_value_len < sizeof(*tt_data))
  2420. return NET_RX_SUCCESS;
  2421. tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
  2422. tvlv_value_len -= sizeof(*tt_data);
  2423. num_entries = batadv_tt_entries(tvlv_value_len);
  2424. switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
  2425. case BATADV_TT_REQUEST:
  2426. batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
  2427. /* If this node cannot provide a TT response the tt_request is
  2428. * forwarded
  2429. */
  2430. ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
  2431. if (!ret) {
  2432. if (tt_data->flags & BATADV_TT_FULL_TABLE)
  2433. tt_flag = 'F';
  2434. else
  2435. tt_flag = '.';
  2436. batadv_dbg(BATADV_DBG_TT, bat_priv,
  2437. "Routing TT_REQUEST to %pM [%c]\n",
  2438. dst, tt_flag);
  2439. /* tvlv API will re-route the packet */
  2440. return NET_RX_DROP;
  2441. }
  2442. break;
  2443. case BATADV_TT_RESPONSE:
  2444. batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
  2445. if (batadv_is_my_mac(bat_priv, dst)) {
  2446. batadv_handle_tt_response(bat_priv, tt_data,
  2447. src, num_entries);
  2448. return NET_RX_SUCCESS;
  2449. }
  2450. if (tt_data->flags & BATADV_TT_FULL_TABLE)
  2451. tt_flag = 'F';
  2452. else
  2453. tt_flag = '.';
  2454. batadv_dbg(BATADV_DBG_TT, bat_priv,
  2455. "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
  2456. /* tvlv API will re-route the packet */
  2457. return NET_RX_DROP;
  2458. }
  2459. return NET_RX_SUCCESS;
  2460. }
  2461. /**
  2462. * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
  2463. * @bat_priv: the bat priv with all the soft interface information
  2464. * @src: mac address of tt tvlv sender
  2465. * @dst: mac address of tt tvlv recipient
  2466. * @tvlv_value: tvlv buffer containing the tt data
  2467. * @tvlv_value_len: tvlv buffer length
  2468. *
  2469. * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
  2470. * otherwise.
  2471. */
  2472. static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
  2473. uint8_t *src, uint8_t *dst,
  2474. void *tvlv_value,
  2475. uint16_t tvlv_value_len)
  2476. {
  2477. struct batadv_tvlv_roam_adv *roaming_adv;
  2478. struct batadv_orig_node *orig_node = NULL;
  2479. /* If this node is not the intended recipient of the
  2480. * roaming advertisement the packet is forwarded
  2481. * (the tvlv API will re-route the packet).
  2482. */
  2483. if (!batadv_is_my_mac(bat_priv, dst))
  2484. return NET_RX_DROP;
  2485. /* check if it is a backbone gateway. we don't accept
  2486. * roaming advertisement from it, as it has the same
  2487. * entries as we have.
  2488. */
  2489. if (batadv_bla_is_backbone_gw_orig(bat_priv, src))
  2490. goto out;
  2491. if (tvlv_value_len < sizeof(*roaming_adv))
  2492. goto out;
  2493. orig_node = batadv_orig_hash_find(bat_priv, src);
  2494. if (!orig_node)
  2495. goto out;
  2496. batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
  2497. roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
  2498. batadv_dbg(BATADV_DBG_TT, bat_priv,
  2499. "Received ROAMING_ADV from %pM (client %pM)\n",
  2500. src, roaming_adv->client);
  2501. batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
  2502. ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
  2503. atomic_read(&orig_node->last_ttvn) + 1);
  2504. out:
  2505. if (orig_node)
  2506. batadv_orig_node_free_ref(orig_node);
  2507. return NET_RX_SUCCESS;
  2508. }
  2509. /**
  2510. * batadv_tt_init - initialise the translation table internals
  2511. * @bat_priv: the bat priv with all the soft interface information
  2512. *
  2513. * Return 0 on success or negative error number in case of failure.
  2514. */
  2515. int batadv_tt_init(struct batadv_priv *bat_priv)
  2516. {
  2517. int ret;
  2518. ret = batadv_tt_local_init(bat_priv);
  2519. if (ret < 0)
  2520. return ret;
  2521. ret = batadv_tt_global_init(bat_priv);
  2522. if (ret < 0)
  2523. return ret;
  2524. batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
  2525. batadv_tt_tvlv_unicast_handler_v1,
  2526. BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
  2527. batadv_tvlv_handler_register(bat_priv, NULL,
  2528. batadv_roam_tvlv_unicast_handler_v1,
  2529. BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
  2530. INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
  2531. queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
  2532. msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
  2533. return 1;
  2534. }