multicast.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) B.A.T.M.A.N. contributors:
  3. *
  4. * Linus Lüssing
  5. */
  6. #include "multicast.h"
  7. #include "main.h"
  8. #include <linux/atomic.h>
  9. #include <linux/bitops.h>
  10. #include <linux/bug.h>
  11. #include <linux/byteorder/generic.h>
  12. #include <linux/container_of.h>
  13. #include <linux/err.h>
  14. #include <linux/errno.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/gfp.h>
  17. #include <linux/icmpv6.h>
  18. #include <linux/if_bridge.h>
  19. #include <linux/if_ether.h>
  20. #include <linux/igmp.h>
  21. #include <linux/in.h>
  22. #include <linux/in6.h>
  23. #include <linux/inetdevice.h>
  24. #include <linux/ip.h>
  25. #include <linux/ipv6.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/list.h>
  28. #include <linux/lockdep.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/netlink.h>
  31. #include <linux/printk.h>
  32. #include <linux/rculist.h>
  33. #include <linux/rcupdate.h>
  34. #include <linux/skbuff.h>
  35. #include <linux/slab.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/sprintf.h>
  38. #include <linux/stddef.h>
  39. #include <linux/string.h>
  40. #include <linux/types.h>
  41. #include <linux/workqueue.h>
  42. #include <net/addrconf.h>
  43. #include <net/genetlink.h>
  44. #include <net/if_inet6.h>
  45. #include <net/ip.h>
  46. #include <net/ipv6.h>
  47. #include <net/netlink.h>
  48. #include <uapi/linux/batadv_packet.h>
  49. #include <uapi/linux/batman_adv.h>
  50. #include "bridge_loop_avoidance.h"
  51. #include "hard-interface.h"
  52. #include "hash.h"
  53. #include "log.h"
  54. #include "netlink.h"
  55. #include "send.h"
  56. #include "translation-table.h"
  57. #include "tvlv.h"
  58. static void batadv_mcast_mla_update(struct work_struct *work);
  59. /**
  60. * batadv_mcast_start_timer() - schedule the multicast periodic worker
  61. * @bat_priv: the bat priv with all the mesh interface information
  62. */
  63. static void batadv_mcast_start_timer(struct batadv_priv *bat_priv)
  64. {
  65. queue_delayed_work(batadv_event_workqueue, &bat_priv->mcast.work,
  66. msecs_to_jiffies(BATADV_MCAST_WORK_PERIOD));
  67. }
  68. /**
  69. * batadv_mcast_get_bridge() - get the bridge on top of the meshif if it exists
  70. * @mesh_iface: netdev struct of the mesh interface
  71. *
  72. * If the given mesh interface has a bridge on top then the refcount
  73. * of the according net device is increased.
  74. *
  75. * Return: NULL if no such bridge exists. Otherwise the net device of the
  76. * bridge.
  77. */
  78. static struct net_device *batadv_mcast_get_bridge(struct net_device *mesh_iface)
  79. {
  80. struct net_device *upper = mesh_iface;
  81. rcu_read_lock();
  82. do {
  83. upper = netdev_master_upper_dev_get_rcu(upper);
  84. } while (upper && !netif_is_bridge_master(upper));
  85. dev_hold(upper);
  86. rcu_read_unlock();
  87. return upper;
  88. }
  89. /**
  90. * batadv_mcast_mla_rtr_flags_meshif_get_ipv4() - get mcast router flags from
  91. * node for IPv4
  92. * @dev: the interface to check
  93. *
  94. * Checks the presence of an IPv4 multicast router on this node.
  95. *
  96. * Caller needs to hold rcu read lock.
  97. *
  98. * Return: BATADV_NO_FLAGS if present, BATADV_MCAST_WANT_NO_RTR4 otherwise.
  99. */
  100. static u8 batadv_mcast_mla_rtr_flags_meshif_get_ipv4(struct net_device *dev)
  101. {
  102. struct in_device *in_dev = __in_dev_get_rcu(dev);
  103. if (in_dev && IN_DEV_MFORWARD(in_dev))
  104. return BATADV_NO_FLAGS;
  105. else
  106. return BATADV_MCAST_WANT_NO_RTR4;
  107. }
  108. /**
  109. * batadv_mcast_mla_rtr_flags_meshif_get_ipv6() - get mcast router flags from
  110. * node for IPv6
  111. * @dev: the interface to check
  112. *
  113. * Checks the presence of an IPv6 multicast router on this node.
  114. *
  115. * Caller needs to hold rcu read lock.
  116. *
  117. * Return: BATADV_NO_FLAGS if present, BATADV_MCAST_WANT_NO_RTR6 otherwise.
  118. */
  119. #if IS_ENABLED(CONFIG_IPV6_MROUTE)
  120. static u8 batadv_mcast_mla_rtr_flags_meshif_get_ipv6(struct net_device *dev)
  121. {
  122. struct inet6_dev *in6_dev = __in6_dev_get(dev);
  123. if (in6_dev && atomic_read(&in6_dev->cnf.mc_forwarding))
  124. return BATADV_NO_FLAGS;
  125. else
  126. return BATADV_MCAST_WANT_NO_RTR6;
  127. }
  128. #else
  129. static inline u8
  130. batadv_mcast_mla_rtr_flags_meshif_get_ipv6(struct net_device *dev)
  131. {
  132. return BATADV_MCAST_WANT_NO_RTR6;
  133. }
  134. #endif
  135. /**
  136. * batadv_mcast_mla_rtr_flags_meshif_get() - get mcast router flags from node
  137. * @bat_priv: the bat priv with all the mesh interface information
  138. * @bridge: bridge interface on top of the mesh_iface if present,
  139. * otherwise pass NULL
  140. *
  141. * Checks the presence of IPv4 and IPv6 multicast routers on this
  142. * node.
  143. *
  144. * Return:
  145. * BATADV_NO_FLAGS: Both an IPv4 and IPv6 multicast router is present
  146. * BATADV_MCAST_WANT_NO_RTR4: No IPv4 multicast router is present
  147. * BATADV_MCAST_WANT_NO_RTR6: No IPv6 multicast router is present
  148. * The former two OR'd: no multicast router is present
  149. */
  150. static u8 batadv_mcast_mla_rtr_flags_meshif_get(struct batadv_priv *bat_priv,
  151. struct net_device *bridge)
  152. {
  153. struct net_device *dev = bridge ? bridge : bat_priv->mesh_iface;
  154. u8 flags = BATADV_NO_FLAGS;
  155. rcu_read_lock();
  156. flags |= batadv_mcast_mla_rtr_flags_meshif_get_ipv4(dev);
  157. flags |= batadv_mcast_mla_rtr_flags_meshif_get_ipv6(dev);
  158. rcu_read_unlock();
  159. return flags;
  160. }
  161. /**
  162. * batadv_mcast_mla_rtr_flags_bridge_get() - get mcast router flags from bridge
  163. * @bat_priv: the bat priv with all the mesh interface information
  164. * @bridge: bridge interface on top of the mesh_iface if present,
  165. * otherwise pass NULL
  166. *
  167. * Checks the presence of IPv4 and IPv6 multicast routers behind a bridge.
  168. *
  169. * Return:
  170. * BATADV_NO_FLAGS: Both an IPv4 and IPv6 multicast router is present
  171. * BATADV_MCAST_WANT_NO_RTR4: No IPv4 multicast router is present
  172. * BATADV_MCAST_WANT_NO_RTR6: No IPv6 multicast router is present
  173. * The former two OR'd: no multicast router is present
  174. */
  175. static u8 batadv_mcast_mla_rtr_flags_bridge_get(struct batadv_priv *bat_priv,
  176. struct net_device *bridge)
  177. {
  178. struct net_device *dev = bat_priv->mesh_iface;
  179. u8 flags = BATADV_NO_FLAGS;
  180. if (!bridge)
  181. return BATADV_MCAST_WANT_NO_RTR4 | BATADV_MCAST_WANT_NO_RTR6;
  182. if (!br_multicast_has_router_adjacent(dev, ETH_P_IP))
  183. flags |= BATADV_MCAST_WANT_NO_RTR4;
  184. if (!br_multicast_has_router_adjacent(dev, ETH_P_IPV6))
  185. flags |= BATADV_MCAST_WANT_NO_RTR6;
  186. return flags;
  187. }
  188. /**
  189. * batadv_mcast_mla_rtr_flags_get() - get multicast router flags
  190. * @bat_priv: the bat priv with all the mesh interface information
  191. * @bridge: bridge interface on top of the mesh_iface if present,
  192. * otherwise pass NULL
  193. *
  194. * Checks the presence of IPv4 and IPv6 multicast routers on this
  195. * node or behind its bridge.
  196. *
  197. * Return:
  198. * BATADV_NO_FLAGS: Both an IPv4 and IPv6 multicast router is present
  199. * BATADV_MCAST_WANT_NO_RTR4: No IPv4 multicast router is present
  200. * BATADV_MCAST_WANT_NO_RTR6: No IPv6 multicast router is present
  201. * The former two OR'd: no multicast router is present
  202. */
  203. static u8 batadv_mcast_mla_rtr_flags_get(struct batadv_priv *bat_priv,
  204. struct net_device *bridge)
  205. {
  206. u8 flags = BATADV_MCAST_WANT_NO_RTR4 | BATADV_MCAST_WANT_NO_RTR6;
  207. flags &= batadv_mcast_mla_rtr_flags_meshif_get(bat_priv, bridge);
  208. flags &= batadv_mcast_mla_rtr_flags_bridge_get(bat_priv, bridge);
  209. return flags;
  210. }
  211. /**
  212. * batadv_mcast_mla_forw_flags_get() - get multicast forwarding flags
  213. * @bat_priv: the bat priv with all the mesh interface information
  214. *
  215. * Checks if all active hard interfaces have an MTU larger or equal to 1280
  216. * bytes (IPv6 minimum MTU).
  217. *
  218. * Return: BATADV_MCAST_HAVE_MC_PTYPE_CAPA if yes, BATADV_NO_FLAGS otherwise.
  219. */
  220. static u8 batadv_mcast_mla_forw_flags_get(struct batadv_priv *bat_priv)
  221. {
  222. const struct batadv_hard_iface *hard_iface;
  223. struct list_head *iter;
  224. rcu_read_lock();
  225. netdev_for_each_lower_private_rcu(bat_priv->mesh_iface, hard_iface, iter) {
  226. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  227. continue;
  228. if (hard_iface->net_dev->mtu < IPV6_MIN_MTU) {
  229. rcu_read_unlock();
  230. return BATADV_NO_FLAGS;
  231. }
  232. }
  233. rcu_read_unlock();
  234. return BATADV_MCAST_HAVE_MC_PTYPE_CAPA;
  235. }
  236. /**
  237. * batadv_mcast_mla_flags_get() - get the new multicast flags
  238. * @bat_priv: the bat priv with all the mesh interface information
  239. *
  240. * Return: A set of flags for the current/next TVLV, querier and
  241. * bridge state.
  242. */
  243. static struct batadv_mcast_mla_flags
  244. batadv_mcast_mla_flags_get(struct batadv_priv *bat_priv)
  245. {
  246. struct net_device *dev = bat_priv->mesh_iface;
  247. struct batadv_mcast_querier_state *qr4, *qr6;
  248. struct batadv_mcast_mla_flags mla_flags;
  249. struct net_device *bridge;
  250. bridge = batadv_mcast_get_bridge(dev);
  251. memset(&mla_flags, 0, sizeof(mla_flags));
  252. mla_flags.enabled = 1;
  253. mla_flags.tvlv_flags |= batadv_mcast_mla_rtr_flags_get(bat_priv,
  254. bridge);
  255. mla_flags.tvlv_flags |= batadv_mcast_mla_forw_flags_get(bat_priv);
  256. if (!bridge)
  257. return mla_flags;
  258. dev_put(bridge);
  259. mla_flags.bridged = 1;
  260. qr4 = &mla_flags.querier_ipv4;
  261. qr6 = &mla_flags.querier_ipv6;
  262. if (!IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING))
  263. pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
  264. qr4->exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP);
  265. qr4->shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP);
  266. qr6->exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6);
  267. qr6->shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6);
  268. mla_flags.tvlv_flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES;
  269. /* 1) If no querier exists at all, then multicast listeners on
  270. * our local TT clients behind the bridge will keep silent.
  271. * 2) If the selected querier is on one of our local TT clients,
  272. * behind the bridge, then this querier might shadow multicast
  273. * listeners on our local TT clients, behind this bridge.
  274. *
  275. * In both cases, we will signalize other batman nodes that
  276. * we need all multicast traffic of the according protocol.
  277. */
  278. if (!qr4->exists || qr4->shadowing) {
  279. mla_flags.tvlv_flags |= BATADV_MCAST_WANT_ALL_IPV4;
  280. mla_flags.tvlv_flags &= ~BATADV_MCAST_WANT_NO_RTR4;
  281. }
  282. if (!qr6->exists || qr6->shadowing) {
  283. mla_flags.tvlv_flags |= BATADV_MCAST_WANT_ALL_IPV6;
  284. mla_flags.tvlv_flags &= ~BATADV_MCAST_WANT_NO_RTR6;
  285. }
  286. return mla_flags;
  287. }
  288. /**
  289. * batadv_mcast_mla_is_duplicate() - check whether an address is in a list
  290. * @mcast_addr: the multicast address to check
  291. * @mcast_list: the list with multicast addresses to search in
  292. *
  293. * Return: true if the given address is already in the given list.
  294. * Otherwise returns false.
  295. */
  296. static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
  297. struct hlist_head *mcast_list)
  298. {
  299. struct batadv_hw_addr *mcast_entry;
  300. hlist_for_each_entry(mcast_entry, mcast_list, list)
  301. if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
  302. return true;
  303. return false;
  304. }
  305. /**
  306. * batadv_mcast_mla_meshif_get_ipv4() - get meshif IPv4 multicast listeners
  307. * @dev: the device to collect multicast addresses from
  308. * @mcast_list: a list to put found addresses into
  309. * @flags: flags indicating the new multicast state
  310. *
  311. * Collects multicast addresses of IPv4 multicast listeners residing
  312. * on this kernel on the given mesh interface, dev, in
  313. * the given mcast_list. In general, multicast listeners provided by
  314. * your multicast receiving applications run directly on this node.
  315. *
  316. * Return: -ENOMEM on memory allocation error or the number of
  317. * items added to the mcast_list otherwise.
  318. */
  319. static int
  320. batadv_mcast_mla_meshif_get_ipv4(struct net_device *dev,
  321. struct hlist_head *mcast_list,
  322. struct batadv_mcast_mla_flags *flags)
  323. {
  324. struct batadv_hw_addr *new;
  325. struct in_device *in_dev;
  326. u8 mcast_addr[ETH_ALEN];
  327. struct ip_mc_list *pmc;
  328. int ret = 0;
  329. if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_IPV4)
  330. return 0;
  331. rcu_read_lock();
  332. in_dev = __in_dev_get_rcu(dev);
  333. if (!in_dev) {
  334. rcu_read_unlock();
  335. return 0;
  336. }
  337. for (pmc = rcu_dereference(in_dev->mc_list); pmc;
  338. pmc = rcu_dereference(pmc->next_rcu)) {
  339. if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
  340. ipv4_is_local_multicast(pmc->multiaddr))
  341. continue;
  342. if (!(flags->tvlv_flags & BATADV_MCAST_WANT_NO_RTR4) &&
  343. !ipv4_is_local_multicast(pmc->multiaddr))
  344. continue;
  345. ip_eth_mc_map(pmc->multiaddr, mcast_addr);
  346. if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
  347. continue;
  348. new = kmalloc_obj(*new, GFP_ATOMIC);
  349. if (!new) {
  350. ret = -ENOMEM;
  351. break;
  352. }
  353. ether_addr_copy(new->addr, mcast_addr);
  354. hlist_add_head(&new->list, mcast_list);
  355. ret++;
  356. }
  357. rcu_read_unlock();
  358. return ret;
  359. }
  360. /**
  361. * batadv_mcast_mla_meshif_get_ipv6() - get meshif IPv6 multicast listeners
  362. * @dev: the device to collect multicast addresses from
  363. * @mcast_list: a list to put found addresses into
  364. * @flags: flags indicating the new multicast state
  365. *
  366. * Collects multicast addresses of IPv6 multicast listeners residing
  367. * on this kernel on the given mesh interface, dev, in
  368. * the given mcast_list. In general, multicast listeners provided by
  369. * your multicast receiving applications run directly on this node.
  370. *
  371. * Return: -ENOMEM on memory allocation error or the number of
  372. * items added to the mcast_list otherwise.
  373. */
  374. #if IS_ENABLED(CONFIG_IPV6)
  375. static int
  376. batadv_mcast_mla_meshif_get_ipv6(struct net_device *dev,
  377. struct hlist_head *mcast_list,
  378. struct batadv_mcast_mla_flags *flags)
  379. {
  380. struct batadv_hw_addr *new;
  381. struct inet6_dev *in6_dev;
  382. u8 mcast_addr[ETH_ALEN];
  383. struct ifmcaddr6 *pmc6;
  384. int ret = 0;
  385. if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_IPV6)
  386. return 0;
  387. rcu_read_lock();
  388. in6_dev = __in6_dev_get(dev);
  389. if (!in6_dev) {
  390. rcu_read_unlock();
  391. return 0;
  392. }
  393. for (pmc6 = rcu_dereference(in6_dev->mc_list);
  394. pmc6;
  395. pmc6 = rcu_dereference(pmc6->next)) {
  396. if (IPV6_ADDR_MC_SCOPE(&pmc6->mca_addr) <
  397. IPV6_ADDR_SCOPE_LINKLOCAL)
  398. continue;
  399. if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
  400. ipv6_addr_is_ll_all_nodes(&pmc6->mca_addr))
  401. continue;
  402. if (!(flags->tvlv_flags & BATADV_MCAST_WANT_NO_RTR6) &&
  403. IPV6_ADDR_MC_SCOPE(&pmc6->mca_addr) >
  404. IPV6_ADDR_SCOPE_LINKLOCAL)
  405. continue;
  406. ipv6_eth_mc_map(&pmc6->mca_addr, mcast_addr);
  407. if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
  408. continue;
  409. new = kmalloc_obj(*new, GFP_ATOMIC);
  410. if (!new) {
  411. ret = -ENOMEM;
  412. break;
  413. }
  414. ether_addr_copy(new->addr, mcast_addr);
  415. hlist_add_head(&new->list, mcast_list);
  416. ret++;
  417. }
  418. rcu_read_unlock();
  419. return ret;
  420. }
  421. #else
  422. static inline int
  423. batadv_mcast_mla_meshif_get_ipv6(struct net_device *dev,
  424. struct hlist_head *mcast_list,
  425. struct batadv_mcast_mla_flags *flags)
  426. {
  427. return 0;
  428. }
  429. #endif
  430. /**
  431. * batadv_mcast_mla_meshif_get() - get meshif multicast listeners
  432. * @dev: the device to collect multicast addresses from
  433. * @mcast_list: a list to put found addresses into
  434. * @flags: flags indicating the new multicast state
  435. *
  436. * Collects multicast addresses of multicast listeners residing
  437. * on this kernel on the given mesh interface, dev, in
  438. * the given mcast_list. In general, multicast listeners provided by
  439. * your multicast receiving applications run directly on this node.
  440. *
  441. * If there is a bridge interface on top of dev, collect from that one
  442. * instead. Just like with IP addresses and routes, multicast listeners
  443. * will(/should) register to the bridge interface instead of an
  444. * enslaved bat0.
  445. *
  446. * Return: -ENOMEM on memory allocation error or the number of
  447. * items added to the mcast_list otherwise.
  448. */
  449. static int
  450. batadv_mcast_mla_meshif_get(struct net_device *dev,
  451. struct hlist_head *mcast_list,
  452. struct batadv_mcast_mla_flags *flags)
  453. {
  454. struct net_device *bridge = batadv_mcast_get_bridge(dev);
  455. int ret4, ret6 = 0;
  456. if (bridge)
  457. dev = bridge;
  458. ret4 = batadv_mcast_mla_meshif_get_ipv4(dev, mcast_list, flags);
  459. if (ret4 < 0)
  460. goto out;
  461. ret6 = batadv_mcast_mla_meshif_get_ipv6(dev, mcast_list, flags);
  462. if (ret6 < 0) {
  463. ret4 = 0;
  464. goto out;
  465. }
  466. out:
  467. dev_put(bridge);
  468. return ret4 + ret6;
  469. }
  470. /**
  471. * batadv_mcast_mla_br_addr_cpy() - copy a bridge multicast address
  472. * @dst: destination to write to - a multicast MAC address
  473. * @src: source to read from - a multicast IP address
  474. *
  475. * Converts a given multicast IPv4/IPv6 address from a bridge
  476. * to its matching multicast MAC address and copies it into the given
  477. * destination buffer.
  478. *
  479. * Caller needs to make sure the destination buffer can hold
  480. * at least ETH_ALEN bytes.
  481. */
  482. static void batadv_mcast_mla_br_addr_cpy(char *dst, const struct br_ip *src)
  483. {
  484. if (src->proto == htons(ETH_P_IP))
  485. ip_eth_mc_map(src->dst.ip4, dst);
  486. #if IS_ENABLED(CONFIG_IPV6)
  487. else if (src->proto == htons(ETH_P_IPV6))
  488. ipv6_eth_mc_map(&src->dst.ip6, dst);
  489. #endif
  490. else
  491. eth_zero_addr(dst);
  492. }
  493. /**
  494. * batadv_mcast_mla_bridge_get() - get bridged-in multicast listeners
  495. * @dev: a bridge slave whose bridge to collect multicast addresses from
  496. * @mcast_list: a list to put found addresses into
  497. * @flags: flags indicating the new multicast state
  498. *
  499. * Collects multicast addresses of multicast listeners residing
  500. * on foreign, non-mesh devices which we gave access to our mesh via
  501. * a bridge on top of the given mesh interface, dev, in the given
  502. * mcast_list.
  503. *
  504. * Return: -ENOMEM on memory allocation error or the number of
  505. * items added to the mcast_list otherwise.
  506. */
  507. static int batadv_mcast_mla_bridge_get(struct net_device *dev,
  508. struct hlist_head *mcast_list,
  509. struct batadv_mcast_mla_flags *flags)
  510. {
  511. struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
  512. struct br_ip_list *br_ip_entry, *tmp;
  513. u8 tvlv_flags = flags->tvlv_flags;
  514. struct batadv_hw_addr *new;
  515. u8 mcast_addr[ETH_ALEN];
  516. int ret;
  517. /* we don't need to detect these devices/listeners, the IGMP/MLD
  518. * snooping code of the Linux bridge already does that for us
  519. */
  520. ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
  521. if (ret < 0)
  522. goto out;
  523. list_for_each_entry(br_ip_entry, &bridge_mcast_list, list) {
  524. if (br_ip_entry->addr.proto == htons(ETH_P_IP)) {
  525. if (tvlv_flags & BATADV_MCAST_WANT_ALL_IPV4)
  526. continue;
  527. if (tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
  528. ipv4_is_local_multicast(br_ip_entry->addr.dst.ip4))
  529. continue;
  530. if (!(tvlv_flags & BATADV_MCAST_WANT_NO_RTR4) &&
  531. !ipv4_is_local_multicast(br_ip_entry->addr.dst.ip4))
  532. continue;
  533. }
  534. #if IS_ENABLED(CONFIG_IPV6)
  535. if (br_ip_entry->addr.proto == htons(ETH_P_IPV6)) {
  536. if (tvlv_flags & BATADV_MCAST_WANT_ALL_IPV6)
  537. continue;
  538. if (tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
  539. ipv6_addr_is_ll_all_nodes(&br_ip_entry->addr.dst.ip6))
  540. continue;
  541. if (!(tvlv_flags & BATADV_MCAST_WANT_NO_RTR6) &&
  542. IPV6_ADDR_MC_SCOPE(&br_ip_entry->addr.dst.ip6) >
  543. IPV6_ADDR_SCOPE_LINKLOCAL)
  544. continue;
  545. }
  546. #endif
  547. batadv_mcast_mla_br_addr_cpy(mcast_addr, &br_ip_entry->addr);
  548. if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
  549. continue;
  550. new = kmalloc_obj(*new, GFP_ATOMIC);
  551. if (!new) {
  552. ret = -ENOMEM;
  553. break;
  554. }
  555. ether_addr_copy(new->addr, mcast_addr);
  556. hlist_add_head(&new->list, mcast_list);
  557. }
  558. out:
  559. list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
  560. list_del(&br_ip_entry->list);
  561. kfree(br_ip_entry);
  562. }
  563. return ret;
  564. }
  565. /**
  566. * batadv_mcast_mla_list_free() - free a list of multicast addresses
  567. * @mcast_list: the list to free
  568. *
  569. * Removes and frees all items in the given mcast_list.
  570. */
  571. static void batadv_mcast_mla_list_free(struct hlist_head *mcast_list)
  572. {
  573. struct batadv_hw_addr *mcast_entry;
  574. struct hlist_node *tmp;
  575. hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
  576. hlist_del(&mcast_entry->list);
  577. kfree(mcast_entry);
  578. }
  579. }
  580. /**
  581. * batadv_mcast_mla_tt_retract() - clean up multicast listener announcements
  582. * @bat_priv: the bat priv with all the mesh interface information
  583. * @mcast_list: a list of addresses which should _not_ be removed
  584. *
  585. * Retracts the announcement of any multicast listener from the
  586. * translation table except the ones listed in the given mcast_list.
  587. *
  588. * If mcast_list is NULL then all are retracted.
  589. */
  590. static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
  591. struct hlist_head *mcast_list)
  592. {
  593. struct batadv_hw_addr *mcast_entry;
  594. struct hlist_node *tmp;
  595. hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
  596. list) {
  597. if (mcast_list &&
  598. batadv_mcast_mla_is_duplicate(mcast_entry->addr,
  599. mcast_list))
  600. continue;
  601. batadv_tt_local_remove(bat_priv, mcast_entry->addr,
  602. BATADV_NO_FLAGS,
  603. "mcast TT outdated", false);
  604. hlist_del(&mcast_entry->list);
  605. kfree(mcast_entry);
  606. }
  607. }
  608. /**
  609. * batadv_mcast_mla_tt_add() - add multicast listener announcements
  610. * @bat_priv: the bat priv with all the mesh interface information
  611. * @mcast_list: a list of addresses which are going to get added
  612. *
  613. * Adds multicast listener announcements from the given mcast_list to the
  614. * translation table if they have not been added yet.
  615. */
  616. static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
  617. struct hlist_head *mcast_list)
  618. {
  619. struct batadv_hw_addr *mcast_entry;
  620. struct hlist_node *tmp;
  621. if (!mcast_list)
  622. return;
  623. hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
  624. if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
  625. &bat_priv->mcast.mla_list))
  626. continue;
  627. if (!batadv_tt_local_add(bat_priv->mesh_iface,
  628. mcast_entry->addr, BATADV_NO_FLAGS,
  629. BATADV_NULL_IFINDEX, BATADV_NO_MARK))
  630. continue;
  631. hlist_del(&mcast_entry->list);
  632. hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
  633. }
  634. }
  635. /**
  636. * batadv_mcast_querier_log() - debug output regarding the querier status on
  637. * link
  638. * @bat_priv: the bat priv with all the mesh interface information
  639. * @str_proto: a string for the querier protocol (e.g. "IGMP" or "MLD")
  640. * @old_state: the previous querier state on our link
  641. * @new_state: the new querier state on our link
  642. *
  643. * Outputs debug messages to the logging facility with log level 'mcast'
  644. * regarding changes to the querier status on the link which are relevant
  645. * to our multicast optimizations.
  646. *
  647. * Usually this is about whether a querier appeared or vanished in
  648. * our mesh or whether the querier is in the suboptimal position of being
  649. * behind our local bridge segment: Snooping switches will directly
  650. * forward listener reports to the querier, therefore batman-adv and
  651. * the bridge will potentially not see these listeners - the querier is
  652. * potentially shadowing listeners from us then.
  653. *
  654. * This is only interesting for nodes with a bridge on top of their
  655. * mesh interface.
  656. */
  657. static void
  658. batadv_mcast_querier_log(struct batadv_priv *bat_priv, char *str_proto,
  659. struct batadv_mcast_querier_state *old_state,
  660. struct batadv_mcast_querier_state *new_state)
  661. {
  662. if (!old_state->exists && new_state->exists)
  663. batadv_info(bat_priv->mesh_iface, "%s Querier appeared\n",
  664. str_proto);
  665. else if (old_state->exists && !new_state->exists)
  666. batadv_info(bat_priv->mesh_iface,
  667. "%s Querier disappeared - multicast optimizations disabled\n",
  668. str_proto);
  669. else if (!bat_priv->mcast.mla_flags.bridged && !new_state->exists)
  670. batadv_info(bat_priv->mesh_iface,
  671. "No %s Querier present - multicast optimizations disabled\n",
  672. str_proto);
  673. if (new_state->exists) {
  674. if ((!old_state->shadowing && new_state->shadowing) ||
  675. (!old_state->exists && new_state->shadowing))
  676. batadv_dbg(BATADV_DBG_MCAST, bat_priv,
  677. "%s Querier is behind our bridged segment: Might shadow listeners\n",
  678. str_proto);
  679. else if (old_state->shadowing && !new_state->shadowing)
  680. batadv_dbg(BATADV_DBG_MCAST, bat_priv,
  681. "%s Querier is not behind our bridged segment\n",
  682. str_proto);
  683. }
  684. }
  685. /**
  686. * batadv_mcast_bridge_log() - debug output for topology changes in bridged
  687. * setups
  688. * @bat_priv: the bat priv with all the mesh interface information
  689. * @new_flags: flags indicating the new multicast state
  690. *
  691. * If no bridges are ever used on this node, then this function does nothing.
  692. *
  693. * Otherwise this function outputs debug information to the 'mcast' log level
  694. * which might be relevant to our multicast optimizations.
  695. *
  696. * More precisely, it outputs information when a bridge interface is added or
  697. * removed from a mesh interface. And when a bridge is present, it further
  698. * outputs information about the querier state which is relevant for the
  699. * multicast flags this node is going to set.
  700. */
  701. static void
  702. batadv_mcast_bridge_log(struct batadv_priv *bat_priv,
  703. struct batadv_mcast_mla_flags *new_flags)
  704. {
  705. struct batadv_mcast_mla_flags *old_flags = &bat_priv->mcast.mla_flags;
  706. if (!old_flags->bridged && new_flags->bridged)
  707. batadv_dbg(BATADV_DBG_MCAST, bat_priv,
  708. "Bridge added: Setting Unsnoopables(U)-flag\n");
  709. else if (old_flags->bridged && !new_flags->bridged)
  710. batadv_dbg(BATADV_DBG_MCAST, bat_priv,
  711. "Bridge removed: Unsetting Unsnoopables(U)-flag\n");
  712. if (new_flags->bridged) {
  713. batadv_mcast_querier_log(bat_priv, "IGMP",
  714. &old_flags->querier_ipv4,
  715. &new_flags->querier_ipv4);
  716. batadv_mcast_querier_log(bat_priv, "MLD",
  717. &old_flags->querier_ipv6,
  718. &new_flags->querier_ipv6);
  719. }
  720. }
  721. /**
  722. * batadv_mcast_flags_log() - output debug information about mcast flag changes
  723. * @bat_priv: the bat priv with all the mesh interface information
  724. * @flags: TVLV flags indicating the new multicast state
  725. *
  726. * Whenever the multicast TVLV flags this node announces change, this function
  727. * should be used to notify userspace about the change.
  728. */
  729. static void batadv_mcast_flags_log(struct batadv_priv *bat_priv, u8 flags)
  730. {
  731. bool old_enabled = bat_priv->mcast.mla_flags.enabled;
  732. u8 old_flags = bat_priv->mcast.mla_flags.tvlv_flags;
  733. char str_old_flags[] = "[.... . .]";
  734. sprintf(str_old_flags, "[%c%c%c%s%s%c]",
  735. (old_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
  736. (old_flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
  737. (old_flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.',
  738. !(old_flags & BATADV_MCAST_WANT_NO_RTR4) ? "R4" : ". ",
  739. !(old_flags & BATADV_MCAST_WANT_NO_RTR6) ? "R6" : ". ",
  740. !(old_flags & BATADV_MCAST_HAVE_MC_PTYPE_CAPA) ? 'P' : '.');
  741. batadv_dbg(BATADV_DBG_MCAST, bat_priv,
  742. "Changing multicast flags from '%s' to '[%c%c%c%s%s%c]'\n",
  743. old_enabled ? str_old_flags : "<undefined>",
  744. (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
  745. (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
  746. (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.',
  747. !(flags & BATADV_MCAST_WANT_NO_RTR4) ? "R4" : ". ",
  748. !(flags & BATADV_MCAST_WANT_NO_RTR6) ? "R6" : ". ",
  749. !(flags & BATADV_MCAST_HAVE_MC_PTYPE_CAPA) ? 'P' : '.');
  750. }
  751. /**
  752. * batadv_mcast_mla_flags_update() - update multicast flags
  753. * @bat_priv: the bat priv with all the mesh interface information
  754. * @flags: flags indicating the new multicast state
  755. *
  756. * Updates the own multicast tvlv with our current multicast related settings,
  757. * capabilities and inabilities.
  758. */
  759. static void
  760. batadv_mcast_mla_flags_update(struct batadv_priv *bat_priv,
  761. struct batadv_mcast_mla_flags *flags)
  762. {
  763. struct batadv_tvlv_mcast_data mcast_data;
  764. if (!memcmp(flags, &bat_priv->mcast.mla_flags, sizeof(*flags)))
  765. return;
  766. batadv_mcast_bridge_log(bat_priv, flags);
  767. batadv_mcast_flags_log(bat_priv, flags->tvlv_flags);
  768. mcast_data.flags = flags->tvlv_flags;
  769. memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
  770. batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
  771. &mcast_data, sizeof(mcast_data));
  772. bat_priv->mcast.mla_flags = *flags;
  773. }
  774. /**
  775. * __batadv_mcast_mla_update() - update the own MLAs
  776. * @bat_priv: the bat priv with all the mesh interface information
  777. *
  778. * Updates the own multicast listener announcements in the translation
  779. * table as well as the own, announced multicast tvlv container.
  780. *
  781. * Note that non-conflicting reads and writes to bat_priv->mcast.mla_list
  782. * in batadv_mcast_mla_tt_retract() and batadv_mcast_mla_tt_add() are
  783. * ensured by the non-parallel execution of the worker this function
  784. * belongs to.
  785. */
  786. static void __batadv_mcast_mla_update(struct batadv_priv *bat_priv)
  787. {
  788. struct net_device *mesh_iface = bat_priv->mesh_iface;
  789. struct hlist_head mcast_list = HLIST_HEAD_INIT;
  790. struct batadv_mcast_mla_flags flags;
  791. int ret;
  792. flags = batadv_mcast_mla_flags_get(bat_priv);
  793. ret = batadv_mcast_mla_meshif_get(mesh_iface, &mcast_list, &flags);
  794. if (ret < 0)
  795. goto out;
  796. ret = batadv_mcast_mla_bridge_get(mesh_iface, &mcast_list, &flags);
  797. if (ret < 0)
  798. goto out;
  799. spin_lock(&bat_priv->mcast.mla_lock);
  800. batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
  801. batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
  802. batadv_mcast_mla_flags_update(bat_priv, &flags);
  803. spin_unlock(&bat_priv->mcast.mla_lock);
  804. out:
  805. batadv_mcast_mla_list_free(&mcast_list);
  806. }
  807. /**
  808. * batadv_mcast_mla_update() - update the own MLAs
  809. * @work: kernel work struct
  810. *
  811. * Updates the own multicast listener announcements in the translation
  812. * table as well as the own, announced multicast tvlv container.
  813. *
  814. * In the end, reschedules the work timer.
  815. */
  816. static void batadv_mcast_mla_update(struct work_struct *work)
  817. {
  818. struct delayed_work *delayed_work;
  819. struct batadv_priv_mcast *priv_mcast;
  820. struct batadv_priv *bat_priv;
  821. delayed_work = to_delayed_work(work);
  822. priv_mcast = container_of(delayed_work, struct batadv_priv_mcast, work);
  823. bat_priv = container_of(priv_mcast, struct batadv_priv, mcast);
  824. __batadv_mcast_mla_update(bat_priv);
  825. batadv_mcast_start_timer(bat_priv);
  826. }
  827. /**
  828. * batadv_mcast_is_report_ipv4() - check for IGMP reports
  829. * @skb: the ethernet frame destined for the mesh
  830. *
  831. * This call might reallocate skb data.
  832. *
  833. * Checks whether the given frame is a valid IGMP report.
  834. *
  835. * Return: If so then true, otherwise false.
  836. */
  837. static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb)
  838. {
  839. if (ip_mc_check_igmp(skb) < 0)
  840. return false;
  841. switch (igmp_hdr(skb)->type) {
  842. case IGMP_HOST_MEMBERSHIP_REPORT:
  843. case IGMPV2_HOST_MEMBERSHIP_REPORT:
  844. case IGMPV3_HOST_MEMBERSHIP_REPORT:
  845. return true;
  846. }
  847. return false;
  848. }
  849. /**
  850. * batadv_mcast_forw_mode_check_ipv4() - check for optimized forwarding
  851. * potential
  852. * @bat_priv: the bat priv with all the mesh interface information
  853. * @skb: the IPv4 packet to check
  854. * @is_unsnoopable: stores whether the destination is snoopable
  855. * @is_routable: stores whether the destination is routable
  856. *
  857. * Checks whether the given IPv4 packet has the potential to be forwarded with a
  858. * mode more optimal than classic flooding.
  859. *
  860. * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
  861. * allocation failure.
  862. */
  863. static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
  864. struct sk_buff *skb,
  865. bool *is_unsnoopable,
  866. int *is_routable)
  867. {
  868. struct iphdr *iphdr;
  869. /* We might fail due to out-of-memory -> drop it */
  870. if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
  871. return -ENOMEM;
  872. if (batadv_mcast_is_report_ipv4(skb))
  873. return -EINVAL;
  874. iphdr = ip_hdr(skb);
  875. /* link-local multicast listeners behind a bridge are
  876. * not snoopable (see RFC4541, section 2.1.2.2)
  877. */
  878. if (ipv4_is_local_multicast(iphdr->daddr))
  879. *is_unsnoopable = true;
  880. else
  881. *is_routable = ETH_P_IP;
  882. return 0;
  883. }
  884. /**
  885. * batadv_mcast_is_report_ipv6() - check for MLD reports
  886. * @skb: the ethernet frame destined for the mesh
  887. *
  888. * This call might reallocate skb data.
  889. *
  890. * Checks whether the given frame is a valid MLD report.
  891. *
  892. * Return: If so then true, otherwise false.
  893. */
  894. static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb)
  895. {
  896. if (ipv6_mc_check_mld(skb) < 0)
  897. return false;
  898. switch (icmp6_hdr(skb)->icmp6_type) {
  899. case ICMPV6_MGM_REPORT:
  900. case ICMPV6_MLD2_REPORT:
  901. return true;
  902. }
  903. return false;
  904. }
  905. /**
  906. * batadv_mcast_forw_mode_check_ipv6() - check for optimized forwarding
  907. * potential
  908. * @bat_priv: the bat priv with all the mesh interface information
  909. * @skb: the IPv6 packet to check
  910. * @is_unsnoopable: stores whether the destination is snoopable
  911. * @is_routable: stores whether the destination is routable
  912. *
  913. * Checks whether the given IPv6 packet has the potential to be forwarded with a
  914. * mode more optimal than classic flooding.
  915. *
  916. * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
  917. */
  918. static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
  919. struct sk_buff *skb,
  920. bool *is_unsnoopable,
  921. int *is_routable)
  922. {
  923. struct ipv6hdr *ip6hdr;
  924. /* We might fail due to out-of-memory -> drop it */
  925. if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
  926. return -ENOMEM;
  927. if (batadv_mcast_is_report_ipv6(skb))
  928. return -EINVAL;
  929. ip6hdr = ipv6_hdr(skb);
  930. if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) < IPV6_ADDR_SCOPE_LINKLOCAL)
  931. return -EINVAL;
  932. /* link-local-all-nodes multicast listeners behind a bridge are
  933. * not snoopable (see RFC4541, section 3, paragraph 3)
  934. */
  935. if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
  936. *is_unsnoopable = true;
  937. else if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) > IPV6_ADDR_SCOPE_LINKLOCAL)
  938. *is_routable = ETH_P_IPV6;
  939. return 0;
  940. }
  941. /**
  942. * batadv_mcast_forw_mode_check() - check for optimized forwarding potential
  943. * @bat_priv: the bat priv with all the mesh interface information
  944. * @skb: the multicast frame to check
  945. * @is_unsnoopable: stores whether the destination is snoopable
  946. * @is_routable: stores whether the destination is routable
  947. *
  948. * Checks whether the given multicast ethernet frame has the potential to be
  949. * forwarded with a mode more optimal than classic flooding.
  950. *
  951. * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
  952. */
  953. static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
  954. struct sk_buff *skb,
  955. bool *is_unsnoopable,
  956. int *is_routable)
  957. {
  958. struct ethhdr *ethhdr = eth_hdr(skb);
  959. if (!atomic_read(&bat_priv->multicast_mode))
  960. return -EINVAL;
  961. switch (ntohs(ethhdr->h_proto)) {
  962. case ETH_P_IP:
  963. return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
  964. is_unsnoopable,
  965. is_routable);
  966. case ETH_P_IPV6:
  967. if (!IS_ENABLED(CONFIG_IPV6))
  968. return -EINVAL;
  969. return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
  970. is_unsnoopable,
  971. is_routable);
  972. default:
  973. return -EINVAL;
  974. }
  975. }
  976. /**
  977. * batadv_mcast_forw_want_all_ip_count() - count nodes with unspecific mcast
  978. * interest
  979. * @bat_priv: the bat priv with all the mesh interface information
  980. * @ethhdr: ethernet header of a packet
  981. *
  982. * Return: the number of nodes which want all IPv4 multicast traffic if the
  983. * given ethhdr is from an IPv4 packet or the number of nodes which want all
  984. * IPv6 traffic if it matches an IPv6 packet.
  985. */
  986. static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
  987. struct ethhdr *ethhdr)
  988. {
  989. switch (ntohs(ethhdr->h_proto)) {
  990. case ETH_P_IP:
  991. return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
  992. case ETH_P_IPV6:
  993. return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
  994. default:
  995. /* we shouldn't be here... */
  996. return 0;
  997. }
  998. }
  999. /**
  1000. * batadv_mcast_forw_rtr_count() - count nodes with a multicast router
  1001. * @bat_priv: the bat priv with all the mesh interface information
  1002. * @protocol: the ethernet protocol type to count multicast routers for
  1003. *
  1004. * Return: the number of nodes which want all routable IPv4 multicast traffic
  1005. * if the protocol is ETH_P_IP or the number of nodes which want all routable
  1006. * IPv6 traffic if the protocol is ETH_P_IPV6. Otherwise returns 0.
  1007. */
  1008. static int batadv_mcast_forw_rtr_count(struct batadv_priv *bat_priv,
  1009. int protocol)
  1010. {
  1011. switch (protocol) {
  1012. case ETH_P_IP:
  1013. return atomic_read(&bat_priv->mcast.num_want_all_rtr4);
  1014. case ETH_P_IPV6:
  1015. return atomic_read(&bat_priv->mcast.num_want_all_rtr6);
  1016. default:
  1017. return 0;
  1018. }
  1019. }
  1020. /**
  1021. * batadv_mcast_forw_mode_by_count() - get forwarding mode by count
  1022. * @bat_priv: the bat priv with all the mesh interface information
  1023. * @skb: the multicast packet to check
  1024. * @vid: the vlan identifier
  1025. * @is_routable: stores whether the destination is routable
  1026. * @count: the number of originators the multicast packet need to be sent to
  1027. *
  1028. * For a multicast packet with multiple destination originators, checks which
  1029. * mode to use. For BATADV_FORW_MCAST it also encapsulates the packet with a
  1030. * complete batman-adv multicast header.
  1031. *
  1032. * Return:
  1033. * BATADV_FORW_MCAST: If all nodes have multicast packet routing
  1034. * capabilities and an MTU >= 1280 on all hard interfaces (including us)
  1035. * and the encapsulated multicast packet with all destination addresses
  1036. * would still fit into an 1280 bytes batman-adv multicast packet
  1037. * (excluding the outer ethernet frame) and we could successfully push
  1038. * the full batman-adv multicast packet header.
  1039. * BATADV_FORW_UCASTS: If the packet cannot be sent in a batman-adv
  1040. * multicast packet and the amount of batman-adv unicast packets needed
  1041. * is smaller or equal to the configured multicast fanout.
  1042. * BATADV_FORW_BCAST: Otherwise.
  1043. */
  1044. static enum batadv_forw_mode
  1045. batadv_mcast_forw_mode_by_count(struct batadv_priv *bat_priv,
  1046. struct sk_buff *skb, unsigned short vid,
  1047. int is_routable, int count)
  1048. {
  1049. unsigned int mcast_hdrlen = batadv_mcast_forw_packet_hdrlen(count);
  1050. u8 own_tvlv_flags = bat_priv->mcast.mla_flags.tvlv_flags;
  1051. if (!atomic_read(&bat_priv->mcast.num_no_mc_ptype_capa) &&
  1052. own_tvlv_flags & BATADV_MCAST_HAVE_MC_PTYPE_CAPA &&
  1053. skb->len + mcast_hdrlen <= IPV6_MIN_MTU &&
  1054. batadv_mcast_forw_push(bat_priv, skb, vid, is_routable, count))
  1055. return BATADV_FORW_MCAST;
  1056. if (count <= atomic_read(&bat_priv->multicast_fanout))
  1057. return BATADV_FORW_UCASTS;
  1058. return BATADV_FORW_BCAST;
  1059. }
  1060. /**
  1061. * batadv_mcast_forw_mode() - check on how to forward a multicast packet
  1062. * @bat_priv: the bat priv with all the mesh interface information
  1063. * @skb: the multicast packet to check
  1064. * @vid: the vlan identifier
  1065. * @is_routable: stores whether the destination is routable
  1066. *
  1067. * Return: The forwarding mode as enum batadv_forw_mode.
  1068. */
  1069. enum batadv_forw_mode
  1070. batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
  1071. unsigned short vid, int *is_routable)
  1072. {
  1073. int ret, tt_count, ip_count, unsnoop_count, total_count;
  1074. bool is_unsnoopable = false;
  1075. struct ethhdr *ethhdr;
  1076. int rtr_count = 0;
  1077. ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable,
  1078. is_routable);
  1079. if (ret == -ENOMEM)
  1080. return BATADV_FORW_NONE;
  1081. else if (ret < 0)
  1082. return BATADV_FORW_BCAST;
  1083. ethhdr = eth_hdr(skb);
  1084. tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
  1085. BATADV_NO_FLAGS);
  1086. ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
  1087. unsnoop_count = !is_unsnoopable ? 0 :
  1088. atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
  1089. rtr_count = batadv_mcast_forw_rtr_count(bat_priv, *is_routable);
  1090. total_count = tt_count + ip_count + unsnoop_count + rtr_count;
  1091. if (!total_count)
  1092. return BATADV_FORW_NONE;
  1093. else if (unsnoop_count)
  1094. return BATADV_FORW_BCAST;
  1095. return batadv_mcast_forw_mode_by_count(bat_priv, skb, vid, *is_routable,
  1096. total_count);
  1097. }
  1098. /**
  1099. * batadv_mcast_forw_send_orig() - send a multicast packet to an originator
  1100. * @bat_priv: the bat priv with all the mesh interface information
  1101. * @skb: the multicast packet to send
  1102. * @vid: the vlan identifier
  1103. * @orig_node: the originator to send the packet to
  1104. *
  1105. * Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.
  1106. */
  1107. static int batadv_mcast_forw_send_orig(struct batadv_priv *bat_priv,
  1108. struct sk_buff *skb,
  1109. unsigned short vid,
  1110. struct batadv_orig_node *orig_node)
  1111. {
  1112. /* Avoid sending multicast-in-unicast packets to other BLA
  1113. * gateways - they already got the frame from the LAN side
  1114. * we share with them.
  1115. * TODO: Refactor to take BLA into account earlier, to avoid
  1116. * reducing the mcast_fanout count.
  1117. */
  1118. if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid)) {
  1119. dev_kfree_skb(skb);
  1120. return NET_XMIT_SUCCESS;
  1121. }
  1122. return batadv_send_skb_unicast(bat_priv, skb, BATADV_UNICAST, 0,
  1123. orig_node, vid);
  1124. }
  1125. /**
  1126. * batadv_mcast_forw_tt() - forwards a packet to multicast listeners
  1127. * @bat_priv: the bat priv with all the mesh interface information
  1128. * @skb: the multicast packet to transmit
  1129. * @vid: the vlan identifier
  1130. *
  1131. * Sends copies of a frame with multicast destination to any multicast
  1132. * listener registered in the translation table. A transmission is performed
  1133. * via a batman-adv unicast packet for each such destination node.
  1134. *
  1135. * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
  1136. * otherwise.
  1137. */
  1138. static int
  1139. batadv_mcast_forw_tt(struct batadv_priv *bat_priv, struct sk_buff *skb,
  1140. unsigned short vid)
  1141. {
  1142. int ret = NET_XMIT_SUCCESS;
  1143. struct sk_buff *newskb;
  1144. struct batadv_tt_orig_list_entry *orig_entry;
  1145. struct batadv_tt_global_entry *tt_global;
  1146. const u8 *addr = eth_hdr(skb)->h_dest;
  1147. tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
  1148. if (!tt_global)
  1149. goto out;
  1150. rcu_read_lock();
  1151. hlist_for_each_entry_rcu(orig_entry, &tt_global->orig_list, list) {
  1152. newskb = skb_copy(skb, GFP_ATOMIC);
  1153. if (!newskb) {
  1154. ret = NET_XMIT_DROP;
  1155. break;
  1156. }
  1157. batadv_mcast_forw_send_orig(bat_priv, newskb, vid,
  1158. orig_entry->orig_node);
  1159. }
  1160. rcu_read_unlock();
  1161. batadv_tt_global_entry_put(tt_global);
  1162. out:
  1163. return ret;
  1164. }
  1165. /**
  1166. * batadv_mcast_forw_want_all_ipv4() - forward to nodes with want-all-ipv4
  1167. * @bat_priv: the bat priv with all the mesh interface information
  1168. * @skb: the multicast packet to transmit
  1169. * @vid: the vlan identifier
  1170. *
  1171. * Sends copies of a frame with multicast destination to any node with a
  1172. * BATADV_MCAST_WANT_ALL_IPV4 flag set. A transmission is performed via a
  1173. * batman-adv unicast packet for each such destination node.
  1174. *
  1175. * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
  1176. * otherwise.
  1177. */
  1178. static int
  1179. batadv_mcast_forw_want_all_ipv4(struct batadv_priv *bat_priv,
  1180. struct sk_buff *skb, unsigned short vid)
  1181. {
  1182. struct batadv_orig_node *orig_node;
  1183. int ret = NET_XMIT_SUCCESS;
  1184. struct sk_buff *newskb;
  1185. rcu_read_lock();
  1186. hlist_for_each_entry_rcu(orig_node,
  1187. &bat_priv->mcast.want_all_ipv4_list,
  1188. mcast_want_all_ipv4_node) {
  1189. newskb = skb_copy(skb, GFP_ATOMIC);
  1190. if (!newskb) {
  1191. ret = NET_XMIT_DROP;
  1192. break;
  1193. }
  1194. batadv_mcast_forw_send_orig(bat_priv, newskb, vid, orig_node);
  1195. }
  1196. rcu_read_unlock();
  1197. return ret;
  1198. }
  1199. /**
  1200. * batadv_mcast_forw_want_all_ipv6() - forward to nodes with want-all-ipv6
  1201. * @bat_priv: the bat priv with all the mesh interface information
  1202. * @skb: The multicast packet to transmit
  1203. * @vid: the vlan identifier
  1204. *
  1205. * Sends copies of a frame with multicast destination to any node with a
  1206. * BATADV_MCAST_WANT_ALL_IPV6 flag set. A transmission is performed via a
  1207. * batman-adv unicast packet for each such destination node.
  1208. *
  1209. * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
  1210. * otherwise.
  1211. */
  1212. static int
  1213. batadv_mcast_forw_want_all_ipv6(struct batadv_priv *bat_priv,
  1214. struct sk_buff *skb, unsigned short vid)
  1215. {
  1216. struct batadv_orig_node *orig_node;
  1217. int ret = NET_XMIT_SUCCESS;
  1218. struct sk_buff *newskb;
  1219. rcu_read_lock();
  1220. hlist_for_each_entry_rcu(orig_node,
  1221. &bat_priv->mcast.want_all_ipv6_list,
  1222. mcast_want_all_ipv6_node) {
  1223. newskb = skb_copy(skb, GFP_ATOMIC);
  1224. if (!newskb) {
  1225. ret = NET_XMIT_DROP;
  1226. break;
  1227. }
  1228. batadv_mcast_forw_send_orig(bat_priv, newskb, vid, orig_node);
  1229. }
  1230. rcu_read_unlock();
  1231. return ret;
  1232. }
  1233. /**
  1234. * batadv_mcast_forw_want_all() - forward packet to nodes in a want-all list
  1235. * @bat_priv: the bat priv with all the mesh interface information
  1236. * @skb: the multicast packet to transmit
  1237. * @vid: the vlan identifier
  1238. *
  1239. * Sends copies of a frame with multicast destination to any node with a
  1240. * BATADV_MCAST_WANT_ALL_IPV4 or BATADV_MCAST_WANT_ALL_IPV6 flag set. A
  1241. * transmission is performed via a batman-adv unicast packet for each such
  1242. * destination node.
  1243. *
  1244. * Return: NET_XMIT_DROP on memory allocation failure or if the protocol family
  1245. * is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise.
  1246. */
  1247. static int
  1248. batadv_mcast_forw_want_all(struct batadv_priv *bat_priv,
  1249. struct sk_buff *skb, unsigned short vid)
  1250. {
  1251. switch (ntohs(eth_hdr(skb)->h_proto)) {
  1252. case ETH_P_IP:
  1253. return batadv_mcast_forw_want_all_ipv4(bat_priv, skb, vid);
  1254. case ETH_P_IPV6:
  1255. return batadv_mcast_forw_want_all_ipv6(bat_priv, skb, vid);
  1256. default:
  1257. /* we shouldn't be here... */
  1258. return NET_XMIT_DROP;
  1259. }
  1260. }
  1261. /**
  1262. * batadv_mcast_forw_want_all_rtr4() - forward to nodes with want-all-rtr4
  1263. * @bat_priv: the bat priv with all the mesh interface information
  1264. * @skb: the multicast packet to transmit
  1265. * @vid: the vlan identifier
  1266. *
  1267. * Sends copies of a frame with multicast destination to any node with a
  1268. * BATADV_MCAST_WANT_NO_RTR4 flag unset. A transmission is performed via a
  1269. * batman-adv unicast packet for each such destination node.
  1270. *
  1271. * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
  1272. * otherwise.
  1273. */
  1274. static int
  1275. batadv_mcast_forw_want_all_rtr4(struct batadv_priv *bat_priv,
  1276. struct sk_buff *skb, unsigned short vid)
  1277. {
  1278. struct batadv_orig_node *orig_node;
  1279. int ret = NET_XMIT_SUCCESS;
  1280. struct sk_buff *newskb;
  1281. rcu_read_lock();
  1282. hlist_for_each_entry_rcu(orig_node,
  1283. &bat_priv->mcast.want_all_rtr4_list,
  1284. mcast_want_all_rtr4_node) {
  1285. newskb = skb_copy(skb, GFP_ATOMIC);
  1286. if (!newskb) {
  1287. ret = NET_XMIT_DROP;
  1288. break;
  1289. }
  1290. batadv_mcast_forw_send_orig(bat_priv, newskb, vid, orig_node);
  1291. }
  1292. rcu_read_unlock();
  1293. return ret;
  1294. }
  1295. /**
  1296. * batadv_mcast_forw_want_all_rtr6() - forward to nodes with want-all-rtr6
  1297. * @bat_priv: the bat priv with all the mesh interface information
  1298. * @skb: The multicast packet to transmit
  1299. * @vid: the vlan identifier
  1300. *
  1301. * Sends copies of a frame with multicast destination to any node with a
  1302. * BATADV_MCAST_WANT_NO_RTR6 flag unset. A transmission is performed via a
  1303. * batman-adv unicast packet for each such destination node.
  1304. *
  1305. * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS
  1306. * otherwise.
  1307. */
  1308. static int
  1309. batadv_mcast_forw_want_all_rtr6(struct batadv_priv *bat_priv,
  1310. struct sk_buff *skb, unsigned short vid)
  1311. {
  1312. struct batadv_orig_node *orig_node;
  1313. int ret = NET_XMIT_SUCCESS;
  1314. struct sk_buff *newskb;
  1315. rcu_read_lock();
  1316. hlist_for_each_entry_rcu(orig_node,
  1317. &bat_priv->mcast.want_all_rtr6_list,
  1318. mcast_want_all_rtr6_node) {
  1319. newskb = skb_copy(skb, GFP_ATOMIC);
  1320. if (!newskb) {
  1321. ret = NET_XMIT_DROP;
  1322. break;
  1323. }
  1324. batadv_mcast_forw_send_orig(bat_priv, newskb, vid, orig_node);
  1325. }
  1326. rcu_read_unlock();
  1327. return ret;
  1328. }
  1329. /**
  1330. * batadv_mcast_forw_want_rtr() - forward packet to nodes in a want-all-rtr list
  1331. * @bat_priv: the bat priv with all the mesh interface information
  1332. * @skb: the multicast packet to transmit
  1333. * @vid: the vlan identifier
  1334. *
  1335. * Sends copies of a frame with multicast destination to any node with a
  1336. * BATADV_MCAST_WANT_NO_RTR4 or BATADV_MCAST_WANT_NO_RTR6 flag unset. A
  1337. * transmission is performed via a batman-adv unicast packet for each such
  1338. * destination node.
  1339. *
  1340. * Return: NET_XMIT_DROP on memory allocation failure or if the protocol family
  1341. * is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise.
  1342. */
  1343. static int
  1344. batadv_mcast_forw_want_rtr(struct batadv_priv *bat_priv,
  1345. struct sk_buff *skb, unsigned short vid)
  1346. {
  1347. switch (ntohs(eth_hdr(skb)->h_proto)) {
  1348. case ETH_P_IP:
  1349. return batadv_mcast_forw_want_all_rtr4(bat_priv, skb, vid);
  1350. case ETH_P_IPV6:
  1351. return batadv_mcast_forw_want_all_rtr6(bat_priv, skb, vid);
  1352. default:
  1353. /* we shouldn't be here... */
  1354. return NET_XMIT_DROP;
  1355. }
  1356. }
  1357. /**
  1358. * batadv_mcast_forw_send() - send packet to any detected multicast recipient
  1359. * @bat_priv: the bat priv with all the mesh interface information
  1360. * @skb: the multicast packet to transmit
  1361. * @vid: the vlan identifier
  1362. * @is_routable: stores whether the destination is routable
  1363. *
  1364. * Sends copies of a frame with multicast destination to any node that signaled
  1365. * interest in it, that is either via the translation table or the according
  1366. * want-all flags. A transmission is performed via a batman-adv unicast packet
  1367. * for each such destination node.
  1368. *
  1369. * The given skb is consumed/freed.
  1370. *
  1371. * Return: NET_XMIT_DROP on memory allocation failure or if the protocol family
  1372. * is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise.
  1373. */
  1374. int batadv_mcast_forw_send(struct batadv_priv *bat_priv, struct sk_buff *skb,
  1375. unsigned short vid, int is_routable)
  1376. {
  1377. int ret;
  1378. ret = batadv_mcast_forw_tt(bat_priv, skb, vid);
  1379. if (ret != NET_XMIT_SUCCESS) {
  1380. kfree_skb(skb);
  1381. return ret;
  1382. }
  1383. ret = batadv_mcast_forw_want_all(bat_priv, skb, vid);
  1384. if (ret != NET_XMIT_SUCCESS) {
  1385. kfree_skb(skb);
  1386. return ret;
  1387. }
  1388. if (!is_routable)
  1389. goto skip_mc_router;
  1390. ret = batadv_mcast_forw_want_rtr(bat_priv, skb, vid);
  1391. if (ret != NET_XMIT_SUCCESS) {
  1392. kfree_skb(skb);
  1393. return ret;
  1394. }
  1395. skip_mc_router:
  1396. consume_skb(skb);
  1397. return ret;
  1398. }
  1399. /**
  1400. * batadv_mcast_want_unsnoop_update() - update unsnoop counter and list
  1401. * @bat_priv: the bat priv with all the mesh interface information
  1402. * @orig: the orig_node which multicast state might have changed of
  1403. * @mcast_flags: flags indicating the new multicast state
  1404. *
  1405. * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
  1406. * orig, has toggled then this method updates the counter and the list
  1407. * accordingly.
  1408. *
  1409. * Caller needs to hold orig->mcast_handler_lock.
  1410. */
  1411. static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
  1412. struct batadv_orig_node *orig,
  1413. u8 mcast_flags)
  1414. {
  1415. struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
  1416. struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
  1417. lockdep_assert_held(&orig->mcast_handler_lock);
  1418. /* switched from flag unset to set */
  1419. if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
  1420. !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
  1421. atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
  1422. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  1423. /* flag checks above + mcast_handler_lock prevents this */
  1424. WARN_ON(!hlist_unhashed(node));
  1425. hlist_add_head_rcu(node, head);
  1426. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  1427. /* switched from flag set to unset */
  1428. } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
  1429. orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
  1430. atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
  1431. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  1432. /* flag checks above + mcast_handler_lock prevents this */
  1433. WARN_ON(hlist_unhashed(node));
  1434. hlist_del_init_rcu(node);
  1435. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  1436. }
  1437. }
  1438. /**
  1439. * batadv_mcast_want_ipv4_update() - update want-all-ipv4 counter and list
  1440. * @bat_priv: the bat priv with all the mesh interface information
  1441. * @orig: the orig_node which multicast state might have changed of
  1442. * @mcast_flags: flags indicating the new multicast state
  1443. *
  1444. * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
  1445. * toggled then this method updates the counter and the list accordingly.
  1446. *
  1447. * Caller needs to hold orig->mcast_handler_lock.
  1448. */
  1449. static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
  1450. struct batadv_orig_node *orig,
  1451. u8 mcast_flags)
  1452. {
  1453. struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
  1454. struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
  1455. lockdep_assert_held(&orig->mcast_handler_lock);
  1456. /* switched from flag unset to set */
  1457. if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
  1458. !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
  1459. atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
  1460. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  1461. /* flag checks above + mcast_handler_lock prevents this */
  1462. WARN_ON(!hlist_unhashed(node));
  1463. hlist_add_head_rcu(node, head);
  1464. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  1465. /* switched from flag set to unset */
  1466. } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
  1467. orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
  1468. atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
  1469. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  1470. /* flag checks above + mcast_handler_lock prevents this */
  1471. WARN_ON(hlist_unhashed(node));
  1472. hlist_del_init_rcu(node);
  1473. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  1474. }
  1475. }
  1476. /**
  1477. * batadv_mcast_want_ipv6_update() - update want-all-ipv6 counter and list
  1478. * @bat_priv: the bat priv with all the mesh interface information
  1479. * @orig: the orig_node which multicast state might have changed of
  1480. * @mcast_flags: flags indicating the new multicast state
  1481. *
  1482. * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
  1483. * toggled then this method updates the counter and the list accordingly.
  1484. *
  1485. * Caller needs to hold orig->mcast_handler_lock.
  1486. */
  1487. static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
  1488. struct batadv_orig_node *orig,
  1489. u8 mcast_flags)
  1490. {
  1491. struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
  1492. struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
  1493. lockdep_assert_held(&orig->mcast_handler_lock);
  1494. /* switched from flag unset to set */
  1495. if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
  1496. !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
  1497. atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
  1498. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  1499. /* flag checks above + mcast_handler_lock prevents this */
  1500. WARN_ON(!hlist_unhashed(node));
  1501. hlist_add_head_rcu(node, head);
  1502. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  1503. /* switched from flag set to unset */
  1504. } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
  1505. orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
  1506. atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
  1507. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  1508. /* flag checks above + mcast_handler_lock prevents this */
  1509. WARN_ON(hlist_unhashed(node));
  1510. hlist_del_init_rcu(node);
  1511. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  1512. }
  1513. }
  1514. /**
  1515. * batadv_mcast_want_rtr4_update() - update want-all-rtr4 counter and list
  1516. * @bat_priv: the bat priv with all the mesh interface information
  1517. * @orig: the orig_node which multicast state might have changed of
  1518. * @mcast_flags: flags indicating the new multicast state
  1519. *
  1520. * If the BATADV_MCAST_WANT_NO_RTR4 flag of this originator, orig, has
  1521. * toggled then this method updates the counter and the list accordingly.
  1522. *
  1523. * Caller needs to hold orig->mcast_handler_lock.
  1524. */
  1525. static void batadv_mcast_want_rtr4_update(struct batadv_priv *bat_priv,
  1526. struct batadv_orig_node *orig,
  1527. u8 mcast_flags)
  1528. {
  1529. struct hlist_node *node = &orig->mcast_want_all_rtr4_node;
  1530. struct hlist_head *head = &bat_priv->mcast.want_all_rtr4_list;
  1531. lockdep_assert_held(&orig->mcast_handler_lock);
  1532. /* switched from flag set to unset */
  1533. if (!(mcast_flags & BATADV_MCAST_WANT_NO_RTR4) &&
  1534. orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR4) {
  1535. atomic_inc(&bat_priv->mcast.num_want_all_rtr4);
  1536. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  1537. /* flag checks above + mcast_handler_lock prevents this */
  1538. WARN_ON(!hlist_unhashed(node));
  1539. hlist_add_head_rcu(node, head);
  1540. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  1541. /* switched from flag unset to set */
  1542. } else if (mcast_flags & BATADV_MCAST_WANT_NO_RTR4 &&
  1543. !(orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR4)) {
  1544. atomic_dec(&bat_priv->mcast.num_want_all_rtr4);
  1545. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  1546. /* flag checks above + mcast_handler_lock prevents this */
  1547. WARN_ON(hlist_unhashed(node));
  1548. hlist_del_init_rcu(node);
  1549. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  1550. }
  1551. }
  1552. /**
  1553. * batadv_mcast_want_rtr6_update() - update want-all-rtr6 counter and list
  1554. * @bat_priv: the bat priv with all the mesh interface information
  1555. * @orig: the orig_node which multicast state might have changed of
  1556. * @mcast_flags: flags indicating the new multicast state
  1557. *
  1558. * If the BATADV_MCAST_WANT_NO_RTR6 flag of this originator, orig, has
  1559. * toggled then this method updates the counter and the list accordingly.
  1560. *
  1561. * Caller needs to hold orig->mcast_handler_lock.
  1562. */
  1563. static void batadv_mcast_want_rtr6_update(struct batadv_priv *bat_priv,
  1564. struct batadv_orig_node *orig,
  1565. u8 mcast_flags)
  1566. {
  1567. struct hlist_node *node = &orig->mcast_want_all_rtr6_node;
  1568. struct hlist_head *head = &bat_priv->mcast.want_all_rtr6_list;
  1569. lockdep_assert_held(&orig->mcast_handler_lock);
  1570. /* switched from flag set to unset */
  1571. if (!(mcast_flags & BATADV_MCAST_WANT_NO_RTR6) &&
  1572. orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR6) {
  1573. atomic_inc(&bat_priv->mcast.num_want_all_rtr6);
  1574. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  1575. /* flag checks above + mcast_handler_lock prevents this */
  1576. WARN_ON(!hlist_unhashed(node));
  1577. hlist_add_head_rcu(node, head);
  1578. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  1579. /* switched from flag unset to set */
  1580. } else if (mcast_flags & BATADV_MCAST_WANT_NO_RTR6 &&
  1581. !(orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR6)) {
  1582. atomic_dec(&bat_priv->mcast.num_want_all_rtr6);
  1583. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  1584. /* flag checks above + mcast_handler_lock prevents this */
  1585. WARN_ON(hlist_unhashed(node));
  1586. hlist_del_init_rcu(node);
  1587. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  1588. }
  1589. }
  1590. /**
  1591. * batadv_mcast_have_mc_ptype_update() - update multicast packet type counter
  1592. * @bat_priv: the bat priv with all the mesh interface information
  1593. * @orig: the orig_node which multicast state might have changed of
  1594. * @mcast_flags: flags indicating the new multicast state
  1595. *
  1596. * If the BATADV_MCAST_HAVE_MC_PTYPE_CAPA flag of this originator, orig, has
  1597. * toggled then this method updates the counter accordingly.
  1598. */
  1599. static void batadv_mcast_have_mc_ptype_update(struct batadv_priv *bat_priv,
  1600. struct batadv_orig_node *orig,
  1601. u8 mcast_flags)
  1602. {
  1603. lockdep_assert_held(&orig->mcast_handler_lock);
  1604. /* switched from flag set to unset */
  1605. if (!(mcast_flags & BATADV_MCAST_HAVE_MC_PTYPE_CAPA) &&
  1606. orig->mcast_flags & BATADV_MCAST_HAVE_MC_PTYPE_CAPA)
  1607. atomic_inc(&bat_priv->mcast.num_no_mc_ptype_capa);
  1608. /* switched from flag unset to set */
  1609. else if (mcast_flags & BATADV_MCAST_HAVE_MC_PTYPE_CAPA &&
  1610. !(orig->mcast_flags & BATADV_MCAST_HAVE_MC_PTYPE_CAPA))
  1611. atomic_dec(&bat_priv->mcast.num_no_mc_ptype_capa);
  1612. }
  1613. /**
  1614. * batadv_mcast_tvlv_flags_get() - get multicast flags from an OGM TVLV
  1615. * @enabled: whether the originator has multicast TVLV support enabled
  1616. * @tvlv_value: tvlv buffer containing the multicast flags
  1617. * @tvlv_value_len: tvlv buffer length
  1618. *
  1619. * Return: multicast flags for the given tvlv buffer
  1620. */
  1621. static u8
  1622. batadv_mcast_tvlv_flags_get(bool enabled, void *tvlv_value, u16 tvlv_value_len)
  1623. {
  1624. u8 mcast_flags = BATADV_NO_FLAGS;
  1625. if (enabled && tvlv_value && tvlv_value_len >= sizeof(mcast_flags))
  1626. mcast_flags = *(u8 *)tvlv_value;
  1627. if (!enabled) {
  1628. mcast_flags |= BATADV_MCAST_WANT_ALL_IPV4;
  1629. mcast_flags |= BATADV_MCAST_WANT_ALL_IPV6;
  1630. }
  1631. /* remove redundant flags to avoid sending duplicate packets later */
  1632. if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)
  1633. mcast_flags |= BATADV_MCAST_WANT_NO_RTR4;
  1634. if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)
  1635. mcast_flags |= BATADV_MCAST_WANT_NO_RTR6;
  1636. return mcast_flags;
  1637. }
  1638. /**
  1639. * batadv_mcast_tvlv_ogm_handler() - process incoming multicast tvlv container
  1640. * @bat_priv: the bat priv with all the mesh interface information
  1641. * @orig: the orig_node of the ogm
  1642. * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
  1643. * @tvlv_value: tvlv buffer containing the multicast data
  1644. * @tvlv_value_len: tvlv buffer length
  1645. */
  1646. static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv,
  1647. struct batadv_orig_node *orig,
  1648. u8 flags,
  1649. void *tvlv_value,
  1650. u16 tvlv_value_len)
  1651. {
  1652. bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
  1653. u8 mcast_flags;
  1654. mcast_flags = batadv_mcast_tvlv_flags_get(orig_mcast_enabled,
  1655. tvlv_value, tvlv_value_len);
  1656. spin_lock_bh(&orig->mcast_handler_lock);
  1657. if (orig_mcast_enabled &&
  1658. !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
  1659. set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
  1660. } else if (!orig_mcast_enabled &&
  1661. test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
  1662. clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
  1663. }
  1664. set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
  1665. batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
  1666. batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
  1667. batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
  1668. batadv_mcast_want_rtr4_update(bat_priv, orig, mcast_flags);
  1669. batadv_mcast_want_rtr6_update(bat_priv, orig, mcast_flags);
  1670. batadv_mcast_have_mc_ptype_update(bat_priv, orig, mcast_flags);
  1671. orig->mcast_flags = mcast_flags;
  1672. spin_unlock_bh(&orig->mcast_handler_lock);
  1673. }
  1674. /**
  1675. * batadv_mcast_init() - initialize the multicast optimizations structures
  1676. * @bat_priv: the bat priv with all the mesh interface information
  1677. */
  1678. void batadv_mcast_init(struct batadv_priv *bat_priv)
  1679. {
  1680. batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
  1681. NULL, NULL, BATADV_TVLV_MCAST, 2,
  1682. BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
  1683. batadv_tvlv_handler_register(bat_priv, NULL, NULL,
  1684. batadv_mcast_forw_tracker_tvlv_handler,
  1685. BATADV_TVLV_MCAST_TRACKER, 1,
  1686. BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
  1687. INIT_DELAYED_WORK(&bat_priv->mcast.work, batadv_mcast_mla_update);
  1688. batadv_mcast_start_timer(bat_priv);
  1689. }
  1690. /**
  1691. * batadv_mcast_mesh_info_put() - put multicast info into a netlink message
  1692. * @msg: buffer for the message
  1693. * @bat_priv: the bat priv with all the mesh interface information
  1694. *
  1695. * Return: 0 or error code.
  1696. */
  1697. int batadv_mcast_mesh_info_put(struct sk_buff *msg,
  1698. struct batadv_priv *bat_priv)
  1699. {
  1700. u32 flags = bat_priv->mcast.mla_flags.tvlv_flags;
  1701. u32 flags_priv = BATADV_NO_FLAGS;
  1702. if (bat_priv->mcast.mla_flags.bridged) {
  1703. flags_priv |= BATADV_MCAST_FLAGS_BRIDGED;
  1704. if (bat_priv->mcast.mla_flags.querier_ipv4.exists)
  1705. flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_EXISTS;
  1706. if (bat_priv->mcast.mla_flags.querier_ipv6.exists)
  1707. flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_EXISTS;
  1708. if (bat_priv->mcast.mla_flags.querier_ipv4.shadowing)
  1709. flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_SHADOWING;
  1710. if (bat_priv->mcast.mla_flags.querier_ipv6.shadowing)
  1711. flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_SHADOWING;
  1712. }
  1713. if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS, flags) ||
  1714. nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS_PRIV, flags_priv))
  1715. return -EMSGSIZE;
  1716. return 0;
  1717. }
  1718. /**
  1719. * batadv_mcast_flags_dump_entry() - dump one entry of the multicast flags table
  1720. * to a netlink socket
  1721. * @msg: buffer for the message
  1722. * @portid: netlink port
  1723. * @cb: Control block containing additional options
  1724. * @orig_node: originator to dump the multicast flags of
  1725. *
  1726. * Return: 0 or error code.
  1727. */
  1728. static int
  1729. batadv_mcast_flags_dump_entry(struct sk_buff *msg, u32 portid,
  1730. struct netlink_callback *cb,
  1731. struct batadv_orig_node *orig_node)
  1732. {
  1733. void *hdr;
  1734. hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
  1735. &batadv_netlink_family, NLM_F_MULTI,
  1736. BATADV_CMD_GET_MCAST_FLAGS);
  1737. if (!hdr)
  1738. return -ENOBUFS;
  1739. genl_dump_check_consistent(cb, hdr);
  1740. if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
  1741. orig_node->orig)) {
  1742. genlmsg_cancel(msg, hdr);
  1743. return -EMSGSIZE;
  1744. }
  1745. if (test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
  1746. &orig_node->capabilities)) {
  1747. if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS,
  1748. orig_node->mcast_flags)) {
  1749. genlmsg_cancel(msg, hdr);
  1750. return -EMSGSIZE;
  1751. }
  1752. }
  1753. genlmsg_end(msg, hdr);
  1754. return 0;
  1755. }
  1756. /**
  1757. * batadv_mcast_flags_dump_bucket() - dump one bucket of the multicast flags
  1758. * table to a netlink socket
  1759. * @msg: buffer for the message
  1760. * @portid: netlink port
  1761. * @cb: Control block containing additional options
  1762. * @hash: hash to dump
  1763. * @bucket: bucket index to dump
  1764. * @idx_skip: How many entries to skip
  1765. *
  1766. * Return: 0 or error code.
  1767. */
  1768. static int
  1769. batadv_mcast_flags_dump_bucket(struct sk_buff *msg, u32 portid,
  1770. struct netlink_callback *cb,
  1771. struct batadv_hashtable *hash,
  1772. unsigned int bucket, long *idx_skip)
  1773. {
  1774. struct batadv_orig_node *orig_node;
  1775. long idx = 0;
  1776. spin_lock_bh(&hash->list_locks[bucket]);
  1777. cb->seq = atomic_read(&hash->generation) << 1 | 1;
  1778. hlist_for_each_entry(orig_node, &hash->table[bucket], hash_entry) {
  1779. if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
  1780. &orig_node->capa_initialized))
  1781. continue;
  1782. if (idx < *idx_skip)
  1783. goto skip;
  1784. if (batadv_mcast_flags_dump_entry(msg, portid, cb, orig_node)) {
  1785. spin_unlock_bh(&hash->list_locks[bucket]);
  1786. *idx_skip = idx;
  1787. return -EMSGSIZE;
  1788. }
  1789. skip:
  1790. idx++;
  1791. }
  1792. spin_unlock_bh(&hash->list_locks[bucket]);
  1793. return 0;
  1794. }
  1795. /**
  1796. * __batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket
  1797. * @msg: buffer for the message
  1798. * @portid: netlink port
  1799. * @cb: Control block containing additional options
  1800. * @bat_priv: the bat priv with all the mesh interface information
  1801. * @bucket: current bucket to dump
  1802. * @idx: index in current bucket to the next entry to dump
  1803. *
  1804. * Return: 0 or error code.
  1805. */
  1806. static int
  1807. __batadv_mcast_flags_dump(struct sk_buff *msg, u32 portid,
  1808. struct netlink_callback *cb,
  1809. struct batadv_priv *bat_priv, long *bucket, long *idx)
  1810. {
  1811. struct batadv_hashtable *hash = bat_priv->orig_hash;
  1812. long bucket_tmp = *bucket;
  1813. long idx_tmp = *idx;
  1814. while (bucket_tmp < hash->size) {
  1815. if (batadv_mcast_flags_dump_bucket(msg, portid, cb, hash,
  1816. bucket_tmp, &idx_tmp))
  1817. break;
  1818. bucket_tmp++;
  1819. idx_tmp = 0;
  1820. }
  1821. *bucket = bucket_tmp;
  1822. *idx = idx_tmp;
  1823. return msg->len;
  1824. }
  1825. /**
  1826. * batadv_mcast_netlink_get_primary() - get primary interface from netlink
  1827. * callback
  1828. * @cb: netlink callback structure
  1829. * @primary_if: the primary interface pointer to return the result in
  1830. *
  1831. * Return: 0 or error code.
  1832. */
  1833. static int
  1834. batadv_mcast_netlink_get_primary(struct netlink_callback *cb,
  1835. struct batadv_hard_iface **primary_if)
  1836. {
  1837. struct batadv_hard_iface *hard_iface = NULL;
  1838. struct net_device *mesh_iface;
  1839. struct batadv_priv *bat_priv;
  1840. int ret = 0;
  1841. mesh_iface = batadv_netlink_get_meshif(cb);
  1842. if (IS_ERR(mesh_iface))
  1843. return PTR_ERR(mesh_iface);
  1844. bat_priv = netdev_priv(mesh_iface);
  1845. hard_iface = batadv_primary_if_get_selected(bat_priv);
  1846. if (!hard_iface || hard_iface->if_status != BATADV_IF_ACTIVE) {
  1847. ret = -ENOENT;
  1848. goto out;
  1849. }
  1850. out:
  1851. dev_put(mesh_iface);
  1852. if (!ret && primary_if)
  1853. *primary_if = hard_iface;
  1854. else
  1855. batadv_hardif_put(hard_iface);
  1856. return ret;
  1857. }
  1858. /**
  1859. * batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket
  1860. * @msg: buffer for the message
  1861. * @cb: callback structure containing arguments
  1862. *
  1863. * Return: message length.
  1864. */
  1865. int batadv_mcast_flags_dump(struct sk_buff *msg, struct netlink_callback *cb)
  1866. {
  1867. struct batadv_hard_iface *primary_if = NULL;
  1868. int portid = NETLINK_CB(cb->skb).portid;
  1869. struct batadv_priv *bat_priv;
  1870. long *bucket = &cb->args[0];
  1871. long *idx = &cb->args[1];
  1872. int ret;
  1873. ret = batadv_mcast_netlink_get_primary(cb, &primary_if);
  1874. if (ret)
  1875. return ret;
  1876. bat_priv = netdev_priv(primary_if->mesh_iface);
  1877. ret = __batadv_mcast_flags_dump(msg, portid, cb, bat_priv, bucket, idx);
  1878. batadv_hardif_put(primary_if);
  1879. return ret;
  1880. }
  1881. /**
  1882. * batadv_mcast_free() - free the multicast optimizations structures
  1883. * @bat_priv: the bat priv with all the mesh interface information
  1884. */
  1885. void batadv_mcast_free(struct batadv_priv *bat_priv)
  1886. {
  1887. cancel_delayed_work_sync(&bat_priv->mcast.work);
  1888. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
  1889. batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST_TRACKER, 1);
  1890. batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
  1891. /* safely calling outside of worker, as worker was canceled above */
  1892. batadv_mcast_mla_tt_retract(bat_priv, NULL);
  1893. }
  1894. /**
  1895. * batadv_mcast_purge_orig() - reset originator global mcast state modifications
  1896. * @orig: the originator which is going to get purged
  1897. */
  1898. void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
  1899. {
  1900. struct batadv_priv *bat_priv = orig->bat_priv;
  1901. spin_lock_bh(&orig->mcast_handler_lock);
  1902. batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
  1903. batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
  1904. batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
  1905. batadv_mcast_want_rtr4_update(bat_priv, orig,
  1906. BATADV_MCAST_WANT_NO_RTR4);
  1907. batadv_mcast_want_rtr6_update(bat_priv, orig,
  1908. BATADV_MCAST_WANT_NO_RTR6);
  1909. batadv_mcast_have_mc_ptype_update(bat_priv, orig,
  1910. BATADV_MCAST_HAVE_MC_PTYPE_CAPA);
  1911. spin_unlock_bh(&orig->mcast_handler_lock);
  1912. }