bat_iv_ogm.c 83 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866
  1. /* Copyright (C) 2007-2017 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. struct sk_buff *skb;
  583. unsigned char *skb_buff;
  584. unsigned int skb_size;
  585. atomic_t *queue_left = own_packet ? NULL : &bat_priv->batman_queue_left;
  586. if (atomic_read(&bat_priv->aggregated_ogms) &&
  587. packet_len < BATADV_MAX_AGGREGATION_BYTES)
  588. skb_size = BATADV_MAX_AGGREGATION_BYTES;
  589. else
  590. skb_size = packet_len;
  591. skb_size += ETH_HLEN;
  592. skb = netdev_alloc_skb_ip_align(NULL, skb_size);
  593. if (!skb)
  594. return;
  595. forw_packet_aggr = batadv_forw_packet_alloc(if_incoming, if_outgoing,
  596. queue_left, bat_priv, skb);
  597. if (!forw_packet_aggr) {
  598. kfree_skb(skb);
  599. return;
  600. }
  601. forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
  602. skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
  603. skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
  604. forw_packet_aggr->packet_len = packet_len;
  605. memcpy(skb_buff, packet_buff, packet_len);
  606. forw_packet_aggr->own = own_packet;
  607. forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
  608. forw_packet_aggr->send_time = send_time;
  609. /* save packet direct link flag status */
  610. if (direct_link)
  611. forw_packet_aggr->direct_link_flags |= 1;
  612. INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
  613. batadv_iv_send_outstanding_bat_ogm_packet);
  614. batadv_forw_packet_ogmv1_queue(bat_priv, forw_packet_aggr, send_time);
  615. }
  616. /* aggregate a new packet into the existing ogm packet */
  617. static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
  618. const unsigned char *packet_buff,
  619. int packet_len, bool direct_link)
  620. {
  621. unsigned long new_direct_link_flag;
  622. skb_put_data(forw_packet_aggr->skb, packet_buff, packet_len);
  623. forw_packet_aggr->packet_len += packet_len;
  624. forw_packet_aggr->num_packets++;
  625. /* save packet direct link flag status */
  626. if (direct_link) {
  627. new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
  628. forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
  629. }
  630. }
  631. /**
  632. * batadv_iv_ogm_queue_add - queue up an OGM for transmission
  633. * @bat_priv: the bat priv with all the soft interface information
  634. * @packet_buff: pointer to the OGM
  635. * @packet_len: (total) length of the OGM
  636. * @if_incoming: interface where the packet was received
  637. * @if_outgoing: interface for which the retransmission should be considered
  638. * @own_packet: true if it is a self-generated ogm
  639. * @send_time: timestamp (jiffies) when the packet is to be sent
  640. */
  641. static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
  642. unsigned char *packet_buff,
  643. int packet_len,
  644. struct batadv_hard_iface *if_incoming,
  645. struct batadv_hard_iface *if_outgoing,
  646. int own_packet, unsigned long send_time)
  647. {
  648. /* _aggr -> pointer to the packet we want to aggregate with
  649. * _pos -> pointer to the position in the queue
  650. */
  651. struct batadv_forw_packet *forw_packet_aggr = NULL;
  652. struct batadv_forw_packet *forw_packet_pos = NULL;
  653. struct batadv_ogm_packet *batadv_ogm_packet;
  654. bool direct_link;
  655. unsigned long max_aggregation_jiffies;
  656. batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
  657. direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK);
  658. max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
  659. /* find position for the packet in the forward queue */
  660. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  661. /* own packets are not to be aggregated */
  662. if (atomic_read(&bat_priv->aggregated_ogms) && !own_packet) {
  663. hlist_for_each_entry(forw_packet_pos,
  664. &bat_priv->forw_bat_list, list) {
  665. if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
  666. bat_priv, packet_len,
  667. send_time, direct_link,
  668. if_incoming,
  669. if_outgoing,
  670. forw_packet_pos)) {
  671. forw_packet_aggr = forw_packet_pos;
  672. break;
  673. }
  674. }
  675. }
  676. /* nothing to aggregate with - either aggregation disabled or no
  677. * suitable aggregation packet found
  678. */
  679. if (!forw_packet_aggr) {
  680. /* the following section can run without the lock */
  681. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  682. /* if we could not aggregate this packet with one of the others
  683. * we hold it back for a while, so that it might be aggregated
  684. * later on
  685. */
  686. if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
  687. send_time += max_aggregation_jiffies;
  688. batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
  689. send_time, direct_link,
  690. if_incoming, if_outgoing,
  691. own_packet);
  692. } else {
  693. batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
  694. packet_len, direct_link);
  695. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  696. }
  697. }
  698. static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
  699. const struct ethhdr *ethhdr,
  700. struct batadv_ogm_packet *batadv_ogm_packet,
  701. bool is_single_hop_neigh,
  702. bool is_from_best_next_hop,
  703. struct batadv_hard_iface *if_incoming,
  704. struct batadv_hard_iface *if_outgoing)
  705. {
  706. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  707. u16 tvlv_len;
  708. if (batadv_ogm_packet->ttl <= 1) {
  709. batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
  710. return;
  711. }
  712. if (!is_from_best_next_hop) {
  713. /* Mark the forwarded packet when it is not coming from our
  714. * best next hop. We still need to forward the packet for our
  715. * neighbor link quality detection to work in case the packet
  716. * originated from a single hop neighbor. Otherwise we can
  717. * simply drop the ogm.
  718. */
  719. if (is_single_hop_neigh)
  720. batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
  721. else
  722. return;
  723. }
  724. tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
  725. batadv_ogm_packet->ttl--;
  726. ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source);
  727. /* apply hop penalty */
  728. batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
  729. bat_priv);
  730. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  731. "Forwarding packet: tq: %i, ttl: %i\n",
  732. batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
  733. if (is_single_hop_neigh)
  734. batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
  735. else
  736. batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
  737. batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
  738. BATADV_OGM_HLEN + tvlv_len,
  739. if_incoming, if_outgoing, 0,
  740. batadv_iv_ogm_fwd_send_time());
  741. }
  742. /**
  743. * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for
  744. * the given interface
  745. * @hard_iface: the interface for which the windows have to be shifted
  746. */
  747. static void
  748. batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
  749. {
  750. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  751. struct batadv_hashtable *hash = bat_priv->orig_hash;
  752. struct hlist_head *head;
  753. struct batadv_orig_node *orig_node;
  754. unsigned long *word;
  755. u32 i;
  756. size_t word_index;
  757. u8 *w;
  758. int if_num;
  759. for (i = 0; i < hash->size; i++) {
  760. head = &hash->table[i];
  761. rcu_read_lock();
  762. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  763. spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  764. word_index = hard_iface->if_num * BATADV_NUM_WORDS;
  765. word = &orig_node->bat_iv.bcast_own[word_index];
  766. batadv_bit_get_packet(bat_priv, word, 1, 0);
  767. if_num = hard_iface->if_num;
  768. w = &orig_node->bat_iv.bcast_own_sum[if_num];
  769. *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
  770. spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  771. }
  772. rcu_read_unlock();
  773. }
  774. }
  775. static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
  776. {
  777. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  778. unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
  779. struct batadv_ogm_packet *batadv_ogm_packet;
  780. struct batadv_hard_iface *primary_if, *tmp_hard_iface;
  781. int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
  782. u32 seqno;
  783. u16 tvlv_len = 0;
  784. unsigned long send_time;
  785. if ((hard_iface->if_status == BATADV_IF_NOT_IN_USE) ||
  786. (hard_iface->if_status == BATADV_IF_TO_BE_REMOVED))
  787. return;
  788. /* the interface gets activated here to avoid race conditions between
  789. * the moment of activating the interface in
  790. * hardif_activate_interface() where the originator mac is set and
  791. * outdated packets (especially uninitialized mac addresses) in the
  792. * packet queue
  793. */
  794. if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
  795. hard_iface->if_status = BATADV_IF_ACTIVE;
  796. primary_if = batadv_primary_if_get_selected(bat_priv);
  797. if (hard_iface == primary_if) {
  798. /* tt changes have to be committed before the tvlv data is
  799. * appended as it may alter the tt tvlv container
  800. */
  801. batadv_tt_local_commit_changes(bat_priv);
  802. tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
  803. ogm_buff_len,
  804. BATADV_OGM_HLEN);
  805. }
  806. batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
  807. batadv_ogm_packet->tvlv_len = htons(tvlv_len);
  808. /* change sequence number to network order */
  809. seqno = (u32)atomic_read(&hard_iface->bat_iv.ogm_seqno);
  810. batadv_ogm_packet->seqno = htonl(seqno);
  811. atomic_inc(&hard_iface->bat_iv.ogm_seqno);
  812. batadv_iv_ogm_slide_own_bcast_window(hard_iface);
  813. send_time = batadv_iv_ogm_emit_send_time(bat_priv);
  814. if (hard_iface != primary_if) {
  815. /* OGMs from secondary interfaces are only scheduled on their
  816. * respective interfaces.
  817. */
  818. batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len,
  819. hard_iface, hard_iface, 1, send_time);
  820. goto out;
  821. }
  822. /* OGMs from primary interfaces are scheduled on all
  823. * interfaces.
  824. */
  825. rcu_read_lock();
  826. list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) {
  827. if (tmp_hard_iface->soft_iface != hard_iface->soft_iface)
  828. continue;
  829. if (!kref_get_unless_zero(&tmp_hard_iface->refcount))
  830. continue;
  831. batadv_iv_ogm_queue_add(bat_priv, *ogm_buff,
  832. *ogm_buff_len, hard_iface,
  833. tmp_hard_iface, 1, send_time);
  834. batadv_hardif_put(tmp_hard_iface);
  835. }
  836. rcu_read_unlock();
  837. out:
  838. if (primary_if)
  839. batadv_hardif_put(primary_if);
  840. }
  841. /**
  842. * batadv_iv_ogm_orig_update - use OGM to update corresponding data in an
  843. * originator
  844. * @bat_priv: the bat priv with all the soft interface information
  845. * @orig_node: the orig node who originally emitted the ogm packet
  846. * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
  847. * @ethhdr: Ethernet header of the OGM
  848. * @batadv_ogm_packet: the ogm packet
  849. * @if_incoming: interface where the packet was received
  850. * @if_outgoing: interface for which the retransmission should be considered
  851. * @dup_status: the duplicate status of this ogm packet.
  852. */
  853. static void
  854. batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
  855. struct batadv_orig_node *orig_node,
  856. struct batadv_orig_ifinfo *orig_ifinfo,
  857. const struct ethhdr *ethhdr,
  858. const struct batadv_ogm_packet *batadv_ogm_packet,
  859. struct batadv_hard_iface *if_incoming,
  860. struct batadv_hard_iface *if_outgoing,
  861. enum batadv_dup_status dup_status)
  862. {
  863. struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
  864. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  865. struct batadv_neigh_node *neigh_node = NULL;
  866. struct batadv_neigh_node *tmp_neigh_node = NULL;
  867. struct batadv_neigh_node *router = NULL;
  868. struct batadv_orig_node *orig_node_tmp;
  869. int if_num;
  870. u8 sum_orig, sum_neigh;
  871. u8 *neigh_addr;
  872. u8 tq_avg;
  873. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  874. "%s(): Searching and updating originator entry of received packet\n",
  875. __func__);
  876. rcu_read_lock();
  877. hlist_for_each_entry_rcu(tmp_neigh_node,
  878. &orig_node->neigh_list, list) {
  879. neigh_addr = tmp_neigh_node->addr;
  880. if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
  881. tmp_neigh_node->if_incoming == if_incoming &&
  882. kref_get_unless_zero(&tmp_neigh_node->refcount)) {
  883. if (WARN(neigh_node, "too many matching neigh_nodes"))
  884. batadv_neigh_node_put(neigh_node);
  885. neigh_node = tmp_neigh_node;
  886. continue;
  887. }
  888. if (dup_status != BATADV_NO_DUP)
  889. continue;
  890. /* only update the entry for this outgoing interface */
  891. neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
  892. if_outgoing);
  893. if (!neigh_ifinfo)
  894. continue;
  895. spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
  896. batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
  897. &neigh_ifinfo->bat_iv.tq_index, 0);
  898. tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
  899. neigh_ifinfo->bat_iv.tq_avg = tq_avg;
  900. spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
  901. batadv_neigh_ifinfo_put(neigh_ifinfo);
  902. neigh_ifinfo = NULL;
  903. }
  904. if (!neigh_node) {
  905. struct batadv_orig_node *orig_tmp;
  906. orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
  907. if (!orig_tmp)
  908. goto unlock;
  909. neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
  910. ethhdr->h_source,
  911. orig_node, orig_tmp);
  912. batadv_orig_node_put(orig_tmp);
  913. if (!neigh_node)
  914. goto unlock;
  915. } else {
  916. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  917. "Updating existing last-hop neighbor of originator\n");
  918. }
  919. rcu_read_unlock();
  920. neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
  921. if (!neigh_ifinfo)
  922. goto out;
  923. neigh_node->last_seen = jiffies;
  924. spin_lock_bh(&neigh_node->ifinfo_lock);
  925. batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
  926. &neigh_ifinfo->bat_iv.tq_index,
  927. batadv_ogm_packet->tq);
  928. tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
  929. neigh_ifinfo->bat_iv.tq_avg = tq_avg;
  930. spin_unlock_bh(&neigh_node->ifinfo_lock);
  931. if (dup_status == BATADV_NO_DUP) {
  932. orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
  933. neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
  934. }
  935. /* if this neighbor already is our next hop there is nothing
  936. * to change
  937. */
  938. router = batadv_orig_router_get(orig_node, if_outgoing);
  939. if (router == neigh_node)
  940. goto out;
  941. if (router) {
  942. router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
  943. if (!router_ifinfo)
  944. goto out;
  945. /* if this neighbor does not offer a better TQ we won't
  946. * consider it
  947. */
  948. if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
  949. goto out;
  950. }
  951. /* if the TQ is the same and the link not more symmetric we
  952. * won't consider it either
  953. */
  954. if (router_ifinfo &&
  955. neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) {
  956. orig_node_tmp = router->orig_node;
  957. spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
  958. if_num = router->if_incoming->if_num;
  959. sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
  960. spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
  961. orig_node_tmp = neigh_node->orig_node;
  962. spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
  963. if_num = neigh_node->if_incoming->if_num;
  964. sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
  965. spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
  966. if (sum_orig >= sum_neigh)
  967. goto out;
  968. }
  969. batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
  970. goto out;
  971. unlock:
  972. rcu_read_unlock();
  973. out:
  974. if (neigh_node)
  975. batadv_neigh_node_put(neigh_node);
  976. if (router)
  977. batadv_neigh_node_put(router);
  978. if (neigh_ifinfo)
  979. batadv_neigh_ifinfo_put(neigh_ifinfo);
  980. if (router_ifinfo)
  981. batadv_neigh_ifinfo_put(router_ifinfo);
  982. }
  983. /**
  984. * batadv_iv_ogm_calc_tq - calculate tq for current received ogm packet
  985. * @orig_node: the orig node who originally emitted the ogm packet
  986. * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
  987. * @batadv_ogm_packet: the ogm packet
  988. * @if_incoming: interface where the packet was received
  989. * @if_outgoing: interface for which the retransmission should be considered
  990. *
  991. * Return: true if the link can be considered bidirectional, false otherwise
  992. */
  993. static bool batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
  994. struct batadv_orig_node *orig_neigh_node,
  995. struct batadv_ogm_packet *batadv_ogm_packet,
  996. struct batadv_hard_iface *if_incoming,
  997. struct batadv_hard_iface *if_outgoing)
  998. {
  999. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  1000. struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
  1001. struct batadv_neigh_ifinfo *neigh_ifinfo;
  1002. u8 total_count;
  1003. u8 orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
  1004. unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
  1005. int if_num;
  1006. unsigned int tq_asym_penalty, inv_asym_penalty;
  1007. unsigned int combined_tq;
  1008. unsigned int tq_iface_penalty;
  1009. bool ret = false;
  1010. /* find corresponding one hop neighbor */
  1011. rcu_read_lock();
  1012. hlist_for_each_entry_rcu(tmp_neigh_node,
  1013. &orig_neigh_node->neigh_list, list) {
  1014. if (!batadv_compare_eth(tmp_neigh_node->addr,
  1015. orig_neigh_node->orig))
  1016. continue;
  1017. if (tmp_neigh_node->if_incoming != if_incoming)
  1018. continue;
  1019. if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
  1020. continue;
  1021. neigh_node = tmp_neigh_node;
  1022. break;
  1023. }
  1024. rcu_read_unlock();
  1025. if (!neigh_node)
  1026. neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
  1027. orig_neigh_node->orig,
  1028. orig_neigh_node,
  1029. orig_neigh_node);
  1030. if (!neigh_node)
  1031. goto out;
  1032. /* if orig_node is direct neighbor update neigh_node last_seen */
  1033. if (orig_node == orig_neigh_node)
  1034. neigh_node->last_seen = jiffies;
  1035. orig_node->last_seen = jiffies;
  1036. /* find packet count of corresponding one hop neighbor */
  1037. spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  1038. if_num = if_incoming->if_num;
  1039. orig_eq_count = orig_neigh_node->bat_iv.bcast_own_sum[if_num];
  1040. neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
  1041. if (neigh_ifinfo) {
  1042. neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
  1043. batadv_neigh_ifinfo_put(neigh_ifinfo);
  1044. } else {
  1045. neigh_rq_count = 0;
  1046. }
  1047. spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  1048. /* pay attention to not get a value bigger than 100 % */
  1049. if (orig_eq_count > neigh_rq_count)
  1050. total_count = neigh_rq_count;
  1051. else
  1052. total_count = orig_eq_count;
  1053. /* if we have too few packets (too less data) we set tq_own to zero
  1054. * if we receive too few packets it is not considered bidirectional
  1055. */
  1056. if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
  1057. neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
  1058. tq_own = 0;
  1059. else
  1060. /* neigh_node->real_packet_count is never zero as we
  1061. * only purge old information when getting new
  1062. * information
  1063. */
  1064. tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
  1065. /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
  1066. * affect the nearly-symmetric links only a little, but
  1067. * punishes asymmetric links more. This will give a value
  1068. * between 0 and TQ_MAX_VALUE
  1069. */
  1070. neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
  1071. neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
  1072. neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
  1073. BATADV_TQ_LOCAL_WINDOW_SIZE *
  1074. BATADV_TQ_LOCAL_WINDOW_SIZE;
  1075. inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
  1076. inv_asym_penalty /= neigh_rq_max_cube;
  1077. tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
  1078. /* penalize if the OGM is forwarded on the same interface. WiFi
  1079. * interfaces and other half duplex devices suffer from throughput
  1080. * drops as they can't send and receive at the same time.
  1081. */
  1082. tq_iface_penalty = BATADV_TQ_MAX_VALUE;
  1083. if (if_outgoing && (if_incoming == if_outgoing) &&
  1084. batadv_is_wifi_hardif(if_outgoing))
  1085. tq_iface_penalty = batadv_hop_penalty(BATADV_TQ_MAX_VALUE,
  1086. bat_priv);
  1087. combined_tq = batadv_ogm_packet->tq *
  1088. tq_own *
  1089. tq_asym_penalty *
  1090. tq_iface_penalty;
  1091. combined_tq /= BATADV_TQ_MAX_VALUE *
  1092. BATADV_TQ_MAX_VALUE *
  1093. BATADV_TQ_MAX_VALUE;
  1094. batadv_ogm_packet->tq = combined_tq;
  1095. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1096. "bidirectional: orig = %pM neigh = %pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, iface_penalty: %3i, total tq: %3i, if_incoming = %s, if_outgoing = %s\n",
  1097. orig_node->orig, orig_neigh_node->orig, total_count,
  1098. neigh_rq_count, tq_own, tq_asym_penalty, tq_iface_penalty,
  1099. batadv_ogm_packet->tq, if_incoming->net_dev->name,
  1100. if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
  1101. /* if link has the minimum required transmission quality
  1102. * consider it bidirectional
  1103. */
  1104. if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
  1105. ret = true;
  1106. out:
  1107. if (neigh_node)
  1108. batadv_neigh_node_put(neigh_node);
  1109. return ret;
  1110. }
  1111. /**
  1112. * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces,
  1113. * adjust the sequence number and find out whether it is a duplicate
  1114. * @ethhdr: ethernet header of the packet
  1115. * @batadv_ogm_packet: OGM packet to be considered
  1116. * @if_incoming: interface on which the OGM packet was received
  1117. * @if_outgoing: interface for which the retransmission should be considered
  1118. *
  1119. * Return: duplicate status as enum batadv_dup_status
  1120. */
  1121. static enum batadv_dup_status
  1122. batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
  1123. const struct batadv_ogm_packet *batadv_ogm_packet,
  1124. const struct batadv_hard_iface *if_incoming,
  1125. struct batadv_hard_iface *if_outgoing)
  1126. {
  1127. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  1128. struct batadv_orig_node *orig_node;
  1129. struct batadv_orig_ifinfo *orig_ifinfo = NULL;
  1130. struct batadv_neigh_node *neigh_node;
  1131. struct batadv_neigh_ifinfo *neigh_ifinfo;
  1132. bool is_dup;
  1133. s32 seq_diff;
  1134. bool need_update = false;
  1135. int set_mark;
  1136. enum batadv_dup_status ret = BATADV_NO_DUP;
  1137. u32 seqno = ntohl(batadv_ogm_packet->seqno);
  1138. u8 *neigh_addr;
  1139. u8 packet_count;
  1140. unsigned long *bitmap;
  1141. orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
  1142. if (!orig_node)
  1143. return BATADV_NO_DUP;
  1144. orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
  1145. if (WARN_ON(!orig_ifinfo)) {
  1146. batadv_orig_node_put(orig_node);
  1147. return 0;
  1148. }
  1149. spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  1150. seq_diff = seqno - orig_ifinfo->last_real_seqno;
  1151. /* signalize caller that the packet is to be dropped. */
  1152. if (!hlist_empty(&orig_node->neigh_list) &&
  1153. batadv_window_protected(bat_priv, seq_diff,
  1154. BATADV_TQ_LOCAL_WINDOW_SIZE,
  1155. &orig_ifinfo->batman_seqno_reset, NULL)) {
  1156. ret = BATADV_PROTECTED;
  1157. goto out;
  1158. }
  1159. rcu_read_lock();
  1160. hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
  1161. neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
  1162. if_outgoing);
  1163. if (!neigh_ifinfo)
  1164. continue;
  1165. neigh_addr = neigh_node->addr;
  1166. is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
  1167. orig_ifinfo->last_real_seqno,
  1168. seqno);
  1169. if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
  1170. neigh_node->if_incoming == if_incoming) {
  1171. set_mark = 1;
  1172. if (is_dup)
  1173. ret = BATADV_NEIGH_DUP;
  1174. } else {
  1175. set_mark = 0;
  1176. if (is_dup && (ret != BATADV_NEIGH_DUP))
  1177. ret = BATADV_ORIG_DUP;
  1178. }
  1179. /* if the window moved, set the update flag. */
  1180. bitmap = neigh_ifinfo->bat_iv.real_bits;
  1181. need_update |= batadv_bit_get_packet(bat_priv, bitmap,
  1182. seq_diff, set_mark);
  1183. packet_count = bitmap_weight(bitmap,
  1184. BATADV_TQ_LOCAL_WINDOW_SIZE);
  1185. neigh_ifinfo->bat_iv.real_packet_count = packet_count;
  1186. batadv_neigh_ifinfo_put(neigh_ifinfo);
  1187. }
  1188. rcu_read_unlock();
  1189. if (need_update) {
  1190. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1191. "%s updating last_seqno: old %u, new %u\n",
  1192. if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
  1193. orig_ifinfo->last_real_seqno, seqno);
  1194. orig_ifinfo->last_real_seqno = seqno;
  1195. }
  1196. out:
  1197. spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  1198. batadv_orig_node_put(orig_node);
  1199. batadv_orig_ifinfo_put(orig_ifinfo);
  1200. return ret;
  1201. }
  1202. /**
  1203. * batadv_iv_ogm_process_per_outif - process a batman iv OGM for an outgoing if
  1204. * @skb: the skb containing the OGM
  1205. * @ogm_offset: offset from skb->data to start of ogm header
  1206. * @orig_node: the (cached) orig node for the originator of this OGM
  1207. * @if_incoming: the interface where this packet was received
  1208. * @if_outgoing: the interface for which the packet should be considered
  1209. */
  1210. static void
  1211. batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
  1212. struct batadv_orig_node *orig_node,
  1213. struct batadv_hard_iface *if_incoming,
  1214. struct batadv_hard_iface *if_outgoing)
  1215. {
  1216. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  1217. struct batadv_hardif_neigh_node *hardif_neigh = NULL;
  1218. struct batadv_neigh_node *router = NULL;
  1219. struct batadv_neigh_node *router_router = NULL;
  1220. struct batadv_orig_node *orig_neigh_node;
  1221. struct batadv_orig_ifinfo *orig_ifinfo;
  1222. struct batadv_neigh_node *orig_neigh_router = NULL;
  1223. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  1224. struct batadv_ogm_packet *ogm_packet;
  1225. enum batadv_dup_status dup_status;
  1226. bool is_from_best_next_hop = false;
  1227. bool is_single_hop_neigh = false;
  1228. bool sameseq, similar_ttl;
  1229. struct sk_buff *skb_priv;
  1230. struct ethhdr *ethhdr;
  1231. u8 *prev_sender;
  1232. bool is_bidirect;
  1233. /* create a private copy of the skb, as some functions change tq value
  1234. * and/or flags.
  1235. */
  1236. skb_priv = skb_copy(skb, GFP_ATOMIC);
  1237. if (!skb_priv)
  1238. return;
  1239. ethhdr = eth_hdr(skb_priv);
  1240. ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
  1241. dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
  1242. if_incoming, if_outgoing);
  1243. if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
  1244. is_single_hop_neigh = true;
  1245. if (dup_status == BATADV_PROTECTED) {
  1246. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1247. "Drop packet: packet within seqno protection time (sender: %pM)\n",
  1248. ethhdr->h_source);
  1249. goto out;
  1250. }
  1251. if (ogm_packet->tq == 0) {
  1252. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1253. "Drop packet: originator packet with tq equal 0\n");
  1254. goto out;
  1255. }
  1256. if (is_single_hop_neigh) {
  1257. hardif_neigh = batadv_hardif_neigh_get(if_incoming,
  1258. ethhdr->h_source);
  1259. if (hardif_neigh)
  1260. hardif_neigh->last_seen = jiffies;
  1261. }
  1262. router = batadv_orig_router_get(orig_node, if_outgoing);
  1263. if (router) {
  1264. router_router = batadv_orig_router_get(router->orig_node,
  1265. if_outgoing);
  1266. router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
  1267. }
  1268. if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
  1269. (batadv_compare_eth(router->addr, ethhdr->h_source)))
  1270. is_from_best_next_hop = true;
  1271. prev_sender = ogm_packet->prev_sender;
  1272. /* avoid temporary routing loops */
  1273. if (router && router_router &&
  1274. (batadv_compare_eth(router->addr, prev_sender)) &&
  1275. !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
  1276. (batadv_compare_eth(router->addr, router_router->addr))) {
  1277. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1278. "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
  1279. ethhdr->h_source);
  1280. goto out;
  1281. }
  1282. if (if_outgoing == BATADV_IF_DEFAULT)
  1283. batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
  1284. /* if sender is a direct neighbor the sender mac equals
  1285. * originator mac
  1286. */
  1287. if (is_single_hop_neigh)
  1288. orig_neigh_node = orig_node;
  1289. else
  1290. orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
  1291. ethhdr->h_source);
  1292. if (!orig_neigh_node)
  1293. goto out;
  1294. /* Update nc_nodes of the originator */
  1295. batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
  1296. ogm_packet, is_single_hop_neigh);
  1297. orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
  1298. if_outgoing);
  1299. /* drop packet if sender is not a direct neighbor and if we
  1300. * don't route towards it
  1301. */
  1302. if (!is_single_hop_neigh && (!orig_neigh_router)) {
  1303. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1304. "Drop packet: OGM via unknown neighbor!\n");
  1305. goto out_neigh;
  1306. }
  1307. is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
  1308. ogm_packet, if_incoming,
  1309. if_outgoing);
  1310. /* update ranking if it is not a duplicate or has the same
  1311. * seqno and similar ttl as the non-duplicate
  1312. */
  1313. orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
  1314. if (!orig_ifinfo)
  1315. goto out_neigh;
  1316. sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
  1317. similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
  1318. if (is_bidirect && ((dup_status == BATADV_NO_DUP) ||
  1319. (sameseq && similar_ttl))) {
  1320. batadv_iv_ogm_orig_update(bat_priv, orig_node,
  1321. orig_ifinfo, ethhdr,
  1322. ogm_packet, if_incoming,
  1323. if_outgoing, dup_status);
  1324. }
  1325. batadv_orig_ifinfo_put(orig_ifinfo);
  1326. /* only forward for specific interface, not for the default one. */
  1327. if (if_outgoing == BATADV_IF_DEFAULT)
  1328. goto out_neigh;
  1329. /* is single hop (direct) neighbor */
  1330. if (is_single_hop_neigh) {
  1331. /* OGMs from secondary interfaces should only scheduled once
  1332. * per interface where it has been received, not multiple times
  1333. */
  1334. if ((ogm_packet->ttl <= 2) &&
  1335. (if_incoming != if_outgoing)) {
  1336. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1337. "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
  1338. goto out_neigh;
  1339. }
  1340. /* mark direct link on incoming interface */
  1341. batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
  1342. is_single_hop_neigh,
  1343. is_from_best_next_hop, if_incoming,
  1344. if_outgoing);
  1345. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1346. "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
  1347. goto out_neigh;
  1348. }
  1349. /* multihop originator */
  1350. if (!is_bidirect) {
  1351. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1352. "Drop packet: not received via bidirectional link\n");
  1353. goto out_neigh;
  1354. }
  1355. if (dup_status == BATADV_NEIGH_DUP) {
  1356. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1357. "Drop packet: duplicate packet received\n");
  1358. goto out_neigh;
  1359. }
  1360. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1361. "Forwarding packet: rebroadcast originator packet\n");
  1362. batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
  1363. is_single_hop_neigh, is_from_best_next_hop,
  1364. if_incoming, if_outgoing);
  1365. out_neigh:
  1366. if ((orig_neigh_node) && (!is_single_hop_neigh))
  1367. batadv_orig_node_put(orig_neigh_node);
  1368. out:
  1369. if (router_ifinfo)
  1370. batadv_neigh_ifinfo_put(router_ifinfo);
  1371. if (router)
  1372. batadv_neigh_node_put(router);
  1373. if (router_router)
  1374. batadv_neigh_node_put(router_router);
  1375. if (orig_neigh_router)
  1376. batadv_neigh_node_put(orig_neigh_router);
  1377. if (hardif_neigh)
  1378. batadv_hardif_neigh_put(hardif_neigh);
  1379. consume_skb(skb_priv);
  1380. }
  1381. /**
  1382. * batadv_iv_ogm_process - process an incoming batman iv OGM
  1383. * @skb: the skb containing the OGM
  1384. * @ogm_offset: offset to the OGM which should be processed (for aggregates)
  1385. * @if_incoming: the interface where this packet was receved
  1386. */
  1387. static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
  1388. struct batadv_hard_iface *if_incoming)
  1389. {
  1390. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  1391. struct batadv_orig_node *orig_neigh_node, *orig_node;
  1392. struct batadv_hard_iface *hard_iface;
  1393. struct batadv_ogm_packet *ogm_packet;
  1394. u32 if_incoming_seqno;
  1395. bool has_directlink_flag;
  1396. struct ethhdr *ethhdr;
  1397. bool is_my_oldorig = false;
  1398. bool is_my_addr = false;
  1399. bool is_my_orig = false;
  1400. ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
  1401. ethhdr = eth_hdr(skb);
  1402. /* Silently drop when the batman packet is actually not a
  1403. * correct packet.
  1404. *
  1405. * This might happen if a packet is padded (e.g. Ethernet has a
  1406. * minimum frame length of 64 byte) and the aggregation interprets
  1407. * it as an additional length.
  1408. *
  1409. * TODO: A more sane solution would be to have a bit in the
  1410. * batadv_ogm_packet to detect whether the packet is the last
  1411. * packet in an aggregation. Here we expect that the padding
  1412. * is always zero (or not 0x01)
  1413. */
  1414. if (ogm_packet->packet_type != BATADV_IV_OGM)
  1415. return;
  1416. /* could be changed by schedule_own_packet() */
  1417. if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
  1418. if (ogm_packet->flags & BATADV_DIRECTLINK)
  1419. has_directlink_flag = true;
  1420. else
  1421. has_directlink_flag = false;
  1422. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1423. "Received BATMAN packet via NB: %pM, IF: %s [%pM] (from OG: %pM, via prev OG: %pM, seqno %u, tq %d, TTL %d, V %d, IDF %d)\n",
  1424. ethhdr->h_source, if_incoming->net_dev->name,
  1425. if_incoming->net_dev->dev_addr, ogm_packet->orig,
  1426. ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
  1427. ogm_packet->tq, ogm_packet->ttl,
  1428. ogm_packet->version, has_directlink_flag);
  1429. rcu_read_lock();
  1430. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  1431. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  1432. continue;
  1433. if (hard_iface->soft_iface != if_incoming->soft_iface)
  1434. continue;
  1435. if (batadv_compare_eth(ethhdr->h_source,
  1436. hard_iface->net_dev->dev_addr))
  1437. is_my_addr = true;
  1438. if (batadv_compare_eth(ogm_packet->orig,
  1439. hard_iface->net_dev->dev_addr))
  1440. is_my_orig = true;
  1441. if (batadv_compare_eth(ogm_packet->prev_sender,
  1442. hard_iface->net_dev->dev_addr))
  1443. is_my_oldorig = true;
  1444. }
  1445. rcu_read_unlock();
  1446. if (is_my_addr) {
  1447. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1448. "Drop packet: received my own broadcast (sender: %pM)\n",
  1449. ethhdr->h_source);
  1450. return;
  1451. }
  1452. if (is_my_orig) {
  1453. unsigned long *word;
  1454. int offset;
  1455. s32 bit_pos;
  1456. s16 if_num;
  1457. u8 *weight;
  1458. orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
  1459. ethhdr->h_source);
  1460. if (!orig_neigh_node)
  1461. return;
  1462. /* neighbor has to indicate direct link and it has to
  1463. * come via the corresponding interface
  1464. * save packet seqno for bidirectional check
  1465. */
  1466. if (has_directlink_flag &&
  1467. batadv_compare_eth(if_incoming->net_dev->dev_addr,
  1468. ogm_packet->orig)) {
  1469. if_num = if_incoming->if_num;
  1470. offset = if_num * BATADV_NUM_WORDS;
  1471. spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
  1472. word = &orig_neigh_node->bat_iv.bcast_own[offset];
  1473. bit_pos = if_incoming_seqno - 2;
  1474. bit_pos -= ntohl(ogm_packet->seqno);
  1475. batadv_set_bit(word, bit_pos);
  1476. weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num];
  1477. *weight = bitmap_weight(word,
  1478. BATADV_TQ_LOCAL_WINDOW_SIZE);
  1479. spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
  1480. }
  1481. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1482. "Drop packet: originator packet from myself (via neighbor)\n");
  1483. batadv_orig_node_put(orig_neigh_node);
  1484. return;
  1485. }
  1486. if (is_my_oldorig) {
  1487. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1488. "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
  1489. ethhdr->h_source);
  1490. return;
  1491. }
  1492. if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
  1493. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1494. "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
  1495. ethhdr->h_source);
  1496. return;
  1497. }
  1498. orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
  1499. if (!orig_node)
  1500. return;
  1501. batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
  1502. if_incoming, BATADV_IF_DEFAULT);
  1503. rcu_read_lock();
  1504. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  1505. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  1506. continue;
  1507. if (hard_iface->soft_iface != bat_priv->soft_iface)
  1508. continue;
  1509. if (!kref_get_unless_zero(&hard_iface->refcount))
  1510. continue;
  1511. batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
  1512. if_incoming, hard_iface);
  1513. batadv_hardif_put(hard_iface);
  1514. }
  1515. rcu_read_unlock();
  1516. batadv_orig_node_put(orig_node);
  1517. }
  1518. static void batadv_iv_send_outstanding_bat_ogm_packet(struct work_struct *work)
  1519. {
  1520. struct delayed_work *delayed_work;
  1521. struct batadv_forw_packet *forw_packet;
  1522. struct batadv_priv *bat_priv;
  1523. bool dropped = false;
  1524. delayed_work = to_delayed_work(work);
  1525. forw_packet = container_of(delayed_work, struct batadv_forw_packet,
  1526. delayed_work);
  1527. bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
  1528. if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) {
  1529. dropped = true;
  1530. goto out;
  1531. }
  1532. batadv_iv_ogm_emit(forw_packet);
  1533. /* we have to have at least one packet in the queue to determine the
  1534. * queues wake up time unless we are shutting down.
  1535. *
  1536. * only re-schedule if this is the "original" copy, e.g. the OGM of the
  1537. * primary interface should only be rescheduled once per period, but
  1538. * this function will be called for the forw_packet instances of the
  1539. * other secondary interfaces as well.
  1540. */
  1541. if (forw_packet->own &&
  1542. forw_packet->if_incoming == forw_packet->if_outgoing)
  1543. batadv_iv_ogm_schedule(forw_packet->if_incoming);
  1544. out:
  1545. /* do we get something for free()? */
  1546. if (batadv_forw_packet_steal(forw_packet,
  1547. &bat_priv->forw_bat_list_lock))
  1548. batadv_forw_packet_free(forw_packet, dropped);
  1549. }
  1550. static int batadv_iv_ogm_receive(struct sk_buff *skb,
  1551. struct batadv_hard_iface *if_incoming)
  1552. {
  1553. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  1554. struct batadv_ogm_packet *ogm_packet;
  1555. u8 *packet_pos;
  1556. int ogm_offset;
  1557. bool res;
  1558. int ret = NET_RX_DROP;
  1559. res = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
  1560. if (!res)
  1561. goto free_skb;
  1562. /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
  1563. * that does not have B.A.T.M.A.N. IV enabled ?
  1564. */
  1565. if (bat_priv->algo_ops->iface.enable != batadv_iv_ogm_iface_enable)
  1566. goto free_skb;
  1567. batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
  1568. batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
  1569. skb->len + ETH_HLEN);
  1570. ogm_offset = 0;
  1571. ogm_packet = (struct batadv_ogm_packet *)skb->data;
  1572. /* unpack the aggregated packets and process them one by one */
  1573. while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
  1574. ogm_packet->tvlv_len)) {
  1575. batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
  1576. ogm_offset += BATADV_OGM_HLEN;
  1577. ogm_offset += ntohs(ogm_packet->tvlv_len);
  1578. packet_pos = skb->data + ogm_offset;
  1579. ogm_packet = (struct batadv_ogm_packet *)packet_pos;
  1580. }
  1581. ret = NET_RX_SUCCESS;
  1582. free_skb:
  1583. if (ret == NET_RX_SUCCESS)
  1584. consume_skb(skb);
  1585. else
  1586. kfree_skb(skb);
  1587. return ret;
  1588. }
  1589. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  1590. /**
  1591. * batadv_iv_ogm_orig_print_neigh - print neighbors for the originator table
  1592. * @orig_node: the orig_node for which the neighbors are printed
  1593. * @if_outgoing: outgoing interface for these entries
  1594. * @seq: debugfs table seq_file struct
  1595. *
  1596. * Must be called while holding an rcu lock.
  1597. */
  1598. static void
  1599. batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node,
  1600. struct batadv_hard_iface *if_outgoing,
  1601. struct seq_file *seq)
  1602. {
  1603. struct batadv_neigh_node *neigh_node;
  1604. struct batadv_neigh_ifinfo *n_ifinfo;
  1605. hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
  1606. n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
  1607. if (!n_ifinfo)
  1608. continue;
  1609. seq_printf(seq, " %pM (%3i)",
  1610. neigh_node->addr,
  1611. n_ifinfo->bat_iv.tq_avg);
  1612. batadv_neigh_ifinfo_put(n_ifinfo);
  1613. }
  1614. }
  1615. /**
  1616. * batadv_iv_ogm_orig_print - print the originator table
  1617. * @bat_priv: the bat priv with all the soft interface information
  1618. * @seq: debugfs table seq_file struct
  1619. * @if_outgoing: the outgoing interface for which this should be printed
  1620. */
  1621. static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
  1622. struct seq_file *seq,
  1623. struct batadv_hard_iface *if_outgoing)
  1624. {
  1625. struct batadv_neigh_node *neigh_node;
  1626. struct batadv_hashtable *hash = bat_priv->orig_hash;
  1627. int last_seen_msecs, last_seen_secs;
  1628. struct batadv_orig_node *orig_node;
  1629. struct batadv_neigh_ifinfo *n_ifinfo;
  1630. unsigned long last_seen_jiffies;
  1631. struct hlist_head *head;
  1632. int batman_count = 0;
  1633. u32 i;
  1634. seq_puts(seq,
  1635. " Originator last-seen (#/255) Nexthop [outgoingIF]: Potential nexthops ...\n");
  1636. for (i = 0; i < hash->size; i++) {
  1637. head = &hash->table[i];
  1638. rcu_read_lock();
  1639. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  1640. neigh_node = batadv_orig_router_get(orig_node,
  1641. if_outgoing);
  1642. if (!neigh_node)
  1643. continue;
  1644. n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
  1645. if_outgoing);
  1646. if (!n_ifinfo)
  1647. goto next;
  1648. if (n_ifinfo->bat_iv.tq_avg == 0)
  1649. goto next;
  1650. last_seen_jiffies = jiffies - orig_node->last_seen;
  1651. last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
  1652. last_seen_secs = last_seen_msecs / 1000;
  1653. last_seen_msecs = last_seen_msecs % 1000;
  1654. seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
  1655. orig_node->orig, last_seen_secs,
  1656. last_seen_msecs, n_ifinfo->bat_iv.tq_avg,
  1657. neigh_node->addr,
  1658. neigh_node->if_incoming->net_dev->name);
  1659. batadv_iv_ogm_orig_print_neigh(orig_node, if_outgoing,
  1660. seq);
  1661. seq_putc(seq, '\n');
  1662. batman_count++;
  1663. next:
  1664. batadv_neigh_node_put(neigh_node);
  1665. if (n_ifinfo)
  1666. batadv_neigh_ifinfo_put(n_ifinfo);
  1667. }
  1668. rcu_read_unlock();
  1669. }
  1670. if (batman_count == 0)
  1671. seq_puts(seq, "No batman nodes in range ...\n");
  1672. }
  1673. #endif
  1674. /**
  1675. * batadv_iv_ogm_neigh_get_tq_avg - Get the TQ average for a neighbour on a
  1676. * given outgoing interface.
  1677. * @neigh_node: Neighbour of interest
  1678. * @if_outgoing: Outgoing interface of interest
  1679. * @tq_avg: Pointer of where to store the TQ average
  1680. *
  1681. * Return: False if no average TQ available, otherwise true.
  1682. */
  1683. static bool
  1684. batadv_iv_ogm_neigh_get_tq_avg(struct batadv_neigh_node *neigh_node,
  1685. struct batadv_hard_iface *if_outgoing,
  1686. u8 *tq_avg)
  1687. {
  1688. struct batadv_neigh_ifinfo *n_ifinfo;
  1689. n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
  1690. if (!n_ifinfo)
  1691. return false;
  1692. *tq_avg = n_ifinfo->bat_iv.tq_avg;
  1693. batadv_neigh_ifinfo_put(n_ifinfo);
  1694. return true;
  1695. }
  1696. /**
  1697. * batadv_iv_ogm_orig_dump_subentry - Dump an originator subentry into a
  1698. * message
  1699. * @msg: Netlink message to dump into
  1700. * @portid: Port making netlink request
  1701. * @seq: Sequence number of netlink message
  1702. * @bat_priv: The bat priv with all the soft interface information
  1703. * @if_outgoing: Limit dump to entries with this outgoing interface
  1704. * @orig_node: Originator to dump
  1705. * @neigh_node: Single hops neighbour
  1706. * @best: Is the best originator
  1707. *
  1708. * Return: Error code, or 0 on success
  1709. */
  1710. static int
  1711. batadv_iv_ogm_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
  1712. struct batadv_priv *bat_priv,
  1713. struct batadv_hard_iface *if_outgoing,
  1714. struct batadv_orig_node *orig_node,
  1715. struct batadv_neigh_node *neigh_node,
  1716. bool best)
  1717. {
  1718. void *hdr;
  1719. u8 tq_avg;
  1720. unsigned int last_seen_msecs;
  1721. last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
  1722. if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node, if_outgoing, &tq_avg))
  1723. return 0;
  1724. if (if_outgoing != BATADV_IF_DEFAULT &&
  1725. if_outgoing != neigh_node->if_incoming)
  1726. return 0;
  1727. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
  1728. NLM_F_MULTI, BATADV_CMD_GET_ORIGINATORS);
  1729. if (!hdr)
  1730. return -ENOBUFS;
  1731. if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
  1732. orig_node->orig) ||
  1733. nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
  1734. neigh_node->addr) ||
  1735. nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
  1736. neigh_node->if_incoming->net_dev->ifindex) ||
  1737. nla_put_u8(msg, BATADV_ATTR_TQ, tq_avg) ||
  1738. nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
  1739. last_seen_msecs))
  1740. goto nla_put_failure;
  1741. if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
  1742. goto nla_put_failure;
  1743. genlmsg_end(msg, hdr);
  1744. return 0;
  1745. nla_put_failure:
  1746. genlmsg_cancel(msg, hdr);
  1747. return -EMSGSIZE;
  1748. }
  1749. /**
  1750. * batadv_iv_ogm_orig_dump_entry - Dump an originator entry into a message
  1751. * @msg: Netlink message to dump into
  1752. * @portid: Port making netlink request
  1753. * @seq: Sequence number of netlink message
  1754. * @bat_priv: The bat priv with all the soft interface information
  1755. * @if_outgoing: Limit dump to entries with this outgoing interface
  1756. * @orig_node: Originator to dump
  1757. * @sub_s: Number of sub entries to skip
  1758. *
  1759. * This function assumes the caller holds rcu_read_lock().
  1760. *
  1761. * Return: Error code, or 0 on success
  1762. */
  1763. static int
  1764. batadv_iv_ogm_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
  1765. struct batadv_priv *bat_priv,
  1766. struct batadv_hard_iface *if_outgoing,
  1767. struct batadv_orig_node *orig_node, int *sub_s)
  1768. {
  1769. struct batadv_neigh_node *neigh_node_best;
  1770. struct batadv_neigh_node *neigh_node;
  1771. int sub = 0;
  1772. bool best;
  1773. u8 tq_avg_best;
  1774. neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
  1775. if (!neigh_node_best)
  1776. goto out;
  1777. if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node_best, if_outgoing,
  1778. &tq_avg_best))
  1779. goto out;
  1780. if (tq_avg_best == 0)
  1781. goto out;
  1782. hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
  1783. if (sub++ < *sub_s)
  1784. continue;
  1785. best = (neigh_node == neigh_node_best);
  1786. if (batadv_iv_ogm_orig_dump_subentry(msg, portid, seq,
  1787. bat_priv, if_outgoing,
  1788. orig_node, neigh_node,
  1789. best)) {
  1790. batadv_neigh_node_put(neigh_node_best);
  1791. *sub_s = sub - 1;
  1792. return -EMSGSIZE;
  1793. }
  1794. }
  1795. out:
  1796. if (neigh_node_best)
  1797. batadv_neigh_node_put(neigh_node_best);
  1798. *sub_s = 0;
  1799. return 0;
  1800. }
  1801. /**
  1802. * batadv_iv_ogm_orig_dump_bucket - Dump an originator bucket into a
  1803. * message
  1804. * @msg: Netlink message to dump into
  1805. * @portid: Port making netlink request
  1806. * @seq: Sequence number of netlink message
  1807. * @bat_priv: The bat priv with all the soft interface information
  1808. * @if_outgoing: Limit dump to entries with this outgoing interface
  1809. * @head: Bucket to be dumped
  1810. * @idx_s: Number of entries to be skipped
  1811. * @sub: Number of sub entries to be skipped
  1812. *
  1813. * Return: Error code, or 0 on success
  1814. */
  1815. static int
  1816. batadv_iv_ogm_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
  1817. struct batadv_priv *bat_priv,
  1818. struct batadv_hard_iface *if_outgoing,
  1819. struct hlist_head *head, int *idx_s, int *sub)
  1820. {
  1821. struct batadv_orig_node *orig_node;
  1822. int idx = 0;
  1823. rcu_read_lock();
  1824. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  1825. if (idx++ < *idx_s)
  1826. continue;
  1827. if (batadv_iv_ogm_orig_dump_entry(msg, portid, seq, bat_priv,
  1828. if_outgoing, orig_node,
  1829. sub)) {
  1830. rcu_read_unlock();
  1831. *idx_s = idx - 1;
  1832. return -EMSGSIZE;
  1833. }
  1834. }
  1835. rcu_read_unlock();
  1836. *idx_s = 0;
  1837. *sub = 0;
  1838. return 0;
  1839. }
  1840. /**
  1841. * batadv_iv_ogm_orig_dump - Dump the originators into a message
  1842. * @msg: Netlink message to dump into
  1843. * @cb: Control block containing additional options
  1844. * @bat_priv: The bat priv with all the soft interface information
  1845. * @if_outgoing: Limit dump to entries with this outgoing interface
  1846. */
  1847. static void
  1848. batadv_iv_ogm_orig_dump(struct sk_buff *msg, struct netlink_callback *cb,
  1849. struct batadv_priv *bat_priv,
  1850. struct batadv_hard_iface *if_outgoing)
  1851. {
  1852. struct batadv_hashtable *hash = bat_priv->orig_hash;
  1853. struct hlist_head *head;
  1854. int bucket = cb->args[0];
  1855. int idx = cb->args[1];
  1856. int sub = cb->args[2];
  1857. int portid = NETLINK_CB(cb->skb).portid;
  1858. while (bucket < hash->size) {
  1859. head = &hash->table[bucket];
  1860. if (batadv_iv_ogm_orig_dump_bucket(msg, portid,
  1861. cb->nlh->nlmsg_seq,
  1862. bat_priv, if_outgoing, head,
  1863. &idx, &sub))
  1864. break;
  1865. bucket++;
  1866. }
  1867. cb->args[0] = bucket;
  1868. cb->args[1] = idx;
  1869. cb->args[2] = sub;
  1870. }
  1871. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  1872. /**
  1873. * batadv_iv_hardif_neigh_print - print a single hop neighbour node
  1874. * @seq: neighbour table seq_file struct
  1875. * @hardif_neigh: hardif neighbour information
  1876. */
  1877. static void
  1878. batadv_iv_hardif_neigh_print(struct seq_file *seq,
  1879. struct batadv_hardif_neigh_node *hardif_neigh)
  1880. {
  1881. int last_secs, last_msecs;
  1882. last_secs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) / 1000;
  1883. last_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen) % 1000;
  1884. seq_printf(seq, " %10s %pM %4i.%03is\n",
  1885. hardif_neigh->if_incoming->net_dev->name,
  1886. hardif_neigh->addr, last_secs, last_msecs);
  1887. }
  1888. /**
  1889. * batadv_iv_ogm_neigh_print - print the single hop neighbour list
  1890. * @bat_priv: the bat priv with all the soft interface information
  1891. * @seq: neighbour table seq_file struct
  1892. */
  1893. static void batadv_iv_neigh_print(struct batadv_priv *bat_priv,
  1894. struct seq_file *seq)
  1895. {
  1896. struct net_device *net_dev = (struct net_device *)seq->private;
  1897. struct batadv_hardif_neigh_node *hardif_neigh;
  1898. struct batadv_hard_iface *hard_iface;
  1899. int batman_count = 0;
  1900. seq_puts(seq, " IF Neighbor last-seen\n");
  1901. rcu_read_lock();
  1902. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  1903. if (hard_iface->soft_iface != net_dev)
  1904. continue;
  1905. hlist_for_each_entry_rcu(hardif_neigh,
  1906. &hard_iface->neigh_list, list) {
  1907. batadv_iv_hardif_neigh_print(seq, hardif_neigh);
  1908. batman_count++;
  1909. }
  1910. }
  1911. rcu_read_unlock();
  1912. if (batman_count == 0)
  1913. seq_puts(seq, "No batman nodes in range ...\n");
  1914. }
  1915. #endif
  1916. /**
  1917. * batadv_iv_ogm_neigh_diff - calculate tq difference of two neighbors
  1918. * @neigh1: the first neighbor object of the comparison
  1919. * @if_outgoing1: outgoing interface for the first neighbor
  1920. * @neigh2: the second neighbor object of the comparison
  1921. * @if_outgoing2: outgoing interface for the second neighbor
  1922. * @diff: pointer to integer receiving the calculated difference
  1923. *
  1924. * The content of *@diff is only valid when this function returns true.
  1925. * It is less, equal to or greater than 0 if the metric via neigh1 is lower,
  1926. * the same as or higher than the metric via neigh2
  1927. *
  1928. * Return: true when the difference could be calculated, false otherwise
  1929. */
  1930. static bool batadv_iv_ogm_neigh_diff(struct batadv_neigh_node *neigh1,
  1931. struct batadv_hard_iface *if_outgoing1,
  1932. struct batadv_neigh_node *neigh2,
  1933. struct batadv_hard_iface *if_outgoing2,
  1934. int *diff)
  1935. {
  1936. struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
  1937. u8 tq1, tq2;
  1938. bool ret = true;
  1939. neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
  1940. neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
  1941. if (!neigh1_ifinfo || !neigh2_ifinfo) {
  1942. ret = false;
  1943. goto out;
  1944. }
  1945. tq1 = neigh1_ifinfo->bat_iv.tq_avg;
  1946. tq2 = neigh2_ifinfo->bat_iv.tq_avg;
  1947. *diff = (int)tq1 - (int)tq2;
  1948. out:
  1949. if (neigh1_ifinfo)
  1950. batadv_neigh_ifinfo_put(neigh1_ifinfo);
  1951. if (neigh2_ifinfo)
  1952. batadv_neigh_ifinfo_put(neigh2_ifinfo);
  1953. return ret;
  1954. }
  1955. /**
  1956. * batadv_iv_ogm_neigh_dump_neigh - Dump a neighbour into a netlink message
  1957. * @msg: Netlink message to dump into
  1958. * @portid: Port making netlink request
  1959. * @seq: Sequence number of netlink message
  1960. * @hardif_neigh: Neighbour to be dumped
  1961. *
  1962. * Return: Error code, or 0 on success
  1963. */
  1964. static int
  1965. batadv_iv_ogm_neigh_dump_neigh(struct sk_buff *msg, u32 portid, u32 seq,
  1966. struct batadv_hardif_neigh_node *hardif_neigh)
  1967. {
  1968. void *hdr;
  1969. unsigned int last_seen_msecs;
  1970. last_seen_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen);
  1971. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
  1972. NLM_F_MULTI, BATADV_CMD_GET_NEIGHBORS);
  1973. if (!hdr)
  1974. return -ENOBUFS;
  1975. if (nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
  1976. hardif_neigh->addr) ||
  1977. nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
  1978. hardif_neigh->if_incoming->net_dev->ifindex) ||
  1979. nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
  1980. last_seen_msecs))
  1981. goto nla_put_failure;
  1982. genlmsg_end(msg, hdr);
  1983. return 0;
  1984. nla_put_failure:
  1985. genlmsg_cancel(msg, hdr);
  1986. return -EMSGSIZE;
  1987. }
  1988. /**
  1989. * batadv_iv_ogm_neigh_dump_hardif - Dump the neighbours of a hard interface
  1990. * into a message
  1991. * @msg: Netlink message to dump into
  1992. * @portid: Port making netlink request
  1993. * @seq: Sequence number of netlink message
  1994. * @bat_priv: The bat priv with all the soft interface information
  1995. * @hard_iface: Hard interface to dump the neighbours for
  1996. * @idx_s: Number of entries to skip
  1997. *
  1998. * This function assumes the caller holds rcu_read_lock().
  1999. *
  2000. * Return: Error code, or 0 on success
  2001. */
  2002. static int
  2003. batadv_iv_ogm_neigh_dump_hardif(struct sk_buff *msg, u32 portid, u32 seq,
  2004. struct batadv_priv *bat_priv,
  2005. struct batadv_hard_iface *hard_iface,
  2006. int *idx_s)
  2007. {
  2008. struct batadv_hardif_neigh_node *hardif_neigh;
  2009. int idx = 0;
  2010. hlist_for_each_entry_rcu(hardif_neigh,
  2011. &hard_iface->neigh_list, list) {
  2012. if (idx++ < *idx_s)
  2013. continue;
  2014. if (batadv_iv_ogm_neigh_dump_neigh(msg, portid, seq,
  2015. hardif_neigh)) {
  2016. *idx_s = idx - 1;
  2017. return -EMSGSIZE;
  2018. }
  2019. }
  2020. *idx_s = 0;
  2021. return 0;
  2022. }
  2023. /**
  2024. * batadv_iv_ogm_neigh_dump - Dump the neighbours into a message
  2025. * @msg: Netlink message to dump into
  2026. * @cb: Control block containing additional options
  2027. * @bat_priv: The bat priv with all the soft interface information
  2028. * @single_hardif: Limit dump to this hard interfaace
  2029. */
  2030. static void
  2031. batadv_iv_ogm_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb,
  2032. struct batadv_priv *bat_priv,
  2033. struct batadv_hard_iface *single_hardif)
  2034. {
  2035. struct batadv_hard_iface *hard_iface;
  2036. int i_hardif = 0;
  2037. int i_hardif_s = cb->args[0];
  2038. int idx = cb->args[1];
  2039. int portid = NETLINK_CB(cb->skb).portid;
  2040. rcu_read_lock();
  2041. if (single_hardif) {
  2042. if (i_hardif_s == 0) {
  2043. if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
  2044. cb->nlh->nlmsg_seq,
  2045. bat_priv,
  2046. single_hardif,
  2047. &idx) == 0)
  2048. i_hardif++;
  2049. }
  2050. } else {
  2051. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list,
  2052. list) {
  2053. if (hard_iface->soft_iface != bat_priv->soft_iface)
  2054. continue;
  2055. if (i_hardif++ < i_hardif_s)
  2056. continue;
  2057. if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
  2058. cb->nlh->nlmsg_seq,
  2059. bat_priv,
  2060. hard_iface, &idx)) {
  2061. i_hardif--;
  2062. break;
  2063. }
  2064. }
  2065. }
  2066. rcu_read_unlock();
  2067. cb->args[0] = i_hardif;
  2068. cb->args[1] = idx;
  2069. }
  2070. /**
  2071. * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors
  2072. * @neigh1: the first neighbor object of the comparison
  2073. * @if_outgoing1: outgoing interface for the first neighbor
  2074. * @neigh2: the second neighbor object of the comparison
  2075. * @if_outgoing2: outgoing interface for the second neighbor
  2076. *
  2077. * Return: a value less, equal to or greater than 0 if the metric via neigh1 is
  2078. * lower, the same as or higher than the metric via neigh2
  2079. */
  2080. static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
  2081. struct batadv_hard_iface *if_outgoing1,
  2082. struct batadv_neigh_node *neigh2,
  2083. struct batadv_hard_iface *if_outgoing2)
  2084. {
  2085. bool ret;
  2086. int diff;
  2087. ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
  2088. if_outgoing2, &diff);
  2089. if (!ret)
  2090. return 0;
  2091. return diff;
  2092. }
  2093. /**
  2094. * batadv_iv_ogm_neigh_is_sob - check if neigh1 is similarly good or better
  2095. * than neigh2 from the metric prospective
  2096. * @neigh1: the first neighbor object of the comparison
  2097. * @if_outgoing1: outgoing interface for the first neighbor
  2098. * @neigh2: the second neighbor object of the comparison
  2099. * @if_outgoing2: outgoing interface for the second neighbor
  2100. *
  2101. * Return: true if the metric via neigh1 is equally good or better than
  2102. * the metric via neigh2, false otherwise.
  2103. */
  2104. static bool
  2105. batadv_iv_ogm_neigh_is_sob(struct batadv_neigh_node *neigh1,
  2106. struct batadv_hard_iface *if_outgoing1,
  2107. struct batadv_neigh_node *neigh2,
  2108. struct batadv_hard_iface *if_outgoing2)
  2109. {
  2110. bool ret;
  2111. int diff;
  2112. ret = batadv_iv_ogm_neigh_diff(neigh1, if_outgoing1, neigh2,
  2113. if_outgoing2, &diff);
  2114. if (!ret)
  2115. return false;
  2116. ret = diff > -BATADV_TQ_SIMILARITY_THRESHOLD;
  2117. return ret;
  2118. }
  2119. static void batadv_iv_iface_activate(struct batadv_hard_iface *hard_iface)
  2120. {
  2121. /* begin scheduling originator messages on that interface */
  2122. batadv_iv_ogm_schedule(hard_iface);
  2123. }
  2124. /**
  2125. * batadv_iv_init_sel_class - initialize GW selection class
  2126. * @bat_priv: the bat priv with all the soft interface information
  2127. */
  2128. static void batadv_iv_init_sel_class(struct batadv_priv *bat_priv)
  2129. {
  2130. /* set default TQ difference threshold to 20 */
  2131. atomic_set(&bat_priv->gw.sel_class, 20);
  2132. }
  2133. static struct batadv_gw_node *
  2134. batadv_iv_gw_get_best_gw_node(struct batadv_priv *bat_priv)
  2135. {
  2136. struct batadv_neigh_node *router;
  2137. struct batadv_neigh_ifinfo *router_ifinfo;
  2138. struct batadv_gw_node *gw_node, *curr_gw = NULL;
  2139. u64 max_gw_factor = 0;
  2140. u64 tmp_gw_factor = 0;
  2141. u8 max_tq = 0;
  2142. u8 tq_avg;
  2143. struct batadv_orig_node *orig_node;
  2144. rcu_read_lock();
  2145. hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
  2146. orig_node = gw_node->orig_node;
  2147. router = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
  2148. if (!router)
  2149. continue;
  2150. router_ifinfo = batadv_neigh_ifinfo_get(router,
  2151. BATADV_IF_DEFAULT);
  2152. if (!router_ifinfo)
  2153. goto next;
  2154. if (!kref_get_unless_zero(&gw_node->refcount))
  2155. goto next;
  2156. tq_avg = router_ifinfo->bat_iv.tq_avg;
  2157. switch (atomic_read(&bat_priv->gw.sel_class)) {
  2158. case 1: /* fast connection */
  2159. tmp_gw_factor = tq_avg * tq_avg;
  2160. tmp_gw_factor *= gw_node->bandwidth_down;
  2161. tmp_gw_factor *= 100 * 100;
  2162. tmp_gw_factor >>= 18;
  2163. if ((tmp_gw_factor > max_gw_factor) ||
  2164. ((tmp_gw_factor == max_gw_factor) &&
  2165. (tq_avg > max_tq))) {
  2166. if (curr_gw)
  2167. batadv_gw_node_put(curr_gw);
  2168. curr_gw = gw_node;
  2169. kref_get(&curr_gw->refcount);
  2170. }
  2171. break;
  2172. default: /* 2: stable connection (use best statistic)
  2173. * 3: fast-switch (use best statistic but change as
  2174. * soon as a better gateway appears)
  2175. * XX: late-switch (use best statistic but change as
  2176. * soon as a better gateway appears which has
  2177. * $routing_class more tq points)
  2178. */
  2179. if (tq_avg > max_tq) {
  2180. if (curr_gw)
  2181. batadv_gw_node_put(curr_gw);
  2182. curr_gw = gw_node;
  2183. kref_get(&curr_gw->refcount);
  2184. }
  2185. break;
  2186. }
  2187. if (tq_avg > max_tq)
  2188. max_tq = tq_avg;
  2189. if (tmp_gw_factor > max_gw_factor)
  2190. max_gw_factor = tmp_gw_factor;
  2191. batadv_gw_node_put(gw_node);
  2192. next:
  2193. batadv_neigh_node_put(router);
  2194. if (router_ifinfo)
  2195. batadv_neigh_ifinfo_put(router_ifinfo);
  2196. }
  2197. rcu_read_unlock();
  2198. return curr_gw;
  2199. }
  2200. static bool batadv_iv_gw_is_eligible(struct batadv_priv *bat_priv,
  2201. struct batadv_orig_node *curr_gw_orig,
  2202. struct batadv_orig_node *orig_node)
  2203. {
  2204. struct batadv_neigh_ifinfo *router_orig_ifinfo = NULL;
  2205. struct batadv_neigh_ifinfo *router_gw_ifinfo = NULL;
  2206. struct batadv_neigh_node *router_gw = NULL;
  2207. struct batadv_neigh_node *router_orig = NULL;
  2208. u8 gw_tq_avg, orig_tq_avg;
  2209. bool ret = false;
  2210. /* dynamic re-election is performed only on fast or late switch */
  2211. if (atomic_read(&bat_priv->gw.sel_class) <= 2)
  2212. return false;
  2213. router_gw = batadv_orig_router_get(curr_gw_orig, BATADV_IF_DEFAULT);
  2214. if (!router_gw) {
  2215. ret = true;
  2216. goto out;
  2217. }
  2218. router_gw_ifinfo = batadv_neigh_ifinfo_get(router_gw,
  2219. BATADV_IF_DEFAULT);
  2220. if (!router_gw_ifinfo) {
  2221. ret = true;
  2222. goto out;
  2223. }
  2224. router_orig = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT);
  2225. if (!router_orig)
  2226. goto out;
  2227. router_orig_ifinfo = batadv_neigh_ifinfo_get(router_orig,
  2228. BATADV_IF_DEFAULT);
  2229. if (!router_orig_ifinfo)
  2230. goto out;
  2231. gw_tq_avg = router_gw_ifinfo->bat_iv.tq_avg;
  2232. orig_tq_avg = router_orig_ifinfo->bat_iv.tq_avg;
  2233. /* the TQ value has to be better */
  2234. if (orig_tq_avg < gw_tq_avg)
  2235. goto out;
  2236. /* if the routing class is greater than 3 the value tells us how much
  2237. * greater the TQ value of the new gateway must be
  2238. */
  2239. if ((atomic_read(&bat_priv->gw.sel_class) > 3) &&
  2240. (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw.sel_class)))
  2241. goto out;
  2242. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  2243. "Restarting gateway selection: better gateway found (tq curr: %i, tq new: %i)\n",
  2244. gw_tq_avg, orig_tq_avg);
  2245. ret = true;
  2246. out:
  2247. if (router_gw_ifinfo)
  2248. batadv_neigh_ifinfo_put(router_gw_ifinfo);
  2249. if (router_orig_ifinfo)
  2250. batadv_neigh_ifinfo_put(router_orig_ifinfo);
  2251. if (router_gw)
  2252. batadv_neigh_node_put(router_gw);
  2253. if (router_orig)
  2254. batadv_neigh_node_put(router_orig);
  2255. return ret;
  2256. }
  2257. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  2258. /* fails if orig_node has no router */
  2259. static int batadv_iv_gw_write_buffer_text(struct batadv_priv *bat_priv,
  2260. struct seq_file *seq,
  2261. const struct batadv_gw_node *gw_node)
  2262. {
  2263. struct batadv_gw_node *curr_gw;
  2264. struct batadv_neigh_node *router;
  2265. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  2266. int ret = -1;
  2267. router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
  2268. if (!router)
  2269. goto out;
  2270. router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
  2271. if (!router_ifinfo)
  2272. goto out;
  2273. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  2274. seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %u.%u/%u.%u MBit\n",
  2275. (curr_gw == gw_node ? "=>" : " "),
  2276. gw_node->orig_node->orig,
  2277. router_ifinfo->bat_iv.tq_avg, router->addr,
  2278. router->if_incoming->net_dev->name,
  2279. gw_node->bandwidth_down / 10,
  2280. gw_node->bandwidth_down % 10,
  2281. gw_node->bandwidth_up / 10,
  2282. gw_node->bandwidth_up % 10);
  2283. ret = seq_has_overflowed(seq) ? -1 : 0;
  2284. if (curr_gw)
  2285. batadv_gw_node_put(curr_gw);
  2286. out:
  2287. if (router_ifinfo)
  2288. batadv_neigh_ifinfo_put(router_ifinfo);
  2289. if (router)
  2290. batadv_neigh_node_put(router);
  2291. return ret;
  2292. }
  2293. static void batadv_iv_gw_print(struct batadv_priv *bat_priv,
  2294. struct seq_file *seq)
  2295. {
  2296. struct batadv_gw_node *gw_node;
  2297. int gw_count = 0;
  2298. seq_puts(seq,
  2299. " Gateway (#/255) Nexthop [outgoingIF]: advertised uplink bandwidth\n");
  2300. rcu_read_lock();
  2301. hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
  2302. /* fails if orig_node has no router */
  2303. if (batadv_iv_gw_write_buffer_text(bat_priv, seq, gw_node) < 0)
  2304. continue;
  2305. gw_count++;
  2306. }
  2307. rcu_read_unlock();
  2308. if (gw_count == 0)
  2309. seq_puts(seq, "No gateways in range ...\n");
  2310. }
  2311. #endif
  2312. /**
  2313. * batadv_iv_gw_dump_entry - Dump a gateway into a message
  2314. * @msg: Netlink message to dump into
  2315. * @portid: Port making netlink request
  2316. * @seq: Sequence number of netlink message
  2317. * @bat_priv: The bat priv with all the soft interface information
  2318. * @gw_node: Gateway to be dumped
  2319. *
  2320. * Return: Error code, or 0 on success
  2321. */
  2322. static int batadv_iv_gw_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
  2323. struct batadv_priv *bat_priv,
  2324. struct batadv_gw_node *gw_node)
  2325. {
  2326. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  2327. struct batadv_neigh_node *router;
  2328. struct batadv_gw_node *curr_gw;
  2329. int ret = -EINVAL;
  2330. void *hdr;
  2331. router = batadv_orig_router_get(gw_node->orig_node, BATADV_IF_DEFAULT);
  2332. if (!router)
  2333. goto out;
  2334. router_ifinfo = batadv_neigh_ifinfo_get(router, BATADV_IF_DEFAULT);
  2335. if (!router_ifinfo)
  2336. goto out;
  2337. curr_gw = batadv_gw_get_selected_gw_node(bat_priv);
  2338. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
  2339. NLM_F_MULTI, BATADV_CMD_GET_GATEWAYS);
  2340. if (!hdr) {
  2341. ret = -ENOBUFS;
  2342. goto out;
  2343. }
  2344. ret = -EMSGSIZE;
  2345. if (curr_gw == gw_node)
  2346. if (nla_put_flag(msg, BATADV_ATTR_FLAG_BEST)) {
  2347. genlmsg_cancel(msg, hdr);
  2348. goto out;
  2349. }
  2350. if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
  2351. gw_node->orig_node->orig) ||
  2352. nla_put_u8(msg, BATADV_ATTR_TQ, router_ifinfo->bat_iv.tq_avg) ||
  2353. nla_put(msg, BATADV_ATTR_ROUTER, ETH_ALEN,
  2354. router->addr) ||
  2355. nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
  2356. router->if_incoming->net_dev->name) ||
  2357. nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_DOWN,
  2358. gw_node->bandwidth_down) ||
  2359. nla_put_u32(msg, BATADV_ATTR_BANDWIDTH_UP,
  2360. gw_node->bandwidth_up)) {
  2361. genlmsg_cancel(msg, hdr);
  2362. goto out;
  2363. }
  2364. genlmsg_end(msg, hdr);
  2365. ret = 0;
  2366. out:
  2367. if (router_ifinfo)
  2368. batadv_neigh_ifinfo_put(router_ifinfo);
  2369. if (router)
  2370. batadv_neigh_node_put(router);
  2371. return ret;
  2372. }
  2373. /**
  2374. * batadv_iv_gw_dump - Dump gateways into a message
  2375. * @msg: Netlink message to dump into
  2376. * @cb: Control block containing additional options
  2377. * @bat_priv: The bat priv with all the soft interface information
  2378. */
  2379. static void batadv_iv_gw_dump(struct sk_buff *msg, struct netlink_callback *cb,
  2380. struct batadv_priv *bat_priv)
  2381. {
  2382. int portid = NETLINK_CB(cb->skb).portid;
  2383. struct batadv_gw_node *gw_node;
  2384. int idx_skip = cb->args[0];
  2385. int idx = 0;
  2386. rcu_read_lock();
  2387. hlist_for_each_entry_rcu(gw_node, &bat_priv->gw.gateway_list, list) {
  2388. if (idx++ < idx_skip)
  2389. continue;
  2390. if (batadv_iv_gw_dump_entry(msg, portid, cb->nlh->nlmsg_seq,
  2391. bat_priv, gw_node)) {
  2392. idx_skip = idx - 1;
  2393. goto unlock;
  2394. }
  2395. }
  2396. idx_skip = idx;
  2397. unlock:
  2398. rcu_read_unlock();
  2399. cb->args[0] = idx_skip;
  2400. }
  2401. static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
  2402. .name = "BATMAN_IV",
  2403. .iface = {
  2404. .activate = batadv_iv_iface_activate,
  2405. .enable = batadv_iv_ogm_iface_enable,
  2406. .disable = batadv_iv_ogm_iface_disable,
  2407. .update_mac = batadv_iv_ogm_iface_update_mac,
  2408. .primary_set = batadv_iv_ogm_primary_iface_set,
  2409. },
  2410. .neigh = {
  2411. .cmp = batadv_iv_ogm_neigh_cmp,
  2412. .is_similar_or_better = batadv_iv_ogm_neigh_is_sob,
  2413. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  2414. .print = batadv_iv_neigh_print,
  2415. #endif
  2416. .dump = batadv_iv_ogm_neigh_dump,
  2417. },
  2418. .orig = {
  2419. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  2420. .print = batadv_iv_ogm_orig_print,
  2421. #endif
  2422. .dump = batadv_iv_ogm_orig_dump,
  2423. .free = batadv_iv_ogm_orig_free,
  2424. .add_if = batadv_iv_ogm_orig_add_if,
  2425. .del_if = batadv_iv_ogm_orig_del_if,
  2426. },
  2427. .gw = {
  2428. .init_sel_class = batadv_iv_init_sel_class,
  2429. .get_best_gw_node = batadv_iv_gw_get_best_gw_node,
  2430. .is_eligible = batadv_iv_gw_is_eligible,
  2431. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  2432. .print = batadv_iv_gw_print,
  2433. #endif
  2434. .dump = batadv_iv_gw_dump,
  2435. },
  2436. };
  2437. int __init batadv_iv_init(void)
  2438. {
  2439. int ret;
  2440. /* batman originator packet */
  2441. ret = batadv_recv_handler_register(BATADV_IV_OGM,
  2442. batadv_iv_ogm_receive);
  2443. if (ret < 0)
  2444. goto out;
  2445. ret = batadv_algo_register(&batadv_batman_iv);
  2446. if (ret < 0)
  2447. goto handler_unregister;
  2448. goto out;
  2449. handler_unregister:
  2450. batadv_recv_handler_unregister(BATADV_IV_OGM);
  2451. out:
  2452. return ret;
  2453. }