multicast.c 67 KB

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