multicast.c 47 KB

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