bat_iv_ogm.c 84 KB

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