bat_iv_ogm.c 80 KB

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