bat_iv_ogm.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855
  1. /* Copyright (C) 2007-2016 B.A.T.M.A.N. contributors:
  2. *
  3. * Marek Lindner, Simon Wunderlich
  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, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "bat_iv_ogm.h"
  18. #include "main.h"
  19. #include <linux/atomic.h>
  20. #include <linux/bitmap.h>
  21. #include <linux/bitops.h>
  22. #include <linux/bug.h>
  23. #include <linux/byteorder/generic.h>
  24. #include <linux/cache.h>
  25. #include <linux/errno.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/fs.h>
  28. #include <linux/if_ether.h>
  29. #include <linux/init.h>
  30. #include <linux/jiffies.h>
  31. #include <linux/kernel.h>
  32. #include <linux/kref.h>
  33. #include <linux/list.h>
  34. #include <linux/lockdep.h>
  35. #include <linux/netdevice.h>
  36. #include <linux/netlink.h>
  37. #include <linux/pkt_sched.h>
  38. #include <linux/printk.h>
  39. #include <linux/random.h>
  40. #include <linux/rculist.h>
  41. #include <linux/rcupdate.h>
  42. #include <linux/seq_file.h>
  43. #include <linux/skbuff.h>
  44. #include <linux/slab.h>
  45. #include <linux/spinlock.h>
  46. #include <linux/stddef.h>
  47. #include <linux/string.h>
  48. #include <linux/types.h>
  49. #include <linux/workqueue.h>
  50. #include <net/genetlink.h>
  51. #include <net/netlink.h>
  52. #include <uapi/linux/batman_adv.h>
  53. #include "bat_algo.h"
  54. #include "bitarray.h"
  55. #include "gateway_client.h"
  56. #include "hard-interface.h"
  57. #include "hash.h"
  58. #include "log.h"
  59. #include "netlink.h"
  60. #include "network-coding.h"
  61. #include "originator.h"
  62. #include "packet.h"
  63. #include "routing.h"
  64. #include "send.h"
  65. #include "translation-table.h"
  66. #include "tvlv.h"
  67. static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work);
  68. /**
  69. * enum batadv_dup_status - duplicate status
  70. * @BATADV_NO_DUP: the packet is no duplicate
  71. * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the
  72. * neighbor)
  73. * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor
  74. * @BATADV_PROTECTED: originator is currently protected (after reboot)
  75. */
  76. enum batadv_dup_status {
  77. BATADV_NO_DUP = 0,
  78. BATADV_ORIG_DUP,
  79. BATADV_NEIGH_DUP,
  80. BATADV_PROTECTED,
  81. };
  82. /**
  83. * batadv_ring_buffer_set - update the ring buffer with the given value
  84. * @lq_recv: pointer to the ring buffer
  85. * @lq_index: index to store the value at
  86. * @value: value to store in the ring buffer
  87. */
  88. static void batadv_ring_buffer_set(u8 lq_recv[], u8 *lq_index, u8 value)
  89. {
  90. lq_recv[*lq_index] = value;
  91. *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
  92. }
  93. /**
  94. * batadv_ring_buffer_avg - compute the average of all non-zero values stored
  95. * in the given ring buffer
  96. * @lq_recv: pointer to the ring buffer
  97. *
  98. * Return: computed average value.
  99. */
  100. static u8 batadv_ring_buffer_avg(const u8 lq_recv[])
  101. {
  102. const u8 *ptr;
  103. u16 count = 0;
  104. u16 i = 0;
  105. u16 sum = 0;
  106. ptr = lq_recv;
  107. while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
  108. if (*ptr != 0) {
  109. count++;
  110. sum += *ptr;
  111. }
  112. i++;
  113. ptr++;
  114. }
  115. if (count == 0)
  116. return 0;
  117. return (u8)(sum / count);
  118. }
  119. /**
  120. * batadv_iv_ogm_orig_free - free the private resources allocated for this
  121. * orig_node
  122. * @orig_node: the orig_node for which the resources have to be free'd
  123. */
  124. static void batadv_iv_ogm_orig_free(struct batadv_orig_node *orig_node)
  125. {
  126. kfree(orig_node->bat_iv.bcast_own);
  127. kfree(orig_node->bat_iv.bcast_own_sum);
  128. }
  129. /**
  130. * batadv_iv_ogm_orig_add_if - change the private structures of the orig_node to
  131. * include the new hard-interface
  132. * @orig_node: the orig_node that has to be changed
  133. * @max_if_num: the current amount of interfaces
  134. *
  135. * Return: 0 on success, a negative error code otherwise.
  136. */
  137. static int batadv_iv_ogm_orig_add_if(struct batadv_orig_node *orig_node,
  138. int max_if_num)
  139. {
  140. void *data_ptr;
  141. size_t old_size;
  142. int ret = -ENOMEM;
  143. spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  144. old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
  145. data_ptr = kmalloc_array(max_if_num,
  146. BATADV_NUM_WORDS * sizeof(unsigned long),
  147. GFP_ATOMIC);
  148. if (!data_ptr)
  149. goto unlock;
  150. memcpy(data_ptr, orig_node->bat_iv.bcast_own, old_size);
  151. kfree(orig_node->bat_iv.bcast_own);
  152. orig_node->bat_iv.bcast_own = data_ptr;
  153. data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
  154. if (!data_ptr)
  155. goto unlock;
  156. memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
  157. (max_if_num - 1) * sizeof(u8));
  158. kfree(orig_node->bat_iv.bcast_own_sum);
  159. orig_node->bat_iv.bcast_own_sum = data_ptr;
  160. ret = 0;
  161. unlock:
  162. spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  163. return ret;
  164. }
  165. /**
  166. * batadv_iv_ogm_drop_bcast_own_entry - drop section of bcast_own
  167. * @orig_node: the orig_node that has to be changed
  168. * @max_if_num: the current amount of interfaces
  169. * @del_if_num: the index of the interface being removed
  170. */
  171. static void
  172. batadv_iv_ogm_drop_bcast_own_entry(struct batadv_orig_node *orig_node,
  173. int max_if_num, int del_if_num)
  174. {
  175. size_t chunk_size;
  176. size_t if_offset;
  177. void *data_ptr;
  178. lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
  179. chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
  180. data_ptr = kmalloc_array(max_if_num, chunk_size, GFP_ATOMIC);
  181. if (!data_ptr)
  182. /* use old buffer when new one could not be allocated */
  183. data_ptr = orig_node->bat_iv.bcast_own;
  184. /* copy first part */
  185. memmove(data_ptr, orig_node->bat_iv.bcast_own, del_if_num * chunk_size);
  186. /* copy second part */
  187. if_offset = (del_if_num + 1) * chunk_size;
  188. memmove((char *)data_ptr + del_if_num * chunk_size,
  189. (uint8_t *)orig_node->bat_iv.bcast_own + if_offset,
  190. (max_if_num - del_if_num) * chunk_size);
  191. /* bcast_own was shrunk down in new buffer; free old one */
  192. if (orig_node->bat_iv.bcast_own != data_ptr) {
  193. kfree(orig_node->bat_iv.bcast_own);
  194. orig_node->bat_iv.bcast_own = data_ptr;
  195. }
  196. }
  197. /**
  198. * batadv_iv_ogm_drop_bcast_own_sum_entry - drop section of bcast_own_sum
  199. * @orig_node: the orig_node that has to be changed
  200. * @max_if_num: the current amount of interfaces
  201. * @del_if_num: the index of the interface being removed
  202. */
  203. static void
  204. batadv_iv_ogm_drop_bcast_own_sum_entry(struct batadv_orig_node *orig_node,
  205. int max_if_num, int del_if_num)
  206. {
  207. size_t if_offset;
  208. void *data_ptr;
  209. lockdep_assert_held(&orig_node->bat_iv.ogm_cnt_lock);
  210. data_ptr = kmalloc_array(max_if_num, sizeof(u8), GFP_ATOMIC);
  211. if (!data_ptr)
  212. /* use old buffer when new one could not be allocated */
  213. data_ptr = orig_node->bat_iv.bcast_own_sum;
  214. memmove(data_ptr, orig_node->bat_iv.bcast_own_sum,
  215. del_if_num * sizeof(u8));
  216. if_offset = (del_if_num + 1) * sizeof(u8);
  217. memmove((char *)data_ptr + del_if_num * sizeof(u8),
  218. orig_node->bat_iv.bcast_own_sum + if_offset,
  219. (max_if_num - del_if_num) * sizeof(u8));
  220. /* bcast_own_sum was shrunk down in new buffer; free old one */
  221. if (orig_node->bat_iv.bcast_own_sum != data_ptr) {
  222. kfree(orig_node->bat_iv.bcast_own_sum);
  223. orig_node->bat_iv.bcast_own_sum = data_ptr;
  224. }
  225. }
  226. /**
  227. * batadv_iv_ogm_orig_del_if - change the private structures of the orig_node to
  228. * exclude the removed interface
  229. * @orig_node: the orig_node that has to be changed
  230. * @max_if_num: the current amount of interfaces
  231. * @del_if_num: the index of the interface being removed
  232. *
  233. * Return: 0 on success, a negative error code otherwise.
  234. */
  235. static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node *orig_node,
  236. int max_if_num, int del_if_num)
  237. {
  238. spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  239. if (max_if_num == 0) {
  240. kfree(orig_node->bat_iv.bcast_own);
  241. kfree(orig_node->bat_iv.bcast_own_sum);
  242. orig_node->bat_iv.bcast_own = NULL;
  243. orig_node->bat_iv.bcast_own_sum = NULL;
  244. } else {
  245. batadv_iv_ogm_drop_bcast_own_entry(orig_node, max_if_num,
  246. del_if_num);
  247. batadv_iv_ogm_drop_bcast_own_sum_entry(orig_node, max_if_num,
  248. del_if_num);
  249. }
  250. spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  251. return 0;
  252. }
  253. /**
  254. * batadv_iv_ogm_orig_get - retrieve or create (if does not exist) an originator
  255. * @bat_priv: the bat priv with all the soft interface information
  256. * @addr: mac address of the originator
  257. *
  258. * Return: the originator object corresponding to the passed mac address or NULL
  259. * on failure.
  260. * If the object does not exists it is created an initialised.
  261. */
  262. static struct batadv_orig_node *
  263. batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const u8 *addr)
  264. {
  265. struct batadv_orig_node *orig_node;
  266. int size, hash_added;
  267. orig_node = batadv_orig_hash_find(bat_priv, addr);
  268. if (orig_node)
  269. return orig_node;
  270. orig_node = batadv_orig_node_new(bat_priv, addr);
  271. if (!orig_node)
  272. return NULL;
  273. spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
  274. size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
  275. orig_node->bat_iv.bcast_own = kzalloc(size, GFP_ATOMIC);
  276. if (!orig_node->bat_iv.bcast_own)
  277. goto free_orig_node;
  278. size = bat_priv->num_ifaces * sizeof(u8);
  279. orig_node->bat_iv.bcast_own_sum = kzalloc(size, GFP_ATOMIC);
  280. if (!orig_node->bat_iv.bcast_own_sum)
  281. goto free_orig_node;
  282. kref_get(&orig_node->refcount);
  283. hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
  284. batadv_choose_orig, orig_node,
  285. &orig_node->hash_entry);
  286. if (hash_added != 0)
  287. goto free_orig_node_hash;
  288. return orig_node;
  289. free_orig_node_hash:
  290. batadv_orig_node_put(orig_node);
  291. free_orig_node:
  292. batadv_orig_node_put(orig_node);
  293. return NULL;
  294. }
  295. static struct batadv_neigh_node *
  296. batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
  297. const u8 *neigh_addr,
  298. struct batadv_orig_node *orig_node,
  299. struct batadv_orig_node *orig_neigh)
  300. {
  301. struct batadv_neigh_node *neigh_node;
  302. neigh_node = batadv_neigh_node_get_or_create(orig_node,
  303. hard_iface, neigh_addr);
  304. if (!neigh_node)
  305. goto out;
  306. neigh_node->orig_node = orig_neigh;
  307. out:
  308. return neigh_node;
  309. }
  310. static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
  311. {
  312. struct batadv_ogm_packet *batadv_ogm_packet;
  313. unsigned char *ogm_buff;
  314. u32 random_seqno;
  315. /* randomize initial seqno to avoid collision */
  316. get_random_bytes(&random_seqno, sizeof(random_seqno));
  317. atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
  318. hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
  319. ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
  320. if (!ogm_buff)
  321. return -ENOMEM;
  322. hard_iface->bat_iv.ogm_buff = ogm_buff;
  323. batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
  324. batadv_ogm_packet->packet_type = BATADV_IV_OGM;
  325. batadv_ogm_packet->version = BATADV_COMPAT_VERSION;
  326. batadv_ogm_packet->ttl = 2;
  327. batadv_ogm_packet->flags = BATADV_NO_FLAGS;
  328. batadv_ogm_packet->reserved = 0;
  329. batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
  330. return 0;
  331. }
  332. static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
  333. {
  334. kfree(hard_iface->bat_iv.ogm_buff);
  335. hard_iface->bat_iv.ogm_buff = NULL;
  336. }
  337. static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
  338. {
  339. struct batadv_ogm_packet *batadv_ogm_packet;
  340. unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
  341. batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
  342. ether_addr_copy(batadv_ogm_packet->orig,
  343. hard_iface->net_dev->dev_addr);
  344. ether_addr_copy(batadv_ogm_packet->prev_sender,
  345. hard_iface->net_dev->dev_addr);
  346. }
  347. static void
  348. batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
  349. {
  350. struct batadv_ogm_packet *batadv_ogm_packet;
  351. unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
  352. batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
  353. batadv_ogm_packet->ttl = BATADV_TTL;
  354. }
  355. /* when do we schedule our own ogm to be sent */
  356. static unsigned long
  357. batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
  358. {
  359. unsigned int msecs;
  360. msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
  361. msecs += prandom_u32() % (2 * BATADV_JITTER);
  362. return jiffies + msecs_to_jiffies(msecs);
  363. }
  364. /* when do we schedule a ogm packet to be sent */
  365. static unsigned long batadv_iv_ogm_fwd_send_time(void)
  366. {
  367. return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
  368. }
  369. /* apply hop penalty for a normal link */
  370. static u8 batadv_hop_penalty(u8 tq, const struct batadv_priv *bat_priv)
  371. {
  372. int hop_penalty = atomic_read(&bat_priv->hop_penalty);
  373. int new_tq;
  374. new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
  375. new_tq /= BATADV_TQ_MAX_VALUE;
  376. return new_tq;
  377. }
  378. /**
  379. * batadv_iv_ogm_aggr_packet - checks if there is another OGM attached
  380. * @buff_pos: current position in the skb
  381. * @packet_len: total length of the skb
  382. * @tvlv_len: tvlv length of the previously considered OGM
  383. *
  384. * Return: true if there is enough space for another OGM, false otherwise.
  385. */
  386. static bool batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
  387. __be16 tvlv_len)
  388. {
  389. int next_buff_pos = 0;
  390. next_buff_pos += buff_pos + BATADV_OGM_HLEN;
  391. next_buff_pos += ntohs(tvlv_len);
  392. return (next_buff_pos <= packet_len) &&
  393. (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
  394. }
  395. /* send a batman ogm to a given interface */
  396. static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
  397. struct batadv_hard_iface *hard_iface)
  398. {
  399. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  400. const char *fwd_str;
  401. u8 packet_num;
  402. s16 buff_pos;
  403. struct batadv_ogm_packet *batadv_ogm_packet;
  404. struct sk_buff *skb;
  405. u8 *packet_pos;
  406. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  407. return;
  408. packet_num = 0;
  409. buff_pos = 0;
  410. packet_pos = forw_packet->skb->data;
  411. batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
  412. /* adjust all flags and log packets */
  413. while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
  414. batadv_ogm_packet->tvlv_len)) {
  415. /* we might have aggregated direct link packets with an
  416. * ordinary base packet
  417. */
  418. if (forw_packet->direct_link_flags & BIT(packet_num) &&
  419. forw_packet->if_incoming == hard_iface)
  420. batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
  421. else
  422. batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
  423. if (packet_num > 0 || !forw_packet->own)
  424. fwd_str = "Forwarding";
  425. else
  426. fwd_str = "Sending own";
  427. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  428. "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
  429. fwd_str, (packet_num > 0 ? "aggregated " : ""),
  430. batadv_ogm_packet->orig,
  431. ntohl(batadv_ogm_packet->seqno),
  432. batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
  433. ((batadv_ogm_packet->flags & BATADV_DIRECTLINK) ?
  434. "on" : "off"),
  435. hard_iface->net_dev->name,
  436. hard_iface->net_dev->dev_addr);
  437. buff_pos += BATADV_OGM_HLEN;
  438. buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
  439. packet_num++;
  440. packet_pos = forw_packet->skb->data + buff_pos;
  441. batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
  442. }
  443. /* create clone because function is called more than once */
  444. skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
  445. if (skb) {
  446. batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
  447. batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
  448. skb->len + ETH_HLEN);
  449. batadv_send_broadcast_skb(skb, hard_iface);
  450. }
  451. }
  452. /* send a batman ogm packet */
  453. static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
  454. {
  455. struct net_device *soft_iface;
  456. if (!forw_packet->if_incoming) {
  457. pr_err("Error - can't forward packet: incoming iface not specified\n");
  458. return;
  459. }
  460. soft_iface = forw_packet->if_incoming->soft_iface;
  461. if (WARN_ON(!forw_packet->if_outgoing))
  462. return;
  463. if (WARN_ON(forw_packet->if_outgoing->soft_iface != soft_iface))
  464. return;
  465. if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
  466. return;
  467. /* only for one specific outgoing interface */
  468. batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing);
  469. }
  470. /**
  471. * batadv_iv_ogm_can_aggregate - find out if an OGM can be aggregated on an
  472. * existing forward packet
  473. * @new_bat_ogm_packet: OGM packet to be aggregated
  474. * @bat_priv: the bat priv with all the soft interface information
  475. * @packet_len: (total) length of the OGM
  476. * @send_time: timestamp (jiffies) when the packet is to be sent
  477. * @directlink: true if this is a direct link packet
  478. * @if_incoming: interface where the packet was received
  479. * @if_outgoing: interface for which the retransmission should be considered
  480. * @forw_packet: the forwarded packet which should be checked
  481. *
  482. * Return: true if new_packet can be aggregated with forw_packet
  483. */
  484. static bool
  485. batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
  486. struct batadv_priv *bat_priv,
  487. int packet_len, unsigned long send_time,
  488. bool directlink,
  489. const struct batadv_hard_iface *if_incoming,
  490. const struct batadv_hard_iface *if_outgoing,
  491. const struct batadv_forw_packet *forw_packet)
  492. {
  493. struct batadv_ogm_packet *batadv_ogm_packet;
  494. int aggregated_bytes = forw_packet->packet_len + packet_len;
  495. struct batadv_hard_iface *primary_if = NULL;
  496. bool res = false;
  497. unsigned long aggregation_end_time;
  498. batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
  499. aggregation_end_time = send_time;
  500. aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
  501. /* we can aggregate the current packet to this aggregated packet
  502. * if:
  503. *
  504. * - the send time is within our MAX_AGGREGATION_MS time
  505. * - the resulting packet wont be bigger than
  506. * MAX_AGGREGATION_BYTES
  507. * otherwise aggregation is not possible
  508. */
  509. if (!time_before(send_time, forw_packet->send_time) ||
  510. !time_after_eq(aggregation_end_time, forw_packet->send_time))
  511. return false;
  512. if (aggregated_bytes > BATADV_MAX_AGGREGATION_BYTES)
  513. return false;
  514. /* packet is not leaving on the same interface. */
  515. if (forw_packet->if_outgoing != if_outgoing)
  516. return false;
  517. /* check aggregation compatibility
  518. * -> direct link packets are broadcasted on
  519. * their interface only
  520. * -> aggregate packet if the current packet is
  521. * a "global" packet as well as the base
  522. * packet
  523. */
  524. primary_if = batadv_primary_if_get_selected(bat_priv);
  525. if (!primary_if)
  526. return false;
  527. /* packets without direct link flag and high TTL
  528. * are flooded through the net
  529. */
  530. if (!directlink &&
  531. !(batadv_ogm_packet->flags & BATADV_DIRECTLINK) &&
  532. batadv_ogm_packet->ttl != 1 &&
  533. /* own packets originating non-primary
  534. * interfaces leave only that interface
  535. */
  536. (!forw_packet->own ||
  537. forw_packet->if_incoming == primary_if)) {
  538. res = true;
  539. goto out;
  540. }
  541. /* if the incoming packet is sent via this one
  542. * interface only - we still can aggregate
  543. */
  544. if (directlink &&
  545. new_bat_ogm_packet->ttl == 1 &&
  546. forw_packet->if_incoming == if_incoming &&
  547. /* packets from direct neighbors or
  548. * own secondary interface packets
  549. * (= secondary interface packets in general)
  550. */
  551. (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
  552. (forw_packet->own &&
  553. forw_packet->if_incoming != primary_if))) {
  554. res = true;
  555. goto out;
  556. }
  557. out:
  558. if (primary_if)
  559. batadv_hardif_put(primary_if);
  560. return res;
  561. }
  562. /**
  563. * batadv_iv_ogm_aggregate_new - create a new aggregated packet and add this
  564. * packet to it.
  565. * @packet_buff: pointer to the OGM
  566. * @packet_len: (total) length of the OGM
  567. * @send_time: timestamp (jiffies) when the packet is to be sent
  568. * @direct_link: whether this OGM has direct link status
  569. * @if_incoming: interface where the packet was received
  570. * @if_outgoing: interface for which the retransmission should be considered
  571. * @own_packet: true if it is a self-generated ogm
  572. */
  573. static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
  574. int packet_len, unsigned long send_time,
  575. bool direct_link,
  576. struct batadv_hard_iface *if_incoming,
  577. struct batadv_hard_iface *if_outgoing,
  578. int own_packet)
  579. {
  580. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  581. struct batadv_forw_packet *forw_packet_aggr;
  582. unsigned char *skb_buff;
  583. unsigned int skb_size;
  584. atomic_t *queue_left = own_packet ? NULL : &bat_priv->batman_queue_left;
  585. forw_packet_aggr = batadv_forw_packet_alloc(if_incoming, if_outgoing,
  586. queue_left, bat_priv);
  587. if (!forw_packet_aggr)
  588. return;
  589. if (atomic_read(&bat_priv->aggregated_ogms) &&
  590. packet_len < BATADV_MAX_AGGREGATION_BYTES)
  591. skb_size = BATADV_MAX_AGGREGATION_BYTES;
  592. else
  593. skb_size = packet_len;
  594. skb_size += ETH_HLEN;
  595. forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size);
  596. if (!forw_packet_aggr->skb) {
  597. batadv_forw_packet_free(forw_packet_aggr, true);
  598. return;
  599. }
  600. forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
  601. skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
  602. skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
  603. forw_packet_aggr->packet_len = packet_len;
  604. memcpy(skb_buff, packet_buff, packet_len);
  605. forw_packet_aggr->own = own_packet;
  606. forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
  607. forw_packet_aggr->send_time = send_time;
  608. /* save packet direct link flag status */
  609. if (direct_link)
  610. forw_packet_aggr->direct_link_flags |= 1;
  611. INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
  612. batadv_iv_send_outstanding_bat_ogm_packet);
  613. batadv_forw_packet_ogmv1_queue(bat_priv, forw_packet_aggr, send_time);
  614. }
  615. /* aggregate a new packet into the existing ogm packet */
  616. static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
  617. const unsigned char *packet_buff,
  618. int packet_len, bool direct_link)
  619. {
  620. unsigned char *skb_buff;
  621. unsigned long new_direct_link_flag;
  622. skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
  623. memcpy(skb_buff, packet_buff, packet_len);
  624. forw_packet_aggr->packet_len += packet_len;
  625. forw_packet_aggr->num_packets++;
  626. /* save packet direct link flag status */
  627. if (direct_link) {
  628. new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
  629. forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
  630. }
  631. }
  632. /**
  633. * batadv_iv_ogm_queue_add - queue up an OGM for transmission
  634. * @bat_priv: the bat priv with all the soft interface information
  635. * @packet_buff: pointer to the OGM
  636. * @packet_len: (total) length of the OGM
  637. * @if_incoming: interface where the packet was received
  638. * @if_outgoing: interface for which the retransmission should be considered
  639. * @own_packet: true if it is a self-generated ogm
  640. * @send_time: timestamp (jiffies) when the packet is to be sent
  641. */
  642. static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
  643. unsigned char *packet_buff,
  644. int packet_len,
  645. struct batadv_hard_iface *if_incoming,
  646. struct batadv_hard_iface *if_outgoing,
  647. int own_packet, unsigned long send_time)
  648. {
  649. /* _aggr -> pointer to the packet we want to aggregate with
  650. * _pos -> pointer to the position in the queue
  651. */
  652. struct batadv_forw_packet *forw_packet_aggr = NULL;
  653. struct batadv_forw_packet *forw_packet_pos = NULL;
  654. struct batadv_ogm_packet *batadv_ogm_packet;
  655. bool direct_link;
  656. unsigned long max_aggregation_jiffies;
  657. batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
  658. direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK);
  659. max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
  660. /* find position for the packet in the forward queue */
  661. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  662. /* own packets are not to be aggregated */
  663. if (atomic_read(&bat_priv->aggregated_ogms) && !own_packet) {
  664. hlist_for_each_entry(forw_packet_pos,
  665. &bat_priv->forw_bat_list, list) {
  666. if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
  667. bat_priv, packet_len,
  668. send_time, direct_link,
  669. if_incoming,
  670. if_outgoing,
  671. forw_packet_pos)) {
  672. forw_packet_aggr = forw_packet_pos;
  673. break;
  674. }
  675. }
  676. }
  677. /* nothing to aggregate with - either aggregation disabled or no
  678. * suitable aggregation packet found
  679. */
  680. if (!forw_packet_aggr) {
  681. /* the following section can run without the lock */
  682. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  683. /* if we could not aggregate this packet with one of the others
  684. * we hold it back for a while, so that it might be aggregated
  685. * later on
  686. */
  687. if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
  688. send_time += max_aggregation_jiffies;
  689. batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
  690. send_time, direct_link,
  691. if_incoming, if_outgoing,
  692. own_packet);
  693. } else {
  694. batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
  695. packet_len, direct_link);
  696. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  697. }
  698. }
  699. static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
  700. const struct ethhdr *ethhdr,
  701. struct batadv_ogm_packet *batadv_ogm_packet,
  702. bool is_single_hop_neigh,
  703. bool is_from_best_next_hop,
  704. struct batadv_hard_iface *if_incoming,
  705. struct batadv_hard_iface *if_outgoing)
  706. {
  707. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  708. u16 tvlv_len;
  709. if (batadv_ogm_packet->ttl <= 1) {
  710. batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
  711. return;
  712. }
  713. if (!is_from_best_next_hop) {
  714. /* Mark the forwarded packet when it is not coming from our
  715. * best next hop. We still need to forward the packet for our
  716. * neighbor link quality detection to work in case the packet
  717. * originated from a single hop neighbor. Otherwise we can
  718. * simply drop the ogm.
  719. */
  720. if (is_single_hop_neigh)
  721. batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
  722. else
  723. return;
  724. }
  725. tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
  726. batadv_ogm_packet->ttl--;
  727. ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source);
  728. /* apply hop penalty */
  729. batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
  730. bat_priv);
  731. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  732. "Forwarding packet: tq: %i, ttl: %i\n",
  733. batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
  734. if (is_single_hop_neigh)
  735. batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
  736. else
  737. batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
  738. batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
  739. BATADV_OGM_HLEN + tvlv_len,
  740. if_incoming, if_outgoing, 0,
  741. batadv_iv_ogm_fwd_send_time());
  742. }
  743. /**
  744. * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for
  745. * the given interface
  746. * @hard_iface: the interface for which the windows have to be shifted
  747. */
  748. static void
  749. batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
  750. {
  751. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  752. struct batadv_hashtable *hash = bat_priv->orig_hash;
  753. struct hlist_head *head;
  754. struct batadv_orig_node *orig_node;
  755. unsigned long *word;
  756. u32 i;
  757. size_t word_index;
  758. u8 *w;
  759. int if_num;
  760. for (i = 0; i < hash->size; i++) {
  761. head = &hash->table[i];
  762. rcu_read_lock();
  763. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  764. spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  765. word_index = hard_iface->if_num * BATADV_NUM_WORDS;
  766. word = &orig_node->bat_iv.bcast_own[word_index];
  767. batadv_bit_get_packet(bat_priv, word, 1, 0);
  768. if_num = hard_iface->if_num;
  769. w = &orig_node->bat_iv.bcast_own_sum[if_num];
  770. *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
  771. spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  772. }
  773. rcu_read_unlock();
  774. }
  775. }
  776. static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
  777. {
  778. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  779. unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
  780. struct batadv_ogm_packet *batadv_ogm_packet;
  781. struct batadv_hard_iface *primary_if, *tmp_hard_iface;
  782. int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
  783. u32 seqno;
  784. u16 tvlv_len = 0;
  785. unsigned long send_time;
  786. if ((hard_iface->if_status == BATADV_IF_NOT_IN_USE) ||
  787. (hard_iface->if_status == BATADV_IF_TO_BE_REMOVED))
  788. return;
  789. /* the interface gets activated here to avoid race conditions between
  790. * the moment of activating the interface in
  791. * hardif_activate_interface() where the originator mac is set and
  792. * outdated packets (especially uninitialized mac addresses) in the
  793. * packet queue
  794. */
  795. if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
  796. hard_iface->if_status = BATADV_IF_ACTIVE;
  797. primary_if = batadv_primary_if_get_selected(bat_priv);
  798. if (hard_iface == primary_if) {
  799. /* tt changes have to be committed before the tvlv data is
  800. * appended as it may alter the tt tvlv container
  801. */
  802. batadv_tt_local_commit_changes(bat_priv);
  803. tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
  804. ogm_buff_len,
  805. BATADV_OGM_HLEN);
  806. }
  807. batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
  808. batadv_ogm_packet->tvlv_len = htons(tvlv_len);
  809. /* change sequence number to network order */
  810. seqno = (u32)atomic_read(&hard_iface->bat_iv.ogm_seqno);
  811. batadv_ogm_packet->seqno = htonl(seqno);
  812. atomic_inc(&hard_iface->bat_iv.ogm_seqno);
  813. batadv_iv_ogm_slide_own_bcast_window(hard_iface);
  814. send_time = batadv_iv_ogm_emit_send_time(bat_priv);
  815. if (hard_iface != primary_if) {
  816. /* OGMs from secondary interfaces are only scheduled on their
  817. * respective interfaces.
  818. */
  819. batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len,
  820. hard_iface, hard_iface, 1, send_time);
  821. goto out;
  822. }
  823. /* OGMs from primary interfaces are scheduled on all
  824. * interfaces.
  825. */
  826. rcu_read_lock();
  827. list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) {
  828. if (tmp_hard_iface->soft_iface != hard_iface->soft_iface)
  829. continue;
  830. if (!kref_get_unless_zero(&tmp_hard_iface->refcount))
  831. continue;
  832. batadv_iv_ogm_queue_add(bat_priv, *ogm_buff,
  833. *ogm_buff_len, hard_iface,
  834. tmp_hard_iface, 1, send_time);
  835. batadv_hardif_put(tmp_hard_iface);
  836. }
  837. rcu_read_unlock();
  838. out:
  839. if (primary_if)
  840. batadv_hardif_put(primary_if);
  841. }
  842. /**
  843. * batadv_iv_ogm_orig_update - use OGM to update corresponding data in an
  844. * originator
  845. * @bat_priv: the bat priv with all the soft interface information
  846. * @orig_node: the orig node who originally emitted the ogm packet
  847. * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
  848. * @ethhdr: Ethernet header of the OGM
  849. * @batadv_ogm_packet: the ogm packet
  850. * @if_incoming: interface where the packet was received
  851. * @if_outgoing: interface for which the retransmission should be considered
  852. * @dup_status: the duplicate status of this ogm packet.
  853. */
  854. static void
  855. batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
  856. struct batadv_orig_node *orig_node,
  857. struct batadv_orig_ifinfo *orig_ifinfo,
  858. const struct ethhdr *ethhdr,
  859. const struct batadv_ogm_packet *batadv_ogm_packet,
  860. struct batadv_hard_iface *if_incoming,
  861. struct batadv_hard_iface *if_outgoing,
  862. enum batadv_dup_status dup_status)
  863. {
  864. struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
  865. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  866. struct batadv_neigh_node *neigh_node = NULL;
  867. struct batadv_neigh_node *tmp_neigh_node = NULL;
  868. struct batadv_neigh_node *router = NULL;
  869. struct batadv_orig_node *orig_node_tmp;
  870. int if_num;
  871. u8 sum_orig, sum_neigh;
  872. u8 *neigh_addr;
  873. u8 tq_avg;
  874. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  875. "update_originator(): Searching and updating originator entry of received packet\n");
  876. rcu_read_lock();
  877. hlist_for_each_entry_rcu(tmp_neigh_node,
  878. &orig_node->neigh_list, list) {
  879. neigh_addr = tmp_neigh_node->addr;
  880. if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
  881. tmp_neigh_node->if_incoming == if_incoming &&
  882. kref_get_unless_zero(&tmp_neigh_node->refcount)) {
  883. if (WARN(neigh_node, "too many matching neigh_nodes"))
  884. batadv_neigh_node_put(neigh_node);
  885. neigh_node = tmp_neigh_node;
  886. continue;
  887. }
  888. if (dup_status != BATADV_NO_DUP)
  889. continue;
  890. /* only update the entry for this outgoing interface */
  891. neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
  892. if_outgoing);
  893. if (!neigh_ifinfo)
  894. continue;
  895. spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
  896. batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
  897. &neigh_ifinfo->bat_iv.tq_index, 0);
  898. tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
  899. neigh_ifinfo->bat_iv.tq_avg = tq_avg;
  900. spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
  901. batadv_neigh_ifinfo_put(neigh_ifinfo);
  902. neigh_ifinfo = NULL;
  903. }
  904. if (!neigh_node) {
  905. struct batadv_orig_node *orig_tmp;
  906. orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
  907. if (!orig_tmp)
  908. goto unlock;
  909. neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
  910. ethhdr->h_source,
  911. orig_node, orig_tmp);
  912. batadv_orig_node_put(orig_tmp);
  913. if (!neigh_node)
  914. goto unlock;
  915. } else {
  916. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  917. "Updating existing last-hop neighbor of originator\n");
  918. }
  919. rcu_read_unlock();
  920. neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
  921. if (!neigh_ifinfo)
  922. goto out;
  923. neigh_node->last_seen = jiffies;
  924. spin_lock_bh(&neigh_node->ifinfo_lock);
  925. batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
  926. &neigh_ifinfo->bat_iv.tq_index,
  927. batadv_ogm_packet->tq);
  928. tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
  929. neigh_ifinfo->bat_iv.tq_avg = tq_avg;
  930. spin_unlock_bh(&neigh_node->ifinfo_lock);
  931. if (dup_status == BATADV_NO_DUP) {
  932. orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
  933. neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
  934. }
  935. /* if this neighbor already is our next hop there is nothing
  936. * to change
  937. */
  938. router = batadv_orig_router_get(orig_node, if_outgoing);
  939. if (router == neigh_node)
  940. goto out;
  941. if (router) {
  942. router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
  943. if (!router_ifinfo)
  944. goto out;
  945. /* if this neighbor does not offer a better TQ we won't
  946. * consider it
  947. */
  948. if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
  949. goto out;
  950. }
  951. /* if the TQ is the same and the link not more symmetric we
  952. * won't consider it either
  953. */
  954. if (router_ifinfo &&
  955. neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) {
  956. orig_node_tmp = router->orig_node;
  957. spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
  958. if_num = router->if_incoming->if_num;
  959. sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
  960. spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
  961. orig_node_tmp = neigh_node->orig_node;
  962. spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
  963. if_num = neigh_node->if_incoming->if_num;
  964. sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
  965. spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
  966. if (sum_orig >= sum_neigh)
  967. goto out;
  968. }
  969. batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
  970. goto out;
  971. unlock:
  972. rcu_read_unlock();
  973. out:
  974. if (neigh_node)
  975. batadv_neigh_node_put(neigh_node);
  976. if (router)
  977. batadv_neigh_node_put(router);
  978. if (neigh_ifinfo)
  979. batadv_neigh_ifinfo_put(neigh_ifinfo);
  980. if (router_ifinfo)
  981. batadv_neigh_ifinfo_put(router_ifinfo);
  982. }
  983. /**
  984. * batadv_iv_ogm_calc_tq - calculate tq for current received ogm packet
  985. * @orig_node: the orig node who originally emitted the ogm packet
  986. * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
  987. * @batadv_ogm_packet: the ogm packet
  988. * @if_incoming: interface where the packet was received
  989. * @if_outgoing: interface for which the retransmission should be considered
  990. *
  991. * Return: true if the link can be considered bidirectional, false otherwise
  992. */
  993. static bool batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
  994. struct batadv_orig_node *orig_neigh_node,
  995. struct batadv_ogm_packet *batadv_ogm_packet,
  996. struct batadv_hard_iface *if_incoming,
  997. struct batadv_hard_iface *if_outgoing)
  998. {
  999. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  1000. struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
  1001. struct batadv_neigh_ifinfo *neigh_ifinfo;
  1002. u8 total_count;
  1003. u8 orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
  1004. unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
  1005. int if_num;
  1006. unsigned int tq_asym_penalty, inv_asym_penalty;
  1007. unsigned int combined_tq;
  1008. unsigned int tq_iface_penalty;
  1009. bool ret = false;
  1010. /* find corresponding one hop neighbor */
  1011. rcu_read_lock();
  1012. hlist_for_each_entry_rcu(tmp_neigh_node,
  1013. &orig_neigh_node->neigh_list, list) {
  1014. if (!batadv_compare_eth(tmp_neigh_node->addr,
  1015. orig_neigh_node->orig))
  1016. continue;
  1017. if (tmp_neigh_node->if_incoming != if_incoming)
  1018. continue;
  1019. if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
  1020. continue;
  1021. neigh_node = tmp_neigh_node;
  1022. break;
  1023. }
  1024. rcu_read_unlock();
  1025. if (!neigh_node)
  1026. neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
  1027. orig_neigh_node->orig,
  1028. orig_neigh_node,
  1029. orig_neigh_node);
  1030. if (!neigh_node)
  1031. goto out;
  1032. /* if orig_node is direct neighbor update neigh_node last_seen */
  1033. if (orig_node == orig_neigh_node)
  1034. neigh_node->last_seen = jiffies;
  1035. orig_node->last_seen = jiffies;
  1036. /* find packet count of corresponding one hop neighbor */
  1037. spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  1038. if_num = if_incoming->if_num;
  1039. orig_eq_count = orig_neigh_node->bat_iv.bcast_own_sum[if_num];
  1040. neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
  1041. if (neigh_ifinfo) {
  1042. neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
  1043. batadv_neigh_ifinfo_put(neigh_ifinfo);
  1044. } else {
  1045. neigh_rq_count = 0;
  1046. }
  1047. spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  1048. /* pay attention to not get a value bigger than 100 % */
  1049. if (orig_eq_count > neigh_rq_count)
  1050. total_count = neigh_rq_count;
  1051. else
  1052. total_count = orig_eq_count;
  1053. /* if we have too few packets (too less data) we set tq_own to zero
  1054. * if we receive too few packets it is not considered bidirectional
  1055. */
  1056. if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
  1057. neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
  1058. tq_own = 0;
  1059. else
  1060. /* neigh_node->real_packet_count is never zero as we
  1061. * only purge old information when getting new
  1062. * information
  1063. */
  1064. tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
  1065. /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
  1066. * affect the nearly-symmetric links only a little, but
  1067. * punishes asymmetric links more. This will give a value
  1068. * between 0 and TQ_MAX_VALUE
  1069. */
  1070. neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
  1071. neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
  1072. neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
  1073. BATADV_TQ_LOCAL_WINDOW_SIZE *
  1074. BATADV_TQ_LOCAL_WINDOW_SIZE;
  1075. inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
  1076. inv_asym_penalty /= neigh_rq_max_cube;
  1077. tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
  1078. /* penalize if the OGM is forwarded on the same interface. WiFi
  1079. * interfaces and other half duplex devices suffer from throughput
  1080. * drops as they can't send and receive at the same time.
  1081. */
  1082. tq_iface_penalty = BATADV_TQ_MAX_VALUE;
  1083. if (if_outgoing && (if_incoming == if_outgoing) &&
  1084. batadv_is_wifi_hardif(if_outgoing))
  1085. tq_iface_penalty = batadv_hop_penalty(BATADV_TQ_MAX_VALUE,
  1086. bat_priv);
  1087. combined_tq = batadv_ogm_packet->tq *
  1088. tq_own *
  1089. tq_asym_penalty *
  1090. tq_iface_penalty;
  1091. combined_tq /= BATADV_TQ_MAX_VALUE *
  1092. BATADV_TQ_MAX_VALUE *
  1093. BATADV_TQ_MAX_VALUE;
  1094. batadv_ogm_packet->tq = combined_tq;
  1095. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1096. "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, iface_penalty: %3i, total tq: %3i, if_incoming = %s, if_outgoing = %s\n",
  1097. orig_node->orig, orig_neigh_node->orig, total_count,
  1098. neigh_rq_count, tq_own, tq_asym_penalty, tq_iface_penalty,
  1099. batadv_ogm_packet->tq, if_incoming->net_dev->name,
  1100. if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
  1101. /* if link has the minimum required transmission quality
  1102. * consider it bidirectional
  1103. */
  1104. if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
  1105. ret = true;
  1106. out:
  1107. if (neigh_node)
  1108. batadv_neigh_node_put(neigh_node);
  1109. return ret;
  1110. }
  1111. /**
  1112. * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces,
  1113. * adjust the sequence number and find out whether it is a duplicate
  1114. * @ethhdr: ethernet header of the packet
  1115. * @batadv_ogm_packet: OGM packet to be considered
  1116. * @if_incoming: interface on which the OGM packet was received
  1117. * @if_outgoing: interface for which the retransmission should be considered
  1118. *
  1119. * Return: duplicate status as enum batadv_dup_status
  1120. */
  1121. static enum batadv_dup_status
  1122. batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
  1123. const struct batadv_ogm_packet *batadv_ogm_packet,
  1124. const struct batadv_hard_iface *if_incoming,
  1125. struct batadv_hard_iface *if_outgoing)
  1126. {
  1127. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  1128. struct batadv_orig_node *orig_node;
  1129. struct batadv_orig_ifinfo *orig_ifinfo = NULL;
  1130. struct batadv_neigh_node *neigh_node;
  1131. struct batadv_neigh_ifinfo *neigh_ifinfo;
  1132. bool is_dup;
  1133. s32 seq_diff;
  1134. bool need_update = false;
  1135. int set_mark;
  1136. enum batadv_dup_status ret = BATADV_NO_DUP;
  1137. u32 seqno = ntohl(batadv_ogm_packet->seqno);
  1138. u8 *neigh_addr;
  1139. u8 packet_count;
  1140. unsigned long *bitmap;
  1141. orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
  1142. if (!orig_node)
  1143. return BATADV_NO_DUP;
  1144. orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
  1145. if (WARN_ON(!orig_ifinfo)) {
  1146. batadv_orig_node_put(orig_node);
  1147. return 0;
  1148. }
  1149. spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  1150. seq_diff = seqno - orig_ifinfo->last_real_seqno;
  1151. /* signalize caller that the packet is to be dropped. */
  1152. if (!hlist_empty(&orig_node->neigh_list) &&
  1153. batadv_window_protected(bat_priv, seq_diff,
  1154. BATADV_TQ_LOCAL_WINDOW_SIZE,
  1155. &orig_ifinfo->batman_seqno_reset, NULL)) {
  1156. ret = BATADV_PROTECTED;
  1157. goto out;
  1158. }
  1159. rcu_read_lock();
  1160. hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
  1161. neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
  1162. if_outgoing);
  1163. if (!neigh_ifinfo)
  1164. continue;
  1165. neigh_addr = neigh_node->addr;
  1166. is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
  1167. orig_ifinfo->last_real_seqno,
  1168. seqno);
  1169. if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
  1170. neigh_node->if_incoming == if_incoming) {
  1171. set_mark = 1;
  1172. if (is_dup)
  1173. ret = BATADV_NEIGH_DUP;
  1174. } else {
  1175. set_mark = 0;
  1176. if (is_dup && (ret != BATADV_NEIGH_DUP))
  1177. ret = BATADV_ORIG_DUP;
  1178. }
  1179. /* if the window moved, set the update flag. */
  1180. bitmap = neigh_ifinfo->bat_iv.real_bits;
  1181. need_update |= batadv_bit_get_packet(bat_priv, bitmap,
  1182. seq_diff, set_mark);
  1183. packet_count = bitmap_weight(bitmap,
  1184. BATADV_TQ_LOCAL_WINDOW_SIZE);
  1185. neigh_ifinfo->bat_iv.real_packet_count = packet_count;
  1186. batadv_neigh_ifinfo_put(neigh_ifinfo);
  1187. }
  1188. rcu_read_unlock();
  1189. if (need_update) {
  1190. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1191. "%s updating last_seqno: old %u, new %u\n",
  1192. if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
  1193. orig_ifinfo->last_real_seqno, seqno);
  1194. orig_ifinfo->last_real_seqno = seqno;
  1195. }
  1196. out:
  1197. spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  1198. batadv_orig_node_put(orig_node);
  1199. batadv_orig_ifinfo_put(orig_ifinfo);
  1200. return ret;
  1201. }
  1202. /**
  1203. * batadv_iv_ogm_process_per_outif - process a batman iv OGM for an outgoing if
  1204. * @skb: the skb containing the OGM
  1205. * @ogm_offset: offset from skb->data to start of ogm header
  1206. * @orig_node: the (cached) orig node for the originator of this OGM
  1207. * @if_incoming: the interface where this packet was received
  1208. * @if_outgoing: the interface for which the packet should be considered
  1209. */
  1210. static void
  1211. batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
  1212. struct batadv_orig_node *orig_node,
  1213. struct batadv_hard_iface *if_incoming,
  1214. struct batadv_hard_iface *if_outgoing)
  1215. {
  1216. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  1217. struct batadv_hardif_neigh_node *hardif_neigh = NULL;
  1218. struct batadv_neigh_node *router = NULL;
  1219. struct batadv_neigh_node *router_router = NULL;
  1220. struct batadv_orig_node *orig_neigh_node;
  1221. struct batadv_orig_ifinfo *orig_ifinfo;
  1222. struct batadv_neigh_node *orig_neigh_router = NULL;
  1223. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  1224. struct batadv_ogm_packet *ogm_packet;
  1225. enum batadv_dup_status dup_status;
  1226. bool is_from_best_next_hop = false;
  1227. bool is_single_hop_neigh = false;
  1228. bool sameseq, similar_ttl;
  1229. struct sk_buff *skb_priv;
  1230. struct ethhdr *ethhdr;
  1231. u8 *prev_sender;
  1232. bool is_bidirect;
  1233. /* create a private copy of the skb, as some functions change tq value
  1234. * and/or flags.
  1235. */
  1236. skb_priv = skb_copy(skb, GFP_ATOMIC);
  1237. if (!skb_priv)
  1238. return;
  1239. ethhdr = eth_hdr(skb_priv);
  1240. ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
  1241. dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
  1242. if_incoming, if_outgoing);
  1243. if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
  1244. is_single_hop_neigh = true;
  1245. if (dup_status == BATADV_PROTECTED) {
  1246. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1247. "Drop packet: packet within seqno protection time (sender: %pM)\n",
  1248. ethhdr->h_source);
  1249. goto out;
  1250. }
  1251. if (ogm_packet->tq == 0) {
  1252. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1253. "Drop packet: originator packet with tq equal 0\n");
  1254. goto out;
  1255. }
  1256. if (is_single_hop_neigh) {
  1257. hardif_neigh = batadv_hardif_neigh_get(if_incoming,
  1258. ethhdr->h_source);
  1259. if (hardif_neigh)
  1260. hardif_neigh->last_seen = jiffies;
  1261. }
  1262. router = batadv_orig_router_get(orig_node, if_outgoing);
  1263. if (router) {
  1264. router_router = batadv_orig_router_get(router->orig_node,
  1265. if_outgoing);
  1266. router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
  1267. }
  1268. if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
  1269. (batadv_compare_eth(router->addr, ethhdr->h_source)))
  1270. is_from_best_next_hop = true;
  1271. prev_sender = ogm_packet->prev_sender;
  1272. /* avoid temporary routing loops */
  1273. if (router && router_router &&
  1274. (batadv_compare_eth(router->addr, prev_sender)) &&
  1275. !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
  1276. (batadv_compare_eth(router->addr, router_router->addr))) {
  1277. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1278. "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
  1279. ethhdr->h_source);
  1280. goto out;
  1281. }
  1282. if (if_outgoing == BATADV_IF_DEFAULT)
  1283. batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
  1284. /* if sender is a direct neighbor the sender mac equals
  1285. * originator mac
  1286. */
  1287. if (is_single_hop_neigh)
  1288. orig_neigh_node = orig_node;
  1289. else
  1290. orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
  1291. ethhdr->h_source);
  1292. if (!orig_neigh_node)
  1293. goto out;
  1294. /* Update nc_nodes of the originator */
  1295. batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
  1296. ogm_packet, is_single_hop_neigh);
  1297. orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
  1298. if_outgoing);
  1299. /* drop packet if sender is not a direct neighbor and if we
  1300. * don't route towards it
  1301. */
  1302. if (!is_single_hop_neigh && (!orig_neigh_router)) {
  1303. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1304. "Drop packet: OGM via unknown neighbor!\n");
  1305. goto out_neigh;
  1306. }
  1307. is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
  1308. ogm_packet, if_incoming,
  1309. if_outgoing);
  1310. /* update ranking if it is not a duplicate or has the same
  1311. * seqno and similar ttl as the non-duplicate
  1312. */
  1313. orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
  1314. if (!orig_ifinfo)
  1315. goto out_neigh;
  1316. sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
  1317. similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
  1318. if (is_bidirect && ((dup_status == BATADV_NO_DUP) ||
  1319. (sameseq && similar_ttl))) {
  1320. batadv_iv_ogm_orig_update(bat_priv, orig_node,
  1321. orig_ifinfo, ethhdr,
  1322. ogm_packet, if_incoming,
  1323. if_outgoing, dup_status);
  1324. }
  1325. batadv_orig_ifinfo_put(orig_ifinfo);
  1326. /* only forward for specific interface, not for the default one. */
  1327. if (if_outgoing == BATADV_IF_DEFAULT)
  1328. goto out_neigh;
  1329. /* is single hop (direct) neighbor */
  1330. if (is_single_hop_neigh) {
  1331. /* OGMs from secondary interfaces should only scheduled once
  1332. * per interface where it has been received, not multiple times
  1333. */
  1334. if ((ogm_packet->ttl <= 2) &&
  1335. (if_incoming != if_outgoing)) {
  1336. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1337. "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
  1338. goto out_neigh;
  1339. }
  1340. /* mark direct link on incoming interface */
  1341. batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
  1342. is_single_hop_neigh,
  1343. is_from_best_next_hop, if_incoming,
  1344. if_outgoing);
  1345. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1346. "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
  1347. goto out_neigh;
  1348. }
  1349. /* multihop originator */
  1350. if (!is_bidirect) {
  1351. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1352. "Drop packet: not received via bidirectional link\n");
  1353. goto out_neigh;
  1354. }
  1355. if (dup_status == BATADV_NEIGH_DUP) {
  1356. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1357. "Drop packet: duplicate packet received\n");
  1358. goto out_neigh;
  1359. }
  1360. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1361. "Forwarding packet: rebroadcast originator packet\n");
  1362. batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
  1363. is_single_hop_neigh, is_from_best_next_hop,
  1364. if_incoming, if_outgoing);
  1365. out_neigh:
  1366. if ((orig_neigh_node) && (!is_single_hop_neigh))
  1367. batadv_orig_node_put(orig_neigh_node);
  1368. out:
  1369. if (router_ifinfo)
  1370. batadv_neigh_ifinfo_put(router_ifinfo);
  1371. if (router)
  1372. batadv_neigh_node_put(router);
  1373. if (router_router)
  1374. batadv_neigh_node_put(router_router);
  1375. if (orig_neigh_router)
  1376. batadv_neigh_node_put(orig_neigh_router);
  1377. if (hardif_neigh)
  1378. batadv_hardif_neigh_put(hardif_neigh);
  1379. consume_skb(skb_priv);
  1380. }
  1381. /**
  1382. * batadv_iv_ogm_process - process an incoming batman iv OGM
  1383. * @skb: the skb containing the OGM
  1384. * @ogm_offset: offset to the OGM which should be processed (for aggregates)
  1385. * @if_incoming: the interface where this packet was receved
  1386. */
  1387. static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
  1388. struct batadv_hard_iface *if_incoming)
  1389. {
  1390. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  1391. struct batadv_orig_node *orig_neigh_node, *orig_node;
  1392. struct batadv_hard_iface *hard_iface;
  1393. struct batadv_ogm_packet *ogm_packet;
  1394. u32 if_incoming_seqno;
  1395. bool has_directlink_flag;
  1396. struct ethhdr *ethhdr;
  1397. bool is_my_oldorig = false;
  1398. bool is_my_addr = false;
  1399. bool is_my_orig = false;
  1400. ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
  1401. ethhdr = eth_hdr(skb);
  1402. /* Silently drop when the batman packet is actually not a
  1403. * correct packet.
  1404. *
  1405. * This might happen if a packet is padded (e.g. Ethernet has a
  1406. * minimum frame length of 64 byte) and the aggregation interprets
  1407. * it as an additional length.
  1408. *
  1409. * TODO: A more sane solution would be to have a bit in the
  1410. * batadv_ogm_packet to detect whether the packet is the last
  1411. * packet in an aggregation. Here we expect that the padding
  1412. * is always zero (or not 0x01)
  1413. */
  1414. if (ogm_packet->packet_type != BATADV_IV_OGM)
  1415. return;
  1416. /* could be changed by schedule_own_packet() */
  1417. if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
  1418. if (ogm_packet->flags & BATADV_DIRECTLINK)
  1419. has_directlink_flag = true;
  1420. else
  1421. has_directlink_flag = false;
  1422. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1423. "Received BATMAN packet via NB: %pM, IF: %s [%pM] (from OG: %pM, via prev OG: %pM, seqno %u, tq %d, TTL %d, V %d, IDF %d)\n",
  1424. ethhdr->h_source, if_incoming->net_dev->name,
  1425. if_incoming->net_dev->dev_addr, ogm_packet->orig,
  1426. ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
  1427. ogm_packet->tq, ogm_packet->ttl,
  1428. ogm_packet->version, has_directlink_flag);
  1429. rcu_read_lock();
  1430. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  1431. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  1432. continue;
  1433. if (hard_iface->soft_iface != if_incoming->soft_iface)
  1434. continue;
  1435. if (batadv_compare_eth(ethhdr->h_source,
  1436. hard_iface->net_dev->dev_addr))
  1437. is_my_addr = true;
  1438. if (batadv_compare_eth(ogm_packet->orig,
  1439. hard_iface->net_dev->dev_addr))
  1440. is_my_orig = true;
  1441. if (batadv_compare_eth(ogm_packet->prev_sender,
  1442. hard_iface->net_dev->dev_addr))
  1443. is_my_oldorig = true;
  1444. }
  1445. rcu_read_unlock();
  1446. if (is_my_addr) {
  1447. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1448. "Drop packet: received my own broadcast (sender: %pM)\n",
  1449. ethhdr->h_source);
  1450. return;
  1451. }
  1452. if (is_my_orig) {
  1453. unsigned long *word;
  1454. int offset;
  1455. s32 bit_pos;
  1456. s16 if_num;
  1457. u8 *weight;
  1458. orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
  1459. ethhdr->h_source);
  1460. if (!orig_neigh_node)
  1461. return;
  1462. /* neighbor has to indicate direct link and it has to
  1463. * come via the corresponding interface
  1464. * save packet seqno for bidirectional check
  1465. */
  1466. if (has_directlink_flag &&
  1467. batadv_compare_eth(if_incoming->net_dev->dev_addr,
  1468. ogm_packet->orig)) {
  1469. if_num = if_incoming->if_num;
  1470. offset = if_num * BATADV_NUM_WORDS;
  1471. spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
  1472. word = &orig_neigh_node->bat_iv.bcast_own[offset];
  1473. bit_pos = if_incoming_seqno - 2;
  1474. bit_pos -= ntohl(ogm_packet->seqno);
  1475. batadv_set_bit(word, bit_pos);
  1476. weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num];
  1477. *weight = bitmap_weight(word,
  1478. BATADV_TQ_LOCAL_WINDOW_SIZE);
  1479. spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
  1480. }
  1481. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1482. "Drop packet: originator packet from myself (via neighbor)\n");
  1483. batadv_orig_node_put(orig_neigh_node);
  1484. return;
  1485. }
  1486. if (is_my_oldorig) {
  1487. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1488. "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
  1489. ethhdr->h_source);
  1490. return;
  1491. }
  1492. if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
  1493. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1494. "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
  1495. ethhdr->h_source);
  1496. return;
  1497. }
  1498. orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
  1499. if (!orig_node)
  1500. return;
  1501. batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
  1502. if_incoming, BATADV_IF_DEFAULT);
  1503. rcu_read_lock();
  1504. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  1505. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  1506. continue;
  1507. if (hard_iface->soft_iface != bat_priv->soft_iface)
  1508. continue;
  1509. if (!kref_get_unless_zero(&hard_iface->refcount))
  1510. continue;
  1511. batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
  1512. if_incoming, hard_iface);
  1513. batadv_hardif_put(hard_iface);
  1514. }
  1515. rcu_read_unlock();
  1516. batadv_orig_node_put(orig_node);
  1517. }
  1518. static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work)
  1519. {
  1520. struct delayed_work *delayed_work;
  1521. struct batadv_forw_packet *forw_packet;
  1522. struct batadv_priv *bat_priv;
  1523. bool dropped = false;
  1524. delayed_work = to_delayed_work(work);
  1525. forw_packet = container_of(delayed_work, struct batadv_forw_packet,
  1526. delayed_work);
  1527. bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
  1528. if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) {
  1529. dropped = true;
  1530. goto out;
  1531. }
  1532. batadv_iv_ogm_emit(forw_packet);
  1533. /* we have to have at least one packet in the queue to determine the
  1534. * queues wake up time unless we are shutting down.
  1535. *
  1536. * only re-schedule if this is the "original" copy, e.g. the OGM of the
  1537. * primary interface should only be rescheduled once per period, but
  1538. * this function will be called for the forw_packet instances of the
  1539. * other secondary interfaces as well.
  1540. */
  1541. if (forw_packet->own &&
  1542. forw_packet->if_incoming == forw_packet->if_outgoing)
  1543. batadv_iv_ogm_schedule(forw_packet->if_incoming);
  1544. out:
  1545. /* do we get something for free()? */
  1546. if (batadv_forw_packet_steal(forw_packet,
  1547. &bat_priv->forw_bat_list_lock))
  1548. batadv_forw_packet_free(forw_packet, dropped);
  1549. }
  1550. static int batadv_iv_ogm_receive(struct sk_buff *skb,
  1551. struct batadv_hard_iface *if_incoming)
  1552. {
  1553. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  1554. struct batadv_ogm_packet *ogm_packet;
  1555. u8 *packet_pos;
  1556. int ogm_offset;
  1557. bool res;
  1558. int ret = NET_RX_DROP;
  1559. res = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
  1560. if (!res)
  1561. goto free_skb;
  1562. /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
  1563. * that does not have B.A.T.M.A.N. IV enabled ?
  1564. */
  1565. if (bat_priv->algo_ops->iface.enable != batadv_iv_ogm_iface_enable)
  1566. goto free_skb;
  1567. batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
  1568. batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
  1569. skb->len + ETH_HLEN);
  1570. ogm_offset = 0;
  1571. ogm_packet = (struct batadv_ogm_packet *)skb->data;
  1572. /* unpack the aggregated packets and process them one by one */
  1573. while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
  1574. ogm_packet->tvlv_len)) {
  1575. batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
  1576. ogm_offset += BATADV_OGM_HLEN;
  1577. ogm_offset += ntohs(ogm_packet->tvlv_len);
  1578. packet_pos = skb->data + ogm_offset;
  1579. ogm_packet = (struct batadv_ogm_packet *)packet_pos;
  1580. }
  1581. ret = NET_RX_SUCCESS;
  1582. free_skb:
  1583. if (ret == NET_RX_SUCCESS)
  1584. consume_skb(skb);
  1585. else
  1586. kfree_skb(skb);
  1587. return ret;
  1588. }
  1589. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  1590. /**
  1591. * batadv_iv_ogm_orig_print_neigh - print neighbors for the originator table
  1592. * @orig_node: the orig_node for which the neighbors are printed
  1593. * @if_outgoing: outgoing interface for these entries
  1594. * @seq: debugfs table seq_file struct
  1595. *
  1596. * Must be called while holding an rcu lock.
  1597. */
  1598. static void
  1599. batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node,
  1600. struct batadv_hard_iface *if_outgoing,
  1601. struct seq_file *seq)
  1602. {
  1603. struct batadv_neigh_node *neigh_node;
  1604. struct batadv_neigh_ifinfo *n_ifinfo;
  1605. hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
  1606. n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
  1607. if (!n_ifinfo)
  1608. continue;
  1609. seq_printf(seq, " %pM (%3i)",
  1610. neigh_node->addr,
  1611. n_ifinfo->bat_iv.tq_avg);
  1612. batadv_neigh_ifinfo_put(n_ifinfo);
  1613. }
  1614. }
  1615. /**
  1616. * batadv_iv_ogm_orig_print - print the originator table
  1617. * @bat_priv: the bat priv with all the soft interface information
  1618. * @seq: debugfs table seq_file struct
  1619. * @if_outgoing: the outgoing interface for which this should be printed
  1620. */
  1621. static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
  1622. struct seq_file *seq,
  1623. struct batadv_hard_iface *if_outgoing)
  1624. {
  1625. struct batadv_neigh_node *neigh_node;
  1626. struct batadv_hashtable *hash = bat_priv->orig_hash;
  1627. int last_seen_msecs, last_seen_secs;
  1628. struct batadv_orig_node *orig_node;
  1629. struct batadv_neigh_ifinfo *n_ifinfo;
  1630. unsigned long last_seen_jiffies;
  1631. struct hlist_head *head;
  1632. int batman_count = 0;
  1633. u32 i;
  1634. seq_puts(seq,
  1635. " Originator last-seen (#/255) Nexthop [outgoingIF]: Potential nexthops ...\n");
  1636. for (i = 0; i < hash->size; i++) {
  1637. head = &hash->table[i];
  1638. rcu_read_lock();
  1639. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  1640. neigh_node = batadv_orig_router_get(orig_node,
  1641. if_outgoing);
  1642. if (!neigh_node)
  1643. continue;
  1644. n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
  1645. if_outgoing);
  1646. if (!n_ifinfo)
  1647. goto next;
  1648. if (n_ifinfo->bat_iv.tq_avg == 0)
  1649. goto next;
  1650. last_seen_jiffies = jiffies - orig_node->last_seen;
  1651. last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
  1652. last_seen_secs = last_seen_msecs / 1000;
  1653. last_seen_msecs = last_seen_msecs % 1000;
  1654. seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
  1655. orig_node->orig, last_seen_secs,
  1656. last_seen_msecs, n_ifinfo->bat_iv.tq_avg,
  1657. neigh_node->addr,
  1658. neigh_node->if_incoming->net_dev->name);
  1659. batadv_iv_ogm_orig_print_neigh(orig_node, if_outgoing,
  1660. seq);
  1661. seq_puts(seq, "\n");
  1662. batman_count++;
  1663. next:
  1664. batadv_neigh_node_put(neigh_node);
  1665. if (n_ifinfo)
  1666. batadv_neigh_ifinfo_put(n_ifinfo);
  1667. }
  1668. rcu_read_unlock();
  1669. }
  1670. if (batman_count == 0)
  1671. seq_puts(seq, "No batman nodes in range ...\n");
  1672. }
  1673. #endif
  1674. /**
  1675. * batadv_iv_ogm_neigh_get_tq_avg - Get the TQ average for a neighbour on a
  1676. * given outgoing interface.
  1677. * @neigh_node: Neighbour of interest
  1678. * @if_outgoing: Outgoing interface of interest
  1679. * @tq_avg: Pointer of where to store the TQ average
  1680. *
  1681. * Return: False if no average TQ available, otherwise true.
  1682. */
  1683. static bool
  1684. batadv_iv_ogm_neigh_get_tq_avg(struct batadv_neigh_node *neigh_node,
  1685. struct batadv_hard_iface *if_outgoing,
  1686. u8 *tq_avg)
  1687. {
  1688. struct batadv_neigh_ifinfo *n_ifinfo;
  1689. n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
  1690. if (!n_ifinfo)
  1691. return false;
  1692. *tq_avg = n_ifinfo->bat_iv.tq_avg;
  1693. batadv_neigh_ifinfo_put(n_ifinfo);
  1694. return true;
  1695. }
  1696. /**
  1697. * batadv_iv_ogm_orig_dump_subentry - Dump an originator subentry into a
  1698. * message
  1699. * @msg: Netlink message to dump into
  1700. * @portid: Port making netlink request
  1701. * @seq: Sequence number of netlink message
  1702. * @bat_priv: The bat priv with all the soft interface information
  1703. * @if_outgoing: Limit dump to entries with this outgoing interface
  1704. * @orig_node: Originator to dump
  1705. * @neigh_node: Single hops neighbour
  1706. * @best: Is the best originator
  1707. *
  1708. * Return: Error code, or 0 on success
  1709. */
  1710. static int
  1711. batadv_iv_ogm_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
  1712. struct batadv_priv *bat_priv,
  1713. struct batadv_hard_iface *if_outgoing,
  1714. struct batadv_orig_node *orig_node,
  1715. struct batadv_neigh_node *neigh_node,
  1716. bool best)
  1717. {
  1718. void *hdr;
  1719. u8 tq_avg;
  1720. unsigned int last_seen_msecs;
  1721. last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
  1722. if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node, if_outgoing, &tq_avg))
  1723. return 0;
  1724. if (if_outgoing != BATADV_IF_DEFAULT &&
  1725. if_outgoing != neigh_node->if_incoming)
  1726. return 0;
  1727. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
  1728. NLM_F_MULTI, BATADV_CMD_GET_ORIGINATORS);
  1729. if (!hdr)
  1730. return -ENOBUFS;
  1731. if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
  1732. orig_node->orig) ||
  1733. nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
  1734. neigh_node->addr) ||
  1735. nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
  1736. neigh_node->if_incoming->net_dev->ifindex) ||
  1737. nla_put_u8(msg, BATADV_ATTR_TQ, tq_avg) ||
  1738. nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
  1739. last_seen_msecs))
  1740. goto nla_put_failure;
  1741. if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
  1742. goto nla_put_failure;
  1743. genlmsg_end(msg, hdr);
  1744. return 0;
  1745. nla_put_failure:
  1746. genlmsg_cancel(msg, hdr);
  1747. return -EMSGSIZE;
  1748. }
  1749. /**
  1750. * batadv_iv_ogm_orig_dump_entry - Dump an originator entry into a message
  1751. * @msg: Netlink message to dump into
  1752. * @portid: Port making netlink request
  1753. * @seq: Sequence number of netlink message
  1754. * @bat_priv: The bat priv with all the soft interface information
  1755. * @if_outgoing: Limit dump to entries with this outgoing interface
  1756. * @orig_node: Originator to dump
  1757. * @sub_s: Number of sub entries to skip
  1758. *
  1759. * This function assumes the caller holds rcu_read_lock().
  1760. *
  1761. * Return: Error code, or 0 on success
  1762. */
  1763. static int
  1764. batadv_iv_ogm_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
  1765. struct batadv_priv *bat_priv,
  1766. struct batadv_hard_iface *if_outgoing,
  1767. struct batadv_orig_node *orig_node, int *sub_s)
  1768. {
  1769. struct batadv_neigh_node *neigh_node_best;
  1770. struct batadv_neigh_node *neigh_node;
  1771. int sub = 0;
  1772. bool best;
  1773. u8 tq_avg_best;
  1774. neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
  1775. if (!neigh_node_best)
  1776. goto out;
  1777. if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node_best, if_outgoing,
  1778. &tq_avg_best))
  1779. goto out;
  1780. if (tq_avg_best == 0)
  1781. goto out;
  1782. hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
  1783. if (sub++ < *sub_s)
  1784. continue;
  1785. best = (neigh_node == neigh_node_best);
  1786. if (batadv_iv_ogm_orig_dump_subentry(msg, portid, seq,
  1787. bat_priv, if_outgoing,
  1788. orig_node, neigh_node,
  1789. best)) {
  1790. batadv_neigh_node_put(neigh_node_best);
  1791. *sub_s = sub - 1;
  1792. return -EMSGSIZE;
  1793. }
  1794. }
  1795. out:
  1796. if (neigh_node_best)
  1797. batadv_neigh_node_put(neigh_node_best);
  1798. *sub_s = 0;
  1799. return 0;
  1800. }
  1801. /**
  1802. * batadv_iv_ogm_orig_dump_bucket - Dump an originator bucket into a
  1803. * message
  1804. * @msg: Netlink message to dump into
  1805. * @portid: Port making netlink request
  1806. * @seq: Sequence number of netlink message
  1807. * @bat_priv: The bat priv with all the soft interface information
  1808. * @if_outgoing: Limit dump to entries with this outgoing interface
  1809. * @head: Bucket to be dumped
  1810. * @idx_s: Number of entries to be skipped
  1811. * @sub: Number of sub entries to be skipped
  1812. *
  1813. * Return: Error code, or 0 on success
  1814. */
  1815. static int
  1816. batadv_iv_ogm_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
  1817. struct batadv_priv *bat_priv,
  1818. struct batadv_hard_iface *if_outgoing,
  1819. struct hlist_head *head, int *idx_s, int *sub)
  1820. {
  1821. struct batadv_orig_node *orig_node;
  1822. int idx = 0;
  1823. rcu_read_lock();
  1824. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  1825. if (idx++ < *idx_s)
  1826. continue;
  1827. if (batadv_iv_ogm_orig_dump_entry(msg, portid, seq, bat_priv,
  1828. if_outgoing, orig_node,
  1829. sub)) {
  1830. rcu_read_unlock();
  1831. *idx_s = idx - 1;
  1832. return -EMSGSIZE;
  1833. }
  1834. }
  1835. rcu_read_unlock();
  1836. *idx_s = 0;
  1837. *sub = 0;
  1838. return 0;
  1839. }
  1840. /**
  1841. * batadv_iv_ogm_orig_dump - Dump the originators into a message
  1842. * @msg: Netlink message to dump into
  1843. * @cb: Control block containing additional options
  1844. * @bat_priv: The bat priv with all the soft interface information
  1845. * @if_outgoing: Limit dump to entries with this outgoing interface
  1846. */
  1847. static void
  1848. batadv_iv_ogm_orig_dump(struct sk_buff *msg, struct netlink_callback *cb,
  1849. struct batadv_priv *bat_priv,
  1850. struct batadv_hard_iface *if_outgoing)
  1851. {
  1852. struct batadv_hashtable *hash = bat_priv->orig_hash;
  1853. struct hlist_head *head;
  1854. int bucket = cb->args[0];
  1855. int idx = cb->args[1];
  1856. int sub = cb->args[2];
  1857. int portid = NETLINK_CB(cb->skb).portid;
  1858. while (bucket < hash->size) {
  1859. head = &hash->table[bucket];
  1860. if (batadv_iv_ogm_orig_dump_bucket(msg, portid,
  1861. cb->nlh->nlmsg_seq,
  1862. bat_priv, if_outgoing, head,
  1863. &idx, &sub))
  1864. break;
  1865. bucket++;
  1866. }
  1867. cb->args[0] = bucket;
  1868. cb->args[1] = idx;
  1869. cb->args[2] = sub;
  1870. }
  1871. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  1872. /**
  1873. * batadv_iv_hardif_neigh_print - print a single hop neighbour node
  1874. * @seq: neighbour table seq_file struct
  1875. * @hardif_neigh: hardif neighbour information
  1876. */
  1877. static void
  1878. batadv_iv_hardif_neigh_print(struct seq_file *seq,
  1879. struct batadv_hardif_neigh_node *hardif_neigh)
  1880. {
  1881. int last_secs, last_msecs;
  1882. last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000;
  1883. last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000;
  1884. seq_printf(seq, " %10s %pM %4i.%03is\n",
  1885. hardif_neigh->if_incoming->net_dev->name,
  1886. hardif_neigh->addr, last_secs, last_msecs);
  1887. }
  1888. /**
  1889. * batadv_iv_ogm_neigh_print - print the single hop neighbour list
  1890. * @bat_priv: the bat priv with all the soft interface information
  1891. * @seq: neighbour table seq_file struct
  1892. */
  1893. static void batadv_iv_neigh_print(struct batadv_priv *bat_priv,
  1894. struct seq_file *seq)
  1895. {
  1896. struct net_device *net_dev = (struct net_device *)seq->private;
  1897. struct batadv_hardif_neigh_node *hardif_neigh;
  1898. struct batadv_hard_iface *hard_iface;
  1899. int batman_count = 0;
  1900. seq_puts(seq, " IF Neighbor last-seen\n");
  1901. rcu_read_lock();
  1902. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  1903. if (hard_iface->soft_iface != net_dev)
  1904. continue;
  1905. hlist_for_each_entry_rcu(hardif_neigh,
  1906. &hard_iface->neigh_list, list) {
  1907. batadv_iv_hardif_neigh_print(seq, hardif_neigh);
  1908. batman_count++;
  1909. }
  1910. }
  1911. rcu_read_unlock();
  1912. if (batman_count == 0)
  1913. seq_puts(seq, "No batman nodes in range ...\n");
  1914. }
  1915. #endif
  1916. /**
  1917. * batadv_iv_ogm_neigh_diff - calculate tq difference of two neighbors
  1918. * @neigh1: the first neighbor object of the comparison
  1919. * @if_outgoing1: outgoing interface for the first neighbor
  1920. * @neigh2: the second neighbor object of the comparison
  1921. * @if_outgoing2: outgoing interface for the second neighbor
  1922. * @diff: pointer to integer receiving the calculated difference
  1923. *
  1924. * The content of *@diff is only valid when this function returns true.
  1925. * It is less, equal to or greater than 0 if the metric via neigh1 is lower,
  1926. * the same as or higher than the metric via neigh2
  1927. *
  1928. * Return: true when the difference could be calculated, false otherwise
  1929. */
  1930. static bool batadv_iv_ogm_neigh_diff(struct batadv_neigh_node *neigh1,
  1931. struct batadv_hard_iface *if_outgoing1,
  1932. struct batadv_neigh_node *neigh2,
  1933. struct batadv_hard_iface *if_outgoing2,
  1934. int *diff)
  1935. {
  1936. struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
  1937. u8 tq1, tq2;
  1938. bool ret = true;
  1939. neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
  1940. neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
  1941. if (!neigh1_ifinfo || !neigh2_ifinfo) {
  1942. ret = false;
  1943. goto out;
  1944. }
  1945. tq1 = neigh1_ifinfo->bat_iv.tq_avg;
  1946. tq2 = neigh2_ifinfo->bat_iv.tq_avg;
  1947. *diff = (int)tq1 - (int)tq2;
  1948. out:
  1949. if (neigh1_ifinfo)
  1950. batadv_neigh_ifinfo_put(neigh1_ifinfo);
  1951. if (neigh2_ifinfo)
  1952. batadv_neigh_ifinfo_put(neigh2_ifinfo);
  1953. return ret;
  1954. }
  1955. /**
  1956. * batadv_iv_ogm_neigh_dump_neigh - Dump a neighbour into a netlink message
  1957. * @msg: Netlink message to dump into
  1958. * @portid: Port making netlink request
  1959. * @seq: Sequence number of netlink message
  1960. * @hardif_neigh: Neighbour to be dumped
  1961. *
  1962. * Return: Error code, or 0 on success
  1963. */
  1964. static int
  1965. batadv_iv_ogm_neigh_dump_neigh(struct sk_buff *msg, u32 portid, u32 seq,
  1966. struct batadv_hardif_neigh_node *hardif_neigh)
  1967. {
  1968. void *hdr;
  1969. unsigned int last_seen_msecs;
  1970. last_seen_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen);
  1971. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
  1972. NLM_F_MULTI, BATADV_CMD_GET_NEIGHBORS);
  1973. if (!hdr)
  1974. return -ENOBUFS;
  1975. if (nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
  1976. hardif_neigh->addr) ||
  1977. nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
  1978. hardif_neigh->if_incoming->net_dev->ifindex) ||
  1979. nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
  1980. last_seen_msecs))
  1981. goto nla_put_failure;
  1982. genlmsg_end(msg, hdr);
  1983. return 0;
  1984. nla_put_failure:
  1985. genlmsg_cancel(msg, hdr);
  1986. return -EMSGSIZE;
  1987. }
  1988. /**
  1989. * batadv_iv_ogm_neigh_dump_hardif - Dump the neighbours of a hard interface
  1990. * into a message
  1991. * @msg: Netlink message to dump into
  1992. * @portid: Port making netlink request
  1993. * @seq: Sequence number of netlink message
  1994. * @bat_priv: The bat priv with all the soft interface information
  1995. * @hard_iface: Hard interface to dump the neighbours for
  1996. * @idx_s: Number of entries to skip
  1997. *
  1998. * This function assumes the caller holds rcu_read_lock().
  1999. *
  2000. * Return: Error code, or 0 on success
  2001. */
  2002. static int
  2003. batadv_iv_ogm_neigh_dump_hardif(struct sk_buff *msg, u32 portid, u32 seq,
  2004. struct batadv_priv *bat_priv,
  2005. struct batadv_hard_iface *hard_iface,
  2006. int *idx_s)
  2007. {
  2008. struct batadv_hardif_neigh_node *hardif_neigh;
  2009. int idx = 0;
  2010. hlist_for_each_entry_rcu(hardif_neigh,
  2011. &hard_iface->neigh_list, list) {
  2012. if (idx++ < *idx_s)
  2013. continue;
  2014. if (batadv_iv_ogm_neigh_dump_neigh(msg, portid, seq,
  2015. hardif_neigh)) {
  2016. *idx_s = idx - 1;
  2017. return -EMSGSIZE;
  2018. }
  2019. }
  2020. *idx_s = 0;
  2021. return 0;
  2022. }
  2023. /**
  2024. * batadv_iv_ogm_neigh_dump - Dump the neighbours into a message
  2025. * @msg: Netlink message to dump into
  2026. * @cb: Control block containing additional options
  2027. * @bat_priv: The bat priv with all the soft interface information
  2028. * @single_hardif: Limit dump to this hard interfaace
  2029. */
  2030. static void
  2031. batadv_iv_ogm_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb,
  2032. struct batadv_priv *bat_priv,
  2033. struct batadv_hard_iface *single_hardif)
  2034. {
  2035. struct batadv_hard_iface *hard_iface;
  2036. int i_hardif = 0;
  2037. int i_hardif_s = cb->args[0];
  2038. int idx = cb->args[1];
  2039. int portid = NETLINK_CB(cb->skb).portid;
  2040. rcu_read_lock();
  2041. if (single_hardif) {
  2042. if (i_hardif_s == 0) {
  2043. if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
  2044. cb->nlh->nlmsg_seq,
  2045. bat_priv,
  2046. single_hardif,
  2047. &idx) == 0)
  2048. i_hardif++;
  2049. }
  2050. } else {
  2051. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list,
  2052. list) {
  2053. if (hard_iface->soft_iface != bat_priv->soft_iface)
  2054. continue;
  2055. if (i_hardif++ < i_hardif_s)
  2056. continue;
  2057. if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
  2058. cb->nlh->nlmsg_seq,
  2059. bat_priv,
  2060. hard_iface, &idx)) {
  2061. i_hardif--;
  2062. break;
  2063. }
  2064. }
  2065. }
  2066. rcu_read_unlock();
  2067. cb->args[0] = i_hardif;
  2068. cb->args[1] = idx;
  2069. }
  2070. /**
  2071. * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors
  2072. * @neigh1: the first neighbor object of the comparison
  2073. * @if_outgoing1: outgoing interface for the first neighbor
  2074. * @neigh2: the second neighbor object of the comparison
  2075. * @if_outgoing2: outgoing interface for the second neighbor
  2076. *
  2077. * Return: a value less, equal to or greater than 0 if the metric via neigh1 is
  2078. * lower, the same as or higher than the metric via neigh2
  2079. */
  2080. static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
  2081. struct batadv_hard_iface *if_outgoing1,
  2082. struct batadv_neigh_node *neigh2,
  2083. struct batadv_hard_iface *if_outgoing2)
  2084. {
  2085. bool ret;
  2086. int diff;
  2087. ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
  2088. if_outgoing2, &diff);
  2089. if (!ret)
  2090. return 0;
  2091. return diff;
  2092. }
  2093. /**
  2094. * batadv_iv_ogm_neigh_is_sob - check if neigh1 is similarly good or better
  2095. * than neigh2 from the metric prospective
  2096. * @neigh1: the first neighbor object of the comparison
  2097. * @if_outgoing1: outgoing interface for the first neighbor
  2098. * @neigh2: the second neighbor object of the comparison
  2099. * @if_outgoing2: outgoing interface for the second neighbor
  2100. *
  2101. * Return: true if the metric via neigh1 is equally good or better than
  2102. * the metric via neigh2, false otherwise.
  2103. */
  2104. static bool
  2105. batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1,
  2106. struct batadv_hard_iface *if_outgoing1,
  2107. struct batadv_neigh_node *neigh2,
  2108. struct batadv_hard_iface *if_outgoing2)
  2109. {
  2110. bool ret;
  2111. int diff;
  2112. ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
  2113. if_outgoing2, &diff);
  2114. if (!ret)
  2115. return false;
  2116. ret = diff > -BATADV_TQ_SIMILARITY_THRESHOLD;
  2117. return ret;
  2118. }
  2119. static void batadv_iv_iface_activate(struct batadv_hard_iface *hard_iface)
  2120. {
  2121. /* begin scheduling originator messages on that interface */
  2122. batadv_iv_ogm_schedule(hard_iface);
  2123. }
  2124. static struct batadv_gw_node *
  2125. batadv_iv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
  2126. {
  2127. struct batadv_neigh_node *router;
  2128. struct batadv_neigh_ifinfo *router_ifinfo;
  2129. struct batadv_gw_node *gw_node, *curr_gw = NULL;
  2130. u64 max_gw_factor = 0;
  2131. u64 tmp_gw_factor = 0;
  2132. u8 max_tq = 0;
  2133. u8 tq_avg;
  2134. struct batadv_orig_node *orig_node;
  2135. rcu_read_lock();
  2136. hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
  2137. orig_node = gw_node->orig_node;
  2138. router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
  2139. if (!router)
  2140. continue;
  2141. router_ifinfo = batadv_neigh_ifinfo_get(router,
  2142. BATADV_IF_DEFAULT);
  2143. if (!router_ifinfo)
  2144. goto next;
  2145. if (!kref_get_unless_zero(&gw_node->refcount))
  2146. goto next;
  2147. tq_avg = router_ifinfo->bat_iv.tq_avg;
  2148. switch (atomic_read(&bat_priv->gw.sel_class)) {
  2149. case 1: /* fast connection */
  2150. tmp_gw_factor = tq_avg * tq_avg;
  2151. tmp_gw_factor *= gw_node->bandwidth_down;
  2152. tmp_gw_factor *= 100 * 100;
  2153. tmp_gw_factor >>= 18;
  2154. if ((tmp_gw_factor > max_gw_factor) ||
  2155. ((tmp_gw_factor == max_gw_factor) &&
  2156. (tq_avg > max_tq))) {
  2157. if (curr_gw)
  2158. batadv_gw_node_put(curr_gw);
  2159. curr_gw = gw_node;
  2160. kref_get(&curr_gw->refcount);
  2161. }
  2162. break;
  2163. default: /* 2: stable connection (use best statistic)
  2164. * 3: fast-switch (use best statistic but change as
  2165. * soon as a better gateway appears)
  2166. * XX: late-switch (use best statistic but change as
  2167. * soon as a better gateway appears which has
  2168. * $routing_class more tq points)
  2169. */
  2170. if (tq_avg > max_tq) {
  2171. if (curr_gw)
  2172. batadv_gw_node_put(curr_gw);
  2173. curr_gw = gw_node;
  2174. kref_get(&curr_gw->refcount);
  2175. }
  2176. break;
  2177. }
  2178. if (tq_avg > max_tq)
  2179. max_tq = tq_avg;
  2180. if (tmp_gw_factor > max_gw_factor)
  2181. max_gw_factor = tmp_gw_factor;
  2182. batadv_gw_node_put(gw_node);
  2183. next:
  2184. batadv_neigh_node_put(router);
  2185. if (router_ifinfo)
  2186. batadv_neigh_ifinfo_put(router_ifinfo);
  2187. }
  2188. rcu_read_unlock();
  2189. return curr_gw;
  2190. }
  2191. static bool batadv_iv_gw_is_eligible(struct batadv_priv *bat_priv,
  2192. struct batadv_orig_node *curr_gw_orig,
  2193. struct batadv_orig_node *orig_node)
  2194. {
  2195. struct batadv_neigh_ifinfo *router_orig_ifinfo = NULL;
  2196. struct batadv_neigh_ifinfo *router_gw_ifinfo = NULL;
  2197. struct batadv_neigh_node *router_gw = NULL;
  2198. struct batadv_neigh_node *router_orig = NULL;
  2199. u8 gw_tq_avg, orig_tq_avg;
  2200. bool ret = false;
  2201. /* dynamic re-election is performed only on fast or late switch */
  2202. if (atomic_read(&bat_priv->gw.sel_class) <= 2)
  2203. return false;
  2204. router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT);
  2205. if (!router_gw) {
  2206. ret = true;
  2207. goto out;
  2208. }
  2209. router_gw_ifinfo = batadv_neigh_ifinfo_get(router_gw,
  2210. BATADV_IF_DEFAULT);
  2211. if (!router_gw_ifinfo) {
  2212. ret = true;
  2213. goto out;
  2214. }
  2215. router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
  2216. if (!router_orig)
  2217. goto out;
  2218. router_orig_ifinfo = batadv_neigh_ifinfo_get(router_orig,
  2219. BATADV_IF_DEFAULT);
  2220. if (!router_orig_ifinfo)
  2221. goto out;
  2222. gw_tq_avg = router_gw_ifinfo->bat_iv.tq_avg;
  2223. orig_tq_avg = router_orig_ifinfo->bat_iv.tq_avg;
  2224. /* the TQ value has to be better */
  2225. if (orig_tq_avg < gw_tq_avg)
  2226. goto out;
  2227. /* if the routing class is greater than 3 the value tells us how much
  2228. * greater the TQ value of the new gateway must be
  2229. */
  2230. if ((atomic_read(&bat_priv->gw.sel_class) > 3) &&
  2231. (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw.sel_class)))
  2232. goto out;
  2233. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  2234. "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
  2235. gw_tq_avg, orig_tq_avg);
  2236. ret = true;
  2237. out:
  2238. if (router_gw_ifinfo)
  2239. batadv_neigh_ifinfo_put(router_gw_ifinfo);
  2240. if (router_orig_ifinfo)
  2241. batadv_neigh_ifinfo_put(router_orig_ifinfo);
  2242. if (router_gw)
  2243. batadv_neigh_node_put(router_gw);
  2244. if (router_orig)
  2245. batadv_neigh_node_put(router_orig);
  2246. return ret;
  2247. }
  2248. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  2249. /* fails if orig_node has no router */
  2250. static int batadv_iv_gw_write_buffer_text(struct batadv_priv *bat_priv,
  2251. struct seq_file *seq,
  2252. const struct batadv_gw_node *gw_node)
  2253. {
  2254. struct batadv_gw_node *curr_gw;
  2255. struct batadv_neigh_node *router;
  2256. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  2257. int ret = -1;
  2258. router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
  2259. if (!router)
  2260. goto out;
  2261. router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
  2262. if (!router_ifinfo)
  2263. goto out;
  2264. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  2265. seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
  2266. (curr_gw == gw_node ? "=>" : " "),
  2267. gw_node->orig_node->orig,
  2268. router_ifinfo->bat_iv.tq_avg, router->addr,
  2269. router->if_incoming->net_dev->name,
  2270. gw_node->bandwidth_down / 10,
  2271. gw_node->bandwidth_down % 10,
  2272. gw_node->bandwidth_up / 10,
  2273. gw_node->bandwidth_up % 10);
  2274. ret = seq_has_overflowed(seq) ? -1 : 0;
  2275. if (curr_gw)
  2276. batadv_gw_node_put(curr_gw);
  2277. out:
  2278. if (router_ifinfo)
  2279. batadv_neigh_ifinfo_put(router_ifinfo);
  2280. if (router)
  2281. batadv_neigh_node_put(router);
  2282. return ret;
  2283. }
  2284. static void batadv_iv_gw_print(struct batadv_priv *bat_priv,
  2285. struct seq_file *seq)
  2286. {
  2287. struct batadv_gw_node *gw_node;
  2288. int gw_count = 0;
  2289. seq_puts(seq,
  2290. " Gateway (#/255) Nexthop [outgoingIF]: advertised uplink bandwidth\n");
  2291. rcu_read_lock();
  2292. hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
  2293. /* fails if orig_node has no router */
  2294. if (batadv_iv_gw_write_buffer_text(bat_priv, seq, gw_node) < 0)
  2295. continue;
  2296. gw_count++;
  2297. }
  2298. rcu_read_unlock();
  2299. if (gw_count == 0)
  2300. seq_puts(seq, "No gateways in range ...\n");
  2301. }
  2302. #endif
  2303. /**
  2304. * batadv_iv_gw_dump_entry - Dump a gateway into a message
  2305. * @msg: Netlink message to dump into
  2306. * @portid: Port making netlink request
  2307. * @seq: Sequence number of netlink message
  2308. * @bat_priv: The bat priv with all the soft interface information
  2309. * @gw_node: Gateway to be dumped
  2310. *
  2311. * Return: Error code, or 0 on success
  2312. */
  2313. static int batadv_iv_gw_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
  2314. struct batadv_priv *bat_priv,
  2315. struct batadv_gw_node *gw_node)
  2316. {
  2317. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  2318. struct batadv_neigh_node *router;
  2319. struct batadv_gw_node *curr_gw;
  2320. int ret = -EINVAL;
  2321. void *hdr;
  2322. router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
  2323. if (!router)
  2324. goto out;
  2325. router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
  2326. if (!router_ifinfo)
  2327. goto out;
  2328. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  2329. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
  2330. NLM_F_MULTI, BATADV_CMD_GET_GATEWAYS);
  2331. if (!hdr) {
  2332. ret = -ENOBUFS;
  2333. goto out;
  2334. }
  2335. ret = -EMSGSIZE;
  2336. if (curr_gw == gw_node)
  2337. if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) {
  2338. genlmsg_cancel(msg, hdr);
  2339. goto out;
  2340. }
  2341. if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
  2342. gw_node->orig_node->orig) ||
  2343. nla_put_u8(msg, BATADV_ATTR_TQ, router_ifinfo->bat_iv.tq_avg) ||
  2344. nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN,
  2345. router->addr) ||
  2346. nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
  2347. router->if_incoming->net_dev->name) ||
  2348. nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN,
  2349. gw_node->bandwidth_down) ||
  2350. nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP,
  2351. gw_node->bandwidth_up)) {
  2352. genlmsg_cancel(msg, hdr);
  2353. goto out;
  2354. }
  2355. genlmsg_end(msg, hdr);
  2356. ret = 0;
  2357. out:
  2358. if (router_ifinfo)
  2359. batadv_neigh_ifinfo_put(router_ifinfo);
  2360. if (router)
  2361. batadv_neigh_node_put(router);
  2362. return ret;
  2363. }
  2364. /**
  2365. * batadv_iv_gw_dump - Dump gateways into a message
  2366. * @msg: Netlink message to dump into
  2367. * @cb: Control block containing additional options
  2368. * @bat_priv: The bat priv with all the soft interface information
  2369. */
  2370. static void batadv_iv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb,
  2371. struct batadv_priv *bat_priv)
  2372. {
  2373. int portid = NETLINK_CB(cb->skb).portid;
  2374. struct batadv_gw_node *gw_node;
  2375. int idx_skip = cb->args[0];
  2376. int idx = 0;
  2377. rcu_read_lock();
  2378. hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
  2379. if (idx++ < idx_skip)
  2380. continue;
  2381. if (batadv_iv_gw_dump_entry(msg, portid, cb->nlh->nlmsg_seq,
  2382. bat_priv, gw_node)) {
  2383. idx_skip = idx - 1;
  2384. goto unlock;
  2385. }
  2386. }
  2387. idx_skip = idx;
  2388. unlock:
  2389. rcu_read_unlock();
  2390. cb->args[0] = idx_skip;
  2391. }
  2392. static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
  2393. .name = "BATMAN_IV",
  2394. .iface = {
  2395. .activate = batadv_iv_iface_activate,
  2396. .enable = batadv_iv_ogm_iface_enable,
  2397. .disable = batadv_iv_ogm_iface_disable,
  2398. .update_mac = batadv_iv_ogm_iface_update_mac,
  2399. .primary_set = batadv_iv_ogm_primary_iface_set,
  2400. },
  2401. .neigh = {
  2402. .cmp = batadv_iv_ogm_neigh_cmp,
  2403. .is_similar_or_better = batadv_iv_ogm_neigh_is_sob,
  2404. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  2405. .print = batadv_iv_neigh_print,
  2406. #endif
  2407. .dump = batadv_iv_ogm_neigh_dump,
  2408. },
  2409. .orig = {
  2410. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  2411. .print = batadv_iv_ogm_orig_print,
  2412. #endif
  2413. .dump = batadv_iv_ogm_orig_dump,
  2414. .free = batadv_iv_ogm_orig_free,
  2415. .add_if = batadv_iv_ogm_orig_add_if,
  2416. .del_if = batadv_iv_ogm_orig_del_if,
  2417. },
  2418. .gw = {
  2419. .get_best_gw_node = batadv_iv_gw_get_best_gw_node,
  2420. .is_eligible = batadv_iv_gw_is_eligible,
  2421. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  2422. .print = batadv_iv_gw_print,
  2423. #endif
  2424. .dump = batadv_iv_gw_dump,
  2425. },
  2426. };
  2427. int __init batadv_iv_init(void)
  2428. {
  2429. int ret;
  2430. /* batman originator packet */
  2431. ret = batadv_recv_handler_register(BATADV_IV_OGM,
  2432. batadv_iv_ogm_receive);
  2433. if (ret < 0)
  2434. goto out;
  2435. ret = batadv_algo_register(&batadv_batman_iv);
  2436. if (ret < 0)
  2437. goto handler_unregister;
  2438. goto out;
  2439. handler_unregister:
  2440. batadv_recv_handler_unregister(BATADV_IV_OGM);
  2441. out:
  2442. return ret;
  2443. }