multicast.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  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. * Do not call outside of the mcast worker! (or cancel mcast worker first)
  294. */
  295. static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
  296. struct hlist_head *mcast_list)
  297. {
  298. struct batadv_hw_addr *mcast_entry;
  299. struct hlist_node *tmp;
  300. WARN_ON(delayed_work_pending(&bat_priv->mcast.work));
  301. hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
  302. list) {
  303. if (mcast_list &&
  304. batadv_mcast_mla_is_duplicate(mcast_entry->addr,
  305. mcast_list))
  306. continue;
  307. batadv_tt_local_remove(bat_priv, mcast_entry->addr,
  308. BATADV_NO_FLAGS,
  309. "mcast TT outdated", false);
  310. hlist_del(&mcast_entry->list);
  311. kfree(mcast_entry);
  312. }
  313. }
  314. /**
  315. * batadv_mcast_mla_tt_add() - add multicast listener announcements
  316. * @bat_priv: the bat priv with all the soft interface information
  317. * @mcast_list: a list of addresses which are going to get added
  318. *
  319. * Adds multicast listener announcements from the given mcast_list to the
  320. * translation table if they have not been added yet.
  321. *
  322. * Do not call outside of the mcast worker! (or cancel mcast worker first)
  323. */
  324. static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
  325. struct hlist_head *mcast_list)
  326. {
  327. struct batadv_hw_addr *mcast_entry;
  328. struct hlist_node *tmp;
  329. WARN_ON(delayed_work_pending(&bat_priv->mcast.work));
  330. if (!mcast_list)
  331. return;
  332. hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
  333. if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
  334. &bat_priv->mcast.mla_list))
  335. continue;
  336. if (!batadv_tt_local_add(bat_priv->soft_iface,
  337. mcast_entry->addr, BATADV_NO_FLAGS,
  338. BATADV_NULL_IFINDEX, BATADV_NO_MARK))
  339. continue;
  340. hlist_del(&mcast_entry->list);
  341. hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
  342. }
  343. }
  344. /**
  345. * batadv_mcast_has_bridge() - check whether the soft-iface is bridged
  346. * @bat_priv: the bat priv with all the soft interface information
  347. *
  348. * Checks whether there is a bridge on top of our soft interface.
  349. *
  350. * Return: true if there is a bridge, false otherwise.
  351. */
  352. static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
  353. {
  354. struct net_device *upper = bat_priv->soft_iface;
  355. rcu_read_lock();
  356. do {
  357. upper = netdev_master_upper_dev_get_rcu(upper);
  358. } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
  359. rcu_read_unlock();
  360. return upper;
  361. }
  362. /**
  363. * batadv_mcast_querier_log() - debug output regarding the querier status on
  364. * link
  365. * @bat_priv: the bat priv with all the soft interface information
  366. * @str_proto: a string for the querier protocol (e.g. "IGMP" or "MLD")
  367. * @old_state: the previous querier state on our link
  368. * @new_state: the new querier state on our link
  369. *
  370. * Outputs debug messages to the logging facility with log level 'mcast'
  371. * regarding changes to the querier status on the link which are relevant
  372. * to our multicast optimizations.
  373. *
  374. * Usually this is about whether a querier appeared or vanished in
  375. * our mesh or whether the querier is in the suboptimal position of being
  376. * behind our local bridge segment: Snooping switches will directly
  377. * forward listener reports to the querier, therefore batman-adv and
  378. * the bridge will potentially not see these listeners - the querier is
  379. * potentially shadowing listeners from us then.
  380. *
  381. * This is only interesting for nodes with a bridge on top of their
  382. * soft interface.
  383. */
  384. static void
  385. batadv_mcast_querier_log(struct batadv_priv *bat_priv, char *str_proto,
  386. struct batadv_mcast_querier_state *old_state,
  387. struct batadv_mcast_querier_state *new_state)
  388. {
  389. if (!old_state->exists && new_state->exists)
  390. batadv_info(bat_priv->soft_iface, "%s Querier appeared\n",
  391. str_proto);
  392. else if (old_state->exists && !new_state->exists)
  393. batadv_info(bat_priv->soft_iface,
  394. "%s Querier disappeared - multicast optimizations disabled\n",
  395. str_proto);
  396. else if (!bat_priv->mcast.bridged && !new_state->exists)
  397. batadv_info(bat_priv->soft_iface,
  398. "No %s Querier present - multicast optimizations disabled\n",
  399. str_proto);
  400. if (new_state->exists) {
  401. if ((!old_state->shadowing && new_state->shadowing) ||
  402. (!old_state->exists && new_state->shadowing))
  403. batadv_dbg(BATADV_DBG_MCAST, bat_priv,
  404. "%s Querier is behind our bridged segment: Might shadow listeners\n",
  405. str_proto);
  406. else if (old_state->shadowing && !new_state->shadowing)
  407. batadv_dbg(BATADV_DBG_MCAST, bat_priv,
  408. "%s Querier is not behind our bridged segment\n",
  409. str_proto);
  410. }
  411. }
  412. /**
  413. * batadv_mcast_bridge_log() - debug output for topology changes in bridged
  414. * setups
  415. * @bat_priv: the bat priv with all the soft interface information
  416. * @bridged: a flag about whether the soft interface is currently bridged or not
  417. * @querier_ipv4: (maybe) new status of a potential, selected IGMP querier
  418. * @querier_ipv6: (maybe) new status of a potential, selected MLD querier
  419. *
  420. * If no bridges are ever used on this node, then this function does nothing.
  421. *
  422. * Otherwise this function outputs debug information to the 'mcast' log level
  423. * which might be relevant to our multicast optimizations.
  424. *
  425. * More precisely, it outputs information when a bridge interface is added or
  426. * removed from a soft interface. And when a bridge is present, it further
  427. * outputs information about the querier state which is relevant for the
  428. * multicast flags this node is going to set.
  429. */
  430. static void
  431. batadv_mcast_bridge_log(struct batadv_priv *bat_priv, bool bridged,
  432. struct batadv_mcast_querier_state *querier_ipv4,
  433. struct batadv_mcast_querier_state *querier_ipv6)
  434. {
  435. if (!bat_priv->mcast.bridged && bridged)
  436. batadv_dbg(BATADV_DBG_MCAST, bat_priv,
  437. "Bridge added: Setting Unsnoopables(U)-flag\n");
  438. else if (bat_priv->mcast.bridged && !bridged)
  439. batadv_dbg(BATADV_DBG_MCAST, bat_priv,
  440. "Bridge removed: Unsetting Unsnoopables(U)-flag\n");
  441. if (bridged) {
  442. batadv_mcast_querier_log(bat_priv, "IGMP",
  443. &bat_priv->mcast.querier_ipv4,
  444. querier_ipv4);
  445. batadv_mcast_querier_log(bat_priv, "MLD",
  446. &bat_priv->mcast.querier_ipv6,
  447. querier_ipv6);
  448. }
  449. }
  450. /**
  451. * batadv_mcast_flags_logs() - output debug information about mcast flag changes
  452. * @bat_priv: the bat priv with all the soft interface information
  453. * @flags: flags indicating the new multicast state
  454. *
  455. * Whenever the multicast flags this nodes announces changes (@mcast_flags vs.
  456. * bat_priv->mcast.flags), this notifies userspace via the 'mcast' log level.
  457. */
  458. static void batadv_mcast_flags_log(struct batadv_priv *bat_priv, u8 flags)
  459. {
  460. u8 old_flags = bat_priv->mcast.flags;
  461. char str_old_flags[] = "[...]";
  462. sprintf(str_old_flags, "[%c%c%c]",
  463. (old_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
  464. (old_flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
  465. (old_flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
  466. batadv_dbg(BATADV_DBG_MCAST, bat_priv,
  467. "Changing multicast flags from '%s' to '[%c%c%c]'\n",
  468. bat_priv->mcast.enabled ? str_old_flags : "<undefined>",
  469. (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
  470. (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
  471. (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
  472. }
  473. /**
  474. * batadv_mcast_mla_tvlv_update() - update multicast tvlv
  475. * @bat_priv: the bat priv with all the soft interface information
  476. *
  477. * Updates the own multicast tvlv with our current multicast related settings,
  478. * capabilities and inabilities.
  479. *
  480. * Return: false if we want all IPv4 && IPv6 multicast traffic and true
  481. * otherwise.
  482. */
  483. static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
  484. {
  485. struct batadv_tvlv_mcast_data mcast_data;
  486. struct batadv_mcast_querier_state querier4 = {false, false};
  487. struct batadv_mcast_querier_state querier6 = {false, false};
  488. struct net_device *dev = bat_priv->soft_iface;
  489. bool bridged;
  490. mcast_data.flags = BATADV_NO_FLAGS;
  491. memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
  492. bridged = batadv_mcast_has_bridge(bat_priv);
  493. if (!bridged)
  494. goto update;
  495. if (!IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING))
  496. pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n");
  497. querier4.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP);
  498. querier4.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP);
  499. querier6.exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6);
  500. querier6.shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6);
  501. mcast_data.flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES;
  502. /* 1) If no querier exists at all, then multicast listeners on
  503. * our local TT clients behind the bridge will keep silent.
  504. * 2) If the selected querier is on one of our local TT clients,
  505. * behind the bridge, then this querier might shadow multicast
  506. * listeners on our local TT clients, behind this bridge.
  507. *
  508. * In both cases, we will signalize other batman nodes that
  509. * we need all multicast traffic of the according protocol.
  510. */
  511. if (!querier4.exists || querier4.shadowing)
  512. mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV4;
  513. if (!querier6.exists || querier6.shadowing)
  514. mcast_data.flags |= BATADV_MCAST_WANT_ALL_IPV6;
  515. update:
  516. batadv_mcast_bridge_log(bat_priv, bridged, &querier4, &querier6);
  517. bat_priv->mcast.querier_ipv4.exists = querier4.exists;
  518. bat_priv->mcast.querier_ipv4.shadowing = querier4.shadowing;
  519. bat_priv->mcast.querier_ipv6.exists = querier6.exists;
  520. bat_priv->mcast.querier_ipv6.shadowing = querier6.shadowing;
  521. bat_priv->mcast.bridged = bridged;
  522. if (!bat_priv->mcast.enabled ||
  523. mcast_data.flags != bat_priv->mcast.flags) {
  524. batadv_mcast_flags_log(bat_priv, mcast_data.flags);
  525. batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2,
  526. &mcast_data, sizeof(mcast_data));
  527. bat_priv->mcast.flags = mcast_data.flags;
  528. bat_priv->mcast.enabled = true;
  529. }
  530. return !(mcast_data.flags & BATADV_MCAST_WANT_ALL_IPV4 &&
  531. mcast_data.flags & BATADV_MCAST_WANT_ALL_IPV6);
  532. }
  533. /**
  534. * __batadv_mcast_mla_update() - update the own MLAs
  535. * @bat_priv: the bat priv with all the soft interface information
  536. *
  537. * Updates the own multicast listener announcements in the translation
  538. * table as well as the own, announced multicast tvlv container.
  539. *
  540. * Note that non-conflicting reads and writes to bat_priv->mcast.mla_list
  541. * in batadv_mcast_mla_tt_retract() and batadv_mcast_mla_tt_add() are
  542. * ensured by the non-parallel execution of the worker this function
  543. * belongs to.
  544. */
  545. static void __batadv_mcast_mla_update(struct batadv_priv *bat_priv)
  546. {
  547. struct net_device *soft_iface = bat_priv->soft_iface;
  548. struct hlist_head mcast_list = HLIST_HEAD_INIT;
  549. int ret;
  550. if (!batadv_mcast_mla_tvlv_update(bat_priv))
  551. goto update;
  552. ret = batadv_mcast_mla_softif_get(bat_priv, soft_iface, &mcast_list);
  553. if (ret < 0)
  554. goto out;
  555. ret = batadv_mcast_mla_bridge_get(bat_priv, soft_iface, &mcast_list);
  556. if (ret < 0)
  557. goto out;
  558. update:
  559. batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
  560. batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
  561. out:
  562. batadv_mcast_mla_list_free(&mcast_list);
  563. }
  564. /**
  565. * batadv_mcast_mla_update() - update the own MLAs
  566. * @work: kernel work struct
  567. *
  568. * Updates the own multicast listener announcements in the translation
  569. * table as well as the own, announced multicast tvlv container.
  570. *
  571. * In the end, reschedules the work timer.
  572. */
  573. static void batadv_mcast_mla_update(struct work_struct *work)
  574. {
  575. struct delayed_work *delayed_work;
  576. struct batadv_priv_mcast *priv_mcast;
  577. struct batadv_priv *bat_priv;
  578. delayed_work = to_delayed_work(work);
  579. priv_mcast = container_of(delayed_work, struct batadv_priv_mcast, work);
  580. bat_priv = container_of(priv_mcast, struct batadv_priv, mcast);
  581. __batadv_mcast_mla_update(bat_priv);
  582. batadv_mcast_start_timer(bat_priv);
  583. }
  584. /**
  585. * batadv_mcast_is_report_ipv4() - check for IGMP reports
  586. * @skb: the ethernet frame destined for the mesh
  587. *
  588. * This call might reallocate skb data.
  589. *
  590. * Checks whether the given frame is a valid IGMP report.
  591. *
  592. * Return: If so then true, otherwise false.
  593. */
  594. static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb)
  595. {
  596. if (ip_mc_check_igmp(skb, NULL) < 0)
  597. return false;
  598. switch (igmp_hdr(skb)->type) {
  599. case IGMP_HOST_MEMBERSHIP_REPORT:
  600. case IGMPV2_HOST_MEMBERSHIP_REPORT:
  601. case IGMPV3_HOST_MEMBERSHIP_REPORT:
  602. return true;
  603. }
  604. return false;
  605. }
  606. /**
  607. * batadv_mcast_forw_mode_check_ipv4() - check for optimized forwarding
  608. * potential
  609. * @bat_priv: the bat priv with all the soft interface information
  610. * @skb: the IPv4 packet to check
  611. * @is_unsnoopable: stores whether the destination is snoopable
  612. *
  613. * Checks whether the given IPv4 packet has the potential to be forwarded with a
  614. * mode more optimal than classic flooding.
  615. *
  616. * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory
  617. * allocation failure.
  618. */
  619. static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
  620. struct sk_buff *skb,
  621. bool *is_unsnoopable)
  622. {
  623. struct iphdr *iphdr;
  624. /* We might fail due to out-of-memory -> drop it */
  625. if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
  626. return -ENOMEM;
  627. if (batadv_mcast_is_report_ipv4(skb))
  628. return -EINVAL;
  629. iphdr = ip_hdr(skb);
  630. /* TODO: Implement Multicast Router Discovery (RFC4286),
  631. * then allow scope > link local, too
  632. */
  633. if (!ipv4_is_local_multicast(iphdr->daddr))
  634. return -EINVAL;
  635. /* link-local multicast listeners behind a bridge are
  636. * not snoopable (see RFC4541, section 2.1.2.2)
  637. */
  638. *is_unsnoopable = true;
  639. return 0;
  640. }
  641. /**
  642. * batadv_mcast_is_report_ipv6() - check for MLD reports
  643. * @skb: the ethernet frame destined for the mesh
  644. *
  645. * This call might reallocate skb data.
  646. *
  647. * Checks whether the given frame is a valid MLD report.
  648. *
  649. * Return: If so then true, otherwise false.
  650. */
  651. static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb)
  652. {
  653. if (ipv6_mc_check_mld(skb, NULL) < 0)
  654. return false;
  655. switch (icmp6_hdr(skb)->icmp6_type) {
  656. case ICMPV6_MGM_REPORT:
  657. case ICMPV6_MLD2_REPORT:
  658. return true;
  659. }
  660. return false;
  661. }
  662. /**
  663. * batadv_mcast_forw_mode_check_ipv6() - check for optimized forwarding
  664. * potential
  665. * @bat_priv: the bat priv with all the soft interface information
  666. * @skb: the IPv6 packet to check
  667. * @is_unsnoopable: stores whether the destination is snoopable
  668. *
  669. * Checks whether the given IPv6 packet has the potential to be forwarded with a
  670. * mode more optimal than classic flooding.
  671. *
  672. * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
  673. */
  674. static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
  675. struct sk_buff *skb,
  676. bool *is_unsnoopable)
  677. {
  678. struct ipv6hdr *ip6hdr;
  679. /* We might fail due to out-of-memory -> drop it */
  680. if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
  681. return -ENOMEM;
  682. if (batadv_mcast_is_report_ipv6(skb))
  683. return -EINVAL;
  684. ip6hdr = ipv6_hdr(skb);
  685. /* TODO: Implement Multicast Router Discovery (RFC4286),
  686. * then allow scope > link local, too
  687. */
  688. if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL)
  689. return -EINVAL;
  690. /* link-local-all-nodes multicast listeners behind a bridge are
  691. * not snoopable (see RFC4541, section 3, paragraph 3)
  692. */
  693. if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
  694. *is_unsnoopable = true;
  695. return 0;
  696. }
  697. /**
  698. * batadv_mcast_forw_mode_check() - check for optimized forwarding potential
  699. * @bat_priv: the bat priv with all the soft interface information
  700. * @skb: the multicast frame to check
  701. * @is_unsnoopable: stores whether the destination is snoopable
  702. *
  703. * Checks whether the given multicast ethernet frame has the potential to be
  704. * forwarded with a mode more optimal than classic flooding.
  705. *
  706. * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory
  707. */
  708. static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
  709. struct sk_buff *skb,
  710. bool *is_unsnoopable)
  711. {
  712. struct ethhdr *ethhdr = eth_hdr(skb);
  713. if (!atomic_read(&bat_priv->multicast_mode))
  714. return -EINVAL;
  715. switch (ntohs(ethhdr->h_proto)) {
  716. case ETH_P_IP:
  717. return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
  718. is_unsnoopable);
  719. case ETH_P_IPV6:
  720. if (!IS_ENABLED(CONFIG_IPV6))
  721. return -EINVAL;
  722. return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
  723. is_unsnoopable);
  724. default:
  725. return -EINVAL;
  726. }
  727. }
  728. /**
  729. * batadv_mcast_forw_want_all_ip_count() - count nodes with unspecific mcast
  730. * interest
  731. * @bat_priv: the bat priv with all the soft interface information
  732. * @ethhdr: ethernet header of a packet
  733. *
  734. * Return: the number of nodes which want all IPv4 multicast traffic if the
  735. * given ethhdr is from an IPv4 packet or the number of nodes which want all
  736. * IPv6 traffic if it matches an IPv6 packet.
  737. */
  738. static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
  739. struct ethhdr *ethhdr)
  740. {
  741. switch (ntohs(ethhdr->h_proto)) {
  742. case ETH_P_IP:
  743. return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
  744. case ETH_P_IPV6:
  745. return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
  746. default:
  747. /* we shouldn't be here... */
  748. return 0;
  749. }
  750. }
  751. /**
  752. * batadv_mcast_forw_tt_node_get() - get a multicast tt node
  753. * @bat_priv: the bat priv with all the soft interface information
  754. * @ethhdr: the ether header containing the multicast destination
  755. *
  756. * Return: an orig_node matching the multicast address provided by ethhdr
  757. * via a translation table lookup. This increases the returned nodes refcount.
  758. */
  759. static struct batadv_orig_node *
  760. batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
  761. struct ethhdr *ethhdr)
  762. {
  763. return batadv_transtable_search(bat_priv, NULL, ethhdr->h_dest,
  764. BATADV_NO_FLAGS);
  765. }
  766. /**
  767. * batadv_mcast_forw_ipv4_node_get() - get a node with an ipv4 flag
  768. * @bat_priv: the bat priv with all the soft interface information
  769. *
  770. * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
  771. * increases its refcount.
  772. */
  773. static struct batadv_orig_node *
  774. batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
  775. {
  776. struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
  777. rcu_read_lock();
  778. hlist_for_each_entry_rcu(tmp_orig_node,
  779. &bat_priv->mcast.want_all_ipv4_list,
  780. mcast_want_all_ipv4_node) {
  781. if (!kref_get_unless_zero(&tmp_orig_node->refcount))
  782. continue;
  783. orig_node = tmp_orig_node;
  784. break;
  785. }
  786. rcu_read_unlock();
  787. return orig_node;
  788. }
  789. /**
  790. * batadv_mcast_forw_ipv6_node_get() - get a node with an ipv6 flag
  791. * @bat_priv: the bat priv with all the soft interface information
  792. *
  793. * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
  794. * and increases its refcount.
  795. */
  796. static struct batadv_orig_node *
  797. batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
  798. {
  799. struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
  800. rcu_read_lock();
  801. hlist_for_each_entry_rcu(tmp_orig_node,
  802. &bat_priv->mcast.want_all_ipv6_list,
  803. mcast_want_all_ipv6_node) {
  804. if (!kref_get_unless_zero(&tmp_orig_node->refcount))
  805. continue;
  806. orig_node = tmp_orig_node;
  807. break;
  808. }
  809. rcu_read_unlock();
  810. return orig_node;
  811. }
  812. /**
  813. * batadv_mcast_forw_ip_node_get() - get a node with an ipv4/ipv6 flag
  814. * @bat_priv: the bat priv with all the soft interface information
  815. * @ethhdr: an ethernet header to determine the protocol family from
  816. *
  817. * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
  818. * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and
  819. * increases its refcount.
  820. */
  821. static struct batadv_orig_node *
  822. batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
  823. struct ethhdr *ethhdr)
  824. {
  825. switch (ntohs(ethhdr->h_proto)) {
  826. case ETH_P_IP:
  827. return batadv_mcast_forw_ipv4_node_get(bat_priv);
  828. case ETH_P_IPV6:
  829. return batadv_mcast_forw_ipv6_node_get(bat_priv);
  830. default:
  831. /* we shouldn't be here... */
  832. return NULL;
  833. }
  834. }
  835. /**
  836. * batadv_mcast_forw_unsnoop_node_get() - get a node with an unsnoopable flag
  837. * @bat_priv: the bat priv with all the soft interface information
  838. *
  839. * Return: an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
  840. * set and increases its refcount.
  841. */
  842. static struct batadv_orig_node *
  843. batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
  844. {
  845. struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
  846. rcu_read_lock();
  847. hlist_for_each_entry_rcu(tmp_orig_node,
  848. &bat_priv->mcast.want_all_unsnoopables_list,
  849. mcast_want_all_unsnoopables_node) {
  850. if (!kref_get_unless_zero(&tmp_orig_node->refcount))
  851. continue;
  852. orig_node = tmp_orig_node;
  853. break;
  854. }
  855. rcu_read_unlock();
  856. return orig_node;
  857. }
  858. /**
  859. * batadv_mcast_forw_mode() - check on how to forward a multicast packet
  860. * @bat_priv: the bat priv with all the soft interface information
  861. * @skb: The multicast packet to check
  862. * @orig: an originator to be set to forward the skb to
  863. *
  864. * Return: the forwarding mode as enum batadv_forw_mode and in case of
  865. * BATADV_FORW_SINGLE set the orig to the single originator the skb
  866. * should be forwarded to.
  867. */
  868. enum batadv_forw_mode
  869. batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
  870. struct batadv_orig_node **orig)
  871. {
  872. int ret, tt_count, ip_count, unsnoop_count, total_count;
  873. bool is_unsnoopable = false;
  874. struct ethhdr *ethhdr;
  875. ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable);
  876. if (ret == -ENOMEM)
  877. return BATADV_FORW_NONE;
  878. else if (ret < 0)
  879. return BATADV_FORW_ALL;
  880. ethhdr = eth_hdr(skb);
  881. tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
  882. BATADV_NO_FLAGS);
  883. ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
  884. unsnoop_count = !is_unsnoopable ? 0 :
  885. atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
  886. total_count = tt_count + ip_count + unsnoop_count;
  887. switch (total_count) {
  888. case 1:
  889. if (tt_count)
  890. *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
  891. else if (ip_count)
  892. *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
  893. else if (unsnoop_count)
  894. *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
  895. if (*orig)
  896. return BATADV_FORW_SINGLE;
  897. /* fall through */
  898. case 0:
  899. return BATADV_FORW_NONE;
  900. default:
  901. return BATADV_FORW_ALL;
  902. }
  903. }
  904. /**
  905. * batadv_mcast_want_unsnoop_update() - update unsnoop counter and list
  906. * @bat_priv: the bat priv with all the soft interface information
  907. * @orig: the orig_node which multicast state might have changed of
  908. * @mcast_flags: flags indicating the new multicast state
  909. *
  910. * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
  911. * orig, has toggled then this method updates counter and list accordingly.
  912. *
  913. * Caller needs to hold orig->mcast_handler_lock.
  914. */
  915. static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
  916. struct batadv_orig_node *orig,
  917. u8 mcast_flags)
  918. {
  919. struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node;
  920. struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list;
  921. lockdep_assert_held(&orig->mcast_handler_lock);
  922. /* switched from flag unset to set */
  923. if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
  924. !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
  925. atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
  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_add_head_rcu(node, head);
  930. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  931. /* switched from flag set to unset */
  932. } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
  933. orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
  934. atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
  935. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  936. /* flag checks above + mcast_handler_lock prevents this */
  937. WARN_ON(hlist_unhashed(node));
  938. hlist_del_init_rcu(node);
  939. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  940. }
  941. }
  942. /**
  943. * batadv_mcast_want_ipv4_update() - update want-all-ipv4 counter and list
  944. * @bat_priv: the bat priv with all the soft interface information
  945. * @orig: the orig_node which multicast state might have changed of
  946. * @mcast_flags: flags indicating the new multicast state
  947. *
  948. * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
  949. * toggled then this method updates counter and list accordingly.
  950. *
  951. * Caller needs to hold orig->mcast_handler_lock.
  952. */
  953. static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
  954. struct batadv_orig_node *orig,
  955. u8 mcast_flags)
  956. {
  957. struct hlist_node *node = &orig->mcast_want_all_ipv4_node;
  958. struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list;
  959. lockdep_assert_held(&orig->mcast_handler_lock);
  960. /* switched from flag unset to set */
  961. if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
  962. !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
  963. atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
  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_add_head_rcu(node, head);
  968. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  969. /* switched from flag set to unset */
  970. } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
  971. orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
  972. atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
  973. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  974. /* flag checks above + mcast_handler_lock prevents this */
  975. WARN_ON(hlist_unhashed(node));
  976. hlist_del_init_rcu(node);
  977. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  978. }
  979. }
  980. /**
  981. * batadv_mcast_want_ipv6_update() - update want-all-ipv6 counter and list
  982. * @bat_priv: the bat priv with all the soft interface information
  983. * @orig: the orig_node which multicast state might have changed of
  984. * @mcast_flags: flags indicating the new multicast state
  985. *
  986. * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
  987. * toggled then this method updates counter and list accordingly.
  988. *
  989. * Caller needs to hold orig->mcast_handler_lock.
  990. */
  991. static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
  992. struct batadv_orig_node *orig,
  993. u8 mcast_flags)
  994. {
  995. struct hlist_node *node = &orig->mcast_want_all_ipv6_node;
  996. struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list;
  997. lockdep_assert_held(&orig->mcast_handler_lock);
  998. /* switched from flag unset to set */
  999. if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
  1000. !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
  1001. atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
  1002. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  1003. /* flag checks above + mcast_handler_lock prevents this */
  1004. WARN_ON(!hlist_unhashed(node));
  1005. hlist_add_head_rcu(node, head);
  1006. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  1007. /* switched from flag set to unset */
  1008. } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
  1009. orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
  1010. atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
  1011. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  1012. /* flag checks above + mcast_handler_lock prevents this */
  1013. WARN_ON(hlist_unhashed(node));
  1014. hlist_del_init_rcu(node);
  1015. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  1016. }
  1017. }
  1018. /**
  1019. * batadv_mcast_tvlv_ogm_handler() - process incoming multicast tvlv container
  1020. * @bat_priv: the bat priv with all the soft interface information
  1021. * @orig: the orig_node of the ogm
  1022. * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
  1023. * @tvlv_value: tvlv buffer containing the multicast data
  1024. * @tvlv_value_len: tvlv buffer length
  1025. */
  1026. static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv,
  1027. struct batadv_orig_node *orig,
  1028. u8 flags,
  1029. void *tvlv_value,
  1030. u16 tvlv_value_len)
  1031. {
  1032. bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
  1033. u8 mcast_flags = BATADV_NO_FLAGS;
  1034. if (orig_mcast_enabled && tvlv_value &&
  1035. tvlv_value_len >= sizeof(mcast_flags))
  1036. mcast_flags = *(u8 *)tvlv_value;
  1037. if (!orig_mcast_enabled) {
  1038. mcast_flags |= BATADV_MCAST_WANT_ALL_IPV4;
  1039. mcast_flags |= BATADV_MCAST_WANT_ALL_IPV6;
  1040. }
  1041. spin_lock_bh(&orig->mcast_handler_lock);
  1042. if (orig_mcast_enabled &&
  1043. !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
  1044. set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
  1045. } else if (!orig_mcast_enabled &&
  1046. test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) {
  1047. clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities);
  1048. }
  1049. set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized);
  1050. batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
  1051. batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
  1052. batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
  1053. orig->mcast_flags = mcast_flags;
  1054. spin_unlock_bh(&orig->mcast_handler_lock);
  1055. }
  1056. /**
  1057. * batadv_mcast_init() - initialize the multicast optimizations structures
  1058. * @bat_priv: the bat priv with all the soft interface information
  1059. */
  1060. void batadv_mcast_init(struct batadv_priv *bat_priv)
  1061. {
  1062. batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler,
  1063. NULL, BATADV_TVLV_MCAST, 2,
  1064. BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
  1065. INIT_DELAYED_WORK(&bat_priv->mcast.work, batadv_mcast_mla_update);
  1066. batadv_mcast_start_timer(bat_priv);
  1067. }
  1068. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  1069. /**
  1070. * batadv_mcast_flags_print_header() - print own mcast flags to debugfs table
  1071. * @bat_priv: the bat priv with all the soft interface information
  1072. * @seq: debugfs table seq_file struct
  1073. *
  1074. * Prints our own multicast flags including a more specific reason why
  1075. * they are set, that is prints the bridge and querier state too, to
  1076. * the debugfs table specified via @seq.
  1077. */
  1078. static void batadv_mcast_flags_print_header(struct batadv_priv *bat_priv,
  1079. struct seq_file *seq)
  1080. {
  1081. u8 flags = bat_priv->mcast.flags;
  1082. char querier4, querier6, shadowing4, shadowing6;
  1083. bool bridged = bat_priv->mcast.bridged;
  1084. if (bridged) {
  1085. querier4 = bat_priv->mcast.querier_ipv4.exists ? '.' : '4';
  1086. querier6 = bat_priv->mcast.querier_ipv6.exists ? '.' : '6';
  1087. shadowing4 = bat_priv->mcast.querier_ipv4.shadowing ? '4' : '.';
  1088. shadowing6 = bat_priv->mcast.querier_ipv6.shadowing ? '6' : '.';
  1089. } else {
  1090. querier4 = '?';
  1091. querier6 = '?';
  1092. shadowing4 = '?';
  1093. shadowing6 = '?';
  1094. }
  1095. seq_printf(seq, "Multicast flags (own flags: [%c%c%c])\n",
  1096. (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.',
  1097. (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.',
  1098. (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.');
  1099. seq_printf(seq, "* Bridged [U]\t\t\t\t%c\n", bridged ? 'U' : '.');
  1100. seq_printf(seq, "* No IGMP/MLD Querier [4/6]:\t\t%c/%c\n",
  1101. querier4, querier6);
  1102. seq_printf(seq, "* Shadowing IGMP/MLD Querier [4/6]:\t%c/%c\n",
  1103. shadowing4, shadowing6);
  1104. seq_puts(seq, "-------------------------------------------\n");
  1105. seq_printf(seq, " %-10s %s\n", "Originator", "Flags");
  1106. }
  1107. /**
  1108. * batadv_mcast_flags_seq_print_text() - print the mcast flags of other nodes
  1109. * @seq: seq file to print on
  1110. * @offset: not used
  1111. *
  1112. * This prints a table of (primary) originators and their according
  1113. * multicast flags, including (in the header) our own.
  1114. *
  1115. * Return: always 0
  1116. */
  1117. int batadv_mcast_flags_seq_print_text(struct seq_file *seq, void *offset)
  1118. {
  1119. struct net_device *net_dev = (struct net_device *)seq->private;
  1120. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  1121. struct batadv_hard_iface *primary_if;
  1122. struct batadv_hashtable *hash = bat_priv->orig_hash;
  1123. struct batadv_orig_node *orig_node;
  1124. struct hlist_head *head;
  1125. u8 flags;
  1126. u32 i;
  1127. primary_if = batadv_seq_print_text_primary_if_get(seq);
  1128. if (!primary_if)
  1129. return 0;
  1130. batadv_mcast_flags_print_header(bat_priv, seq);
  1131. for (i = 0; i < hash->size; i++) {
  1132. head = &hash->table[i];
  1133. rcu_read_lock();
  1134. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  1135. if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
  1136. &orig_node->capa_initialized))
  1137. continue;
  1138. if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
  1139. &orig_node->capabilities)) {
  1140. seq_printf(seq, "%pM -\n", orig_node->orig);
  1141. continue;
  1142. }
  1143. flags = orig_node->mcast_flags;
  1144. seq_printf(seq, "%pM [%c%c%c]\n", orig_node->orig,
  1145. (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)
  1146. ? 'U' : '.',
  1147. (flags & BATADV_MCAST_WANT_ALL_IPV4)
  1148. ? '4' : '.',
  1149. (flags & BATADV_MCAST_WANT_ALL_IPV6)
  1150. ? '6' : '.');
  1151. }
  1152. rcu_read_unlock();
  1153. }
  1154. batadv_hardif_put(primary_if);
  1155. return 0;
  1156. }
  1157. #endif
  1158. /**
  1159. * batadv_mcast_mesh_info_put() - put multicast info into a netlink message
  1160. * @msg: buffer for the message
  1161. * @bat_priv: the bat priv with all the soft interface information
  1162. *
  1163. * Return: 0 or error code.
  1164. */
  1165. int batadv_mcast_mesh_info_put(struct sk_buff *msg,
  1166. struct batadv_priv *bat_priv)
  1167. {
  1168. u32 flags = bat_priv->mcast.flags;
  1169. u32 flags_priv = BATADV_NO_FLAGS;
  1170. if (bat_priv->mcast.bridged) {
  1171. flags_priv |= BATADV_MCAST_FLAGS_BRIDGED;
  1172. if (bat_priv->mcast.querier_ipv4.exists)
  1173. flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_EXISTS;
  1174. if (bat_priv->mcast.querier_ipv6.exists)
  1175. flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_EXISTS;
  1176. if (bat_priv->mcast.querier_ipv4.shadowing)
  1177. flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_SHADOWING;
  1178. if (bat_priv->mcast.querier_ipv6.shadowing)
  1179. flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_SHADOWING;
  1180. }
  1181. if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS, flags) ||
  1182. nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS_PRIV, flags_priv))
  1183. return -EMSGSIZE;
  1184. return 0;
  1185. }
  1186. /**
  1187. * batadv_mcast_flags_dump_entry() - dump one entry of the multicast flags table
  1188. * to a netlink socket
  1189. * @msg: buffer for the message
  1190. * @portid: netlink port
  1191. * @seq: Sequence number of netlink message
  1192. * @orig_node: originator to dump the multicast flags of
  1193. *
  1194. * Return: 0 or error code.
  1195. */
  1196. static int
  1197. batadv_mcast_flags_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
  1198. struct batadv_orig_node *orig_node)
  1199. {
  1200. void *hdr;
  1201. hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
  1202. NLM_F_MULTI, BATADV_CMD_GET_MCAST_FLAGS);
  1203. if (!hdr)
  1204. return -ENOBUFS;
  1205. if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
  1206. orig_node->orig)) {
  1207. genlmsg_cancel(msg, hdr);
  1208. return -EMSGSIZE;
  1209. }
  1210. if (test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
  1211. &orig_node->capabilities)) {
  1212. if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS,
  1213. orig_node->mcast_flags)) {
  1214. genlmsg_cancel(msg, hdr);
  1215. return -EMSGSIZE;
  1216. }
  1217. }
  1218. genlmsg_end(msg, hdr);
  1219. return 0;
  1220. }
  1221. /**
  1222. * batadv_mcast_flags_dump_bucket() - dump one bucket of the multicast flags
  1223. * table to a netlink socket
  1224. * @msg: buffer for the message
  1225. * @portid: netlink port
  1226. * @seq: Sequence number of netlink message
  1227. * @head: bucket to dump
  1228. * @idx_skip: How many entries to skip
  1229. *
  1230. * Return: 0 or error code.
  1231. */
  1232. static int
  1233. batadv_mcast_flags_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
  1234. struct hlist_head *head, long *idx_skip)
  1235. {
  1236. struct batadv_orig_node *orig_node;
  1237. long idx = 0;
  1238. rcu_read_lock();
  1239. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  1240. if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST,
  1241. &orig_node->capa_initialized))
  1242. continue;
  1243. if (idx < *idx_skip)
  1244. goto skip;
  1245. if (batadv_mcast_flags_dump_entry(msg, portid, seq,
  1246. orig_node)) {
  1247. rcu_read_unlock();
  1248. *idx_skip = idx;
  1249. return -EMSGSIZE;
  1250. }
  1251. skip:
  1252. idx++;
  1253. }
  1254. rcu_read_unlock();
  1255. return 0;
  1256. }
  1257. /**
  1258. * __batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket
  1259. * @msg: buffer for the message
  1260. * @portid: netlink port
  1261. * @seq: Sequence number of netlink message
  1262. * @bat_priv: the bat priv with all the soft interface information
  1263. * @bucket: current bucket to dump
  1264. * @idx: index in current bucket to the next entry to dump
  1265. *
  1266. * Return: 0 or error code.
  1267. */
  1268. static int
  1269. __batadv_mcast_flags_dump(struct sk_buff *msg, u32 portid, u32 seq,
  1270. struct batadv_priv *bat_priv, long *bucket, long *idx)
  1271. {
  1272. struct batadv_hashtable *hash = bat_priv->orig_hash;
  1273. long bucket_tmp = *bucket;
  1274. struct hlist_head *head;
  1275. long idx_tmp = *idx;
  1276. while (bucket_tmp < hash->size) {
  1277. head = &hash->table[bucket_tmp];
  1278. if (batadv_mcast_flags_dump_bucket(msg, portid, seq, head,
  1279. &idx_tmp))
  1280. break;
  1281. bucket_tmp++;
  1282. idx_tmp = 0;
  1283. }
  1284. *bucket = bucket_tmp;
  1285. *idx = idx_tmp;
  1286. return msg->len;
  1287. }
  1288. /**
  1289. * batadv_mcast_netlink_get_primary() - get primary interface from netlink
  1290. * callback
  1291. * @cb: netlink callback structure
  1292. * @primary_if: the primary interface pointer to return the result in
  1293. *
  1294. * Return: 0 or error code.
  1295. */
  1296. static int
  1297. batadv_mcast_netlink_get_primary(struct netlink_callback *cb,
  1298. struct batadv_hard_iface **primary_if)
  1299. {
  1300. struct batadv_hard_iface *hard_iface = NULL;
  1301. struct net *net = sock_net(cb->skb->sk);
  1302. struct net_device *soft_iface;
  1303. struct batadv_priv *bat_priv;
  1304. int ifindex;
  1305. int ret = 0;
  1306. ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
  1307. if (!ifindex)
  1308. return -EINVAL;
  1309. soft_iface = dev_get_by_index(net, ifindex);
  1310. if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
  1311. ret = -ENODEV;
  1312. goto out;
  1313. }
  1314. bat_priv = netdev_priv(soft_iface);
  1315. hard_iface = batadv_primary_if_get_selected(bat_priv);
  1316. if (!hard_iface || hard_iface->if_status != BATADV_IF_ACTIVE) {
  1317. ret = -ENOENT;
  1318. goto out;
  1319. }
  1320. out:
  1321. if (soft_iface)
  1322. dev_put(soft_iface);
  1323. if (!ret && primary_if)
  1324. *primary_if = hard_iface;
  1325. else if (hard_iface)
  1326. batadv_hardif_put(hard_iface);
  1327. return ret;
  1328. }
  1329. /**
  1330. * batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket
  1331. * @msg: buffer for the message
  1332. * @cb: callback structure containing arguments
  1333. *
  1334. * Return: message length.
  1335. */
  1336. int batadv_mcast_flags_dump(struct sk_buff *msg, struct netlink_callback *cb)
  1337. {
  1338. struct batadv_hard_iface *primary_if = NULL;
  1339. int portid = NETLINK_CB(cb->skb).portid;
  1340. struct batadv_priv *bat_priv;
  1341. long *bucket = &cb->args[0];
  1342. long *idx = &cb->args[1];
  1343. int ret;
  1344. ret = batadv_mcast_netlink_get_primary(cb, &primary_if);
  1345. if (ret)
  1346. return ret;
  1347. bat_priv = netdev_priv(primary_if->soft_iface);
  1348. ret = __batadv_mcast_flags_dump(msg, portid, cb->nlh->nlmsg_seq,
  1349. bat_priv, bucket, idx);
  1350. batadv_hardif_put(primary_if);
  1351. return ret;
  1352. }
  1353. /**
  1354. * batadv_mcast_free() - free the multicast optimizations structures
  1355. * @bat_priv: the bat priv with all the soft interface information
  1356. */
  1357. void batadv_mcast_free(struct batadv_priv *bat_priv)
  1358. {
  1359. cancel_delayed_work_sync(&bat_priv->mcast.work);
  1360. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
  1361. batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2);
  1362. /* safely calling outside of worker, as worker was canceled above */
  1363. batadv_mcast_mla_tt_retract(bat_priv, NULL);
  1364. }
  1365. /**
  1366. * batadv_mcast_purge_orig() - reset originator global mcast state modifications
  1367. * @orig: the originator which is going to get purged
  1368. */
  1369. void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
  1370. {
  1371. struct batadv_priv *bat_priv = orig->bat_priv;
  1372. spin_lock_bh(&orig->mcast_handler_lock);
  1373. batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
  1374. batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
  1375. batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
  1376. spin_unlock_bh(&orig->mcast_handler_lock);
  1377. }