multicast.c 41 KB

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