multicast.c 41 KB

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