multicast.c 25 KB

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