multicast.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /* Copyright (C) 2014-2015 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/byteorder/generic.h>
  21. #include <linux/errno.h>
  22. #include <linux/etherdevice.h>
  23. #include <linux/fs.h>
  24. #include <linux/if_ether.h>
  25. #include <linux/in6.h>
  26. #include <linux/in.h>
  27. #include <linux/ip.h>
  28. #include <linux/ipv6.h>
  29. #include <linux/list.h>
  30. #include <linux/netdevice.h>
  31. #include <linux/rculist.h>
  32. #include <linux/rcupdate.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/slab.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/stddef.h>
  37. #include <linux/string.h>
  38. #include <linux/types.h>
  39. #include <net/addrconf.h>
  40. #include <net/ipv6.h>
  41. #include "packet.h"
  42. #include "translation-table.h"
  43. /**
  44. * batadv_mcast_mla_softif_get - get softif multicast listeners
  45. * @dev: the device to collect multicast addresses from
  46. * @mcast_list: a list to put found addresses into
  47. *
  48. * Collect multicast addresses of the local multicast listeners
  49. * on the given soft interface, dev, in the given mcast_list.
  50. *
  51. * Returns -ENOMEM on memory allocation error or the number of
  52. * items added to the mcast_list otherwise.
  53. */
  54. static int batadv_mcast_mla_softif_get(struct net_device *dev,
  55. struct hlist_head *mcast_list)
  56. {
  57. struct netdev_hw_addr *mc_list_entry;
  58. struct batadv_hw_addr *new;
  59. int ret = 0;
  60. netif_addr_lock_bh(dev);
  61. netdev_for_each_mc_addr(mc_list_entry, dev) {
  62. new = kmalloc(sizeof(*new), GFP_ATOMIC);
  63. if (!new) {
  64. ret = -ENOMEM;
  65. break;
  66. }
  67. ether_addr_copy(new->addr, mc_list_entry->addr);
  68. hlist_add_head(&new->list, mcast_list);
  69. ret++;
  70. }
  71. netif_addr_unlock_bh(dev);
  72. return ret;
  73. }
  74. /**
  75. * batadv_mcast_mla_is_duplicate - check whether an address is in a list
  76. * @mcast_addr: the multicast address to check
  77. * @mcast_list: the list with multicast addresses to search in
  78. *
  79. * Returns true if the given address is already in the given list.
  80. * Otherwise returns false.
  81. */
  82. static bool batadv_mcast_mla_is_duplicate(uint8_t *mcast_addr,
  83. struct hlist_head *mcast_list)
  84. {
  85. struct batadv_hw_addr *mcast_entry;
  86. hlist_for_each_entry(mcast_entry, mcast_list, list)
  87. if (batadv_compare_eth(mcast_entry->addr, mcast_addr))
  88. return true;
  89. return false;
  90. }
  91. /**
  92. * batadv_mcast_mla_list_free - free a list of multicast addresses
  93. * @mcast_list: the list to free
  94. *
  95. * Removes and frees all items in the given mcast_list.
  96. */
  97. static void batadv_mcast_mla_list_free(struct hlist_head *mcast_list)
  98. {
  99. struct batadv_hw_addr *mcast_entry;
  100. struct hlist_node *tmp;
  101. hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
  102. hlist_del(&mcast_entry->list);
  103. kfree(mcast_entry);
  104. }
  105. }
  106. /**
  107. * batadv_mcast_mla_tt_retract - clean up multicast listener announcements
  108. * @bat_priv: the bat priv with all the soft interface information
  109. * @mcast_list: a list of addresses which should _not_ be removed
  110. *
  111. * Retracts the announcement of any multicast listener from the
  112. * translation table except the ones listed in the given mcast_list.
  113. *
  114. * If mcast_list is NULL then all are retracted.
  115. */
  116. static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv,
  117. struct hlist_head *mcast_list)
  118. {
  119. struct batadv_hw_addr *mcast_entry;
  120. struct hlist_node *tmp;
  121. hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list,
  122. list) {
  123. if (mcast_list &&
  124. batadv_mcast_mla_is_duplicate(mcast_entry->addr,
  125. mcast_list))
  126. continue;
  127. batadv_tt_local_remove(bat_priv, mcast_entry->addr,
  128. BATADV_NO_FLAGS,
  129. "mcast TT outdated", false);
  130. hlist_del(&mcast_entry->list);
  131. kfree(mcast_entry);
  132. }
  133. }
  134. /**
  135. * batadv_mcast_mla_tt_add - add multicast listener announcements
  136. * @bat_priv: the bat priv with all the soft interface information
  137. * @mcast_list: a list of addresses which are going to get added
  138. *
  139. * Adds multicast listener announcements from the given mcast_list to the
  140. * translation table if they have not been added yet.
  141. */
  142. static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv,
  143. struct hlist_head *mcast_list)
  144. {
  145. struct batadv_hw_addr *mcast_entry;
  146. struct hlist_node *tmp;
  147. if (!mcast_list)
  148. return;
  149. hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) {
  150. if (batadv_mcast_mla_is_duplicate(mcast_entry->addr,
  151. &bat_priv->mcast.mla_list))
  152. continue;
  153. if (!batadv_tt_local_add(bat_priv->soft_iface,
  154. mcast_entry->addr, BATADV_NO_FLAGS,
  155. BATADV_NULL_IFINDEX, BATADV_NO_MARK))
  156. continue;
  157. hlist_del(&mcast_entry->list);
  158. hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list);
  159. }
  160. }
  161. /**
  162. * batadv_mcast_has_bridge - check whether the soft-iface is bridged
  163. * @bat_priv: the bat priv with all the soft interface information
  164. *
  165. * Checks whether there is a bridge on top of our soft interface. Returns
  166. * true if so, false otherwise.
  167. */
  168. static bool batadv_mcast_has_bridge(struct batadv_priv *bat_priv)
  169. {
  170. struct net_device *upper = bat_priv->soft_iface;
  171. rcu_read_lock();
  172. do {
  173. upper = netdev_master_upper_dev_get_rcu(upper);
  174. } while (upper && !(upper->priv_flags & IFF_EBRIDGE));
  175. rcu_read_unlock();
  176. return upper;
  177. }
  178. /**
  179. * batadv_mcast_mla_tvlv_update - update multicast tvlv
  180. * @bat_priv: the bat priv with all the soft interface information
  181. *
  182. * Updates the own multicast tvlv with our current multicast related settings,
  183. * capabilities and inabilities.
  184. *
  185. * Returns true if the tvlv container is registered afterwards. Otherwise
  186. * returns false.
  187. */
  188. static bool batadv_mcast_mla_tvlv_update(struct batadv_priv *bat_priv)
  189. {
  190. struct batadv_tvlv_mcast_data mcast_data;
  191. mcast_data.flags = BATADV_NO_FLAGS;
  192. memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved));
  193. /* Avoid attaching MLAs, if there is a bridge on top of our soft
  194. * interface, we don't support that yet (TODO)
  195. */
  196. if (batadv_mcast_has_bridge(bat_priv)) {
  197. if (bat_priv->mcast.enabled) {
  198. batadv_tvlv_container_unregister(bat_priv,
  199. BATADV_TVLV_MCAST, 1);
  200. bat_priv->mcast.enabled = false;
  201. }
  202. return false;
  203. }
  204. if (!bat_priv->mcast.enabled ||
  205. mcast_data.flags != bat_priv->mcast.flags) {
  206. batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 1,
  207. &mcast_data, sizeof(mcast_data));
  208. bat_priv->mcast.flags = mcast_data.flags;
  209. bat_priv->mcast.enabled = true;
  210. }
  211. return true;
  212. }
  213. /**
  214. * batadv_mcast_mla_update - update the own MLAs
  215. * @bat_priv: the bat priv with all the soft interface information
  216. *
  217. * Updates the own multicast listener announcements in the translation
  218. * table as well as the own, announced multicast tvlv container.
  219. */
  220. void batadv_mcast_mla_update(struct batadv_priv *bat_priv)
  221. {
  222. struct net_device *soft_iface = bat_priv->soft_iface;
  223. struct hlist_head mcast_list = HLIST_HEAD_INIT;
  224. int ret;
  225. if (!batadv_mcast_mla_tvlv_update(bat_priv))
  226. goto update;
  227. ret = batadv_mcast_mla_softif_get(soft_iface, &mcast_list);
  228. if (ret < 0)
  229. goto out;
  230. update:
  231. batadv_mcast_mla_tt_retract(bat_priv, &mcast_list);
  232. batadv_mcast_mla_tt_add(bat_priv, &mcast_list);
  233. out:
  234. batadv_mcast_mla_list_free(&mcast_list);
  235. }
  236. /**
  237. * batadv_mcast_forw_mode_check_ipv4 - check for optimized forwarding potential
  238. * @bat_priv: the bat priv with all the soft interface information
  239. * @skb: the IPv4 packet to check
  240. * @is_unsnoopable: stores whether the destination is snoopable
  241. *
  242. * Checks whether the given IPv4 packet has the potential to be forwarded with a
  243. * mode more optimal than classic flooding.
  244. *
  245. * If so then returns 0. Otherwise -EINVAL is returned or -ENOMEM in case of
  246. * memory allocation failure.
  247. */
  248. static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv,
  249. struct sk_buff *skb,
  250. bool *is_unsnoopable)
  251. {
  252. struct iphdr *iphdr;
  253. /* We might fail due to out-of-memory -> drop it */
  254. if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr)))
  255. return -ENOMEM;
  256. iphdr = ip_hdr(skb);
  257. /* TODO: Implement Multicast Router Discovery (RFC4286),
  258. * then allow scope > link local, too
  259. */
  260. if (!ipv4_is_local_multicast(iphdr->daddr))
  261. return -EINVAL;
  262. /* link-local multicast listeners behind a bridge are
  263. * not snoopable (see RFC4541, section 2.1.2.2)
  264. */
  265. *is_unsnoopable = true;
  266. return 0;
  267. }
  268. /**
  269. * batadv_mcast_forw_mode_check_ipv6 - check for optimized forwarding potential
  270. * @bat_priv: the bat priv with all the soft interface information
  271. * @skb: the IPv6 packet to check
  272. * @is_unsnoopable: stores whether the destination is snoopable
  273. *
  274. * Checks whether the given IPv6 packet has the potential to be forwarded with a
  275. * mode more optimal than classic flooding.
  276. *
  277. * If so then returns 0. Otherwise -EINVAL is returned or -ENOMEM if we are out
  278. * of memory.
  279. */
  280. static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv,
  281. struct sk_buff *skb,
  282. bool *is_unsnoopable)
  283. {
  284. struct ipv6hdr *ip6hdr;
  285. /* We might fail due to out-of-memory -> drop it */
  286. if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr)))
  287. return -ENOMEM;
  288. ip6hdr = ipv6_hdr(skb);
  289. /* TODO: Implement Multicast Router Discovery (RFC4286),
  290. * then allow scope > link local, too
  291. */
  292. if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) != IPV6_ADDR_SCOPE_LINKLOCAL)
  293. return -EINVAL;
  294. /* link-local-all-nodes multicast listeners behind a bridge are
  295. * not snoopable (see RFC4541, section 3, paragraph 3)
  296. */
  297. if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr))
  298. *is_unsnoopable = true;
  299. return 0;
  300. }
  301. /**
  302. * batadv_mcast_forw_mode_check - check for optimized forwarding potential
  303. * @bat_priv: the bat priv with all the soft interface information
  304. * @skb: the multicast frame to check
  305. * @is_unsnoopable: stores whether the destination is snoopable
  306. *
  307. * Checks whether the given multicast ethernet frame has the potential to be
  308. * forwarded with a mode more optimal than classic flooding.
  309. *
  310. * If so then returns 0. Otherwise -EINVAL is returned or -ENOMEM if we are out
  311. * of memory.
  312. */
  313. static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv,
  314. struct sk_buff *skb,
  315. bool *is_unsnoopable)
  316. {
  317. struct ethhdr *ethhdr = eth_hdr(skb);
  318. if (!atomic_read(&bat_priv->multicast_mode))
  319. return -EINVAL;
  320. if (atomic_read(&bat_priv->mcast.num_disabled))
  321. return -EINVAL;
  322. switch (ntohs(ethhdr->h_proto)) {
  323. case ETH_P_IP:
  324. return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb,
  325. is_unsnoopable);
  326. case ETH_P_IPV6:
  327. return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb,
  328. is_unsnoopable);
  329. default:
  330. return -EINVAL;
  331. }
  332. }
  333. /**
  334. * batadv_mcast_want_all_ip_count - count nodes with unspecific mcast interest
  335. * @bat_priv: the bat priv with all the soft interface information
  336. * @ethhdr: ethernet header of a packet
  337. *
  338. * Returns the number of nodes which want all IPv4 multicast traffic if the
  339. * given ethhdr is from an IPv4 packet or the number of nodes which want all
  340. * IPv6 traffic if it matches an IPv6 packet.
  341. */
  342. static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv,
  343. struct ethhdr *ethhdr)
  344. {
  345. switch (ntohs(ethhdr->h_proto)) {
  346. case ETH_P_IP:
  347. return atomic_read(&bat_priv->mcast.num_want_all_ipv4);
  348. case ETH_P_IPV6:
  349. return atomic_read(&bat_priv->mcast.num_want_all_ipv6);
  350. default:
  351. /* we shouldn't be here... */
  352. return 0;
  353. }
  354. }
  355. /**
  356. * batadv_mcast_forw_tt_node_get - get a multicast tt node
  357. * @bat_priv: the bat priv with all the soft interface information
  358. * @ethhdr: the ether header containing the multicast destination
  359. *
  360. * Returns an orig_node matching the multicast address provided by ethhdr
  361. * via a translation table lookup. This increases the returned nodes refcount.
  362. */
  363. static struct batadv_orig_node *
  364. batadv_mcast_forw_tt_node_get(struct batadv_priv *bat_priv,
  365. struct ethhdr *ethhdr)
  366. {
  367. return batadv_transtable_search(bat_priv, ethhdr->h_source,
  368. ethhdr->h_dest, BATADV_NO_FLAGS);
  369. }
  370. /**
  371. * batadv_mcast_want_forw_ipv4_node_get - get a node with an ipv4 flag
  372. * @bat_priv: the bat priv with all the soft interface information
  373. *
  374. * Returns an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 flag set and
  375. * increases its refcount.
  376. */
  377. static struct batadv_orig_node *
  378. batadv_mcast_forw_ipv4_node_get(struct batadv_priv *bat_priv)
  379. {
  380. struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
  381. rcu_read_lock();
  382. hlist_for_each_entry_rcu(tmp_orig_node,
  383. &bat_priv->mcast.want_all_ipv4_list,
  384. mcast_want_all_ipv4_node) {
  385. if (!atomic_inc_not_zero(&tmp_orig_node->refcount))
  386. continue;
  387. orig_node = tmp_orig_node;
  388. break;
  389. }
  390. rcu_read_unlock();
  391. return orig_node;
  392. }
  393. /**
  394. * batadv_mcast_want_forw_ipv6_node_get - get a node with an ipv6 flag
  395. * @bat_priv: the bat priv with all the soft interface information
  396. *
  397. * Returns an orig_node which has the BATADV_MCAST_WANT_ALL_IPV6 flag set
  398. * and increases its refcount.
  399. */
  400. static struct batadv_orig_node *
  401. batadv_mcast_forw_ipv6_node_get(struct batadv_priv *bat_priv)
  402. {
  403. struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
  404. rcu_read_lock();
  405. hlist_for_each_entry_rcu(tmp_orig_node,
  406. &bat_priv->mcast.want_all_ipv6_list,
  407. mcast_want_all_ipv6_node) {
  408. if (!atomic_inc_not_zero(&tmp_orig_node->refcount))
  409. continue;
  410. orig_node = tmp_orig_node;
  411. break;
  412. }
  413. rcu_read_unlock();
  414. return orig_node;
  415. }
  416. /**
  417. * batadv_mcast_want_forw_ip_node_get - get a node with an ipv4/ipv6 flag
  418. * @bat_priv: the bat priv with all the soft interface information
  419. * @ethhdr: an ethernet header to determine the protocol family from
  420. *
  421. * Returns an orig_node which has the BATADV_MCAST_WANT_ALL_IPV4 or
  422. * BATADV_MCAST_WANT_ALL_IPV6 flag, depending on the provided ethhdr, set and
  423. * increases its refcount.
  424. */
  425. static struct batadv_orig_node *
  426. batadv_mcast_forw_ip_node_get(struct batadv_priv *bat_priv,
  427. struct ethhdr *ethhdr)
  428. {
  429. switch (ntohs(ethhdr->h_proto)) {
  430. case ETH_P_IP:
  431. return batadv_mcast_forw_ipv4_node_get(bat_priv);
  432. case ETH_P_IPV6:
  433. return batadv_mcast_forw_ipv6_node_get(bat_priv);
  434. default:
  435. /* we shouldn't be here... */
  436. return NULL;
  437. }
  438. }
  439. /**
  440. * batadv_mcast_want_forw_unsnoop_node_get - get a node with an unsnoopable flag
  441. * @bat_priv: the bat priv with all the soft interface information
  442. *
  443. * Returns an orig_node which has the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag
  444. * set and increases its refcount.
  445. */
  446. static struct batadv_orig_node *
  447. batadv_mcast_forw_unsnoop_node_get(struct batadv_priv *bat_priv)
  448. {
  449. struct batadv_orig_node *tmp_orig_node, *orig_node = NULL;
  450. rcu_read_lock();
  451. hlist_for_each_entry_rcu(tmp_orig_node,
  452. &bat_priv->mcast.want_all_unsnoopables_list,
  453. mcast_want_all_unsnoopables_node) {
  454. if (!atomic_inc_not_zero(&tmp_orig_node->refcount))
  455. continue;
  456. orig_node = tmp_orig_node;
  457. break;
  458. }
  459. rcu_read_unlock();
  460. return orig_node;
  461. }
  462. /**
  463. * batadv_mcast_forw_mode - check on how to forward a multicast packet
  464. * @bat_priv: the bat priv with all the soft interface information
  465. * @skb: The multicast packet to check
  466. * @orig: an originator to be set to forward the skb to
  467. *
  468. * Returns the forwarding mode as enum batadv_forw_mode and in case of
  469. * BATADV_FORW_SINGLE set the orig to the single originator the skb
  470. * should be forwarded to.
  471. */
  472. enum batadv_forw_mode
  473. batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb,
  474. struct batadv_orig_node **orig)
  475. {
  476. int ret, tt_count, ip_count, unsnoop_count, total_count;
  477. bool is_unsnoopable = false;
  478. struct ethhdr *ethhdr;
  479. ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable);
  480. if (ret == -ENOMEM)
  481. return BATADV_FORW_NONE;
  482. else if (ret < 0)
  483. return BATADV_FORW_ALL;
  484. ethhdr = eth_hdr(skb);
  485. tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest,
  486. BATADV_NO_FLAGS);
  487. ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr);
  488. unsnoop_count = !is_unsnoopable ? 0 :
  489. atomic_read(&bat_priv->mcast.num_want_all_unsnoopables);
  490. total_count = tt_count + ip_count + unsnoop_count;
  491. switch (total_count) {
  492. case 1:
  493. if (tt_count)
  494. *orig = batadv_mcast_forw_tt_node_get(bat_priv, ethhdr);
  495. else if (ip_count)
  496. *orig = batadv_mcast_forw_ip_node_get(bat_priv, ethhdr);
  497. else if (unsnoop_count)
  498. *orig = batadv_mcast_forw_unsnoop_node_get(bat_priv);
  499. if (*orig)
  500. return BATADV_FORW_SINGLE;
  501. /* fall through */
  502. case 0:
  503. return BATADV_FORW_NONE;
  504. default:
  505. return BATADV_FORW_ALL;
  506. }
  507. }
  508. /**
  509. * batadv_mcast_want_unsnoop_update - update unsnoop counter and list
  510. * @bat_priv: the bat priv with all the soft interface information
  511. * @orig: the orig_node which multicast state might have changed of
  512. * @mcast_flags: flags indicating the new multicast state
  513. *
  514. * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator,
  515. * orig, has toggled then this method updates counter and list accordingly.
  516. */
  517. static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv,
  518. struct batadv_orig_node *orig,
  519. uint8_t mcast_flags)
  520. {
  521. /* switched from flag unset to set */
  522. if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES &&
  523. !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) {
  524. atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables);
  525. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  526. hlist_add_head_rcu(&orig->mcast_want_all_unsnoopables_node,
  527. &bat_priv->mcast.want_all_unsnoopables_list);
  528. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  529. /* switched from flag set to unset */
  530. } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) &&
  531. orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) {
  532. atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables);
  533. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  534. hlist_del_rcu(&orig->mcast_want_all_unsnoopables_node);
  535. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  536. }
  537. }
  538. /**
  539. * batadv_mcast_want_ipv4_update - update want-all-ipv4 counter and list
  540. * @bat_priv: the bat priv with all the soft interface information
  541. * @orig: the orig_node which multicast state might have changed of
  542. * @mcast_flags: flags indicating the new multicast state
  543. *
  544. * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has
  545. * toggled then this method updates counter and list accordingly.
  546. */
  547. static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv,
  548. struct batadv_orig_node *orig,
  549. uint8_t mcast_flags)
  550. {
  551. /* switched from flag unset to set */
  552. if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 &&
  553. !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) {
  554. atomic_inc(&bat_priv->mcast.num_want_all_ipv4);
  555. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  556. hlist_add_head_rcu(&orig->mcast_want_all_ipv4_node,
  557. &bat_priv->mcast.want_all_ipv4_list);
  558. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  559. /* switched from flag set to unset */
  560. } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) &&
  561. orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) {
  562. atomic_dec(&bat_priv->mcast.num_want_all_ipv4);
  563. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  564. hlist_del_rcu(&orig->mcast_want_all_ipv4_node);
  565. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  566. }
  567. }
  568. /**
  569. * batadv_mcast_want_ipv6_update - update want-all-ipv6 counter and list
  570. * @bat_priv: the bat priv with all the soft interface information
  571. * @orig: the orig_node which multicast state might have changed of
  572. * @mcast_flags: flags indicating the new multicast state
  573. *
  574. * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has
  575. * toggled then this method updates counter and list accordingly.
  576. */
  577. static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv,
  578. struct batadv_orig_node *orig,
  579. uint8_t mcast_flags)
  580. {
  581. /* switched from flag unset to set */
  582. if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 &&
  583. !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) {
  584. atomic_inc(&bat_priv->mcast.num_want_all_ipv6);
  585. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  586. hlist_add_head_rcu(&orig->mcast_want_all_ipv6_node,
  587. &bat_priv->mcast.want_all_ipv6_list);
  588. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  589. /* switched from flag set to unset */
  590. } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) &&
  591. orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) {
  592. atomic_dec(&bat_priv->mcast.num_want_all_ipv6);
  593. spin_lock_bh(&bat_priv->mcast.want_lists_lock);
  594. hlist_del_rcu(&orig->mcast_want_all_ipv6_node);
  595. spin_unlock_bh(&bat_priv->mcast.want_lists_lock);
  596. }
  597. }
  598. /**
  599. * batadv_mcast_tvlv_ogm_handler_v1 - process incoming multicast tvlv container
  600. * @bat_priv: the bat priv with all the soft interface information
  601. * @orig: the orig_node of the ogm
  602. * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
  603. * @tvlv_value: tvlv buffer containing the multicast data
  604. * @tvlv_value_len: tvlv buffer length
  605. */
  606. static void batadv_mcast_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
  607. struct batadv_orig_node *orig,
  608. uint8_t flags,
  609. void *tvlv_value,
  610. uint16_t tvlv_value_len)
  611. {
  612. bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
  613. uint8_t mcast_flags = BATADV_NO_FLAGS;
  614. bool orig_initialized;
  615. orig_initialized = orig->capa_initialized & BATADV_ORIG_CAPA_HAS_MCAST;
  616. /* If mcast support is turned on decrease the disabled mcast node
  617. * counter only if we had increased it for this node before. If this
  618. * is a completely new orig_node no need to decrease the counter.
  619. */
  620. if (orig_mcast_enabled &&
  621. !(orig->capabilities & BATADV_ORIG_CAPA_HAS_MCAST)) {
  622. if (orig_initialized)
  623. atomic_dec(&bat_priv->mcast.num_disabled);
  624. orig->capabilities |= BATADV_ORIG_CAPA_HAS_MCAST;
  625. /* If mcast support is being switched off or if this is an initial
  626. * OGM without mcast support then increase the disabled mcast
  627. * node counter.
  628. */
  629. } else if (!orig_mcast_enabled &&
  630. (orig->capabilities & BATADV_ORIG_CAPA_HAS_MCAST ||
  631. !orig_initialized)) {
  632. atomic_inc(&bat_priv->mcast.num_disabled);
  633. orig->capabilities &= ~BATADV_ORIG_CAPA_HAS_MCAST;
  634. }
  635. orig->capa_initialized |= BATADV_ORIG_CAPA_HAS_MCAST;
  636. if (orig_mcast_enabled && tvlv_value &&
  637. (tvlv_value_len >= sizeof(mcast_flags)))
  638. mcast_flags = *(uint8_t *)tvlv_value;
  639. batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags);
  640. batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags);
  641. batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags);
  642. orig->mcast_flags = mcast_flags;
  643. }
  644. /**
  645. * batadv_mcast_init - initialize the multicast optimizations structures
  646. * @bat_priv: the bat priv with all the soft interface information
  647. */
  648. void batadv_mcast_init(struct batadv_priv *bat_priv)
  649. {
  650. batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler_v1,
  651. NULL, BATADV_TVLV_MCAST, 1,
  652. BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
  653. }
  654. /**
  655. * batadv_mcast_free - free the multicast optimizations structures
  656. * @bat_priv: the bat priv with all the soft interface information
  657. */
  658. void batadv_mcast_free(struct batadv_priv *bat_priv)
  659. {
  660. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 1);
  661. batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 1);
  662. batadv_mcast_mla_tt_retract(bat_priv, NULL);
  663. }
  664. /**
  665. * batadv_mcast_purge_orig - reset originator global mcast state modifications
  666. * @orig: the originator which is going to get purged
  667. */
  668. void batadv_mcast_purge_orig(struct batadv_orig_node *orig)
  669. {
  670. struct batadv_priv *bat_priv = orig->bat_priv;
  671. if (!(orig->capabilities & BATADV_ORIG_CAPA_HAS_MCAST) &&
  672. orig->capa_initialized & BATADV_ORIG_CAPA_HAS_MCAST)
  673. atomic_dec(&bat_priv->mcast.num_disabled);
  674. batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS);
  675. batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS);
  676. batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS);
  677. }