bat_iv_ogm.c 83 KB

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