bat_iv_ogm.c 83 KB

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