bat_iv_ogm.c 73 KB

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