bat_iv_ogm.c 82 KB

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