multicast.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  1. /* Copyright (C) 2014-2016 B.A.T.M.A.N. contributors:
  2. *
  3. * Linus Lüssing
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "multicast.h"
  18. #include "main.h"
  19. #include <linux/atomic.h>
  20. #include <linux/bitops.h>
  21. #include <linux/bug.h>
  22. #include <linux/byteorder/generic.h>
  23. #include <linux/errno.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/fs.h>
  26. #include <linux/icmpv6.h>
  27. #include <linux/if_bridge.h>
  28. #include <linux/if_ether.h>
  29. #include <linux/igmp.h>
  30. #include <linux/in.h>
  31. #include <linux/in6.h>
  32. #include <linux/ip.h>
  33. #include <linux/ipv6.h>
  34. #include <linux/kernel.h>
  35. #include <linux/kref.h>
  36. #include <linux/list.h>
  37. #include <linux/lockdep.h>
  38. #include <linux/netdevice.h>
  39. #include <linux/printk.h>
  40. #include <linux/rculist.h>
  41. #include <linux/rcupdate.h>
  42. #include <linux/seq_file.h>
  43. #include <linux/skbuff.h>
  44. #include <linux/slab.h>
  45. #include <linux/spinlock.h>
  46. #include <linux/stddef.h>
  47. #include <linux/string.h>
  48. #include <linux/types.h>
  49. #include <net/addrconf.h>
  50. #include <net/if_inet6.h>
  51. #include <net/ip.h>
  52. #include <net/ipv6.h>
  53. #include "hard-interface.h"
  54. #include "hash.h"
  55. #include "log.h"
  56. #include "packet.h"
  57. #include "translation-table.h"
  58. #include "tvlv.h"
  59. /**
  60. * batadv_mcast_get_bridge - get the bridge on top of the softif if it exists
  61. * @soft_iface: netdev struct of the mesh interface
  62. *
  63. * If the given soft interface has a bridge on top then the refcount
  64. * of the according net device is increased.
  65. *
  66. * Return: NULL if no such bridge exists. Otherwise the net device of the
  67. * bridge.
  68. */
  69. static struct net_device *batadv_mcast_get_bridge(struct net_device *soft_iface)
  70. {
  71. struct net_device *upper = soft_iface;
  72. rcu_read_lock();
  73. do {
  74. upper = netdev_master_upper_dev_get_rcu(upper);
  75. } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
  76. if (upper)
  77. dev_hold(upper);
  78. rcu_read_unlock();
  79. return upper;
  80. }
  81. /**
  82. * batadv_mcast_mla_softif_get - get softif multicast listeners
  83. * @dev: the device to collect multicast addresses from
  84. * @mcast_list: a list to put found addresses into
  85. *
  86. * Collects multicast addresses of multicast listeners residing
  87. * on this kernel on the given soft interface, dev, in
  88. * the given mcast_list. In general, multicast listeners provided by
  89. * your multicast receiving applications run directly on this node.
  90. *
  91. * If there is a bridge interface on top of dev, collects from that one
  92. * instead. Just like with IP addresses and routes, multicast listeners
  93. * will(/should) register to the bridge interface instead of an
  94. * enslaved bat0.
  95. *
  96. * Return: -ENOMEM on memory allocation error or the number of
  97. * items added to the mcast_list otherwise.
  98. */
  99. static int batadv_mcast_mla_softif_get(struct net_device *dev,
  100. struct hlist_head *mcast_list)
  101. {
  102. struct net_device *bridge = batadv_mcast_get_bridge(dev);
  103. struct netdev_hw_addr *mc_list_entry;
  104. struct batadv_hw_addr *new;
  105. int ret = 0;
  106. netif_addr_lock_bh(bridge ? bridge : dev);
  107. netdev_for_each_mc_addr(mc_list_entry, bridge ? bridge : dev) {
  108. new = kmalloc(sizeof(*new), GFP_ATOMIC);
  109. if (!new) {
  110. ret = -ENOMEM;
  111. break;
  112. }
  113. ether_addr_copy(new->addr, mc_list_entry->addr);
  114. hlist_add_head(&new->list, mcast_list);
  115. ret++;
  116. }
  117. netif_addr_unlock_bh(bridge ? bridge : dev);
  118. if (bridge)
  119. dev_put(bridge);
  120. return ret;
  121. }
  122. /**
  123. * batadv_mcast_mla_is_duplicate - check whether an address is in a list
  124. * @mcast_addr: the multicast address to check
  125. * @mcast_list: the list with multicast addresses to search in
  126. *
  127. * Return: true if the given address is already in the given list.
  128. * Otherwise returns false.
  129. */
  130. static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr,
  131. struct hlist_head *mcast_list)
  132. {
  133. struct batadv_hw_addr *mcast_entry;
  134. hlist_for_each_entry(mcast_entry, mcast_list, list)
  135. if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
  136. return true;
  137. return false;
  138. }
  139. /**
  140. * batadv_mcast_mla_br_addr_cpy - copy a bridge multicast address
  141. * @dst: destination to write to - a multicast MAC address
  142. * @src: source to read from - a multicast IP address
  143. *
  144. * Converts a given multicast IPv4/IPv6 address from a bridge
  145. * to its matching multicast MAC address and copies it into the given
  146. * destination buffer.
  147. *
  148. * Caller needs to make sure the destination buffer can hold
  149. * at least ETH_ALEN bytes.
  150. */
  151. static void batadv_mcast_mla_br_addr_cpy(char *dst, const struct br_ip *src)
  152. {
  153. if (src->proto == htons(ETH_P_IP))
  154. ip_eth_mc_map(src->u.ip4, dst);
  155. #if IS_ENABLED(CONFIG_IPV6)
  156. else if (src->proto == htons(ETH_P_IPV6))
  157. ipv6_eth_mc_map(&src->u.ip6, dst);
  158. #endif
  159. else
  160. eth_zero_addr(dst);
  161. }
  162. /**
  163. * batadv_mcast_mla_bridge_get - get bridged-in multicast listeners
  164. * @dev: a bridge slave whose bridge to collect multicast addresses from
  165. * @mcast_list: a list to put found addresses into
  166. *
  167. * Collects multicast addresses of multicast listeners residing
  168. * on foreign, non-mesh devices which we gave access to our mesh via
  169. * a bridge on top of the given soft interface, dev, in the given
  170. * mcast_list.
  171. *
  172. * Return: -ENOMEM on memory allocation error or the number of
  173. * items added to the mcast_list otherwise.
  174. */
  175. static int batadv_mcast_mla_bridge_get(struct net_device *dev,
  176. struct hlist_head *mcast_list)
  177. {
  178. struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list);
  179. struct br_ip_list *br_ip_entry, *tmp;
  180. struct batadv_hw_addr *new;
  181. u8 mcast_addr[ETH_ALEN];
  182. int ret;
  183. /* we don't need to detect these devices/listeners, the IGMP/MLD
  184. * snooping code of the Linux bridge already does that for us
  185. */
  186. ret = br_multicast_list_adjacent(dev, &bridge_mcast_list);
  187. if (ret < 0)
  188. goto out;
  189. list_for_each_entry(br_ip_entry, &bridge_mcast_list, list) {
  190. batadv_mcast_mla_br_addr_cpy(mcast_addr, &br_ip_entry->addr);
  191. if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list))
  192. continue;
  193. new = kmalloc(sizeof(*new), GFP_ATOMIC);
  194. if (!new) {
  195. ret = -ENOMEM;
  196. break;
  197. }
  198. ether_addr_copy(new->addr, mcast_addr);
  199. hlist_add_head(&new->list, mcast_list);
  200. }
  201. out:
  202. list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) {
  203. list_del(&br_ip_entry->list);
  204. kfree(br_ip_entry);
  205. }
  206. return ret;
  207. }
  208. /**
  209. * batadv_mcast_mla_list_free - free a list of multicast addresses
  210. * @bat_priv: the bat priv with all the soft interface information
  211. * @mcast_list: the list to free
  212. *
  213. * Removes and frees all items in the given mcast_list.
  214. */
  215. static void batadv_mcast_mla_list_free(struct batadv_priv *bat_priv,
  216. struct hlist_head *mcast_list)
  217. {
  218. struct batadv_hw_addr *mcast_entry;
  219. struct hlist_node *tmp;
  220. lockdep_assert_held(&bat_priv->tt.commit_lock);
  221. hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
  222. hlist_del(&mcast_entry->list);
  223. kfree(mcast_entry);
  224. }
  225. }
  226. /**
  227. * batadv_mcast_mla_tt_retract - clean up multicast listener announcements
  228. * @bat_priv: the bat priv with all the soft interface information
  229. * @mcast_list: a list of addresses which should _not_ be removed
  230. *
  231. * Retracts the announcement of any multicast listener from the
  232. * translation table except the ones listed in the given mcast_list.
  233. *
  234. * If mcast_list is NULL then all are retracted.
  235. */
  236. static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
  237. struct hlist_head *mcast_list)
  238. {
  239. struct batadv_hw_addr *mcast_entry;
  240. struct hlist_node *tmp;
  241. lockdep_assert_held(&bat_priv->tt.commit_lock);
  242. hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
  243. list) {
  244. if (mcast_list &&
  245. batadv_mcast_mla_is_duplicate(mcast_entry->addr,
  246. mcast_list))
  247. continue;
  248. batadv_tt_local_remove(bat_priv, mcast_entry->addr,
  249. BATADV_NO_FLAGS,
  250. "mcast TT outdated", false);
  251. hlist_del(&mcast_entry->list);
  252. kfree(mcast_entry);
  253. }
  254. }
  255. /**
  256. * batadv_mcast_mla_tt_add - add multicast listener announcements
  257. * @bat_priv: the bat priv with all the soft interface information
  258. * @mcast_list: a list of addresses which are going to get added
  259. *
  260. * Adds multicast listener announcements from the given mcast_list to the
  261. * translation table if they have not been added yet.
  262. */
  263. static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
  264. struct hlist_head *mcast_list)
  265. {
  266. struct batadv_hw_addr *mcast_entry;
  267. struct hlist_node *tmp;
  268. lockdep_assert_held(&bat_priv->tt.commit_lock);
  269. if (!mcast_list)
  270. return;
  271. hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
  272. if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
  273. &bat_priv->mcast.mla_list))
  274. continue;
  275. if (!batadv_tt_local_add(bat_priv->soft_iface,
  276. mcast_entry->addr, BATADV_NO_FLAGS,
  277. BATADV_NULL_IFINDEX, BATADV_NO_MARK))
  278. continue;
  279. hlist_del(&mcast_entry->list);
  280. hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
  281. }
  282. }
  283. /**
  284. * batadv_mcast_has_bridge - check whether the soft-iface is bridged
  285. * @bat_priv: the bat priv with all the soft interface information
  286. *
  287. * Checks whether there is a bridge on top of our soft interface.
  288. *
  289. * Return: true if there is a bridge, false otherwise.
  290. */
  291. static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
  292. {
  293. struct net_device *upper = bat_priv->soft_iface;
  294. rcu_read_lock();
  295. do {
  296. upper = netdev_master_upper_dev_get_rcu(upper);
  297. } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
  298. rcu_read_unlock();
  299. return upper;
  300. }
  301. /**
  302. * batadv_mcast_querier_log - debug output regarding the querier status on link
  303. * @bat_priv: the bat priv with all the soft interface information
  304. * @str_proto: a string for the querier protocol (e.g. "IGMP" or "MLD")
  305. * @old_state: the previous querier state on our link
  306. * @new_state: the new querier state on our link
  307. *
  308. * Outputs debug messages to the logging facility with log level 'mcast'
  309. * regarding changes to the querier status on the link which are relevant
  310. * to our multicast optimizations.
  311. *
  312. * Usually this is about whether a querier appeared or vanished in
  313. * our mesh or whether the querier is in the suboptimal position of being
  314. * behind our local bridge segment: Snooping switches will directly
  315. * forward listener reports to the querier, therefore batman-adv and
  316. * the bridge will potentially not see these listeners - the querier is
  317. * potentially shadowing listeners from us then.
  318. *
  319. * This is only interesting for nodes with a bridge on top of their
  320. * soft interface.
  321. */
  322. static void
  323. batadv_mcast_querier_log(struct batadv_priv *bat_priv, char *str_proto,
  324. struct batadv_mcast_querier_state *old_state,
  325. struct batadv_mcast_querier_state *new_state)
  326. {
  327. if (!old_state->exists && new_state->exists)
  328. batadv_info(bat_priv->soft_iface, "%s Querier appeared\n",
  329. str_proto);
  330. else if (old_state->exists && !new_state->exists)
  331. batadv_info(bat_priv->soft_iface,
  332. "%s Querier disappeared - multicast optimizations disabled\n",
  333. str_proto);
  334. else if (!bat_priv->mcast.bridged && !new_state->exists)
  335. batadv_info(bat_priv->soft_iface,
  336. "No %s Querier present - multicast optimizations disabled\n",
  337. str_proto);
  338. if (new_state->exists) {
  339. if ((!old_state->shadowing && new_state->shadowing) ||
  340. (!old_state->exists && new_state->shadowing))
  341. batadv_dbg(BATADV_DBG_MCAST, bat_priv,
  342. "%s Querier is behind our bridged segment: Might shadow listeners\n",
  343. str_proto);
  344. else if (old_state->shadowing && !new_state->shadowing)
  345. batadv_dbg(BATADV_DBG_MCAST, bat_priv,
  346. "%s Querier is not behind our bridged segment\n",
  347. str_proto);
  348. }
  349. }
  350. /**
  351. * batadv_mcast_bridge_log - debug output for topology changes in bridged setups
  352. * @bat_priv: the bat priv with all the soft interface information
  353. * @bridged: a flag about whether the soft interface is currently bridged or not
  354. * @querier_ipv4: (maybe) new status of a potential, selected IGMP querier
  355. * @querier_ipv6: (maybe) new status of a potential, selected MLD querier
  356. *
  357. * If no bridges are ever used on this node, then this function does nothing.
  358. *
  359. * Otherwise this function outputs debug information to the 'mcast' log level
  360. * which might be relevant to our multicast optimizations.
  361. *
  362. * More precisely, it outputs information when a bridge interface is added or
  363. * removed from a soft interface. And when a bridge is present, it further
  364. * outputs information about the querier state which is relevant for the
  365. * multicast flags this node is going to set.
  366. */
  367. static void
  368. batadv_mcast_bridge_log(struct batadv_priv *bat_priv, bool bridged,
  369. struct batadv_mcast_querier_state *querier_ipv4,
  370. struct batadv_mcast_querier_state *querier_ipv6)
  371. {
  372. if (!bat_priv->mcast.bridged && bridged)
  373. batadv_dbg(BATADV_DBG_MCAST, bat_priv,
  374. "Bridge added: Setting Unsnoopables(U)-flag\n");
  375. else if (bat_priv->mcast.bridged && !bridged)
  376. batadv_dbg(BATADV_DBG_MCAST, bat_priv,
  377. "Bridge removed: Unsetting Unsnoopables(U)-flag\n");
  378. if (bridged) {
  379. batadv_mcast_querier_log(bat_priv, "IGMP",
  380. &bat_priv->mcast.querier_ipv4,
  381. querier_ipv4);
  382. batadv_mcast_querier_log(bat_priv, "MLD",
  383. &bat_priv->mcast.querier_ipv6,
  384. querier_ipv6);
  385. }
  386. }
  387. /**
  388. * batadv_mcast_flags_logs - output debug information about mcast flag changes
  389. * @bat_priv: the bat priv with all the soft interface information
  390. * @flags: flags indicating the new multicast state
  391. *
  392. * Whenever the multicast flags this nodes announces changes (@mcast_flags vs.
  393. * bat_priv->mcast.flags), this notifies userspace via the 'mcast' log level.
  394. */
  395. static void batadv_mcast_flags_log(struct batadv_priv *bat_priv, u8 flags)
  396. {
  397. u8 old_flags = bat_priv->mcast.flags;
  398. char str_old_flags[] = "[...]";
  399. sprintf(str_old_flags, "[%c%c%c]",
  400. (old_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
  401. (old_flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
  402. (old_flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
  403. batadv_dbg(BATADV_DBG_MCAST, bat_priv,
  404. "Changing multicast flags from '%s' to '[%c%c%c]'\n",
  405. bat_priv->mcast.enabled ? str_old_flags : "<undefined>",
  406. (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
  407. (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
  408. (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
  409. }
  410. /**
  411. * batadv_mcast_mla_tvlv_update - update multicast tvlv
  412. * @bat_priv: the bat priv with all the soft interface information
  413. *
  414. * Updates the own multicast tvlv with our current multicast related settings,
  415. * capabilities and inabilities.
  416. *
  417. * Return: false if we want all IPv4 && IPv6 multicast traffic and true
  418. * otherwise.
  419. */
  420. static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
  421. {
  422. struct batadv_tvlv_mcast_data mcast_data;
  423. struct batadv_mcast_querier_state querier4 = {false, false};
  424. struct batadv_mcast_querier_state querier6 = {false, false};
  425. struct net_device *dev = bat_priv->soft_iface;
  426. bool bridged;
  427. mcast_data.flags = BATADV_NO_FLAGS;
  428. memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
  429. bridged = batadv_mcast_has_bridge(bat_priv);
  430. if (!bridged)
  431. goto update;
  432. #if !IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING)
  433. pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
  434. #endif
  435. querier4.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP);
  436. querier4.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP);
  437. querier6.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6);
  438. querier6.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6);
  439. mcast_data.flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES;
  440. /* 1) If no querier exists at all, then multicast listeners on
  441. * our local TT clients behind the bridge will keep silent.
  442. * 2) If the selected querier is on one of our local TT clients,
  443. * behind the bridge, then this querier might shadow multicast
  444. * listeners on our local TT clients, behind this bridge.
  445. *
  446. * In both cases, we will signalize other batman nodes that
  447. * we need all multicast traffic of the according protocol.
  448. */
  449. if (!querier4.exists || querier4.shadowing)
  450. mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV4;
  451. if (!querier6.exists || querier6.shadowing)
  452. mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV6;
  453. update:
  454. batadv_mcast_bridge_log(bat_priv, bridged, &querier4, &querier6);
  455. bat_priv->mcast.querier_ipv4.exists = querier4.exists;
  456. bat_priv->mcast.querier_ipv4.shadowing = querier4.shadowing;
  457. bat_priv->mcast.querier_ipv6.exists = querier6.exists;
  458. bat_priv->mcast.querier_ipv6.shadowing = querier6.shadowing;
  459. bat_priv->mcast.bridged = bridged;
  460. if (!bat_priv->mcast.enabled ||
  461. mcast_data.flags != bat_priv->mcast.flags) {
  462. batadv_mcast_flags_log(bat_priv, mcast_data.flags);
  463. batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
  464. &mcast_data, sizeof(mcast_data));
  465. bat_priv->mcast.flags = mcast_data.flags;
  466. bat_priv->mcast.enabled = true;
  467. }
  468. return !(mcast_data.flags & BATADV_MCAST_WANT_ALL_IPV4 &&
  469. mcast_data.flags & BATADV_MCAST_WANT_ALL_IPV6);
  470. }
  471. /**
  472. * batadv_mcast_mla_update - update the own MLAs
  473. * @bat_priv: the bat priv with all the soft interface information
  474. *
  475. * Updates the own multicast listener announcements in the translation
  476. * table as well as the own, announced multicast tvlv container.
  477. */
  478. void batadv_mcast_mla_update(struct batadv_priv *bat_priv)
  479. {
  480. struct net_device *soft_iface = bat_priv->soft_iface;
  481. struct hlist_head mcast_list = HLIST_HEAD_INIT;
  482. int ret;
  483. if (!batadv_mcast_mla_tvlv_update(bat_priv))
  484. goto update;
  485. ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list);
  486. if (ret < 0)
  487. goto out;
  488. ret = batadv_mcast_mla_bridge_get(soft_iface, &mcast_list);
  489. if (ret < 0)
  490. goto out;
  491. update:
  492. batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
  493. batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
  494. out:
  495. batadv_mcast_mla_list_free(bat_priv, &mcast_list);
  496. }
  497. /**
  498. * batadv_mcast_is_report_ipv4 - check for IGMP reports
  499. * @skb: the ethernet frame destined for the mesh
  500. *
  501. * This call might reallocate skb data.
  502. *
  503. * Checks whether the given frame is a valid IGMP report.
  504. *
  505. * Return: If so then true, otherwise false.
  506. */
  507. static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb)
  508. {
  509. if (ip_mc_check_igmp(skb, NULL) < 0)
  510. return false;
  511. switch (igmp_hdr(skb)->type) {
  512. case IGMP_HOST_MEMBERSHIP_REPORT:
  513. case IGMPV2_HOST_MEMBERSHIP_REPORT:
  514. case IGMPV3_HOST_MEMBERSHIP_REPORT:
  515. return true;
  516. }
  517. return false;
  518. }
  519. /**
  520. * batadv_mcast_forw_mode_check_ipv4 - check for optimized forwarding potential
  521. * @bat_priv: the bat priv with all the soft interface information
  522. * @skb: the IPv4 packet to check
  523. * @is_unsnoopable: stores whether the destination is snoopable
  524. *
  525. * Checks whether the given IPv4 packet has the potential to be forwarded with a
  526. * mode more optimal than classic flooding.
  527. *
  528. * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
  529. * allocation failure.
  530. */
  531. static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
  532. struct sk_buff *skb,
  533. bool *is_unsnoopable)
  534. {
  535. struct iphdr *iphdr;
  536. /* We might fail due to out-of-memory -> drop it */
  537. if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
  538. return -ENOMEM;
  539. if (batadv_mcast_is_report_ipv4(skb))
  540. return -EINVAL;
  541. iphdr = ip_hdr(skb);
  542. /* TODO: Implement Multicast Router Discovery (RFC4286),
  543. * then allow scope > link local, too
  544. */
  545. if (!ipv4_is_local_multicast(iphdr->daddr))
  546. return -EINVAL;
  547. /* link-local multicast listeners behind a bridge are
  548. * not snoopable (see RFC4541, section 2.1.2.2)
  549. */
  550. *is_unsnoopable = true;
  551. return 0;
  552. }
  553. #if IS_ENABLED(CONFIG_IPV6)
  554. /**
  555. * batadv_mcast_is_report_ipv6 - check for MLD reports
  556. * @skb: the ethernet frame destined for the mesh
  557. *
  558. * This call might reallocate skb data.
  559. *
  560. * Checks whether the given frame is a valid MLD report.
  561. *
  562. * Return: If so then true, otherwise false.
  563. */
  564. static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb)
  565. {
  566. if (ipv6_mc_check_mld(skb, NULL) < 0)
  567. return false;
  568. switch (icmp6_hdr(skb)->icmp6_type) {
  569. case ICMPV6_MGM_REPORT:
  570. case ICMPV6_MLD2_REPORT:
  571. return true;
  572. }
  573. return false;
  574. }
  575. /**
  576. * batadv_mcast_forw_mode_check_ipv6 - check for optimized forwarding potential
  577. * @bat_priv: the bat priv with all the soft interface information
  578. * @skb: the IPv6 packet to check
  579. * @is_unsnoopable: stores whether the destination is snoopable
  580. *
  581. * Checks whether the given IPv6 packet has the potential to be forwarded with a
  582. * mode more optimal than classic flooding.
  583. *
  584. * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
  585. */
  586. static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
  587. struct sk_buff *skb,
  588. bool *is_unsnoopable)
  589. {
  590. struct ipv6hdr *ip6hdr;
  591. /* We might fail due to out-of-memory -> drop it */
  592. if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
  593. return -ENOMEM;
  594. if (batadv_mcast_is_report_ipv6(skb))
  595. return -EINVAL;
  596. ip6hdr = ipv6_hdr(skb);
  597. /* TODO: Implement Multicast Router Discovery (RFC4286),
  598. * then allow scope > link local, too
  599. */
  600. if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL)
  601. return -EINVAL;
  602. /* link-local-all-nodes multicast listeners behind a bridge are
  603. * not snoopable (see RFC4541, section 3, paragraph 3)
  604. */
  605. if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
  606. *is_unsnoopable = true;
  607. return 0;
  608. }
  609. #endif
  610. /**
  611. * batadv_mcast_forw_mode_check - check for optimized forwarding potential
  612. * @bat_priv: the bat priv with all the soft interface information
  613. * @skb: the multicast frame to check
  614. * @is_unsnoopable: stores whether the destination is snoopable
  615. *
  616. * Checks whether the given multicast ethernet frame has the potential to be
  617. * forwarded with a mode more optimal than classic flooding.
  618. *
  619. * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
  620. */
  621. static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
  622. struct sk_buff *skb,
  623. bool *is_unsnoopable)
  624. {
  625. struct ethhdr *ethhdr = eth_hdr(skb);
  626. if (!atomic_read(&bat_priv->multicast_mode))
  627. return -EINVAL;
  628. if (atomic_read(&bat_priv->mcast.num_disabled))
  629. return -EINVAL;
  630. switch (ntohs(ethhdr->h_proto)) {
  631. case ETH_P_IP:
  632. return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
  633. is_unsnoopable);
  634. #if IS_ENABLED(CONFIG_IPV6)
  635. case ETH_P_IPV6:
  636. return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
  637. is_unsnoopable);
  638. #endif
  639. default:
  640. return -EINVAL;
  641. }
  642. }
  643. /**
  644. * batadv_mcast_forw_want_all_ip_count - count nodes with unspecific mcast
  645. * interest
  646. * @bat_priv: the bat priv with all the soft interface information
  647. * @ethhdr: ethernet header of a packet
  648. *
  649. * Return: the number of nodes which want all IPv4 multicast traffic if the
  650. * given ethhdr is from an IPv4 packet or the number of nodes which want all
  651. * IPv6 traffic if it matches an IPv6 packet.
  652. */
  653. static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
  654. struct ethhdr *ethhdr)
  655. {
  656. switch (ntohs(ethhdr->h_proto)) {
  657. case ETH_P_IP:
  658. return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
  659. case ETH_P_IPV6:
  660. return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
  661. default:
  662. /* we shouldn't be here... */
  663. return 0;
  664. }
  665. }
  666. /**
  667. * batadv_mcast_forw_tt_node_get - get a multicast tt node
  668. * @bat_priv: the bat priv with all the soft interface information
  669. * @ethhdr: the ether header containing the multicast destination
  670. *
  671. * Return: an orig_node matching the multicast address provided by ethhdr
  672. * via a translation table lookup. This increases the returned nodes refcount.
  673. */
  674. static struct batadv_orig_node *
  675. batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
  676. struct ethhdr *ethhdr)
  677. {
  678. return batadv_transtable_search(bat_priv, NULL, ethhdr->h_dest,
  679. BATADV_NO_FLAGS);
  680. }
  681. /**
  682. * batadv_mcast_forw_ipv4_node_get - get a node with an ipv4 flag
  683. * @bat_priv: the bat priv with all the soft interface information
  684. *
  685. * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
  686. * increases its refcount.
  687. */
  688. static struct batadv_orig_node *
  689. batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
  690. {
  691. struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
  692. rcu_read_lock();
  693. hlist_for_each_entry_rcu(tmp_orig_node,
  694. &bat_priv->mcast.want_all_ipv4_list,
  695. mcast_want_all_ipv4_node) {
  696. if (!kref_get_unless_zero(&tmp_orig_node->refcount))
  697. continue;
  698. orig_node = tmp_orig_node;
  699. break;
  700. }
  701. rcu_read_unlock();
  702. return orig_node;
  703. }
  704. /**
  705. * batadv_mcast_forw_ipv6_node_get - get a node with an ipv6 flag
  706. * @bat_priv: the bat priv with all the soft interface information
  707. *
  708. * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
  709. * and increases its refcount.
  710. */
  711. static struct batadv_orig_node *
  712. batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
  713. {
  714. struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
  715. rcu_read_lock();
  716. hlist_for_each_entry_rcu(tmp_orig_node,
  717. &bat_priv->mcast.want_all_ipv6_list,
  718. mcast_want_all_ipv6_node) {
  719. if (!kref_get_unless_zero(&tmp_orig_node->refcount))
  720. continue;
  721. orig_node = tmp_orig_node;
  722. break;
  723. }
  724. rcu_read_unlock();
  725. return orig_node;
  726. }
  727. /**
  728. * batadv_mcast_forw_ip_node_get - get a node with an ipv4/ipv6 flag
  729. * @bat_priv: the bat priv with all the soft interface information
  730. * @ethhdr: an ethernet header to determine the protocol family from
  731. *
  732. * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
  733. * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and
  734. * increases its refcount.
  735. */
  736. static struct batadv_orig_node *
  737. batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
  738. struct ethhdr *ethhdr)
  739. {
  740. switch (ntohs(ethhdr->h_proto)) {
  741. case ETH_P_IP:
  742. return batadv_mcast_forw_ipv4_node_get(bat_priv);
  743. case ETH_P_IPV6:
  744. return batadv_mcast_forw_ipv6_node_get(bat_priv);
  745. default:
  746. /* we shouldn't be here... */
  747. return NULL;
  748. }
  749. }
  750. /**
  751. * batadv_mcast_forw_unsnoop_node_get - get a node with an unsnoopable flag
  752. * @bat_priv: the bat priv with all the soft interface information
  753. *
  754. * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
  755. * set and increases its refcount.
  756. */
  757. static struct batadv_orig_node *
  758. batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
  759. {
  760. struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
  761. rcu_read_lock();
  762. hlist_for_each_entry_rcu(tmp_orig_node,
  763. &bat_priv->mcast.want_all_unsnoopables_list,
  764. mcast_want_all_unsnoopables_node) {
  765. if (!kref_get_unless_zero(&tmp_orig_node->refcount))
  766. continue;
  767. orig_node = tmp_orig_node;
  768. break;
  769. }
  770. rcu_read_unlock();
  771. return orig_node;
  772. }
  773. /**
  774. * batadv_mcast_forw_mode - check on how to forward a multicast packet
  775. * @bat_priv: the bat priv with all the soft interface information
  776. * @skb: The multicast packet to check
  777. * @orig: an originator to be set to forward the skb to
  778. *
  779. * Return: the forwarding mode as enum batadv_forw_mode and in case of
  780. * BATADV_FORW_SINGLE set the orig to the single originator the skb
  781. * should be forwarded to.
  782. */
  783. enum batadv_forw_mode
  784. batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
  785. struct batadv_orig_node **orig)
  786. {
  787. int ret, tt_count, ip_count, unsnoop_count, total_count;
  788. bool is_unsnoopable = false;
  789. struct ethhdr *ethhdr;
  790. ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable);
  791. if (ret == -ENOMEM)
  792. return BATADV_FORW_NONE;
  793. else if (ret < 0)
  794. return BATADV_FORW_ALL;
  795. ethhdr = eth_hdr(skb);
  796. tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
  797. BATADV_NO_FLAGS);
  798. ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
  799. unsnoop_count = !is_unsnoopable ? 0 :
  800. atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
  801. total_count = tt_count + ip_count + unsnoop_count;
  802. switch (total_count) {
  803. case 1:
  804. if (tt_count)
  805. *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
  806. else if (ip_count)
  807. *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
  808. else if (unsnoop_count)
  809. *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
  810. if (*orig)
  811. return BATADV_FORW_SINGLE;
  812. /* fall through */
  813. case 0:
  814. return BATADV_FORW_NONE;
  815. default:
  816. return BATADV_FORW_ALL;
  817. }
  818. }
  819. /**
  820. * batadv_mcast_want_unsnoop_update - update unsnoop counter and list
  821. * @bat_priv: the bat priv with all the soft interface information
  822. * @orig: the orig_node which multicast state might have changed of
  823. * @mcast_flags: flags indicating the new multicast state
  824. *
  825. * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
  826. * orig, has toggled then this method updates counter and list accordingly.
  827. *
  828. * Caller needs to hold orig->mcast_handler_lock.
  829. */
  830. static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
  831. struct batadv_orig_node *orig,
  832. u8 mcast_flags)
  833. {
  834. struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
  835. struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
  836. lockdep_assert_held(&orig->mcast_handler_lock);
  837. /* switched from flag unset to set */
  838. if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
  839. !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
  840. atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
  841. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  842. /* flag checks above + mcast_handler_lock prevents this */
  843. WARN_ON(!hlist_unhashed(node));
  844. hlist_add_head_rcu(node, head);
  845. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  846. /* switched from flag set to unset */
  847. } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
  848. orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
  849. atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
  850. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  851. /* flag checks above + mcast_handler_lock prevents this */
  852. WARN_ON(hlist_unhashed(node));
  853. hlist_del_init_rcu(node);
  854. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  855. }
  856. }
  857. /**
  858. * batadv_mcast_want_ipv4_update - update want-all-ipv4 counter and list
  859. * @bat_priv: the bat priv with all the soft interface information
  860. * @orig: the orig_node which multicast state might have changed of
  861. * @mcast_flags: flags indicating the new multicast state
  862. *
  863. * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
  864. * toggled then this method updates counter and list accordingly.
  865. *
  866. * Caller needs to hold orig->mcast_handler_lock.
  867. */
  868. static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
  869. struct batadv_orig_node *orig,
  870. u8 mcast_flags)
  871. {
  872. struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
  873. struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
  874. lockdep_assert_held(&orig->mcast_handler_lock);
  875. /* switched from flag unset to set */
  876. if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
  877. !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
  878. atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
  879. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  880. /* flag checks above + mcast_handler_lock prevents this */
  881. WARN_ON(!hlist_unhashed(node));
  882. hlist_add_head_rcu(node, head);
  883. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  884. /* switched from flag set to unset */
  885. } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
  886. orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
  887. atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
  888. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  889. /* flag checks above + mcast_handler_lock prevents this */
  890. WARN_ON(hlist_unhashed(node));
  891. hlist_del_init_rcu(node);
  892. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  893. }
  894. }
  895. /**
  896. * batadv_mcast_want_ipv6_update - update want-all-ipv6 counter and list
  897. * @bat_priv: the bat priv with all the soft interface information
  898. * @orig: the orig_node which multicast state might have changed of
  899. * @mcast_flags: flags indicating the new multicast state
  900. *
  901. * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
  902. * toggled then this method updates counter and list accordingly.
  903. *
  904. * Caller needs to hold orig->mcast_handler_lock.
  905. */
  906. static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
  907. struct batadv_orig_node *orig,
  908. u8 mcast_flags)
  909. {
  910. struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
  911. struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
  912. lockdep_assert_held(&orig->mcast_handler_lock);
  913. /* switched from flag unset to set */
  914. if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
  915. !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
  916. atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
  917. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  918. /* flag checks above + mcast_handler_lock prevents this */
  919. WARN_ON(!hlist_unhashed(node));
  920. hlist_add_head_rcu(node, head);
  921. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  922. /* switched from flag set to unset */
  923. } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
  924. orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
  925. atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
  926. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  927. /* flag checks above + mcast_handler_lock prevents this */
  928. WARN_ON(hlist_unhashed(node));
  929. hlist_del_init_rcu(node);
  930. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  931. }
  932. }
  933. /**
  934. * batadv_mcast_tvlv_ogm_handler - process incoming multicast tvlv container
  935. * @bat_priv: the bat priv with all the soft interface information
  936. * @orig: the orig_node of the ogm
  937. * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
  938. * @tvlv_value: tvlv buffer containing the multicast data
  939. * @tvlv_value_len: tvlv buffer length
  940. */
  941. static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv,
  942. struct batadv_orig_node *orig,
  943. u8 flags,
  944. void *tvlv_value,
  945. u16 tvlv_value_len)
  946. {
  947. bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
  948. u8 mcast_flags = BATADV_NO_FLAGS;
  949. bool orig_initialized;
  950. if (orig_mcast_enabled && tvlv_value &&
  951. (tvlv_value_len >= sizeof(mcast_flags)))
  952. mcast_flags = *(u8 *)tvlv_value;
  953. spin_lock_bh(&orig->mcast_handler_lock);
  954. orig_initialized = test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
  955. &orig->capa_initialized);
  956. /* If mcast support is turned on decrease the disabled mcast node
  957. * counter only if we had increased it for this node before. If this
  958. * is a completely new orig_node no need to decrease the counter.
  959. */
  960. if (orig_mcast_enabled &&
  961. !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
  962. if (orig_initialized)
  963. atomic_dec(&bat_priv->mcast.num_disabled);
  964. set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
  965. /* If mcast support is being switched off or if this is an initial
  966. * OGM without mcast support then increase the disabled mcast
  967. * node counter.
  968. */
  969. } else if (!orig_mcast_enabled &&
  970. (test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) ||
  971. !orig_initialized)) {
  972. atomic_inc(&bat_priv->mcast.num_disabled);
  973. clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
  974. }
  975. set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
  976. batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
  977. batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
  978. batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
  979. orig->mcast_flags = mcast_flags;
  980. spin_unlock_bh(&orig->mcast_handler_lock);
  981. }
  982. /**
  983. * batadv_mcast_init - initialize the multicast optimizations structures
  984. * @bat_priv: the bat priv with all the soft interface information
  985. */
  986. void batadv_mcast_init(struct batadv_priv *bat_priv)
  987. {
  988. batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
  989. NULL, BATADV_TVLV_MCAST, 2,
  990. BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
  991. }
  992. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  993. /**
  994. * batadv_mcast_flags_print_header - print own mcast flags to debugfs table
  995. * @bat_priv: the bat priv with all the soft interface information
  996. * @seq: debugfs table seq_file struct
  997. *
  998. * Prints our own multicast flags including a more specific reason why
  999. * they are set, that is prints the bridge and querier state too, to
  1000. * the debugfs table specified via @seq.
  1001. */
  1002. static void batadv_mcast_flags_print_header(struct batadv_priv *bat_priv,
  1003. struct seq_file *seq)
  1004. {
  1005. u8 flags = bat_priv->mcast.flags;
  1006. char querier4, querier6, shadowing4, shadowing6;
  1007. bool bridged = bat_priv->mcast.bridged;
  1008. if (bridged) {
  1009. querier4 = bat_priv->mcast.querier_ipv4.exists ? '.' : '4';
  1010. querier6 = bat_priv->mcast.querier_ipv6.exists ? '.' : '6';
  1011. shadowing4 = bat_priv->mcast.querier_ipv4.shadowing ? '4' : '.';
  1012. shadowing6 = bat_priv->mcast.querier_ipv6.shadowing ? '6' : '.';
  1013. } else {
  1014. querier4 = '?';
  1015. querier6 = '?';
  1016. shadowing4 = '?';
  1017. shadowing6 = '?';
  1018. }
  1019. seq_printf(seq, "Multicast flags (own flags: [%c%c%c])\n",
  1020. (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
  1021. (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
  1022. (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
  1023. seq_printf(seq, "* Bridged [U]\t\t\t\t%c\n", bridged ? 'U' : '.');
  1024. seq_printf(seq, "* No IGMP/MLD Querier [4/6]:\t\t%c/%c\n",
  1025. querier4, querier6);
  1026. seq_printf(seq, "* Shadowing IGMP/MLD Querier [4/6]:\t%c/%c\n",
  1027. shadowing4, shadowing6);
  1028. seq_puts(seq, "-------------------------------------------\n");
  1029. seq_printf(seq, " %-10s %s\n", "Originator", "Flags");
  1030. }
  1031. /**
  1032. * batadv_mcast_flags_seq_print_text - print the mcast flags of other nodes
  1033. * @seq: seq file to print on
  1034. * @offset: not used
  1035. *
  1036. * This prints a table of (primary) originators and their according
  1037. * multicast flags, including (in the header) our own.
  1038. *
  1039. * Return: always 0
  1040. */
  1041. int batadv_mcast_flags_seq_print_text(struct seq_file *seq, void *offset)
  1042. {
  1043. struct net_device *net_dev = (struct net_device *)seq->private;
  1044. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  1045. struct batadv_hard_iface *primary_if;
  1046. struct batadv_hashtable *hash = bat_priv->orig_hash;
  1047. struct batadv_orig_node *orig_node;
  1048. struct hlist_head *head;
  1049. u8 flags;
  1050. u32 i;
  1051. primary_if = batadv_seq_print_text_primary_if_get(seq);
  1052. if (!primary_if)
  1053. return 0;
  1054. batadv_mcast_flags_print_header(bat_priv, seq);
  1055. for (i = 0; i < hash->size; i++) {
  1056. head = &hash->table[i];
  1057. rcu_read_lock();
  1058. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  1059. if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
  1060. &orig_node->capa_initialized))
  1061. continue;
  1062. if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
  1063. &orig_node->capabilities)) {
  1064. seq_printf(seq, "%pM -\n", orig_node->orig);
  1065. continue;
  1066. }
  1067. flags = orig_node->mcast_flags;
  1068. seq_printf(seq, "%pM [%c%c%c]\n", orig_node->orig,
  1069. (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)
  1070. ? 'U' : '.',
  1071. (flags & BATADV_MCAST_WANT_ALL_IPV4)
  1072. ? '4' : '.',
  1073. (flags & BATADV_MCAST_WANT_ALL_IPV6)
  1074. ? '6' : '.');
  1075. }
  1076. rcu_read_unlock();
  1077. }
  1078. batadv_hardif_put(primary_if);
  1079. return 0;
  1080. }
  1081. #endif
  1082. /**
  1083. * batadv_mcast_free - free the multicast optimizations structures
  1084. * @bat_priv: the bat priv with all the soft interface information
  1085. */
  1086. void batadv_mcast_free(struct batadv_priv *bat_priv)
  1087. {
  1088. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
  1089. batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
  1090. spin_lock_bh(&bat_priv->tt.commit_lock);
  1091. batadv_mcast_mla_tt_retract(bat_priv, NULL);
  1092. spin_unlock_bh(&bat_priv->tt.commit_lock);
  1093. }
  1094. /**
  1095. * batadv_mcast_purge_orig - reset originator global mcast state modifications
  1096. * @orig: the originator which is going to get purged
  1097. */
  1098. void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
  1099. {
  1100. struct batadv_priv *bat_priv = orig->bat_priv;
  1101. spin_lock_bh(&orig->mcast_handler_lock);
  1102. if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities) &&
  1103. test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized))
  1104. atomic_dec(&bat_priv->mcast.num_disabled);
  1105. batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
  1106. batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
  1107. batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
  1108. spin_unlock_bh(&orig->mcast_handler_lock);
  1109. }