1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (C) B.A.T.M.A.N. contributors: 3 * 4 * Linus Lüssing 5 */ 6 7 #include "multicast.h" 8 #include "main.h" 9 10 #include <linux/atomic.h> 11 #include <linux/bitops.h> 12 #include <linux/bug.h> 13 #include <linux/byteorder/generic.h> 14 #include <linux/container_of.h> 15 #include <linux/err.h> 16 #include <linux/errno.h> 17 #include <linux/etherdevice.h> 18 #include <linux/gfp.h> 19 #include <linux/icmpv6.h> 20 #include <linux/if_bridge.h> 21 #include <linux/if_ether.h> 22 #include <linux/igmp.h> 23 #include <linux/in.h> 24 #include <linux/in6.h> 25 #include <linux/inetdevice.h> 26 #include <linux/ip.h> 27 #include <linux/ipv6.h> 28 #include <linux/jiffies.h> 29 #include <linux/list.h> 30 #include <linux/lockdep.h> 31 #include <linux/netdevice.h> 32 #include <linux/netlink.h> 33 #include <linux/printk.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/sprintf.h> 40 #include <linux/stddef.h> 41 #include <linux/string.h> 42 #include <linux/types.h> 43 #include <linux/workqueue.h> 44 #include <net/addrconf.h> 45 #include <net/genetlink.h> 46 #include <net/if_inet6.h> 47 #include <net/ip.h> 48 #include <net/ipv6.h> 49 #include <net/netlink.h> 50 #include <uapi/linux/batadv_packet.h> 51 #include <uapi/linux/batman_adv.h> 52 53 #include "bridge_loop_avoidance.h" 54 #include "hard-interface.h" 55 #include "hash.h" 56 #include "log.h" 57 #include "netlink.h" 58 #include "send.h" 59 #include "translation-table.h" 60 #include "tvlv.h" 61 62 static void batadv_mcast_mla_update(struct work_struct *work); 63 64 /** 65 * batadv_mcast_start_timer() - schedule the multicast periodic worker 66 * @bat_priv: the bat priv with all the mesh interface information 67 */ 68 static void batadv_mcast_start_timer(struct batadv_priv *bat_priv) 69 { 70 queue_delayed_work(batadv_event_workqueue, &bat_priv->mcast.work, 71 msecs_to_jiffies(BATADV_MCAST_WORK_PERIOD)); 72 } 73 74 /** 75 * batadv_mcast_get_bridge() - get the bridge on top of the meshif if it exists 76 * @mesh_iface: netdev struct of the mesh interface 77 * 78 * If the given mesh interface has a bridge on top then the refcount 79 * of the according net device is increased. 80 * 81 * Return: NULL if no such bridge exists. Otherwise the net device of the 82 * bridge. 83 */ 84 static struct net_device *batadv_mcast_get_bridge(struct net_device *mesh_iface) 85 { 86 struct net_device *upper = mesh_iface; 87 88 rcu_read_lock(); 89 do { 90 upper = netdev_master_upper_dev_get_rcu(upper); 91 } while (upper && !netif_is_bridge_master(upper)); 92 93 dev_hold(upper); 94 rcu_read_unlock(); 95 96 return upper; 97 } 98 99 /** 100 * batadv_mcast_mla_rtr_flags_meshif_get_ipv4() - get mcast router flags from 101 * node for IPv4 102 * @dev: the interface to check 103 * 104 * Checks the presence of an IPv4 multicast router on this node. 105 * 106 * Caller needs to hold rcu read lock. 107 * 108 * Return: BATADV_NO_FLAGS if present, BATADV_MCAST_WANT_NO_RTR4 otherwise. 109 */ 110 static u8 batadv_mcast_mla_rtr_flags_meshif_get_ipv4(struct net_device *dev) 111 { 112 struct in_device *in_dev = __in_dev_get_rcu(dev); 113 114 if (in_dev && IN_DEV_MFORWARD(in_dev)) 115 return BATADV_NO_FLAGS; 116 else 117 return BATADV_MCAST_WANT_NO_RTR4; 118 } 119 120 /** 121 * batadv_mcast_mla_rtr_flags_meshif_get_ipv6() - get mcast router flags from 122 * node for IPv6 123 * @dev: the interface to check 124 * 125 * Checks the presence of an IPv6 multicast router on this node. 126 * 127 * Caller needs to hold rcu read lock. 128 * 129 * Return: BATADV_NO_FLAGS if present, BATADV_MCAST_WANT_NO_RTR6 otherwise. 130 */ 131 #if IS_ENABLED(CONFIG_IPV6_MROUTE) 132 static u8 batadv_mcast_mla_rtr_flags_meshif_get_ipv6(struct net_device *dev) 133 { 134 struct inet6_dev *in6_dev = __in6_dev_get(dev); 135 136 if (in6_dev && atomic_read(&in6_dev->cnf.mc_forwarding)) 137 return BATADV_NO_FLAGS; 138 else 139 return BATADV_MCAST_WANT_NO_RTR6; 140 } 141 #else 142 static inline u8 143 batadv_mcast_mla_rtr_flags_meshif_get_ipv6(struct net_device *dev) 144 { 145 return BATADV_MCAST_WANT_NO_RTR6; 146 } 147 #endif 148 149 /** 150 * batadv_mcast_mla_rtr_flags_meshif_get() - get mcast router flags from node 151 * @bat_priv: the bat priv with all the mesh interface information 152 * @bridge: bridge interface on top of the mesh_iface if present, 153 * otherwise pass NULL 154 * 155 * Checks the presence of IPv4 and IPv6 multicast routers on this 156 * node. 157 * 158 * Return: 159 * BATADV_NO_FLAGS: Both an IPv4 and IPv6 multicast router is present 160 * BATADV_MCAST_WANT_NO_RTR4: No IPv4 multicast router is present 161 * BATADV_MCAST_WANT_NO_RTR6: No IPv6 multicast router is present 162 * The former two OR'd: no multicast router is present 163 */ 164 static u8 batadv_mcast_mla_rtr_flags_meshif_get(struct batadv_priv *bat_priv, 165 struct net_device *bridge) 166 { 167 struct net_device *dev = bridge ? bridge : bat_priv->mesh_iface; 168 u8 flags = BATADV_NO_FLAGS; 169 170 rcu_read_lock(); 171 172 flags |= batadv_mcast_mla_rtr_flags_meshif_get_ipv4(dev); 173 flags |= batadv_mcast_mla_rtr_flags_meshif_get_ipv6(dev); 174 175 rcu_read_unlock(); 176 177 return flags; 178 } 179 180 /** 181 * batadv_mcast_mla_rtr_flags_bridge_get() - get mcast router flags from bridge 182 * @bat_priv: the bat priv with all the mesh interface information 183 * @bridge: bridge interface on top of the mesh_iface if present, 184 * otherwise pass NULL 185 * 186 * Checks the presence of IPv4 and IPv6 multicast routers behind a bridge. 187 * 188 * Return: 189 * BATADV_NO_FLAGS: Both an IPv4 and IPv6 multicast router is present 190 * BATADV_MCAST_WANT_NO_RTR4: No IPv4 multicast router is present 191 * BATADV_MCAST_WANT_NO_RTR6: No IPv6 multicast router is present 192 * The former two OR'd: no multicast router is present 193 */ 194 static u8 batadv_mcast_mla_rtr_flags_bridge_get(struct batadv_priv *bat_priv, 195 struct net_device *bridge) 196 { 197 struct net_device *dev = bat_priv->mesh_iface; 198 u8 flags = BATADV_NO_FLAGS; 199 200 if (!bridge) 201 return BATADV_MCAST_WANT_NO_RTR4 | BATADV_MCAST_WANT_NO_RTR6; 202 203 if (!br_multicast_has_router_adjacent(dev, ETH_P_IP)) 204 flags |= BATADV_MCAST_WANT_NO_RTR4; 205 if (!br_multicast_has_router_adjacent(dev, ETH_P_IPV6)) 206 flags |= BATADV_MCAST_WANT_NO_RTR6; 207 208 return flags; 209 } 210 211 /** 212 * batadv_mcast_mla_rtr_flags_get() - get multicast router flags 213 * @bat_priv: the bat priv with all the mesh interface information 214 * @bridge: bridge interface on top of the mesh_iface if present, 215 * otherwise pass NULL 216 * 217 * Checks the presence of IPv4 and IPv6 multicast routers on this 218 * node or behind its bridge. 219 * 220 * Return: 221 * BATADV_NO_FLAGS: Both an IPv4 and IPv6 multicast router is present 222 * BATADV_MCAST_WANT_NO_RTR4: No IPv4 multicast router is present 223 * BATADV_MCAST_WANT_NO_RTR6: No IPv6 multicast router is present 224 * The former two OR'd: no multicast router is present 225 */ 226 static u8 batadv_mcast_mla_rtr_flags_get(struct batadv_priv *bat_priv, 227 struct net_device *bridge) 228 { 229 u8 flags = BATADV_MCAST_WANT_NO_RTR4 | BATADV_MCAST_WANT_NO_RTR6; 230 231 flags &= batadv_mcast_mla_rtr_flags_meshif_get(bat_priv, bridge); 232 flags &= batadv_mcast_mla_rtr_flags_bridge_get(bat_priv, bridge); 233 234 return flags; 235 } 236 237 /** 238 * batadv_mcast_mla_forw_flags_get() - get multicast forwarding flags 239 * @bat_priv: the bat priv with all the mesh interface information 240 * 241 * Checks if all active hard interfaces have an MTU larger or equal to 1280 242 * bytes (IPv6 minimum MTU). 243 * 244 * Return: BATADV_MCAST_HAVE_MC_PTYPE_CAPA if yes, BATADV_NO_FLAGS otherwise. 245 */ 246 static u8 batadv_mcast_mla_forw_flags_get(struct batadv_priv *bat_priv) 247 { 248 const struct batadv_hard_iface *hard_iface; 249 struct list_head *iter; 250 251 rcu_read_lock(); 252 netdev_for_each_lower_private_rcu(bat_priv->mesh_iface, hard_iface, iter) { 253 if (hard_iface->if_status != BATADV_IF_ACTIVE) 254 continue; 255 256 if (hard_iface->net_dev->mtu < IPV6_MIN_MTU) { 257 rcu_read_unlock(); 258 return BATADV_NO_FLAGS; 259 } 260 } 261 rcu_read_unlock(); 262 263 return BATADV_MCAST_HAVE_MC_PTYPE_CAPA; 264 } 265 266 /** 267 * batadv_mcast_mla_flags_get() - get the new multicast flags 268 * @bat_priv: the bat priv with all the mesh interface information 269 * 270 * Return: A set of flags for the current/next TVLV, querier and 271 * bridge state. 272 */ 273 static struct batadv_mcast_mla_flags 274 batadv_mcast_mla_flags_get(struct batadv_priv *bat_priv) 275 { 276 struct net_device *dev = bat_priv->mesh_iface; 277 struct batadv_mcast_mla_flags mla_flags; 278 struct batadv_mcast_querier_state *qr4; 279 struct batadv_mcast_querier_state *qr6; 280 struct net_device *bridge; 281 282 bridge = batadv_mcast_get_bridge(dev); 283 284 memset(&mla_flags, 0, sizeof(mla_flags)); 285 mla_flags.enabled = 1; 286 mla_flags.tvlv_flags |= batadv_mcast_mla_rtr_flags_get(bat_priv, 287 bridge); 288 mla_flags.tvlv_flags |= batadv_mcast_mla_forw_flags_get(bat_priv); 289 290 if (!bridge) 291 return mla_flags; 292 293 dev_put(bridge); 294 295 mla_flags.bridged = 1; 296 qr4 = &mla_flags.querier_ipv4; 297 qr6 = &mla_flags.querier_ipv6; 298 299 if (!IS_ENABLED(CONFIG_BRIDGE_IGMP_SNOOPING)) 300 pr_warn_once("No bridge IGMP snooping compiled - multicast optimizations disabled\n"); 301 302 qr4->exists = br_multicast_has_querier_anywhere(dev, ETH_P_IP); 303 qr4->shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IP); 304 305 qr6->exists = br_multicast_has_querier_anywhere(dev, ETH_P_IPV6); 306 qr6->shadowing = br_multicast_has_querier_adjacent(dev, ETH_P_IPV6); 307 308 mla_flags.tvlv_flags |= BATADV_MCAST_WANT_ALL_UNSNOOPABLES; 309 310 /* 1) If no querier exists at all, then multicast listeners on 311 * our local TT clients behind the bridge will keep silent. 312 * 2) If the selected querier is on one of our local TT clients, 313 * behind the bridge, then this querier might shadow multicast 314 * listeners on our local TT clients, behind this bridge. 315 * 316 * In both cases, we will signalize other batman nodes that 317 * we need all multicast traffic of the according protocol. 318 */ 319 if (!qr4->exists || qr4->shadowing) { 320 mla_flags.tvlv_flags |= BATADV_MCAST_WANT_ALL_IPV4; 321 mla_flags.tvlv_flags &= ~BATADV_MCAST_WANT_NO_RTR4; 322 } 323 324 if (!qr6->exists || qr6->shadowing) { 325 mla_flags.tvlv_flags |= BATADV_MCAST_WANT_ALL_IPV6; 326 mla_flags.tvlv_flags &= ~BATADV_MCAST_WANT_NO_RTR6; 327 } 328 329 return mla_flags; 330 } 331 332 /** 333 * batadv_mcast_mla_is_duplicate() - check whether an address is in a list 334 * @mcast_addr: the multicast address to check 335 * @mcast_list: the list with multicast addresses to search in 336 * 337 * Return: true if the given address is already in the given list. 338 * Otherwise returns false. 339 */ 340 static bool batadv_mcast_mla_is_duplicate(u8 *mcast_addr, 341 struct hlist_head *mcast_list) 342 { 343 struct batadv_hw_addr *mcast_entry; 344 345 hlist_for_each_entry(mcast_entry, mcast_list, list) 346 if (batadv_compare_eth(mcast_entry->addr, mcast_addr)) 347 return true; 348 349 return false; 350 } 351 352 /** 353 * batadv_mcast_mla_meshif_get_ipv4() - get meshif IPv4 multicast listeners 354 * @dev: the device to collect multicast addresses from 355 * @mcast_list: a list to put found addresses into 356 * @flags: flags indicating the new multicast state 357 * 358 * Collects multicast addresses of IPv4 multicast listeners residing 359 * on this kernel on the given mesh interface, dev, in 360 * the given mcast_list. In general, multicast listeners provided by 361 * your multicast receiving applications run directly on this node. 362 * 363 * Return: -ENOMEM on memory allocation error or the number of 364 * items added to the mcast_list otherwise. 365 */ 366 static int 367 batadv_mcast_mla_meshif_get_ipv4(struct net_device *dev, 368 struct hlist_head *mcast_list, 369 struct batadv_mcast_mla_flags *flags) 370 { 371 struct batadv_hw_addr *new; 372 struct in_device *in_dev; 373 u8 mcast_addr[ETH_ALEN]; 374 struct ip_mc_list *pmc; 375 int ret = 0; 376 377 if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_IPV4) 378 return 0; 379 380 rcu_read_lock(); 381 382 in_dev = __in_dev_get_rcu(dev); 383 if (!in_dev) { 384 rcu_read_unlock(); 385 return 0; 386 } 387 388 for (pmc = rcu_dereference(in_dev->mc_list); pmc; 389 pmc = rcu_dereference(pmc->next_rcu)) { 390 if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES && 391 ipv4_is_local_multicast(pmc->multiaddr)) 392 continue; 393 394 if (!(flags->tvlv_flags & BATADV_MCAST_WANT_NO_RTR4) && 395 !ipv4_is_local_multicast(pmc->multiaddr)) 396 continue; 397 398 ip_eth_mc_map(pmc->multiaddr, mcast_addr); 399 400 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list)) 401 continue; 402 403 new = kmalloc_obj(*new, GFP_ATOMIC); 404 if (!new) { 405 ret = -ENOMEM; 406 break; 407 } 408 409 ether_addr_copy(new->addr, mcast_addr); 410 hlist_add_head(&new->list, mcast_list); 411 ret++; 412 } 413 rcu_read_unlock(); 414 415 return ret; 416 } 417 418 /** 419 * batadv_mcast_mla_meshif_get_ipv6() - get meshif IPv6 multicast listeners 420 * @dev: the device to collect multicast addresses from 421 * @mcast_list: a list to put found addresses into 422 * @flags: flags indicating the new multicast state 423 * 424 * Collects multicast addresses of IPv6 multicast listeners residing 425 * on this kernel on the given mesh interface, dev, in 426 * the given mcast_list. In general, multicast listeners provided by 427 * your multicast receiving applications run directly on this node. 428 * 429 * Return: -ENOMEM on memory allocation error or the number of 430 * items added to the mcast_list otherwise. 431 */ 432 #if IS_ENABLED(CONFIG_IPV6) 433 static int 434 batadv_mcast_mla_meshif_get_ipv6(struct net_device *dev, 435 struct hlist_head *mcast_list, 436 struct batadv_mcast_mla_flags *flags) 437 { 438 struct batadv_hw_addr *new; 439 struct inet6_dev *in6_dev; 440 u8 mcast_addr[ETH_ALEN]; 441 struct ifmcaddr6 *pmc6; 442 int ret = 0; 443 444 if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_IPV6) 445 return 0; 446 447 rcu_read_lock(); 448 449 in6_dev = __in6_dev_get(dev); 450 if (!in6_dev) { 451 rcu_read_unlock(); 452 return 0; 453 } 454 455 for (pmc6 = rcu_dereference(in6_dev->mc_list); 456 pmc6; 457 pmc6 = rcu_dereference(pmc6->next)) { 458 if (IPV6_ADDR_MC_SCOPE(&pmc6->mca_addr) < 459 IPV6_ADDR_SCOPE_LINKLOCAL) 460 continue; 461 462 if (flags->tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES && 463 ipv6_addr_is_ll_all_nodes(&pmc6->mca_addr)) 464 continue; 465 466 if (!(flags->tvlv_flags & BATADV_MCAST_WANT_NO_RTR6) && 467 IPV6_ADDR_MC_SCOPE(&pmc6->mca_addr) > 468 IPV6_ADDR_SCOPE_LINKLOCAL) 469 continue; 470 471 ipv6_eth_mc_map(&pmc6->mca_addr, mcast_addr); 472 473 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list)) 474 continue; 475 476 new = kmalloc_obj(*new, GFP_ATOMIC); 477 if (!new) { 478 ret = -ENOMEM; 479 break; 480 } 481 482 ether_addr_copy(new->addr, mcast_addr); 483 hlist_add_head(&new->list, mcast_list); 484 ret++; 485 } 486 rcu_read_unlock(); 487 488 return ret; 489 } 490 #else 491 static inline int 492 batadv_mcast_mla_meshif_get_ipv6(struct net_device *dev, 493 struct hlist_head *mcast_list, 494 struct batadv_mcast_mla_flags *flags) 495 { 496 return 0; 497 } 498 #endif 499 500 /** 501 * batadv_mcast_mla_meshif_get() - get meshif multicast listeners 502 * @dev: the device to collect multicast addresses from 503 * @mcast_list: a list to put found addresses into 504 * @flags: flags indicating the new multicast state 505 * 506 * Collects multicast addresses of multicast listeners residing 507 * on this kernel on the given mesh interface, dev, in 508 * the given mcast_list. In general, multicast listeners provided by 509 * your multicast receiving applications run directly on this node. 510 * 511 * If there is a bridge interface on top of dev, collect from that one 512 * instead. Just like with IP addresses and routes, multicast listeners 513 * will(/should) register to the bridge interface instead of an 514 * enslaved bat0. 515 * 516 * Return: -ENOMEM on memory allocation error or the number of 517 * items added to the mcast_list otherwise. 518 */ 519 static int 520 batadv_mcast_mla_meshif_get(struct net_device *dev, 521 struct hlist_head *mcast_list, 522 struct batadv_mcast_mla_flags *flags) 523 { 524 struct net_device *bridge = batadv_mcast_get_bridge(dev); 525 int ret6 = 0; 526 int ret4; 527 528 if (bridge) 529 dev = bridge; 530 531 ret4 = batadv_mcast_mla_meshif_get_ipv4(dev, mcast_list, flags); 532 if (ret4 < 0) 533 goto out; 534 535 ret6 = batadv_mcast_mla_meshif_get_ipv6(dev, mcast_list, flags); 536 if (ret6 < 0) { 537 ret4 = 0; 538 goto out; 539 } 540 541 out: 542 dev_put(bridge); 543 544 return ret4 + ret6; 545 } 546 547 /** 548 * batadv_mcast_mla_br_addr_cpy() - copy a bridge multicast address 549 * @dst: destination to write to - a multicast MAC address 550 * @src: source to read from - a multicast IP address 551 * 552 * Converts a given multicast IPv4/IPv6 address from a bridge 553 * to its matching multicast MAC address and copies it into the given 554 * destination buffer. 555 * 556 * Caller needs to make sure the destination buffer can hold 557 * at least ETH_ALEN bytes. 558 */ 559 static void batadv_mcast_mla_br_addr_cpy(char *dst, const struct br_ip *src) 560 { 561 if (src->proto == htons(ETH_P_IP)) 562 ip_eth_mc_map(src->dst.ip4, dst); 563 #if IS_ENABLED(CONFIG_IPV6) 564 else if (src->proto == htons(ETH_P_IPV6)) 565 ipv6_eth_mc_map(&src->dst.ip6, dst); 566 #endif 567 else 568 eth_zero_addr(dst); 569 } 570 571 /** 572 * batadv_mcast_mla_bridge_get() - get bridged-in multicast listeners 573 * @dev: a bridge slave whose bridge to collect multicast addresses from 574 * @mcast_list: a list to put found addresses into 575 * @flags: flags indicating the new multicast state 576 * 577 * Collects multicast addresses of multicast listeners residing 578 * on foreign, non-mesh devices which we gave access to our mesh via 579 * a bridge on top of the given mesh interface, dev, in the given 580 * mcast_list. 581 * 582 * Return: -ENOMEM on memory allocation error or the number of 583 * items added to the mcast_list otherwise. 584 */ 585 static int batadv_mcast_mla_bridge_get(struct net_device *dev, 586 struct hlist_head *mcast_list, 587 struct batadv_mcast_mla_flags *flags) 588 { 589 struct list_head bridge_mcast_list = LIST_HEAD_INIT(bridge_mcast_list); 590 u8 tvlv_flags = flags->tvlv_flags; 591 struct br_ip_list *br_ip_entry; 592 struct batadv_hw_addr *new; 593 u8 mcast_addr[ETH_ALEN]; 594 struct br_ip_list *tmp; 595 int ret; 596 597 /* we don't need to detect these devices/listeners, the IGMP/MLD 598 * snooping code of the Linux bridge already does that for us 599 */ 600 ret = br_multicast_list_adjacent(dev, &bridge_mcast_list); 601 if (ret < 0) 602 goto out; 603 604 list_for_each_entry(br_ip_entry, &bridge_mcast_list, list) { 605 if (br_ip_entry->addr.proto == htons(ETH_P_IP)) { 606 if (tvlv_flags & BATADV_MCAST_WANT_ALL_IPV4) 607 continue; 608 609 if (tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES && 610 ipv4_is_local_multicast(br_ip_entry->addr.dst.ip4)) 611 continue; 612 613 if (!(tvlv_flags & BATADV_MCAST_WANT_NO_RTR4) && 614 !ipv4_is_local_multicast(br_ip_entry->addr.dst.ip4)) 615 continue; 616 } 617 618 #if IS_ENABLED(CONFIG_IPV6) 619 if (br_ip_entry->addr.proto == htons(ETH_P_IPV6)) { 620 if (tvlv_flags & BATADV_MCAST_WANT_ALL_IPV6) 621 continue; 622 623 if (tvlv_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES && 624 ipv6_addr_is_ll_all_nodes(&br_ip_entry->addr.dst.ip6)) 625 continue; 626 627 if (!(tvlv_flags & BATADV_MCAST_WANT_NO_RTR6) && 628 IPV6_ADDR_MC_SCOPE(&br_ip_entry->addr.dst.ip6) > 629 IPV6_ADDR_SCOPE_LINKLOCAL) 630 continue; 631 } 632 #endif 633 634 batadv_mcast_mla_br_addr_cpy(mcast_addr, &br_ip_entry->addr); 635 if (batadv_mcast_mla_is_duplicate(mcast_addr, mcast_list)) 636 continue; 637 638 new = kmalloc_obj(*new, GFP_ATOMIC); 639 if (!new) { 640 ret = -ENOMEM; 641 break; 642 } 643 644 ether_addr_copy(new->addr, mcast_addr); 645 hlist_add_head(&new->list, mcast_list); 646 } 647 648 out: 649 list_for_each_entry_safe(br_ip_entry, tmp, &bridge_mcast_list, list) { 650 list_del(&br_ip_entry->list); 651 kfree(br_ip_entry); 652 } 653 654 return ret; 655 } 656 657 /** 658 * batadv_mcast_mla_list_free() - free a list of multicast addresses 659 * @mcast_list: the list to free 660 * 661 * Removes and frees all items in the given mcast_list. 662 */ 663 static void batadv_mcast_mla_list_free(struct hlist_head *mcast_list) 664 { 665 struct batadv_hw_addr *mcast_entry; 666 struct hlist_node *tmp; 667 668 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) { 669 hlist_del(&mcast_entry->list); 670 kfree(mcast_entry); 671 } 672 } 673 674 /** 675 * batadv_mcast_mla_tt_retract() - clean up multicast listener announcements 676 * @bat_priv: the bat priv with all the mesh interface information 677 * @mcast_list: a list of addresses which should _not_ be removed 678 * 679 * Retracts the announcement of any multicast listener from the 680 * translation table except the ones listed in the given mcast_list. 681 * 682 * If mcast_list is NULL then all are retracted. 683 */ 684 static void batadv_mcast_mla_tt_retract(struct batadv_priv *bat_priv, 685 struct hlist_head *mcast_list) 686 { 687 struct batadv_hw_addr *mcast_entry; 688 struct hlist_node *tmp; 689 690 hlist_for_each_entry_safe(mcast_entry, tmp, &bat_priv->mcast.mla_list, 691 list) { 692 if (mcast_list && 693 batadv_mcast_mla_is_duplicate(mcast_entry->addr, 694 mcast_list)) 695 continue; 696 697 batadv_tt_local_remove(bat_priv, mcast_entry->addr, 698 BATADV_NO_FLAGS, 699 "mcast TT outdated", false); 700 701 hlist_del(&mcast_entry->list); 702 kfree(mcast_entry); 703 } 704 } 705 706 /** 707 * batadv_mcast_mla_tt_add() - add multicast listener announcements 708 * @bat_priv: the bat priv with all the mesh interface information 709 * @mcast_list: a list of addresses which are going to get added 710 * 711 * Adds multicast listener announcements from the given mcast_list to the 712 * translation table if they have not been added yet. 713 */ 714 static void batadv_mcast_mla_tt_add(struct batadv_priv *bat_priv, 715 struct hlist_head *mcast_list) 716 { 717 struct batadv_hw_addr *mcast_entry; 718 struct hlist_node *tmp; 719 720 if (!mcast_list) 721 return; 722 723 hlist_for_each_entry_safe(mcast_entry, tmp, mcast_list, list) { 724 if (batadv_mcast_mla_is_duplicate(mcast_entry->addr, 725 &bat_priv->mcast.mla_list)) 726 continue; 727 728 if (!batadv_tt_local_add(bat_priv->mesh_iface, 729 mcast_entry->addr, BATADV_NO_FLAGS, 730 BATADV_NULL_IFINDEX, BATADV_NO_MARK)) 731 continue; 732 733 hlist_del(&mcast_entry->list); 734 hlist_add_head(&mcast_entry->list, &bat_priv->mcast.mla_list); 735 } 736 } 737 738 /** 739 * batadv_mcast_querier_log() - debug output regarding the querier status on 740 * link 741 * @bat_priv: the bat priv with all the mesh interface information 742 * @str_proto: a string for the querier protocol (e.g. "IGMP" or "MLD") 743 * @old_state: the previous querier state on our link 744 * @new_state: the new querier state on our link 745 * 746 * Outputs debug messages to the logging facility with log level 'mcast' 747 * regarding changes to the querier status on the link which are relevant 748 * to our multicast optimizations. 749 * 750 * Usually this is about whether a querier appeared or vanished in 751 * our mesh or whether the querier is in the suboptimal position of being 752 * behind our local bridge segment: Snooping switches will directly 753 * forward listener reports to the querier, therefore batman-adv and 754 * the bridge will potentially not see these listeners - the querier is 755 * potentially shadowing listeners from us then. 756 * 757 * This is only interesting for nodes with a bridge on top of their 758 * mesh interface. 759 */ 760 static void 761 batadv_mcast_querier_log(struct batadv_priv *bat_priv, char *str_proto, 762 struct batadv_mcast_querier_state *old_state, 763 struct batadv_mcast_querier_state *new_state) 764 { 765 if (!old_state->exists && new_state->exists) 766 batadv_info(bat_priv->mesh_iface, "%s Querier appeared\n", 767 str_proto); 768 else if (old_state->exists && !new_state->exists) 769 batadv_info(bat_priv->mesh_iface, 770 "%s Querier disappeared - multicast optimizations disabled\n", 771 str_proto); 772 else if (!bat_priv->mcast.mla_flags.bridged && !new_state->exists) 773 batadv_info(bat_priv->mesh_iface, 774 "No %s Querier present - multicast optimizations disabled\n", 775 str_proto); 776 777 if (new_state->exists) { 778 if ((!old_state->shadowing && new_state->shadowing) || 779 (!old_state->exists && new_state->shadowing)) 780 batadv_dbg(BATADV_DBG_MCAST, bat_priv, 781 "%s Querier is behind our bridged segment: Might shadow listeners\n", 782 str_proto); 783 else if (old_state->shadowing && !new_state->shadowing) 784 batadv_dbg(BATADV_DBG_MCAST, bat_priv, 785 "%s Querier is not behind our bridged segment\n", 786 str_proto); 787 } 788 } 789 790 /** 791 * batadv_mcast_bridge_log() - debug output for topology changes in bridged 792 * setups 793 * @bat_priv: the bat priv with all the mesh interface information 794 * @new_flags: flags indicating the new multicast state 795 * 796 * If no bridges are ever used on this node, then this function does nothing. 797 * 798 * Otherwise this function outputs debug information to the 'mcast' log level 799 * which might be relevant to our multicast optimizations. 800 * 801 * More precisely, it outputs information when a bridge interface is added or 802 * removed from a mesh interface. And when a bridge is present, it further 803 * outputs information about the querier state which is relevant for the 804 * multicast flags this node is going to set. 805 */ 806 static void 807 batadv_mcast_bridge_log(struct batadv_priv *bat_priv, 808 struct batadv_mcast_mla_flags *new_flags) 809 { 810 struct batadv_mcast_mla_flags *old_flags = &bat_priv->mcast.mla_flags; 811 812 if (!old_flags->bridged && new_flags->bridged) 813 batadv_dbg(BATADV_DBG_MCAST, bat_priv, 814 "Bridge added: Setting Unsnoopables(U)-flag\n"); 815 else if (old_flags->bridged && !new_flags->bridged) 816 batadv_dbg(BATADV_DBG_MCAST, bat_priv, 817 "Bridge removed: Unsetting Unsnoopables(U)-flag\n"); 818 819 if (new_flags->bridged) { 820 batadv_mcast_querier_log(bat_priv, "IGMP", 821 &old_flags->querier_ipv4, 822 &new_flags->querier_ipv4); 823 batadv_mcast_querier_log(bat_priv, "MLD", 824 &old_flags->querier_ipv6, 825 &new_flags->querier_ipv6); 826 } 827 } 828 829 /** 830 * batadv_mcast_flags_log() - output debug information about mcast flag changes 831 * @bat_priv: the bat priv with all the mesh interface information 832 * @flags: TVLV flags indicating the new multicast state 833 * 834 * Whenever the multicast TVLV flags this node announces change, this function 835 * should be used to notify userspace about the change. 836 */ 837 static void batadv_mcast_flags_log(struct batadv_priv *bat_priv, u8 flags) 838 { 839 bool old_enabled = bat_priv->mcast.mla_flags.enabled; 840 u8 old_flags = bat_priv->mcast.mla_flags.tvlv_flags; 841 char str_old_flags[] = "[.... . .]"; 842 843 sprintf(str_old_flags, "[%c%c%c%s%s%c]", 844 (old_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.', 845 (old_flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.', 846 (old_flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.', 847 !(old_flags & BATADV_MCAST_WANT_NO_RTR4) ? "R4" : ". ", 848 !(old_flags & BATADV_MCAST_WANT_NO_RTR6) ? "R6" : ". ", 849 !(old_flags & BATADV_MCAST_HAVE_MC_PTYPE_CAPA) ? 'P' : '.'); 850 851 batadv_dbg(BATADV_DBG_MCAST, bat_priv, 852 "Changing multicast flags from '%s' to '[%c%c%c%s%s%c]'\n", 853 old_enabled ? str_old_flags : "<undefined>", 854 (flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) ? 'U' : '.', 855 (flags & BATADV_MCAST_WANT_ALL_IPV4) ? '4' : '.', 856 (flags & BATADV_MCAST_WANT_ALL_IPV6) ? '6' : '.', 857 !(flags & BATADV_MCAST_WANT_NO_RTR4) ? "R4" : ". ", 858 !(flags & BATADV_MCAST_WANT_NO_RTR6) ? "R6" : ". ", 859 !(flags & BATADV_MCAST_HAVE_MC_PTYPE_CAPA) ? 'P' : '.'); 860 } 861 862 /** 863 * batadv_mcast_mla_flags_update() - update multicast flags 864 * @bat_priv: the bat priv with all the mesh interface information 865 * @flags: flags indicating the new multicast state 866 * 867 * Updates the own multicast tvlv with our current multicast related settings, 868 * capabilities and inabilities. 869 */ 870 static void 871 batadv_mcast_mla_flags_update(struct batadv_priv *bat_priv, 872 struct batadv_mcast_mla_flags *flags) 873 { 874 struct batadv_tvlv_mcast_data mcast_data; 875 876 if (!memcmp(flags, &bat_priv->mcast.mla_flags, sizeof(*flags))) 877 return; 878 879 batadv_mcast_bridge_log(bat_priv, flags); 880 batadv_mcast_flags_log(bat_priv, flags->tvlv_flags); 881 882 mcast_data.flags = flags->tvlv_flags; 883 memset(mcast_data.reserved, 0, sizeof(mcast_data.reserved)); 884 885 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_MCAST, 2, 886 &mcast_data, sizeof(mcast_data)); 887 888 bat_priv->mcast.mla_flags = *flags; 889 } 890 891 /** 892 * __batadv_mcast_mla_update() - update the own MLAs 893 * @bat_priv: the bat priv with all the mesh interface information 894 * 895 * Updates the own multicast listener announcements in the translation 896 * table as well as the own, announced multicast tvlv container. 897 * 898 * Note that non-conflicting reads and writes to bat_priv->mcast.mla_list 899 * in batadv_mcast_mla_tt_retract() and batadv_mcast_mla_tt_add() are 900 * ensured by the non-parallel execution of the worker this function 901 * belongs to. 902 */ 903 static void __batadv_mcast_mla_update(struct batadv_priv *bat_priv) 904 { 905 struct net_device *mesh_iface = bat_priv->mesh_iface; 906 struct hlist_head mcast_list = HLIST_HEAD_INIT; 907 struct batadv_mcast_mla_flags flags; 908 int ret; 909 910 flags = batadv_mcast_mla_flags_get(bat_priv); 911 912 ret = batadv_mcast_mla_meshif_get(mesh_iface, &mcast_list, &flags); 913 if (ret < 0) 914 goto out; 915 916 ret = batadv_mcast_mla_bridge_get(mesh_iface, &mcast_list, &flags); 917 if (ret < 0) 918 goto out; 919 920 spin_lock(&bat_priv->mcast.mla_lock); 921 batadv_mcast_mla_tt_retract(bat_priv, &mcast_list); 922 batadv_mcast_mla_tt_add(bat_priv, &mcast_list); 923 batadv_mcast_mla_flags_update(bat_priv, &flags); 924 spin_unlock(&bat_priv->mcast.mla_lock); 925 926 out: 927 batadv_mcast_mla_list_free(&mcast_list); 928 } 929 930 /** 931 * batadv_mcast_mla_update() - update the own MLAs 932 * @work: kernel work struct 933 * 934 * Updates the own multicast listener announcements in the translation 935 * table as well as the own, announced multicast tvlv container. 936 * 937 * In the end, reschedules the work timer. 938 */ 939 static void batadv_mcast_mla_update(struct work_struct *work) 940 { 941 struct batadv_priv_mcast *priv_mcast; 942 struct delayed_work *delayed_work; 943 struct batadv_priv *bat_priv; 944 945 delayed_work = to_delayed_work(work); 946 priv_mcast = container_of(delayed_work, struct batadv_priv_mcast, work); 947 bat_priv = container_of(priv_mcast, struct batadv_priv, mcast); 948 949 __batadv_mcast_mla_update(bat_priv); 950 batadv_mcast_start_timer(bat_priv); 951 } 952 953 /** 954 * batadv_mcast_is_report_ipv4() - check for IGMP reports 955 * @skb: the ethernet frame destined for the mesh 956 * 957 * Warning: This function may reallocate the skb data buffer via 958 * ip_mc_check_igmp()/... Any pointer into the skb data (e.g. 959 * obtained from skb->data or eth_hdr()) before this call must be considered 960 * invalid afterwards and has to be reacquired. 961 * 962 * Checks whether the given frame is a valid IGMP report. 963 * 964 * Return: If so then true, otherwise false. 965 */ 966 static bool batadv_mcast_is_report_ipv4(struct sk_buff *skb) 967 { 968 if (ip_mc_check_igmp(skb) < 0) 969 return false; 970 971 switch (igmp_hdr(skb)->type) { 972 case IGMP_HOST_MEMBERSHIP_REPORT: 973 case IGMPV2_HOST_MEMBERSHIP_REPORT: 974 case IGMPV3_HOST_MEMBERSHIP_REPORT: 975 return true; 976 } 977 978 return false; 979 } 980 981 /** 982 * batadv_mcast_forw_mode_check_ipv4() - check for optimized forwarding 983 * potential 984 * @bat_priv: the bat priv with all the mesh interface information 985 * @skb: the IPv4 packet to check 986 * @is_unsnoopable: stores whether the destination is snoopable 987 * @is_routable: stores whether the destination is routable 988 * 989 * Checks whether the given IPv4 packet has the potential to be forwarded with a 990 * mode more optimal than classic flooding. 991 * 992 * Return: If so then 0. Otherwise -EINVAL or -ENOMEM in case of memory 993 * allocation failure. 994 */ 995 static int batadv_mcast_forw_mode_check_ipv4(struct batadv_priv *bat_priv, 996 struct sk_buff *skb, 997 bool *is_unsnoopable, 998 int *is_routable) 999 { 1000 struct iphdr *iphdr; 1001 1002 /* We might fail due to out-of-memory -> drop it */ 1003 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*iphdr))) 1004 return -ENOMEM; 1005 1006 if (batadv_mcast_is_report_ipv4(skb)) 1007 return -EINVAL; 1008 1009 iphdr = ip_hdr(skb); 1010 1011 /* link-local multicast listeners behind a bridge are 1012 * not snoopable (see RFC4541, section 2.1.2.2) 1013 */ 1014 if (ipv4_is_local_multicast(iphdr->daddr)) 1015 *is_unsnoopable = true; 1016 else 1017 *is_routable = ETH_P_IP; 1018 1019 return 0; 1020 } 1021 1022 /** 1023 * batadv_mcast_is_report_ipv6() - check for MLD reports 1024 * @skb: the ethernet frame destined for the mesh 1025 * 1026 * Warning: This function may reallocate the skb data buffer via 1027 * ipv6_mc_check_mld()/... Any pointer into the skb data (e.g. 1028 * obtained from skb->data or eth_hdr()) before this call must be considered 1029 * invalid afterwards and has to be reacquired. 1030 * 1031 * Checks whether the given frame is a valid MLD report. 1032 * 1033 * Return: If so then true, otherwise false. 1034 */ 1035 static bool batadv_mcast_is_report_ipv6(struct sk_buff *skb) 1036 { 1037 if (ipv6_mc_check_mld(skb) < 0) 1038 return false; 1039 1040 switch (icmp6_hdr(skb)->icmp6_type) { 1041 case ICMPV6_MGM_REPORT: 1042 case ICMPV6_MLD2_REPORT: 1043 return true; 1044 } 1045 1046 return false; 1047 } 1048 1049 /** 1050 * batadv_mcast_forw_mode_check_ipv6() - check for optimized forwarding 1051 * potential 1052 * @bat_priv: the bat priv with all the mesh interface information 1053 * @skb: the IPv6 packet to check 1054 * @is_unsnoopable: stores whether the destination is snoopable 1055 * @is_routable: stores whether the destination is routable 1056 * 1057 * Checks whether the given IPv6 packet has the potential to be forwarded with a 1058 * mode more optimal than classic flooding. 1059 * 1060 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory 1061 */ 1062 static int batadv_mcast_forw_mode_check_ipv6(struct batadv_priv *bat_priv, 1063 struct sk_buff *skb, 1064 bool *is_unsnoopable, 1065 int *is_routable) 1066 { 1067 struct ipv6hdr *ip6hdr; 1068 1069 /* We might fail due to out-of-memory -> drop it */ 1070 if (!pskb_may_pull(skb, sizeof(struct ethhdr) + sizeof(*ip6hdr))) 1071 return -ENOMEM; 1072 1073 if (batadv_mcast_is_report_ipv6(skb)) 1074 return -EINVAL; 1075 1076 ip6hdr = ipv6_hdr(skb); 1077 1078 if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) < IPV6_ADDR_SCOPE_LINKLOCAL) 1079 return -EINVAL; 1080 1081 /* link-local-all-nodes multicast listeners behind a bridge are 1082 * not snoopable (see RFC4541, section 3, paragraph 3) 1083 */ 1084 if (ipv6_addr_is_ll_all_nodes(&ip6hdr->daddr)) 1085 *is_unsnoopable = true; 1086 else if (IPV6_ADDR_MC_SCOPE(&ip6hdr->daddr) > IPV6_ADDR_SCOPE_LINKLOCAL) 1087 *is_routable = ETH_P_IPV6; 1088 1089 return 0; 1090 } 1091 1092 /** 1093 * batadv_mcast_forw_mode_check() - check for optimized forwarding potential 1094 * @bat_priv: the bat priv with all the mesh interface information 1095 * @skb: the multicast frame to check 1096 * @is_unsnoopable: stores whether the destination is snoopable 1097 * @is_routable: stores whether the destination is routable 1098 * 1099 * Checks whether the given multicast ethernet frame has the potential to be 1100 * forwarded with a mode more optimal than classic flooding. 1101 * 1102 * Return: If so then 0. Otherwise -EINVAL is or -ENOMEM if we are out of memory 1103 */ 1104 static int batadv_mcast_forw_mode_check(struct batadv_priv *bat_priv, 1105 struct sk_buff *skb, 1106 bool *is_unsnoopable, 1107 int *is_routable) 1108 { 1109 struct ethhdr *ethhdr = eth_hdr(skb); 1110 1111 if (!READ_ONCE(bat_priv->multicast_mode)) 1112 return -EINVAL; 1113 1114 switch (ntohs(ethhdr->h_proto)) { 1115 case ETH_P_IP: 1116 return batadv_mcast_forw_mode_check_ipv4(bat_priv, skb, 1117 is_unsnoopable, 1118 is_routable); 1119 case ETH_P_IPV6: 1120 if (!IS_ENABLED(CONFIG_IPV6)) 1121 return -EINVAL; 1122 1123 return batadv_mcast_forw_mode_check_ipv6(bat_priv, skb, 1124 is_unsnoopable, 1125 is_routable); 1126 default: 1127 return -EINVAL; 1128 } 1129 } 1130 1131 /** 1132 * batadv_mcast_forw_want_all_ip_count() - count nodes with unspecific mcast 1133 * interest 1134 * @bat_priv: the bat priv with all the mesh interface information 1135 * @ethhdr: ethernet header of a packet 1136 * 1137 * Return: the number of nodes which want all IPv4 multicast traffic if the 1138 * given ethhdr is from an IPv4 packet or the number of nodes which want all 1139 * IPv6 traffic if it matches an IPv6 packet. 1140 */ 1141 static int batadv_mcast_forw_want_all_ip_count(struct batadv_priv *bat_priv, 1142 struct ethhdr *ethhdr) 1143 { 1144 switch (ntohs(ethhdr->h_proto)) { 1145 case ETH_P_IP: 1146 return atomic_read(&bat_priv->mcast.num_want_all_ipv4); 1147 case ETH_P_IPV6: 1148 return atomic_read(&bat_priv->mcast.num_want_all_ipv6); 1149 default: 1150 /* we shouldn't be here... */ 1151 return 0; 1152 } 1153 } 1154 1155 /** 1156 * batadv_mcast_forw_rtr_count() - count nodes with a multicast router 1157 * @bat_priv: the bat priv with all the mesh interface information 1158 * @protocol: the ethernet protocol type to count multicast routers for 1159 * 1160 * Return: the number of nodes which want all routable IPv4 multicast traffic 1161 * if the protocol is ETH_P_IP or the number of nodes which want all routable 1162 * IPv6 traffic if the protocol is ETH_P_IPV6. Otherwise returns 0. 1163 */ 1164 1165 static int batadv_mcast_forw_rtr_count(struct batadv_priv *bat_priv, 1166 int protocol) 1167 { 1168 switch (protocol) { 1169 case ETH_P_IP: 1170 return atomic_read(&bat_priv->mcast.num_want_all_rtr4); 1171 case ETH_P_IPV6: 1172 return atomic_read(&bat_priv->mcast.num_want_all_rtr6); 1173 default: 1174 return 0; 1175 } 1176 } 1177 1178 /** 1179 * batadv_mcast_forw_mode_by_count() - get forwarding mode by count 1180 * @bat_priv: the bat priv with all the mesh interface information 1181 * @skb: the multicast packet to check 1182 * @vid: the vlan identifier 1183 * @is_routable: stores whether the destination is routable 1184 * @count: the number of originators the multicast packet need to be sent to 1185 * 1186 * For a multicast packet with multiple destination originators, checks which 1187 * mode to use. For BATADV_FORW_MCAST it also encapsulates the packet with a 1188 * complete batman-adv multicast header. 1189 * 1190 * Return: 1191 * BATADV_FORW_MCAST: If all nodes have multicast packet routing 1192 * capabilities and an MTU >= 1280 on all hard interfaces (including us) 1193 * and the encapsulated multicast packet with all destination addresses 1194 * would still fit into an 1280 bytes batman-adv multicast packet 1195 * (excluding the outer ethernet frame) and we could successfully push 1196 * the full batman-adv multicast packet header. 1197 * BATADV_FORW_UCASTS: If the packet cannot be sent in a batman-adv 1198 * multicast packet and the amount of batman-adv unicast packets needed 1199 * is smaller or equal to the configured multicast fanout. 1200 * BATADV_FORW_BCAST: Otherwise. 1201 */ 1202 static enum batadv_forw_mode 1203 batadv_mcast_forw_mode_by_count(struct batadv_priv *bat_priv, 1204 struct sk_buff *skb, unsigned short vid, 1205 int is_routable, int count) 1206 { 1207 unsigned int mcast_hdrlen = batadv_mcast_forw_packet_hdrlen(count); 1208 u8 own_tvlv_flags = bat_priv->mcast.mla_flags.tvlv_flags; 1209 1210 if (!atomic_read(&bat_priv->mcast.num_no_mc_ptype_capa) && 1211 own_tvlv_flags & BATADV_MCAST_HAVE_MC_PTYPE_CAPA && 1212 skb->len + mcast_hdrlen <= IPV6_MIN_MTU && 1213 batadv_mcast_forw_push(bat_priv, skb, vid, is_routable, count)) 1214 return BATADV_FORW_MCAST; 1215 1216 if (count <= READ_ONCE(bat_priv->multicast_fanout)) 1217 return BATADV_FORW_UCASTS; 1218 1219 return BATADV_FORW_BCAST; 1220 } 1221 1222 /** 1223 * batadv_mcast_forw_mode() - check on how to forward a multicast packet 1224 * @bat_priv: the bat priv with all the mesh interface information 1225 * @skb: the multicast packet to check 1226 * @vid: the vlan identifier 1227 * @is_routable: stores whether the destination is routable 1228 * 1229 * Return: The forwarding mode as enum batadv_forw_mode. 1230 */ 1231 enum batadv_forw_mode 1232 batadv_mcast_forw_mode(struct batadv_priv *bat_priv, struct sk_buff *skb, 1233 unsigned short vid, int *is_routable) 1234 { 1235 bool is_unsnoopable = false; 1236 struct ethhdr *ethhdr; 1237 int unsnoop_count; 1238 int rtr_count = 0; 1239 int total_count; 1240 int tt_count; 1241 int ip_count; 1242 int ret; 1243 1244 ret = batadv_mcast_forw_mode_check(bat_priv, skb, &is_unsnoopable, 1245 is_routable); 1246 if (ret == -ENOMEM) 1247 return BATADV_FORW_NONE; 1248 else if (ret < 0) 1249 return BATADV_FORW_BCAST; 1250 1251 ethhdr = eth_hdr(skb); 1252 1253 tt_count = batadv_tt_global_hash_count(bat_priv, ethhdr->h_dest, 1254 BATADV_NO_FLAGS); 1255 ip_count = batadv_mcast_forw_want_all_ip_count(bat_priv, ethhdr); 1256 unsnoop_count = !is_unsnoopable ? 0 : 1257 atomic_read(&bat_priv->mcast.num_want_all_unsnoopables); 1258 rtr_count = batadv_mcast_forw_rtr_count(bat_priv, *is_routable); 1259 1260 total_count = tt_count + ip_count + unsnoop_count + rtr_count; 1261 1262 if (!total_count) 1263 return BATADV_FORW_NONE; 1264 else if (unsnoop_count) 1265 return BATADV_FORW_BCAST; 1266 1267 return batadv_mcast_forw_mode_by_count(bat_priv, skb, vid, *is_routable, 1268 total_count); 1269 } 1270 1271 /** 1272 * batadv_mcast_forw_send_orig() - send a multicast packet to an originator 1273 * @bat_priv: the bat priv with all the mesh interface information 1274 * @skb: the multicast packet to send 1275 * @vid: the vlan identifier 1276 * @orig_node: the originator to send the packet to 1277 * 1278 * Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise. 1279 */ 1280 static int batadv_mcast_forw_send_orig(struct batadv_priv *bat_priv, 1281 struct sk_buff *skb, 1282 unsigned short vid, 1283 struct batadv_orig_node *orig_node) 1284 { 1285 /* Avoid sending multicast-in-unicast packets to other BLA 1286 * gateways - they already got the frame from the LAN side 1287 * we share with them. 1288 * TODO: Refactor to take BLA into account earlier, to avoid 1289 * reducing the mcast_fanout count. 1290 */ 1291 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid)) { 1292 dev_kfree_skb(skb); 1293 return NET_XMIT_SUCCESS; 1294 } 1295 1296 return batadv_send_skb_unicast(bat_priv, skb, BATADV_UNICAST, 0, 1297 orig_node, vid); 1298 } 1299 1300 /** 1301 * batadv_mcast_forw_tt() - forwards a packet to multicast listeners 1302 * @bat_priv: the bat priv with all the mesh interface information 1303 * @skb: the multicast packet to transmit 1304 * @vid: the vlan identifier 1305 * 1306 * Sends copies of a frame with multicast destination to any multicast 1307 * listener registered in the translation table. A transmission is performed 1308 * via a batman-adv unicast packet for each such destination node. 1309 * 1310 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS 1311 * otherwise. 1312 */ 1313 static int 1314 batadv_mcast_forw_tt(struct batadv_priv *bat_priv, struct sk_buff *skb, 1315 unsigned short vid) 1316 { 1317 struct batadv_tt_orig_list_entry *orig_entry; 1318 struct batadv_tt_global_entry *tt_global; 1319 const u8 *addr = eth_hdr(skb)->h_dest; 1320 int ret = NET_XMIT_SUCCESS; 1321 struct sk_buff *newskb; 1322 1323 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid); 1324 if (!tt_global) 1325 goto out; 1326 1327 rcu_read_lock(); 1328 hlist_for_each_entry_rcu(orig_entry, &tt_global->orig_list, list) { 1329 newskb = skb_copy(skb, GFP_ATOMIC); 1330 if (!newskb) { 1331 ret = NET_XMIT_DROP; 1332 break; 1333 } 1334 1335 batadv_mcast_forw_send_orig(bat_priv, newskb, vid, 1336 orig_entry->orig_node); 1337 } 1338 rcu_read_unlock(); 1339 1340 batadv_tt_global_entry_put(tt_global); 1341 1342 out: 1343 return ret; 1344 } 1345 1346 /** 1347 * batadv_mcast_forw_want_all_ipv4() - forward to nodes with want-all-ipv4 1348 * @bat_priv: the bat priv with all the mesh interface information 1349 * @skb: the multicast packet to transmit 1350 * @vid: the vlan identifier 1351 * 1352 * Sends copies of a frame with multicast destination to any node with a 1353 * BATADV_MCAST_WANT_ALL_IPV4 flag set. A transmission is performed via a 1354 * batman-adv unicast packet for each such destination node. 1355 * 1356 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS 1357 * otherwise. 1358 */ 1359 static int 1360 batadv_mcast_forw_want_all_ipv4(struct batadv_priv *bat_priv, 1361 struct sk_buff *skb, unsigned short vid) 1362 { 1363 struct batadv_orig_node *orig_node; 1364 int ret = NET_XMIT_SUCCESS; 1365 struct sk_buff *newskb; 1366 1367 rcu_read_lock(); 1368 hlist_for_each_entry_rcu(orig_node, 1369 &bat_priv->mcast.want_all_ipv4_list, 1370 mcast_want_all_ipv4_node) { 1371 newskb = skb_copy(skb, GFP_ATOMIC); 1372 if (!newskb) { 1373 ret = NET_XMIT_DROP; 1374 break; 1375 } 1376 1377 batadv_mcast_forw_send_orig(bat_priv, newskb, vid, orig_node); 1378 } 1379 rcu_read_unlock(); 1380 return ret; 1381 } 1382 1383 /** 1384 * batadv_mcast_forw_want_all_ipv6() - forward to nodes with want-all-ipv6 1385 * @bat_priv: the bat priv with all the mesh interface information 1386 * @skb: The multicast packet to transmit 1387 * @vid: the vlan identifier 1388 * 1389 * Sends copies of a frame with multicast destination to any node with a 1390 * BATADV_MCAST_WANT_ALL_IPV6 flag set. A transmission is performed via a 1391 * batman-adv unicast packet for each such destination node. 1392 * 1393 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS 1394 * otherwise. 1395 */ 1396 static int 1397 batadv_mcast_forw_want_all_ipv6(struct batadv_priv *bat_priv, 1398 struct sk_buff *skb, unsigned short vid) 1399 { 1400 struct batadv_orig_node *orig_node; 1401 int ret = NET_XMIT_SUCCESS; 1402 struct sk_buff *newskb; 1403 1404 rcu_read_lock(); 1405 hlist_for_each_entry_rcu(orig_node, 1406 &bat_priv->mcast.want_all_ipv6_list, 1407 mcast_want_all_ipv6_node) { 1408 newskb = skb_copy(skb, GFP_ATOMIC); 1409 if (!newskb) { 1410 ret = NET_XMIT_DROP; 1411 break; 1412 } 1413 1414 batadv_mcast_forw_send_orig(bat_priv, newskb, vid, orig_node); 1415 } 1416 rcu_read_unlock(); 1417 return ret; 1418 } 1419 1420 /** 1421 * batadv_mcast_forw_want_all() - forward packet to nodes in a want-all list 1422 * @bat_priv: the bat priv with all the mesh interface information 1423 * @skb: the multicast packet to transmit 1424 * @vid: the vlan identifier 1425 * 1426 * Sends copies of a frame with multicast destination to any node with a 1427 * BATADV_MCAST_WANT_ALL_IPV4 or BATADV_MCAST_WANT_ALL_IPV6 flag set. A 1428 * transmission is performed via a batman-adv unicast packet for each such 1429 * destination node. 1430 * 1431 * Return: NET_XMIT_DROP on memory allocation failure or if the protocol family 1432 * is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise. 1433 */ 1434 static int 1435 batadv_mcast_forw_want_all(struct batadv_priv *bat_priv, 1436 struct sk_buff *skb, unsigned short vid) 1437 { 1438 switch (ntohs(eth_hdr(skb)->h_proto)) { 1439 case ETH_P_IP: 1440 return batadv_mcast_forw_want_all_ipv4(bat_priv, skb, vid); 1441 case ETH_P_IPV6: 1442 return batadv_mcast_forw_want_all_ipv6(bat_priv, skb, vid); 1443 default: 1444 /* we shouldn't be here... */ 1445 return NET_XMIT_DROP; 1446 } 1447 } 1448 1449 /** 1450 * batadv_mcast_forw_want_all_rtr4() - forward to nodes with want-all-rtr4 1451 * @bat_priv: the bat priv with all the mesh interface information 1452 * @skb: the multicast packet to transmit 1453 * @vid: the vlan identifier 1454 * 1455 * Sends copies of a frame with multicast destination to any node with a 1456 * BATADV_MCAST_WANT_NO_RTR4 flag unset. A transmission is performed via a 1457 * batman-adv unicast packet for each such destination node. 1458 * 1459 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS 1460 * otherwise. 1461 */ 1462 static int 1463 batadv_mcast_forw_want_all_rtr4(struct batadv_priv *bat_priv, 1464 struct sk_buff *skb, unsigned short vid) 1465 { 1466 struct batadv_orig_node *orig_node; 1467 int ret = NET_XMIT_SUCCESS; 1468 struct sk_buff *newskb; 1469 1470 rcu_read_lock(); 1471 hlist_for_each_entry_rcu(orig_node, 1472 &bat_priv->mcast.want_all_rtr4_list, 1473 mcast_want_all_rtr4_node) { 1474 newskb = skb_copy(skb, GFP_ATOMIC); 1475 if (!newskb) { 1476 ret = NET_XMIT_DROP; 1477 break; 1478 } 1479 1480 batadv_mcast_forw_send_orig(bat_priv, newskb, vid, orig_node); 1481 } 1482 rcu_read_unlock(); 1483 return ret; 1484 } 1485 1486 /** 1487 * batadv_mcast_forw_want_all_rtr6() - forward to nodes with want-all-rtr6 1488 * @bat_priv: the bat priv with all the mesh interface information 1489 * @skb: The multicast packet to transmit 1490 * @vid: the vlan identifier 1491 * 1492 * Sends copies of a frame with multicast destination to any node with a 1493 * BATADV_MCAST_WANT_NO_RTR6 flag unset. A transmission is performed via a 1494 * batman-adv unicast packet for each such destination node. 1495 * 1496 * Return: NET_XMIT_DROP on memory allocation failure, NET_XMIT_SUCCESS 1497 * otherwise. 1498 */ 1499 static int 1500 batadv_mcast_forw_want_all_rtr6(struct batadv_priv *bat_priv, 1501 struct sk_buff *skb, unsigned short vid) 1502 { 1503 struct batadv_orig_node *orig_node; 1504 int ret = NET_XMIT_SUCCESS; 1505 struct sk_buff *newskb; 1506 1507 rcu_read_lock(); 1508 hlist_for_each_entry_rcu(orig_node, 1509 &bat_priv->mcast.want_all_rtr6_list, 1510 mcast_want_all_rtr6_node) { 1511 newskb = skb_copy(skb, GFP_ATOMIC); 1512 if (!newskb) { 1513 ret = NET_XMIT_DROP; 1514 break; 1515 } 1516 1517 batadv_mcast_forw_send_orig(bat_priv, newskb, vid, orig_node); 1518 } 1519 rcu_read_unlock(); 1520 return ret; 1521 } 1522 1523 /** 1524 * batadv_mcast_forw_want_rtr() - forward packet to nodes in a want-all-rtr list 1525 * @bat_priv: the bat priv with all the mesh interface information 1526 * @skb: the multicast packet to transmit 1527 * @vid: the vlan identifier 1528 * 1529 * Sends copies of a frame with multicast destination to any node with a 1530 * BATADV_MCAST_WANT_NO_RTR4 or BATADV_MCAST_WANT_NO_RTR6 flag unset. A 1531 * transmission is performed via a batman-adv unicast packet for each such 1532 * destination node. 1533 * 1534 * Return: NET_XMIT_DROP on memory allocation failure or if the protocol family 1535 * is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise. 1536 */ 1537 static int 1538 batadv_mcast_forw_want_rtr(struct batadv_priv *bat_priv, 1539 struct sk_buff *skb, unsigned short vid) 1540 { 1541 switch (ntohs(eth_hdr(skb)->h_proto)) { 1542 case ETH_P_IP: 1543 return batadv_mcast_forw_want_all_rtr4(bat_priv, skb, vid); 1544 case ETH_P_IPV6: 1545 return batadv_mcast_forw_want_all_rtr6(bat_priv, skb, vid); 1546 default: 1547 /* we shouldn't be here... */ 1548 return NET_XMIT_DROP; 1549 } 1550 } 1551 1552 /** 1553 * batadv_mcast_forw_send() - send packet to any detected multicast recipient 1554 * @bat_priv: the bat priv with all the mesh interface information 1555 * @skb: the multicast packet to transmit 1556 * @vid: the vlan identifier 1557 * @is_routable: stores whether the destination is routable 1558 * 1559 * Sends copies of a frame with multicast destination to any node that signaled 1560 * interest in it, that is either via the translation table or the according 1561 * want-all flags. A transmission is performed via a batman-adv unicast packet 1562 * for each such destination node. 1563 * 1564 * The given skb is consumed/freed. 1565 * 1566 * Return: NET_XMIT_DROP on memory allocation failure or if the protocol family 1567 * is neither IPv4 nor IPv6. NET_XMIT_SUCCESS otherwise. 1568 */ 1569 int batadv_mcast_forw_send(struct batadv_priv *bat_priv, struct sk_buff *skb, 1570 unsigned short vid, int is_routable) 1571 { 1572 int ret; 1573 1574 ret = batadv_mcast_forw_tt(bat_priv, skb, vid); 1575 if (ret != NET_XMIT_SUCCESS) { 1576 kfree_skb(skb); 1577 return ret; 1578 } 1579 1580 ret = batadv_mcast_forw_want_all(bat_priv, skb, vid); 1581 if (ret != NET_XMIT_SUCCESS) { 1582 kfree_skb(skb); 1583 return ret; 1584 } 1585 1586 if (!is_routable) 1587 goto skip_mc_router; 1588 1589 ret = batadv_mcast_forw_want_rtr(bat_priv, skb, vid); 1590 if (ret != NET_XMIT_SUCCESS) { 1591 kfree_skb(skb); 1592 return ret; 1593 } 1594 1595 skip_mc_router: 1596 consume_skb(skb); 1597 return ret; 1598 } 1599 1600 /** 1601 * batadv_mcast_want_unsnoop_update() - update unsnoop counter and list 1602 * @bat_priv: the bat priv with all the mesh interface information 1603 * @orig: the orig_node which multicast state might have changed of 1604 * @mcast_flags: flags indicating the new multicast state 1605 * 1606 * If the BATADV_MCAST_WANT_ALL_UNSNOOPABLES flag of this originator, 1607 * orig, has toggled then this method updates the counter and the list 1608 * accordingly. 1609 * 1610 * Caller needs to hold orig->mcast_handler_lock. 1611 */ 1612 static void batadv_mcast_want_unsnoop_update(struct batadv_priv *bat_priv, 1613 struct batadv_orig_node *orig, 1614 u8 mcast_flags) 1615 { 1616 struct hlist_head *head = &bat_priv->mcast.want_all_unsnoopables_list; 1617 struct hlist_node *node = &orig->mcast_want_all_unsnoopables_node; 1618 1619 lockdep_assert_held(&orig->mcast_handler_lock); 1620 1621 /* switched from flag unset to set */ 1622 if (mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES && 1623 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES)) { 1624 atomic_inc(&bat_priv->mcast.num_want_all_unsnoopables); 1625 1626 spin_lock_bh(&bat_priv->mcast.want_lists_lock); 1627 /* flag checks above + mcast_handler_lock prevents this */ 1628 WARN_ON(!hlist_unhashed(node)); 1629 1630 hlist_add_head_rcu(node, head); 1631 spin_unlock_bh(&bat_priv->mcast.want_lists_lock); 1632 /* switched from flag set to unset */ 1633 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) && 1634 orig->mcast_flags & BATADV_MCAST_WANT_ALL_UNSNOOPABLES) { 1635 atomic_dec(&bat_priv->mcast.num_want_all_unsnoopables); 1636 1637 spin_lock_bh(&bat_priv->mcast.want_lists_lock); 1638 /* flag checks above + mcast_handler_lock prevents this */ 1639 WARN_ON(hlist_unhashed(node)); 1640 1641 hlist_del_init_rcu(node); 1642 spin_unlock_bh(&bat_priv->mcast.want_lists_lock); 1643 } 1644 } 1645 1646 /** 1647 * batadv_mcast_want_ipv4_update() - update want-all-ipv4 counter and list 1648 * @bat_priv: the bat priv with all the mesh interface information 1649 * @orig: the orig_node which multicast state might have changed of 1650 * @mcast_flags: flags indicating the new multicast state 1651 * 1652 * If the BATADV_MCAST_WANT_ALL_IPV4 flag of this originator, orig, has 1653 * toggled then this method updates the counter and the list accordingly. 1654 * 1655 * Caller needs to hold orig->mcast_handler_lock. 1656 */ 1657 static void batadv_mcast_want_ipv4_update(struct batadv_priv *bat_priv, 1658 struct batadv_orig_node *orig, 1659 u8 mcast_flags) 1660 { 1661 struct hlist_head *head = &bat_priv->mcast.want_all_ipv4_list; 1662 struct hlist_node *node = &orig->mcast_want_all_ipv4_node; 1663 1664 lockdep_assert_held(&orig->mcast_handler_lock); 1665 1666 /* switched from flag unset to set */ 1667 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4 && 1668 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4)) { 1669 atomic_inc(&bat_priv->mcast.num_want_all_ipv4); 1670 1671 spin_lock_bh(&bat_priv->mcast.want_lists_lock); 1672 /* flag checks above + mcast_handler_lock prevents this */ 1673 WARN_ON(!hlist_unhashed(node)); 1674 1675 hlist_add_head_rcu(node, head); 1676 spin_unlock_bh(&bat_priv->mcast.want_lists_lock); 1677 /* switched from flag set to unset */ 1678 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) && 1679 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) { 1680 atomic_dec(&bat_priv->mcast.num_want_all_ipv4); 1681 1682 spin_lock_bh(&bat_priv->mcast.want_lists_lock); 1683 /* flag checks above + mcast_handler_lock prevents this */ 1684 WARN_ON(hlist_unhashed(node)); 1685 1686 hlist_del_init_rcu(node); 1687 spin_unlock_bh(&bat_priv->mcast.want_lists_lock); 1688 } 1689 } 1690 1691 /** 1692 * batadv_mcast_want_ipv6_update() - update want-all-ipv6 counter and list 1693 * @bat_priv: the bat priv with all the mesh interface information 1694 * @orig: the orig_node which multicast state might have changed of 1695 * @mcast_flags: flags indicating the new multicast state 1696 * 1697 * If the BATADV_MCAST_WANT_ALL_IPV6 flag of this originator, orig, has 1698 * toggled then this method updates the counter and the list accordingly. 1699 * 1700 * Caller needs to hold orig->mcast_handler_lock. 1701 */ 1702 static void batadv_mcast_want_ipv6_update(struct batadv_priv *bat_priv, 1703 struct batadv_orig_node *orig, 1704 u8 mcast_flags) 1705 { 1706 struct hlist_head *head = &bat_priv->mcast.want_all_ipv6_list; 1707 struct hlist_node *node = &orig->mcast_want_all_ipv6_node; 1708 1709 lockdep_assert_held(&orig->mcast_handler_lock); 1710 1711 /* switched from flag unset to set */ 1712 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6 && 1713 !(orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6)) { 1714 atomic_inc(&bat_priv->mcast.num_want_all_ipv6); 1715 1716 spin_lock_bh(&bat_priv->mcast.want_lists_lock); 1717 /* flag checks above + mcast_handler_lock prevents this */ 1718 WARN_ON(!hlist_unhashed(node)); 1719 1720 hlist_add_head_rcu(node, head); 1721 spin_unlock_bh(&bat_priv->mcast.want_lists_lock); 1722 /* switched from flag set to unset */ 1723 } else if (!(mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) && 1724 orig->mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) { 1725 atomic_dec(&bat_priv->mcast.num_want_all_ipv6); 1726 1727 spin_lock_bh(&bat_priv->mcast.want_lists_lock); 1728 /* flag checks above + mcast_handler_lock prevents this */ 1729 WARN_ON(hlist_unhashed(node)); 1730 1731 hlist_del_init_rcu(node); 1732 spin_unlock_bh(&bat_priv->mcast.want_lists_lock); 1733 } 1734 } 1735 1736 /** 1737 * batadv_mcast_want_rtr4_update() - update want-all-rtr4 counter and list 1738 * @bat_priv: the bat priv with all the mesh interface information 1739 * @orig: the orig_node which multicast state might have changed of 1740 * @mcast_flags: flags indicating the new multicast state 1741 * 1742 * If the BATADV_MCAST_WANT_NO_RTR4 flag of this originator, orig, has 1743 * toggled then this method updates the counter and the list accordingly. 1744 * 1745 * Caller needs to hold orig->mcast_handler_lock. 1746 */ 1747 static void batadv_mcast_want_rtr4_update(struct batadv_priv *bat_priv, 1748 struct batadv_orig_node *orig, 1749 u8 mcast_flags) 1750 { 1751 struct hlist_head *head = &bat_priv->mcast.want_all_rtr4_list; 1752 struct hlist_node *node = &orig->mcast_want_all_rtr4_node; 1753 1754 lockdep_assert_held(&orig->mcast_handler_lock); 1755 1756 /* switched from flag set to unset */ 1757 if (!(mcast_flags & BATADV_MCAST_WANT_NO_RTR4) && 1758 orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR4) { 1759 atomic_inc(&bat_priv->mcast.num_want_all_rtr4); 1760 1761 spin_lock_bh(&bat_priv->mcast.want_lists_lock); 1762 /* flag checks above + mcast_handler_lock prevents this */ 1763 WARN_ON(!hlist_unhashed(node)); 1764 1765 hlist_add_head_rcu(node, head); 1766 spin_unlock_bh(&bat_priv->mcast.want_lists_lock); 1767 /* switched from flag unset to set */ 1768 } else if (mcast_flags & BATADV_MCAST_WANT_NO_RTR4 && 1769 !(orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR4)) { 1770 atomic_dec(&bat_priv->mcast.num_want_all_rtr4); 1771 1772 spin_lock_bh(&bat_priv->mcast.want_lists_lock); 1773 /* flag checks above + mcast_handler_lock prevents this */ 1774 WARN_ON(hlist_unhashed(node)); 1775 1776 hlist_del_init_rcu(node); 1777 spin_unlock_bh(&bat_priv->mcast.want_lists_lock); 1778 } 1779 } 1780 1781 /** 1782 * batadv_mcast_want_rtr6_update() - update want-all-rtr6 counter and list 1783 * @bat_priv: the bat priv with all the mesh interface information 1784 * @orig: the orig_node which multicast state might have changed of 1785 * @mcast_flags: flags indicating the new multicast state 1786 * 1787 * If the BATADV_MCAST_WANT_NO_RTR6 flag of this originator, orig, has 1788 * toggled then this method updates the counter and the list accordingly. 1789 * 1790 * Caller needs to hold orig->mcast_handler_lock. 1791 */ 1792 static void batadv_mcast_want_rtr6_update(struct batadv_priv *bat_priv, 1793 struct batadv_orig_node *orig, 1794 u8 mcast_flags) 1795 { 1796 struct hlist_head *head = &bat_priv->mcast.want_all_rtr6_list; 1797 struct hlist_node *node = &orig->mcast_want_all_rtr6_node; 1798 1799 lockdep_assert_held(&orig->mcast_handler_lock); 1800 1801 /* switched from flag set to unset */ 1802 if (!(mcast_flags & BATADV_MCAST_WANT_NO_RTR6) && 1803 orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR6) { 1804 atomic_inc(&bat_priv->mcast.num_want_all_rtr6); 1805 1806 spin_lock_bh(&bat_priv->mcast.want_lists_lock); 1807 /* flag checks above + mcast_handler_lock prevents this */ 1808 WARN_ON(!hlist_unhashed(node)); 1809 1810 hlist_add_head_rcu(node, head); 1811 spin_unlock_bh(&bat_priv->mcast.want_lists_lock); 1812 /* switched from flag unset to set */ 1813 } else if (mcast_flags & BATADV_MCAST_WANT_NO_RTR6 && 1814 !(orig->mcast_flags & BATADV_MCAST_WANT_NO_RTR6)) { 1815 atomic_dec(&bat_priv->mcast.num_want_all_rtr6); 1816 1817 spin_lock_bh(&bat_priv->mcast.want_lists_lock); 1818 /* flag checks above + mcast_handler_lock prevents this */ 1819 WARN_ON(hlist_unhashed(node)); 1820 1821 hlist_del_init_rcu(node); 1822 spin_unlock_bh(&bat_priv->mcast.want_lists_lock); 1823 } 1824 } 1825 1826 /** 1827 * batadv_mcast_have_mc_ptype_update() - update multicast packet type counter 1828 * @bat_priv: the bat priv with all the mesh interface information 1829 * @orig: the orig_node which multicast state might have changed of 1830 * @mcast_flags: flags indicating the new multicast state 1831 * 1832 * If the BATADV_MCAST_HAVE_MC_PTYPE_CAPA flag of this originator, orig, has 1833 * toggled then this method updates the counter accordingly. 1834 */ 1835 static void batadv_mcast_have_mc_ptype_update(struct batadv_priv *bat_priv, 1836 struct batadv_orig_node *orig, 1837 u8 mcast_flags) 1838 { 1839 lockdep_assert_held(&orig->mcast_handler_lock); 1840 1841 /* switched from flag set to unset */ 1842 if (!(mcast_flags & BATADV_MCAST_HAVE_MC_PTYPE_CAPA) && 1843 orig->mcast_flags & BATADV_MCAST_HAVE_MC_PTYPE_CAPA) 1844 atomic_inc(&bat_priv->mcast.num_no_mc_ptype_capa); 1845 /* switched from flag unset to set */ 1846 else if (mcast_flags & BATADV_MCAST_HAVE_MC_PTYPE_CAPA && 1847 !(orig->mcast_flags & BATADV_MCAST_HAVE_MC_PTYPE_CAPA)) 1848 atomic_dec(&bat_priv->mcast.num_no_mc_ptype_capa); 1849 } 1850 1851 /** 1852 * batadv_mcast_tvlv_flags_get() - get multicast flags from an OGM TVLV 1853 * @enabled: whether the originator has multicast TVLV support enabled 1854 * @tvlv_value: tvlv buffer containing the multicast flags 1855 * @tvlv_value_len: tvlv buffer length 1856 * 1857 * Return: multicast flags for the given tvlv buffer 1858 */ 1859 static u8 1860 batadv_mcast_tvlv_flags_get(bool enabled, void *tvlv_value, u16 tvlv_value_len) 1861 { 1862 u8 mcast_flags = BATADV_NO_FLAGS; 1863 1864 if (enabled && tvlv_value && tvlv_value_len >= sizeof(mcast_flags)) 1865 mcast_flags = *(u8 *)tvlv_value; 1866 1867 if (!enabled) { 1868 mcast_flags |= BATADV_MCAST_WANT_ALL_IPV4; 1869 mcast_flags |= BATADV_MCAST_WANT_ALL_IPV6; 1870 } 1871 1872 /* remove redundant flags to avoid sending duplicate packets later */ 1873 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV4) 1874 mcast_flags |= BATADV_MCAST_WANT_NO_RTR4; 1875 1876 if (mcast_flags & BATADV_MCAST_WANT_ALL_IPV6) 1877 mcast_flags |= BATADV_MCAST_WANT_NO_RTR6; 1878 1879 return mcast_flags; 1880 } 1881 1882 /** 1883 * batadv_mcast_tvlv_ogm_handler() - process incoming multicast tvlv container 1884 * @bat_priv: the bat priv with all the mesh interface information 1885 * @orig: the orig_node of the ogm 1886 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags) 1887 * @tvlv_value: tvlv buffer containing the multicast data 1888 * @tvlv_value_len: tvlv buffer length 1889 */ 1890 static void batadv_mcast_tvlv_ogm_handler(struct batadv_priv *bat_priv, 1891 struct batadv_orig_node *orig, 1892 u8 flags, 1893 void *tvlv_value, 1894 u16 tvlv_value_len) 1895 { 1896 bool orig_mcast_enabled = !(flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND); 1897 u8 mcast_flags; 1898 1899 mcast_flags = batadv_mcast_tvlv_flags_get(orig_mcast_enabled, 1900 tvlv_value, tvlv_value_len); 1901 1902 spin_lock_bh(&orig->mcast_handler_lock); 1903 1904 if (orig_mcast_enabled && 1905 !test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) { 1906 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities); 1907 } else if (!orig_mcast_enabled && 1908 test_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities)) { 1909 clear_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capabilities); 1910 } 1911 1912 set_bit(BATADV_ORIG_CAPA_HAS_MCAST, &orig->capa_initialized); 1913 1914 batadv_mcast_want_unsnoop_update(bat_priv, orig, mcast_flags); 1915 batadv_mcast_want_ipv4_update(bat_priv, orig, mcast_flags); 1916 batadv_mcast_want_ipv6_update(bat_priv, orig, mcast_flags); 1917 batadv_mcast_want_rtr4_update(bat_priv, orig, mcast_flags); 1918 batadv_mcast_want_rtr6_update(bat_priv, orig, mcast_flags); 1919 batadv_mcast_have_mc_ptype_update(bat_priv, orig, mcast_flags); 1920 1921 orig->mcast_flags = mcast_flags; 1922 spin_unlock_bh(&orig->mcast_handler_lock); 1923 } 1924 1925 /** 1926 * batadv_mcast_init() - initialize the multicast optimizations structures 1927 * @bat_priv: the bat priv with all the mesh interface information 1928 */ 1929 void batadv_mcast_init(struct batadv_priv *bat_priv) 1930 { 1931 batadv_tvlv_handler_register(bat_priv, batadv_mcast_tvlv_ogm_handler, 1932 NULL, NULL, BATADV_TVLV_MCAST, 2, 1933 BATADV_TVLV_HANDLER_OGM_CIFNOTFND); 1934 batadv_tvlv_handler_register(bat_priv, NULL, NULL, 1935 batadv_mcast_forw_tracker_tvlv_handler, 1936 BATADV_TVLV_MCAST_TRACKER, 1, 1937 BATADV_TVLV_HANDLER_OGM_CIFNOTFND); 1938 1939 INIT_DELAYED_WORK(&bat_priv->mcast.work, batadv_mcast_mla_update); 1940 batadv_mcast_start_timer(bat_priv); 1941 } 1942 1943 /** 1944 * batadv_mcast_mesh_info_put() - put multicast info into a netlink message 1945 * @msg: buffer for the message 1946 * @bat_priv: the bat priv with all the mesh interface information 1947 * 1948 * Return: 0 or error code. 1949 */ 1950 int batadv_mcast_mesh_info_put(struct sk_buff *msg, 1951 struct batadv_priv *bat_priv) 1952 { 1953 u32 flags = bat_priv->mcast.mla_flags.tvlv_flags; 1954 u32 flags_priv = BATADV_NO_FLAGS; 1955 1956 if (bat_priv->mcast.mla_flags.bridged) { 1957 flags_priv |= BATADV_MCAST_FLAGS_BRIDGED; 1958 1959 if (bat_priv->mcast.mla_flags.querier_ipv4.exists) 1960 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_EXISTS; 1961 if (bat_priv->mcast.mla_flags.querier_ipv6.exists) 1962 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_EXISTS; 1963 if (bat_priv->mcast.mla_flags.querier_ipv4.shadowing) 1964 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV4_SHADOWING; 1965 if (bat_priv->mcast.mla_flags.querier_ipv6.shadowing) 1966 flags_priv |= BATADV_MCAST_FLAGS_QUERIER_IPV6_SHADOWING; 1967 } 1968 1969 if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS, flags) || 1970 nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS_PRIV, flags_priv)) 1971 return -EMSGSIZE; 1972 1973 return 0; 1974 } 1975 1976 /** 1977 * batadv_mcast_flags_dump_entry() - dump one entry of the multicast flags table 1978 * to a netlink socket 1979 * @msg: buffer for the message 1980 * @portid: netlink port 1981 * @cb: Control block containing additional options 1982 * @orig_node: originator to dump the multicast flags of 1983 * 1984 * Return: 0 or error code. 1985 */ 1986 static int 1987 batadv_mcast_flags_dump_entry(struct sk_buff *msg, u32 portid, 1988 struct netlink_callback *cb, 1989 struct batadv_orig_node *orig_node) 1990 { 1991 void *hdr; 1992 1993 hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq, 1994 &batadv_netlink_family, NLM_F_MULTI, 1995 BATADV_CMD_GET_MCAST_FLAGS); 1996 if (!hdr) 1997 return -ENOBUFS; 1998 1999 genl_dump_check_consistent(cb, hdr); 2000 2001 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN, 2002 orig_node->orig)) { 2003 genlmsg_cancel(msg, hdr); 2004 return -EMSGSIZE; 2005 } 2006 2007 if (test_bit(BATADV_ORIG_CAPA_HAS_MCAST, 2008 &orig_node->capabilities)) { 2009 if (nla_put_u32(msg, BATADV_ATTR_MCAST_FLAGS, 2010 orig_node->mcast_flags)) { 2011 genlmsg_cancel(msg, hdr); 2012 return -EMSGSIZE; 2013 } 2014 } 2015 2016 genlmsg_end(msg, hdr); 2017 return 0; 2018 } 2019 2020 /** 2021 * batadv_mcast_flags_dump_bucket() - dump one bucket of the multicast flags 2022 * table to a netlink socket 2023 * @msg: buffer for the message 2024 * @portid: netlink port 2025 * @cb: Control block containing additional options 2026 * @hash: hash to dump 2027 * @bucket: bucket index to dump 2028 * @idx_skip: How many entries to skip 2029 * 2030 * Return: 0 or error code. 2031 */ 2032 static int 2033 batadv_mcast_flags_dump_bucket(struct sk_buff *msg, u32 portid, 2034 struct netlink_callback *cb, 2035 struct batadv_hashtable *hash, 2036 unsigned int bucket, long *idx_skip) 2037 { 2038 struct batadv_orig_node *orig_node; 2039 long idx = 0; 2040 2041 spin_lock_bh(&hash->list_locks[bucket]); 2042 cb->seq = atomic_read(&hash->generation) << 1 | 1; 2043 2044 hlist_for_each_entry(orig_node, &hash->table[bucket], hash_entry) { 2045 if (!test_bit(BATADV_ORIG_CAPA_HAS_MCAST, 2046 &orig_node->capa_initialized)) 2047 continue; 2048 2049 if (idx < *idx_skip) 2050 goto skip; 2051 2052 if (batadv_mcast_flags_dump_entry(msg, portid, cb, orig_node)) { 2053 spin_unlock_bh(&hash->list_locks[bucket]); 2054 *idx_skip = idx; 2055 2056 return -EMSGSIZE; 2057 } 2058 2059 skip: 2060 idx++; 2061 } 2062 spin_unlock_bh(&hash->list_locks[bucket]); 2063 2064 return 0; 2065 } 2066 2067 /** 2068 * __batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket 2069 * @msg: buffer for the message 2070 * @portid: netlink port 2071 * @cb: Control block containing additional options 2072 * @bat_priv: the bat priv with all the mesh interface information 2073 * @bucket: current bucket to dump 2074 * @idx: index in current bucket to the next entry to dump 2075 * 2076 * Return: 0 or error code. 2077 */ 2078 static int 2079 __batadv_mcast_flags_dump(struct sk_buff *msg, u32 portid, 2080 struct netlink_callback *cb, 2081 struct batadv_priv *bat_priv, long *bucket, long *idx) 2082 { 2083 struct batadv_hashtable *hash = bat_priv->orig_hash; 2084 long bucket_tmp = *bucket; 2085 long idx_tmp = *idx; 2086 2087 while (bucket_tmp < hash->size) { 2088 if (batadv_mcast_flags_dump_bucket(msg, portid, cb, hash, 2089 bucket_tmp, &idx_tmp)) 2090 break; 2091 2092 bucket_tmp++; 2093 idx_tmp = 0; 2094 } 2095 2096 *bucket = bucket_tmp; 2097 *idx = idx_tmp; 2098 2099 return msg->len; 2100 } 2101 2102 /** 2103 * batadv_mcast_netlink_get_primary() - get primary interface from netlink 2104 * callback 2105 * @cb: netlink callback structure 2106 * @primary_if: the primary interface pointer to return the result in 2107 * 2108 * Return: 0 or error code. 2109 */ 2110 static int 2111 batadv_mcast_netlink_get_primary(struct netlink_callback *cb, 2112 struct batadv_hard_iface **primary_if) 2113 { 2114 struct batadv_hard_iface *hard_iface = NULL; 2115 struct net_device *mesh_iface; 2116 struct batadv_priv *bat_priv; 2117 int ret = 0; 2118 2119 mesh_iface = batadv_netlink_get_meshif(cb); 2120 if (IS_ERR(mesh_iface)) 2121 return PTR_ERR(mesh_iface); 2122 2123 bat_priv = netdev_priv(mesh_iface); 2124 2125 hard_iface = batadv_primary_if_get_selected(bat_priv); 2126 if (!hard_iface || hard_iface->if_status != BATADV_IF_ACTIVE) { 2127 ret = -ENOENT; 2128 goto out; 2129 } 2130 2131 out: 2132 dev_put(mesh_iface); 2133 2134 if (!ret && primary_if) 2135 *primary_if = hard_iface; 2136 else 2137 batadv_hardif_put(hard_iface); 2138 2139 return ret; 2140 } 2141 2142 /** 2143 * batadv_mcast_flags_dump() - dump multicast flags table to a netlink socket 2144 * @msg: buffer for the message 2145 * @cb: callback structure containing arguments 2146 * 2147 * Return: message length. 2148 */ 2149 int batadv_mcast_flags_dump(struct sk_buff *msg, struct netlink_callback *cb) 2150 { 2151 struct batadv_hard_iface *primary_if = NULL; 2152 int portid = NETLINK_CB(cb->skb).portid; 2153 struct batadv_priv *bat_priv; 2154 long *bucket = &cb->args[0]; 2155 long *idx = &cb->args[1]; 2156 int ret; 2157 2158 ret = batadv_mcast_netlink_get_primary(cb, &primary_if); 2159 if (ret) 2160 return ret; 2161 2162 bat_priv = netdev_priv(primary_if->mesh_iface); 2163 ret = __batadv_mcast_flags_dump(msg, portid, cb, bat_priv, bucket, idx); 2164 2165 batadv_hardif_put(primary_if); 2166 return ret; 2167 } 2168 2169 /** 2170 * batadv_mcast_free() - free the multicast optimizations structures 2171 * @bat_priv: the bat priv with all the mesh interface information 2172 */ 2173 void batadv_mcast_free(struct batadv_priv *bat_priv) 2174 { 2175 disable_delayed_work_sync(&bat_priv->mcast.work); 2176 2177 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_MCAST, 2); 2178 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST_TRACKER, 1); 2179 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_MCAST, 2); 2180 2181 /* safely calling outside of worker, as worker was canceled above */ 2182 batadv_mcast_mla_tt_retract(bat_priv, NULL); 2183 } 2184 2185 /** 2186 * batadv_mcast_purge_orig() - reset originator global mcast state modifications 2187 * @orig: the originator which is going to get purged 2188 */ 2189 void batadv_mcast_purge_orig(struct batadv_orig_node *orig) 2190 { 2191 struct batadv_priv *bat_priv = orig->bat_priv; 2192 2193 spin_lock_bh(&orig->mcast_handler_lock); 2194 2195 batadv_mcast_want_unsnoop_update(bat_priv, orig, BATADV_NO_FLAGS); 2196 batadv_mcast_want_ipv4_update(bat_priv, orig, BATADV_NO_FLAGS); 2197 batadv_mcast_want_ipv6_update(bat_priv, orig, BATADV_NO_FLAGS); 2198 batadv_mcast_want_rtr4_update(bat_priv, orig, 2199 BATADV_MCAST_WANT_NO_RTR4); 2200 batadv_mcast_want_rtr6_update(bat_priv, orig, 2201 BATADV_MCAST_WANT_NO_RTR6); 2202 batadv_mcast_have_mc_ptype_update(bat_priv, orig, 2203 BATADV_MCAST_HAVE_MC_PTYPE_CAPA); 2204 2205 spin_unlock_bh(&orig->mcast_handler_lock); 2206 } 2207