bat_iv_ogm.c 79 KB

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