Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Bridge multicast support. |
| 4 | * |
| 5 | * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au> |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/err.h> |
Linus Lüssing | 07f8ac4 | 2014-06-07 18:26:28 +0200 | [diff] [blame] | 9 | #include <linux/export.h> |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 10 | #include <linux/if_ether.h> |
| 11 | #include <linux/igmp.h> |
Linus Lüssing | 4b3087c | 2019-01-21 07:26:28 +0100 | [diff] [blame] | 12 | #include <linux/in.h> |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 13 | #include <linux/jhash.h> |
| 14 | #include <linux/kernel.h> |
Herbert Xu | b195167 | 2010-02-27 19:41:51 +0000 | [diff] [blame] | 15 | #include <linux/log2.h> |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 16 | #include <linux/netdevice.h> |
| 17 | #include <linux/netfilter_bridge.h> |
| 18 | #include <linux/random.h> |
| 19 | #include <linux/rculist.h> |
| 20 | #include <linux/skbuff.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/timer.h> |
Cong Wang | 1c8ad5b | 2013-05-21 21:52:54 +0000 | [diff] [blame] | 23 | #include <linux/inetdevice.h> |
Nikolay Aleksandrov | 91b02d3 | 2016-10-31 13:21:05 +0100 | [diff] [blame] | 24 | #include <linux/mroute.h> |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 25 | #include <net/ip.h> |
Nogah Frankel | 147c1e9 | 2017-02-09 14:54:40 +0100 | [diff] [blame] | 26 | #include <net/switchdev.h> |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 27 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Lüssing | 4b3087c | 2019-01-21 07:26:28 +0100 | [diff] [blame] | 28 | #include <linux/icmpv6.h> |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 29 | #include <net/ipv6.h> |
| 30 | #include <net/mld.h> |
David S. Miller | d4c4f07 | 2010-04-27 10:16:54 -0700 | [diff] [blame] | 31 | #include <net/ip6_checksum.h> |
Linus Lüssing | 3c3769e | 2013-09-04 02:13:39 +0200 | [diff] [blame] | 32 | #include <net/addrconf.h> |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 33 | #endif |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 34 | |
| 35 | #include "br_private.h" |
| 36 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 37 | static const struct rhashtable_params br_mdb_rht_params = { |
| 38 | .head_offset = offsetof(struct net_bridge_mdb_entry, rhnode), |
| 39 | .key_offset = offsetof(struct net_bridge_mdb_entry, addr), |
| 40 | .key_len = sizeof(struct br_ip), |
| 41 | .automatic_shrinking = true, |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 42 | }; |
| 43 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 44 | static void br_multicast_start_querier(struct net_bridge *br, |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 45 | struct bridge_mcast_own_query *query); |
Satish Ashok | 754bc54 | 2015-06-19 01:22:57 -0700 | [diff] [blame] | 46 | static void br_multicast_add_router(struct net_bridge *br, |
| 47 | struct net_bridge_port *port); |
Satish Ashok | bc8c20a | 2015-07-13 05:28:37 -0700 | [diff] [blame] | 48 | static void br_ip4_multicast_leave_group(struct net_bridge *br, |
| 49 | struct net_bridge_port *port, |
| 50 | __be32 group, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 51 | __u16 vid, |
| 52 | const unsigned char *src); |
| 53 | |
Nogah Frankel | f12e7d9 | 2017-02-09 14:54:41 +0100 | [diff] [blame] | 54 | static void __del_port_router(struct net_bridge_port *p); |
Satish Ashok | bc8c20a | 2015-07-13 05:28:37 -0700 | [diff] [blame] | 55 | #if IS_ENABLED(CONFIG_IPV6) |
| 56 | static void br_ip6_multicast_leave_group(struct net_bridge *br, |
| 57 | struct net_bridge_port *port, |
| 58 | const struct in6_addr *group, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 59 | __u16 vid, const unsigned char *src); |
Satish Ashok | bc8c20a | 2015-07-13 05:28:37 -0700 | [diff] [blame] | 60 | #endif |
Herbert Xu | c83b8fa | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 61 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 62 | static struct net_bridge_mdb_entry *br_mdb_ip_get_rcu(struct net_bridge *br, |
| 63 | struct br_ip *dst) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 64 | { |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 65 | return rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 68 | struct net_bridge_mdb_entry *br_mdb_ip_get(struct net_bridge *br, |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 69 | struct br_ip *dst) |
Herbert Xu | 7f285fa | 2010-07-05 14:50:08 +0000 | [diff] [blame] | 70 | { |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 71 | struct net_bridge_mdb_entry *ent; |
Herbert Xu | 7f285fa | 2010-07-05 14:50:08 +0000 | [diff] [blame] | 72 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 73 | lockdep_assert_held_once(&br->multicast_lock); |
| 74 | |
| 75 | rcu_read_lock(); |
| 76 | ent = rhashtable_lookup(&br->mdb_hash_tbl, dst, br_mdb_rht_params); |
| 77 | rcu_read_unlock(); |
| 78 | |
| 79 | return ent; |
Herbert Xu | 7f285fa | 2010-07-05 14:50:08 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 82 | static struct net_bridge_mdb_entry *br_mdb_ip4_get(struct net_bridge *br, |
| 83 | __be32 dst, __u16 vid) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 84 | { |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 85 | struct br_ip br_dst; |
Herbert Xu | 0821ec5 | 2010-03-15 20:38:25 -0700 | [diff] [blame] | 86 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 87 | memset(&br_dst, 0, sizeof(br_dst)); |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 88 | br_dst.u.ip4 = dst; |
| 89 | br_dst.proto = htons(ETH_P_IP); |
Vlad Yasevich | b0e9a30 | 2013-02-13 12:00:17 +0000 | [diff] [blame] | 90 | br_dst.vid = vid; |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 91 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 92 | return br_mdb_ip_get(br, &br_dst); |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 93 | } |
| 94 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 95 | #if IS_ENABLED(CONFIG_IPV6) |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 96 | static struct net_bridge_mdb_entry *br_mdb_ip6_get(struct net_bridge *br, |
| 97 | const struct in6_addr *dst, |
| 98 | __u16 vid) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 99 | { |
| 100 | struct br_ip br_dst; |
| 101 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 102 | memset(&br_dst, 0, sizeof(br_dst)); |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 103 | br_dst.u.ip6 = *dst; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 104 | br_dst.proto = htons(ETH_P_IPV6); |
Vlad Yasevich | b0e9a30 | 2013-02-13 12:00:17 +0000 | [diff] [blame] | 105 | br_dst.vid = vid; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 106 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 107 | return br_mdb_ip_get(br, &br_dst); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 108 | } |
| 109 | #endif |
| 110 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 111 | struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br, |
Cong Wang | fbca58a | 2013-03-07 03:05:33 +0000 | [diff] [blame] | 112 | struct sk_buff *skb, u16 vid) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 113 | { |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 114 | struct br_ip ip; |
| 115 | |
Nikolay Aleksandrov | 13cefad | 2018-09-26 17:01:03 +0300 | [diff] [blame] | 116 | if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 117 | return NULL; |
| 118 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 119 | if (BR_INPUT_SKB_CB(skb)->igmp) |
| 120 | return NULL; |
| 121 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 122 | memset(&ip, 0, sizeof(ip)); |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 123 | ip.proto = skb->protocol; |
Cong Wang | fbca58a | 2013-03-07 03:05:33 +0000 | [diff] [blame] | 124 | ip.vid = vid; |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 125 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 126 | switch (skb->protocol) { |
| 127 | case htons(ETH_P_IP): |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 128 | ip.u.ip4 = ip_hdr(skb)->daddr; |
| 129 | break; |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 130 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 131 | case htons(ETH_P_IPV6): |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 132 | ip.u.ip6 = ipv6_hdr(skb)->daddr; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 133 | break; |
| 134 | #endif |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 135 | default: |
| 136 | return NULL; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 139 | return br_mdb_ip_get_rcu(br, &ip); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 142 | static void br_multicast_group_expired(struct timer_list *t) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 143 | { |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 144 | struct net_bridge_mdb_entry *mp = from_timer(mp, t, timer); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 145 | struct net_bridge *br = mp->br; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 146 | |
| 147 | spin_lock(&br->multicast_lock); |
| 148 | if (!netif_running(br->dev) || timer_pending(&mp->timer)) |
| 149 | goto out; |
| 150 | |
Andrew Lunn | ff0fd34 | 2017-11-09 23:10:57 +0100 | [diff] [blame] | 151 | mp->host_joined = false; |
Andrew Lunn | 2a26028 | 2017-11-09 23:10:58 +0100 | [diff] [blame] | 152 | br_mdb_notify(br->dev, NULL, &mp->addr, RTM_DELMDB, 0); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 153 | |
| 154 | if (mp->ports) |
| 155 | goto out; |
| 156 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 157 | rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode, |
| 158 | br_mdb_rht_params); |
| 159 | hlist_del_rcu(&mp->mdb_node); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 160 | |
Nikolay Aleksandrov | 4329596 | 2018-12-05 15:14:25 +0200 | [diff] [blame] | 161 | kfree_rcu(mp, rcu); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 162 | |
| 163 | out: |
| 164 | spin_unlock(&br->multicast_lock); |
| 165 | } |
| 166 | |
| 167 | static void br_multicast_del_pg(struct net_bridge *br, |
| 168 | struct net_bridge_port_group *pg) |
| 169 | { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 170 | struct net_bridge_mdb_entry *mp; |
| 171 | struct net_bridge_port_group *p; |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 172 | struct net_bridge_port_group __rcu **pp; |
| 173 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 174 | mp = br_mdb_ip_get(br, &pg->addr); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 175 | if (WARN_ON(!mp)) |
| 176 | return; |
| 177 | |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 178 | for (pp = &mp->ports; |
| 179 | (p = mlock_dereference(*pp, br)) != NULL; |
| 180 | pp = &p->next) { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 181 | if (p != pg) |
| 182 | continue; |
| 183 | |
stephen hemminger | 83f6a74 | 2010-04-27 15:01:06 +0000 | [diff] [blame] | 184 | rcu_assign_pointer(*pp, p->next); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 185 | hlist_del_init(&p->mglist); |
| 186 | del_timer(&p->timer); |
Elad Raz | 45ebcce | 2016-04-21 12:52:45 +0200 | [diff] [blame] | 187 | br_mdb_notify(br->dev, p->port, &pg->addr, RTM_DELMDB, |
| 188 | p->flags); |
Nikolay Aleksandrov | 4329596 | 2018-12-05 15:14:25 +0200 | [diff] [blame] | 189 | kfree_rcu(p, rcu); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 190 | |
Andrew Lunn | ff0fd34 | 2017-11-09 23:10:57 +0100 | [diff] [blame] | 191 | if (!mp->ports && !mp->host_joined && |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 192 | netif_running(br->dev)) |
| 193 | mod_timer(&mp->timer, jiffies); |
| 194 | |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | WARN_ON(1); |
| 199 | } |
| 200 | |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 201 | static void br_multicast_port_group_expired(struct timer_list *t) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 202 | { |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 203 | struct net_bridge_port_group *pg = from_timer(pg, t, timer); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 204 | struct net_bridge *br = pg->port->br; |
| 205 | |
| 206 | spin_lock(&br->multicast_lock); |
| 207 | if (!netif_running(br->dev) || timer_pending(&pg->timer) || |
Elad Raz | 9d06b6d | 2016-02-03 09:57:05 +0100 | [diff] [blame] | 208 | hlist_unhashed(&pg->mglist) || pg->flags & MDB_PG_FLAGS_PERMANENT) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 209 | goto out; |
| 210 | |
| 211 | br_multicast_del_pg(br, pg); |
| 212 | |
| 213 | out: |
| 214 | spin_unlock(&br->multicast_lock); |
| 215 | } |
| 216 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 217 | static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br, |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 218 | __be32 group, |
| 219 | u8 *igmp_type) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 220 | { |
Nikolay Aleksandrov | 5e92358 | 2016-11-21 13:03:24 +0100 | [diff] [blame] | 221 | struct igmpv3_query *ihv3; |
| 222 | size_t igmp_hdr_size; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 223 | struct sk_buff *skb; |
| 224 | struct igmphdr *ih; |
| 225 | struct ethhdr *eth; |
| 226 | struct iphdr *iph; |
| 227 | |
Nikolay Aleksandrov | 5e92358 | 2016-11-21 13:03:24 +0100 | [diff] [blame] | 228 | igmp_hdr_size = sizeof(*ih); |
| 229 | if (br->multicast_igmp_version == 3) |
| 230 | igmp_hdr_size = sizeof(*ihv3); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 231 | skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) + |
Nikolay Aleksandrov | 5e92358 | 2016-11-21 13:03:24 +0100 | [diff] [blame] | 232 | igmp_hdr_size + 4); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 233 | if (!skb) |
| 234 | goto out; |
| 235 | |
| 236 | skb->protocol = htons(ETH_P_IP); |
| 237 | |
| 238 | skb_reset_mac_header(skb); |
| 239 | eth = eth_hdr(skb); |
| 240 | |
Joe Perches | e5a727f | 2014-02-23 00:05:25 -0800 | [diff] [blame] | 241 | ether_addr_copy(eth->h_source, br->dev->dev_addr); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 242 | eth->h_dest[0] = 1; |
| 243 | eth->h_dest[1] = 0; |
| 244 | eth->h_dest[2] = 0x5e; |
| 245 | eth->h_dest[3] = 0; |
| 246 | eth->h_dest[4] = 0; |
| 247 | eth->h_dest[5] = 1; |
| 248 | eth->h_proto = htons(ETH_P_IP); |
| 249 | skb_put(skb, sizeof(*eth)); |
| 250 | |
| 251 | skb_set_network_header(skb, skb->len); |
| 252 | iph = ip_hdr(skb); |
| 253 | |
| 254 | iph->version = 4; |
| 255 | iph->ihl = 6; |
| 256 | iph->tos = 0xc0; |
Nikolay Aleksandrov | 5e92358 | 2016-11-21 13:03:24 +0100 | [diff] [blame] | 257 | iph->tot_len = htons(sizeof(*iph) + igmp_hdr_size + 4); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 258 | iph->id = 0; |
| 259 | iph->frag_off = htons(IP_DF); |
| 260 | iph->ttl = 1; |
| 261 | iph->protocol = IPPROTO_IGMP; |
Nikolay Aleksandrov | 675779a | 2018-09-26 17:01:04 +0300 | [diff] [blame] | 262 | iph->saddr = br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR) ? |
Cong Wang | 1c8ad5b | 2013-05-21 21:52:54 +0000 | [diff] [blame] | 263 | inet_select_addr(br->dev, 0, RT_SCOPE_LINK) : 0; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 264 | iph->daddr = htonl(INADDR_ALLHOSTS_GROUP); |
| 265 | ((u8 *)&iph[1])[0] = IPOPT_RA; |
| 266 | ((u8 *)&iph[1])[1] = 4; |
| 267 | ((u8 *)&iph[1])[2] = 0; |
| 268 | ((u8 *)&iph[1])[3] = 0; |
| 269 | ip_send_check(iph); |
| 270 | skb_put(skb, 24); |
| 271 | |
| 272 | skb_set_transport_header(skb, skb->len); |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 273 | *igmp_type = IGMP_HOST_MEMBERSHIP_QUERY; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 274 | |
Nikolay Aleksandrov | 5e92358 | 2016-11-21 13:03:24 +0100 | [diff] [blame] | 275 | switch (br->multicast_igmp_version) { |
| 276 | case 2: |
| 277 | ih = igmp_hdr(skb); |
| 278 | ih->type = IGMP_HOST_MEMBERSHIP_QUERY; |
| 279 | ih->code = (group ? br->multicast_last_member_interval : |
| 280 | br->multicast_query_response_interval) / |
| 281 | (HZ / IGMP_TIMER_SCALE); |
| 282 | ih->group = group; |
| 283 | ih->csum = 0; |
| 284 | ih->csum = ip_compute_csum((void *)ih, sizeof(*ih)); |
| 285 | break; |
| 286 | case 3: |
| 287 | ihv3 = igmpv3_query_hdr(skb); |
| 288 | ihv3->type = IGMP_HOST_MEMBERSHIP_QUERY; |
| 289 | ihv3->code = (group ? br->multicast_last_member_interval : |
| 290 | br->multicast_query_response_interval) / |
| 291 | (HZ / IGMP_TIMER_SCALE); |
| 292 | ihv3->group = group; |
| 293 | ihv3->qqic = br->multicast_query_interval / HZ; |
| 294 | ihv3->nsrcs = 0; |
| 295 | ihv3->resv = 0; |
| 296 | ihv3->suppress = 0; |
| 297 | ihv3->qrv = 2; |
| 298 | ihv3->csum = 0; |
| 299 | ihv3->csum = ip_compute_csum((void *)ihv3, sizeof(*ihv3)); |
| 300 | break; |
| 301 | } |
| 302 | |
| 303 | skb_put(skb, igmp_hdr_size); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 304 | __skb_pull(skb, sizeof(*eth)); |
| 305 | |
| 306 | out: |
| 307 | return skb; |
| 308 | } |
| 309 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 310 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 311 | static struct sk_buff *br_ip6_multicast_alloc_query(struct net_bridge *br, |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 312 | const struct in6_addr *grp, |
| 313 | u8 *igmp_type) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 314 | { |
Nikolay Aleksandrov | aa2ae3e | 2016-11-21 13:03:25 +0100 | [diff] [blame] | 315 | struct mld2_query *mld2q; |
| 316 | unsigned long interval; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 317 | struct ipv6hdr *ip6h; |
| 318 | struct mld_msg *mldq; |
Nikolay Aleksandrov | aa2ae3e | 2016-11-21 13:03:25 +0100 | [diff] [blame] | 319 | size_t mld_hdr_size; |
| 320 | struct sk_buff *skb; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 321 | struct ethhdr *eth; |
| 322 | u8 *hopopt; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 323 | |
Nikolay Aleksandrov | aa2ae3e | 2016-11-21 13:03:25 +0100 | [diff] [blame] | 324 | mld_hdr_size = sizeof(*mldq); |
| 325 | if (br->multicast_mld_version == 2) |
| 326 | mld_hdr_size = sizeof(*mld2q); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 327 | skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*ip6h) + |
Nikolay Aleksandrov | aa2ae3e | 2016-11-21 13:03:25 +0100 | [diff] [blame] | 328 | 8 + mld_hdr_size); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 329 | if (!skb) |
| 330 | goto out; |
| 331 | |
| 332 | skb->protocol = htons(ETH_P_IPV6); |
| 333 | |
| 334 | /* Ethernet header */ |
| 335 | skb_reset_mac_header(skb); |
| 336 | eth = eth_hdr(skb); |
| 337 | |
Joe Perches | e5a727f | 2014-02-23 00:05:25 -0800 | [diff] [blame] | 338 | ether_addr_copy(eth->h_source, br->dev->dev_addr); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 339 | eth->h_proto = htons(ETH_P_IPV6); |
| 340 | skb_put(skb, sizeof(*eth)); |
| 341 | |
| 342 | /* IPv6 header + HbH option */ |
| 343 | skb_set_network_header(skb, skb->len); |
| 344 | ip6h = ipv6_hdr(skb); |
| 345 | |
| 346 | *(__force __be32 *)ip6h = htonl(0x60000000); |
Nikolay Aleksandrov | aa2ae3e | 2016-11-21 13:03:25 +0100 | [diff] [blame] | 347 | ip6h->payload_len = htons(8 + mld_hdr_size); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 348 | ip6h->nexthdr = IPPROTO_HOPOPTS; |
| 349 | ip6h->hop_limit = 1; |
Linus Lüssing | a7bff75 | 2011-03-22 11:40:32 +0000 | [diff] [blame] | 350 | ipv6_addr_set(&ip6h->daddr, htonl(0xff020000), 0, 0, htonl(1)); |
Ulrich Weber | d1d81d4 | 2012-03-05 04:52:44 +0000 | [diff] [blame] | 351 | if (ipv6_dev_get_saddr(dev_net(br->dev), br->dev, &ip6h->daddr, 0, |
| 352 | &ip6h->saddr)) { |
| 353 | kfree_skb(skb); |
Nikolay Aleksandrov | 675779a | 2018-09-26 17:01:04 +0300 | [diff] [blame] | 354 | br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, false); |
Ulrich Weber | d1d81d4 | 2012-03-05 04:52:44 +0000 | [diff] [blame] | 355 | return NULL; |
| 356 | } |
daniel | 0888d5f | 2016-06-24 12:35:18 +0200 | [diff] [blame] | 357 | |
Nikolay Aleksandrov | 675779a | 2018-09-26 17:01:04 +0300 | [diff] [blame] | 358 | br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true); |
Linus Lüssing | 36cff5a | 2011-02-17 08:17:51 +0000 | [diff] [blame] | 359 | ipv6_eth_mc_map(&ip6h->daddr, eth->h_dest); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 360 | |
| 361 | hopopt = (u8 *)(ip6h + 1); |
| 362 | hopopt[0] = IPPROTO_ICMPV6; /* next hdr */ |
| 363 | hopopt[1] = 0; /* length of HbH */ |
| 364 | hopopt[2] = IPV6_TLV_ROUTERALERT; /* Router Alert */ |
| 365 | hopopt[3] = 2; /* Length of RA Option */ |
| 366 | hopopt[4] = 0; /* Type = 0x0000 (MLD) */ |
| 367 | hopopt[5] = 0; |
Eldad Zack | 1de5a71 | 2012-05-17 06:00:25 +0000 | [diff] [blame] | 368 | hopopt[6] = IPV6_TLV_PAD1; /* Pad1 */ |
| 369 | hopopt[7] = IPV6_TLV_PAD1; /* Pad1 */ |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 370 | |
| 371 | skb_put(skb, sizeof(*ip6h) + 8); |
| 372 | |
| 373 | /* ICMPv6 */ |
| 374 | skb_set_transport_header(skb, skb->len); |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 375 | interval = ipv6_addr_any(grp) ? |
Linus Lüssing | 32de868 | 2013-06-16 23:20:34 +0200 | [diff] [blame] | 376 | br->multicast_query_response_interval : |
| 377 | br->multicast_last_member_interval; |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 378 | *igmp_type = ICMPV6_MGM_QUERY; |
Nikolay Aleksandrov | aa2ae3e | 2016-11-21 13:03:25 +0100 | [diff] [blame] | 379 | switch (br->multicast_mld_version) { |
| 380 | case 1: |
| 381 | mldq = (struct mld_msg *)icmp6_hdr(skb); |
| 382 | mldq->mld_type = ICMPV6_MGM_QUERY; |
| 383 | mldq->mld_code = 0; |
| 384 | mldq->mld_cksum = 0; |
| 385 | mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval)); |
| 386 | mldq->mld_reserved = 0; |
| 387 | mldq->mld_mca = *grp; |
| 388 | mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, |
| 389 | sizeof(*mldq), IPPROTO_ICMPV6, |
| 390 | csum_partial(mldq, |
| 391 | sizeof(*mldq), |
| 392 | 0)); |
| 393 | break; |
| 394 | case 2: |
| 395 | mld2q = (struct mld2_query *)icmp6_hdr(skb); |
Lance Richardson | 53631a5 | 2017-01-16 18:11:35 -0500 | [diff] [blame] | 396 | mld2q->mld2q_mrc = htons((u16)jiffies_to_msecs(interval)); |
Nikolay Aleksandrov | aa2ae3e | 2016-11-21 13:03:25 +0100 | [diff] [blame] | 397 | mld2q->mld2q_type = ICMPV6_MGM_QUERY; |
| 398 | mld2q->mld2q_code = 0; |
| 399 | mld2q->mld2q_cksum = 0; |
| 400 | mld2q->mld2q_resv1 = 0; |
| 401 | mld2q->mld2q_resv2 = 0; |
| 402 | mld2q->mld2q_suppress = 0; |
| 403 | mld2q->mld2q_qrv = 2; |
| 404 | mld2q->mld2q_nsrcs = 0; |
| 405 | mld2q->mld2q_qqic = br->multicast_query_interval / HZ; |
| 406 | mld2q->mld2q_mca = *grp; |
| 407 | mld2q->mld2q_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, |
| 408 | sizeof(*mld2q), |
| 409 | IPPROTO_ICMPV6, |
| 410 | csum_partial(mld2q, |
| 411 | sizeof(*mld2q), |
| 412 | 0)); |
| 413 | break; |
| 414 | } |
| 415 | skb_put(skb, mld_hdr_size); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 416 | |
| 417 | __skb_pull(skb, sizeof(*eth)); |
| 418 | |
| 419 | out: |
| 420 | return skb; |
| 421 | } |
| 422 | #endif |
| 423 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 424 | static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br, |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 425 | struct br_ip *addr, |
| 426 | u8 *igmp_type) |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 427 | { |
| 428 | switch (addr->proto) { |
| 429 | case htons(ETH_P_IP): |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 430 | return br_ip4_multicast_alloc_query(br, addr->u.ip4, igmp_type); |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 431 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 432 | case htons(ETH_P_IPV6): |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 433 | return br_ip6_multicast_alloc_query(br, &addr->u.ip6, |
| 434 | igmp_type); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 435 | #endif |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 436 | } |
| 437 | return NULL; |
| 438 | } |
| 439 | |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 440 | struct net_bridge_mdb_entry *br_multicast_new_group(struct net_bridge *br, |
Nikolay Aleksandrov | 5e92358 | 2016-11-21 13:03:24 +0100 | [diff] [blame] | 441 | struct br_ip *group) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 442 | { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 443 | struct net_bridge_mdb_entry *mp; |
Tobias Klauser | 4c0833b | 2010-12-10 03:18:04 +0000 | [diff] [blame] | 444 | int err; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 445 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 446 | mp = br_mdb_ip_get(br, group); |
| 447 | if (mp) |
| 448 | return mp; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 449 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 450 | if (atomic_read(&br->mdb_hash_tbl.nelems) >= br->hash_max) { |
| 451 | br_opt_toggle(br, BROPT_MULTICAST_ENABLED, false); |
| 452 | return ERR_PTR(-E2BIG); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | mp = kzalloc(sizeof(*mp), GFP_ATOMIC); |
| 456 | if (unlikely(!mp)) |
Tobias Klauser | 4c0833b | 2010-12-10 03:18:04 +0000 | [diff] [blame] | 457 | return ERR_PTR(-ENOMEM); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 458 | |
| 459 | mp->br = br; |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 460 | mp->addr = *group; |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 461 | timer_setup(&mp->timer, br_multicast_group_expired, 0); |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 462 | err = rhashtable_lookup_insert_fast(&br->mdb_hash_tbl, &mp->rhnode, |
| 463 | br_mdb_rht_params); |
| 464 | if (err) { |
| 465 | kfree(mp); |
| 466 | mp = ERR_PTR(err); |
| 467 | } else { |
| 468 | hlist_add_head_rcu(&mp->mdb_node, &br->mdb_list); |
| 469 | } |
Eric Dumazet | 1faabf2 | 2013-07-19 20:07:16 -0700 | [diff] [blame] | 470 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 471 | return mp; |
| 472 | } |
| 473 | |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 474 | struct net_bridge_port_group *br_multicast_new_port_group( |
| 475 | struct net_bridge_port *port, |
| 476 | struct br_ip *group, |
Amerigo Wang | ccb1c31 | 2012-12-14 22:09:51 +0000 | [diff] [blame] | 477 | struct net_bridge_port_group __rcu *next, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 478 | unsigned char flags, |
| 479 | const unsigned char *src) |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 480 | { |
| 481 | struct net_bridge_port_group *p; |
| 482 | |
| 483 | p = kzalloc(sizeof(*p), GFP_ATOMIC); |
| 484 | if (unlikely(!p)) |
| 485 | return NULL; |
| 486 | |
| 487 | p->addr = *group; |
| 488 | p->port = port; |
Elad Raz | 9d06b6d | 2016-02-03 09:57:05 +0100 | [diff] [blame] | 489 | p->flags = flags; |
stephen hemminger | eca2a43 | 2012-12-13 06:51:28 +0000 | [diff] [blame] | 490 | rcu_assign_pointer(p->next, next); |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 491 | hlist_add_head(&p->mglist, &port->mglist); |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 492 | timer_setup(&p->timer, br_multicast_port_group_expired, 0); |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 493 | |
| 494 | if (src) |
| 495 | memcpy(p->eth_addr, src, ETH_ALEN); |
| 496 | else |
Mao Wenan | 1bfe45f | 2019-03-20 10:06:57 +0800 | [diff] [blame] | 497 | eth_broadcast_addr(p->eth_addr); |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 498 | |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 499 | return p; |
| 500 | } |
| 501 | |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 502 | static bool br_port_group_equal(struct net_bridge_port_group *p, |
| 503 | struct net_bridge_port *port, |
| 504 | const unsigned char *src) |
| 505 | { |
| 506 | if (p->port != port) |
| 507 | return false; |
| 508 | |
| 509 | if (!(port->flags & BR_MULTICAST_TO_UNICAST)) |
| 510 | return true; |
| 511 | |
| 512 | return ether_addr_equal(src, p->eth_addr); |
| 513 | } |
| 514 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 515 | static int br_multicast_add_group(struct net_bridge *br, |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 516 | struct net_bridge_port *port, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 517 | struct br_ip *group, |
| 518 | const unsigned char *src) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 519 | { |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 520 | struct net_bridge_port_group __rcu **pp; |
Nikolay Aleksandrov | 5e92358 | 2016-11-21 13:03:24 +0100 | [diff] [blame] | 521 | struct net_bridge_port_group *p; |
| 522 | struct net_bridge_mdb_entry *mp; |
Linus Lüssing | 454594f | 2013-10-20 00:58:57 +0200 | [diff] [blame] | 523 | unsigned long now = jiffies; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 524 | int err; |
| 525 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 526 | spin_lock(&br->multicast_lock); |
| 527 | if (!netif_running(br->dev) || |
| 528 | (port && port->state == BR_STATE_DISABLED)) |
| 529 | goto out; |
| 530 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 531 | mp = br_multicast_new_group(br, group); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 532 | err = PTR_ERR(mp); |
Tobias Klauser | 4c0833b | 2010-12-10 03:18:04 +0000 | [diff] [blame] | 533 | if (IS_ERR(mp)) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 534 | goto err; |
| 535 | |
| 536 | if (!port) { |
Andrew Lunn | 2a26028 | 2017-11-09 23:10:58 +0100 | [diff] [blame] | 537 | if (!mp->host_joined) { |
| 538 | mp->host_joined = true; |
| 539 | br_mdb_notify(br->dev, NULL, &mp->addr, RTM_NEWMDB, 0); |
| 540 | } |
Linus Lüssing | 454594f | 2013-10-20 00:58:57 +0200 | [diff] [blame] | 541 | mod_timer(&mp->timer, now + br->multicast_membership_interval); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 542 | goto out; |
| 543 | } |
| 544 | |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 545 | for (pp = &mp->ports; |
| 546 | (p = mlock_dereference(*pp, br)) != NULL; |
| 547 | pp = &p->next) { |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 548 | if (br_port_group_equal(p, port, src)) |
Linus Lüssing | 454594f | 2013-10-20 00:58:57 +0200 | [diff] [blame] | 549 | goto found; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 550 | if ((unsigned long)p->port < (unsigned long)port) |
| 551 | break; |
| 552 | } |
| 553 | |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 554 | p = br_multicast_new_port_group(port, group, *pp, 0, src); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 555 | if (unlikely(!p)) |
| 556 | goto err; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 557 | rcu_assign_pointer(*pp, p); |
Elad Raz | 45ebcce | 2016-04-21 12:52:45 +0200 | [diff] [blame] | 558 | br_mdb_notify(br->dev, port, group, RTM_NEWMDB, 0); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 559 | |
Linus Lüssing | 454594f | 2013-10-20 00:58:57 +0200 | [diff] [blame] | 560 | found: |
| 561 | mod_timer(&p->timer, now + br->multicast_membership_interval); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 562 | out: |
| 563 | err = 0; |
| 564 | |
| 565 | err: |
| 566 | spin_unlock(&br->multicast_lock); |
| 567 | return err; |
| 568 | } |
| 569 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 570 | static int br_ip4_multicast_add_group(struct net_bridge *br, |
| 571 | struct net_bridge_port *port, |
Vlad Yasevich | b0e9a30 | 2013-02-13 12:00:17 +0000 | [diff] [blame] | 572 | __be32 group, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 573 | __u16 vid, |
| 574 | const unsigned char *src) |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 575 | { |
| 576 | struct br_ip br_group; |
| 577 | |
| 578 | if (ipv4_is_local_multicast(group)) |
| 579 | return 0; |
| 580 | |
Nikolay Aleksandrov | 1515a63f | 2019-04-03 23:27:24 +0300 | [diff] [blame] | 581 | memset(&br_group, 0, sizeof(br_group)); |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 582 | br_group.u.ip4 = group; |
| 583 | br_group.proto = htons(ETH_P_IP); |
Vlad Yasevich | b0e9a30 | 2013-02-13 12:00:17 +0000 | [diff] [blame] | 584 | br_group.vid = vid; |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 585 | |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 586 | return br_multicast_add_group(br, port, &br_group, src); |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 587 | } |
| 588 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 589 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 590 | static int br_ip6_multicast_add_group(struct net_bridge *br, |
| 591 | struct net_bridge_port *port, |
Vlad Yasevich | b0e9a30 | 2013-02-13 12:00:17 +0000 | [diff] [blame] | 592 | const struct in6_addr *group, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 593 | __u16 vid, |
| 594 | const unsigned char *src) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 595 | { |
| 596 | struct br_ip br_group; |
| 597 | |
Linus Lüssing | 3c3769e | 2013-09-04 02:13:39 +0200 | [diff] [blame] | 598 | if (ipv6_addr_is_ll_all_nodes(group)) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 599 | return 0; |
| 600 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 601 | memset(&br_group, 0, sizeof(br_group)); |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 602 | br_group.u.ip6 = *group; |
Linus Lüssing | 9cc6e0c | 2011-02-15 13:19:17 +0000 | [diff] [blame] | 603 | br_group.proto = htons(ETH_P_IPV6); |
Vlad Yasevich | b0e9a30 | 2013-02-13 12:00:17 +0000 | [diff] [blame] | 604 | br_group.vid = vid; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 605 | |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 606 | return br_multicast_add_group(br, port, &br_group, src); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 607 | } |
| 608 | #endif |
| 609 | |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 610 | static void br_multicast_router_expired(struct timer_list *t) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 611 | { |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 612 | struct net_bridge_port *port = |
| 613 | from_timer(port, t, multicast_router_timer); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 614 | struct net_bridge *br = port->br; |
| 615 | |
| 616 | spin_lock(&br->multicast_lock); |
Nikolay Aleksandrov | a55d824 | 2016-02-26 21:20:03 +0100 | [diff] [blame] | 617 | if (port->multicast_router == MDB_RTR_TYPE_DISABLED || |
| 618 | port->multicast_router == MDB_RTR_TYPE_PERM || |
Nogah Frankel | f12e7d9 | 2017-02-09 14:54:41 +0100 | [diff] [blame] | 619 | timer_pending(&port->multicast_router_timer)) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 620 | goto out; |
| 621 | |
Nogah Frankel | f12e7d9 | 2017-02-09 14:54:41 +0100 | [diff] [blame] | 622 | __del_port_router(port); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 623 | out: |
| 624 | spin_unlock(&br->multicast_lock); |
| 625 | } |
| 626 | |
Yotam Gigi | 7704142 | 2017-10-09 11:15:31 +0200 | [diff] [blame] | 627 | static void br_mc_router_state_change(struct net_bridge *p, |
| 628 | bool is_mc_router) |
| 629 | { |
| 630 | struct switchdev_attr attr = { |
| 631 | .orig_dev = p->dev, |
| 632 | .id = SWITCHDEV_ATTR_ID_BRIDGE_MROUTER, |
| 633 | .flags = SWITCHDEV_F_DEFER, |
| 634 | .u.mrouter = is_mc_router, |
| 635 | }; |
| 636 | |
| 637 | switchdev_port_attr_set(p->dev, &attr); |
| 638 | } |
| 639 | |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 640 | static void br_multicast_local_router_expired(struct timer_list *t) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 641 | { |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 642 | struct net_bridge *br = from_timer(br, t, multicast_router_timer); |
Yotam Gigi | 7704142 | 2017-10-09 11:15:31 +0200 | [diff] [blame] | 643 | |
| 644 | spin_lock(&br->multicast_lock); |
| 645 | if (br->multicast_router == MDB_RTR_TYPE_DISABLED || |
| 646 | br->multicast_router == MDB_RTR_TYPE_PERM || |
| 647 | timer_pending(&br->multicast_router_timer)) |
| 648 | goto out; |
| 649 | |
| 650 | br_mc_router_state_change(br, false); |
| 651 | out: |
| 652 | spin_unlock(&br->multicast_lock); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 655 | static void br_multicast_querier_expired(struct net_bridge *br, |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 656 | struct bridge_mcast_own_query *query) |
Herbert Xu | c83b8fa | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 657 | { |
Herbert Xu | c83b8fa | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 658 | spin_lock(&br->multicast_lock); |
Nikolay Aleksandrov | 13cefad | 2018-09-26 17:01:03 +0300 | [diff] [blame] | 659 | if (!netif_running(br->dev) || !br_opt_get(br, BROPT_MULTICAST_ENABLED)) |
Herbert Xu | c83b8fa | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 660 | goto out; |
| 661 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 662 | br_multicast_start_querier(br, query); |
Herbert Xu | c83b8fa | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 663 | |
| 664 | out: |
| 665 | spin_unlock(&br->multicast_lock); |
| 666 | } |
| 667 | |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 668 | static void br_ip4_multicast_querier_expired(struct timer_list *t) |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 669 | { |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 670 | struct net_bridge *br = from_timer(br, t, ip4_other_query.timer); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 671 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 672 | br_multicast_querier_expired(br, &br->ip4_own_query); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | #if IS_ENABLED(CONFIG_IPV6) |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 676 | static void br_ip6_multicast_querier_expired(struct timer_list *t) |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 677 | { |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 678 | struct net_bridge *br = from_timer(br, t, ip6_other_query.timer); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 679 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 680 | br_multicast_querier_expired(br, &br->ip6_own_query); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 681 | } |
| 682 | #endif |
| 683 | |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 684 | static void br_multicast_select_own_querier(struct net_bridge *br, |
| 685 | struct br_ip *ip, |
| 686 | struct sk_buff *skb) |
| 687 | { |
| 688 | if (ip->proto == htons(ETH_P_IP)) |
| 689 | br->ip4_querier.addr.u.ip4 = ip_hdr(skb)->saddr; |
| 690 | #if IS_ENABLED(CONFIG_IPV6) |
| 691 | else |
| 692 | br->ip6_querier.addr.u.ip6 = ipv6_hdr(skb)->saddr; |
| 693 | #endif |
| 694 | } |
| 695 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 696 | static void __br_multicast_send_query(struct net_bridge *br, |
| 697 | struct net_bridge_port *port, |
| 698 | struct br_ip *ip) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 699 | { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 700 | struct sk_buff *skb; |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 701 | u8 igmp_type; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 702 | |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 703 | skb = br_multicast_alloc_query(br, ip, &igmp_type); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 704 | if (!skb) |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 705 | return; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 706 | |
| 707 | if (port) { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 708 | skb->dev = port->dev; |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 709 | br_multicast_count(br, port, skb, igmp_type, |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 710 | BR_MCAST_DIR_TX); |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 711 | NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, |
| 712 | dev_net(port->dev), NULL, skb, NULL, skb->dev, |
Linus Lüssing | f0b4eec | 2014-11-17 12:20:28 +0100 | [diff] [blame] | 713 | br_dev_queue_push_xmit); |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 714 | } else { |
| 715 | br_multicast_select_own_querier(br, ip, skb); |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 716 | br_multicast_count(br, port, skb, igmp_type, |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 717 | BR_MCAST_DIR_RX); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 718 | netif_rx(skb); |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 719 | } |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 720 | } |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 721 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 722 | static void br_multicast_send_query(struct net_bridge *br, |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 723 | struct net_bridge_port *port, |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 724 | struct bridge_mcast_own_query *own_query) |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 725 | { |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 726 | struct bridge_mcast_other_query *other_query = NULL; |
Nikolay Aleksandrov | 5e92358 | 2016-11-21 13:03:24 +0100 | [diff] [blame] | 727 | struct br_ip br_group; |
| 728 | unsigned long time; |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 729 | |
Nikolay Aleksandrov | 13cefad | 2018-09-26 17:01:03 +0300 | [diff] [blame] | 730 | if (!netif_running(br->dev) || |
| 731 | !br_opt_get(br, BROPT_MULTICAST_ENABLED) || |
Nikolay Aleksandrov | 675779a | 2018-09-26 17:01:04 +0300 | [diff] [blame] | 732 | !br_opt_get(br, BROPT_MULTICAST_QUERIER)) |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 733 | return; |
| 734 | |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 735 | memset(&br_group.u, 0, sizeof(br_group.u)); |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 736 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 737 | if (port ? (own_query == &port->ip4_own_query) : |
| 738 | (own_query == &br->ip4_own_query)) { |
| 739 | other_query = &br->ip4_other_query; |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 740 | br_group.proto = htons(ETH_P_IP); |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 741 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 742 | } else { |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 743 | other_query = &br->ip6_other_query; |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 744 | br_group.proto = htons(ETH_P_IPV6); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 745 | #endif |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 746 | } |
| 747 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 748 | if (!other_query || timer_pending(&other_query->timer)) |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 749 | return; |
| 750 | |
| 751 | __br_multicast_send_query(br, port, &br_group); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 752 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 753 | time = jiffies; |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 754 | time += own_query->startup_sent < br->multicast_startup_query_count ? |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 755 | br->multicast_startup_query_interval : |
| 756 | br->multicast_query_interval; |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 757 | mod_timer(&own_query->timer, time); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 758 | } |
| 759 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 760 | static void |
| 761 | br_multicast_port_query_expired(struct net_bridge_port *port, |
| 762 | struct bridge_mcast_own_query *query) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 763 | { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 764 | struct net_bridge *br = port->br; |
| 765 | |
| 766 | spin_lock(&br->multicast_lock); |
Dan Carpenter | 02a780c | 2010-03-06 01:14:09 +0000 | [diff] [blame] | 767 | if (port->state == BR_STATE_DISABLED || |
| 768 | port->state == BR_STATE_BLOCKING) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 769 | goto out; |
| 770 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 771 | if (query->startup_sent < br->multicast_startup_query_count) |
| 772 | query->startup_sent++; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 773 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 774 | br_multicast_send_query(port->br, port, query); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 775 | |
| 776 | out: |
| 777 | spin_unlock(&br->multicast_lock); |
| 778 | } |
| 779 | |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 780 | static void br_ip4_multicast_port_query_expired(struct timer_list *t) |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 781 | { |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 782 | struct net_bridge_port *port = from_timer(port, t, ip4_own_query.timer); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 783 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 784 | br_multicast_port_query_expired(port, &port->ip4_own_query); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | #if IS_ENABLED(CONFIG_IPV6) |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 788 | static void br_ip6_multicast_port_query_expired(struct timer_list *t) |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 789 | { |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 790 | struct net_bridge_port *port = from_timer(port, t, ip6_own_query.timer); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 791 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 792 | br_multicast_port_query_expired(port, &port->ip6_own_query); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 793 | } |
| 794 | #endif |
| 795 | |
Nogah Frankel | 147c1e9 | 2017-02-09 14:54:40 +0100 | [diff] [blame] | 796 | static void br_mc_disabled_update(struct net_device *dev, bool value) |
| 797 | { |
| 798 | struct switchdev_attr attr = { |
| 799 | .orig_dev = dev, |
| 800 | .id = SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED, |
| 801 | .flags = SWITCHDEV_F_DEFER, |
Nikolay Aleksandrov | 13cefad | 2018-09-26 17:01:03 +0300 | [diff] [blame] | 802 | .u.mc_disabled = !value, |
Nogah Frankel | 147c1e9 | 2017-02-09 14:54:40 +0100 | [diff] [blame] | 803 | }; |
| 804 | |
| 805 | switchdev_port_attr_set(dev, &attr); |
| 806 | } |
| 807 | |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 808 | int br_multicast_add_port(struct net_bridge_port *port) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 809 | { |
Nikolay Aleksandrov | 7f0aec7 | 2016-02-26 21:20:01 +0100 | [diff] [blame] | 810 | port->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 811 | |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 812 | timer_setup(&port->multicast_router_timer, |
| 813 | br_multicast_router_expired, 0); |
| 814 | timer_setup(&port->ip4_own_query.timer, |
| 815 | br_ip4_multicast_port_query_expired, 0); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 816 | #if IS_ENABLED(CONFIG_IPV6) |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 817 | timer_setup(&port->ip6_own_query.timer, |
| 818 | br_ip6_multicast_port_query_expired, 0); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 819 | #endif |
Nikolay Aleksandrov | 13cefad | 2018-09-26 17:01:03 +0300 | [diff] [blame] | 820 | br_mc_disabled_update(port->dev, |
| 821 | br_opt_get(port->br, BROPT_MULTICAST_ENABLED)); |
Nogah Frankel | 147c1e9 | 2017-02-09 14:54:40 +0100 | [diff] [blame] | 822 | |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 823 | port->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats); |
| 824 | if (!port->mcast_stats) |
| 825 | return -ENOMEM; |
| 826 | |
| 827 | return 0; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | void br_multicast_del_port(struct net_bridge_port *port) |
| 831 | { |
Satish Ashok | e10177a | 2015-07-15 07:16:51 -0700 | [diff] [blame] | 832 | struct net_bridge *br = port->br; |
| 833 | struct net_bridge_port_group *pg; |
| 834 | struct hlist_node *n; |
| 835 | |
| 836 | /* Take care of the remaining groups, only perm ones should be left */ |
| 837 | spin_lock_bh(&br->multicast_lock); |
| 838 | hlist_for_each_entry_safe(pg, n, &port->mglist, mglist) |
| 839 | br_multicast_del_pg(br, pg); |
| 840 | spin_unlock_bh(&br->multicast_lock); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 841 | del_timer_sync(&port->multicast_router_timer); |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 842 | free_percpu(port->mcast_stats); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 845 | static void br_multicast_enable(struct bridge_mcast_own_query *query) |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 846 | { |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 847 | query->startup_sent = 0; |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 848 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 849 | if (try_to_del_timer_sync(&query->timer) >= 0 || |
| 850 | del_timer(&query->timer)) |
| 851 | mod_timer(&query->timer, jiffies); |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 852 | } |
| 853 | |
Nikolay Aleksandrov | 7cb3f92 | 2016-10-18 18:09:48 +0200 | [diff] [blame] | 854 | static void __br_multicast_enable_port(struct net_bridge_port *port) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 855 | { |
| 856 | struct net_bridge *br = port->br; |
| 857 | |
Nikolay Aleksandrov | 13cefad | 2018-09-26 17:01:03 +0300 | [diff] [blame] | 858 | if (!br_opt_get(br, BROPT_MULTICAST_ENABLED) || !netif_running(br->dev)) |
Nikolay Aleksandrov | 7cb3f92 | 2016-10-18 18:09:48 +0200 | [diff] [blame] | 859 | return; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 860 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 861 | br_multicast_enable(&port->ip4_own_query); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 862 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 863 | br_multicast_enable(&port->ip6_own_query); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 864 | #endif |
Nikolay Aleksandrov | 7f0aec7 | 2016-02-26 21:20:01 +0100 | [diff] [blame] | 865 | if (port->multicast_router == MDB_RTR_TYPE_PERM && |
| 866 | hlist_unhashed(&port->rlist)) |
Satish Ashok | 754bc54 | 2015-06-19 01:22:57 -0700 | [diff] [blame] | 867 | br_multicast_add_router(br, port); |
Nikolay Aleksandrov | 7cb3f92 | 2016-10-18 18:09:48 +0200 | [diff] [blame] | 868 | } |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 869 | |
Nikolay Aleksandrov | 7cb3f92 | 2016-10-18 18:09:48 +0200 | [diff] [blame] | 870 | void br_multicast_enable_port(struct net_bridge_port *port) |
| 871 | { |
| 872 | struct net_bridge *br = port->br; |
| 873 | |
| 874 | spin_lock(&br->multicast_lock); |
| 875 | __br_multicast_enable_port(port); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 876 | spin_unlock(&br->multicast_lock); |
| 877 | } |
| 878 | |
| 879 | void br_multicast_disable_port(struct net_bridge_port *port) |
| 880 | { |
| 881 | struct net_bridge *br = port->br; |
| 882 | struct net_bridge_port_group *pg; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 883 | struct hlist_node *n; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 884 | |
| 885 | spin_lock(&br->multicast_lock); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 886 | hlist_for_each_entry_safe(pg, n, &port->mglist, mglist) |
Elad Raz | 9d06b6d | 2016-02-03 09:57:05 +0100 | [diff] [blame] | 887 | if (!(pg->flags & MDB_PG_FLAGS_PERMANENT)) |
Satish Ashok | e10177a | 2015-07-15 07:16:51 -0700 | [diff] [blame] | 888 | br_multicast_del_pg(br, pg); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 889 | |
Nogah Frankel | f12e7d9 | 2017-02-09 14:54:41 +0100 | [diff] [blame] | 890 | __del_port_router(port); |
| 891 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 892 | del_timer(&port->multicast_router_timer); |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 893 | del_timer(&port->ip4_own_query.timer); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 894 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 895 | del_timer(&port->ip6_own_query.timer); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 896 | #endif |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 897 | spin_unlock(&br->multicast_lock); |
| 898 | } |
| 899 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 900 | static int br_ip4_multicast_igmp3_report(struct net_bridge *br, |
| 901 | struct net_bridge_port *port, |
Vlad Yasevich | 0649909 | 2013-10-28 15:45:07 -0400 | [diff] [blame] | 902 | struct sk_buff *skb, |
| 903 | u16 vid) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 904 | { |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 905 | const unsigned char *src; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 906 | struct igmpv3_report *ih; |
| 907 | struct igmpv3_grec *grec; |
| 908 | int i; |
| 909 | int len; |
| 910 | int num; |
| 911 | int type; |
| 912 | int err = 0; |
| 913 | __be32 group; |
Nikolay Aleksandrov | e57f618 | 2019-07-02 15:00:18 +0300 | [diff] [blame] | 914 | u16 nsrcs; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 915 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 916 | ih = igmpv3_report_hdr(skb); |
| 917 | num = ntohs(ih->ngrec); |
Linus Lüssing | c2d4fbd | 2015-09-11 18:39:48 +0200 | [diff] [blame] | 918 | len = skb_transport_offset(skb) + sizeof(*ih); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 919 | |
| 920 | for (i = 0; i < num; i++) { |
| 921 | len += sizeof(*grec); |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 922 | if (!ip_mc_may_pull(skb, len)) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 923 | return -EINVAL; |
| 924 | |
Herbert Xu | fd218cf | 2010-04-07 21:20:47 -0700 | [diff] [blame] | 925 | grec = (void *)(skb->data + len - sizeof(*grec)); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 926 | group = grec->grec_mca; |
| 927 | type = grec->grec_type; |
Nikolay Aleksandrov | e57f618 | 2019-07-02 15:00:18 +0300 | [diff] [blame] | 928 | nsrcs = ntohs(grec->grec_nsrcs); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 929 | |
Nikolay Aleksandrov | e57f618 | 2019-07-02 15:00:18 +0300 | [diff] [blame] | 930 | len += nsrcs * 4; |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 931 | if (!ip_mc_may_pull(skb, len)) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 932 | return -EINVAL; |
| 933 | |
| 934 | /* We treat this as an IGMPv2 report for now. */ |
| 935 | switch (type) { |
| 936 | case IGMPV3_MODE_IS_INCLUDE: |
| 937 | case IGMPV3_MODE_IS_EXCLUDE: |
| 938 | case IGMPV3_CHANGE_TO_INCLUDE: |
| 939 | case IGMPV3_CHANGE_TO_EXCLUDE: |
| 940 | case IGMPV3_ALLOW_NEW_SOURCES: |
| 941 | case IGMPV3_BLOCK_OLD_SOURCES: |
| 942 | break; |
| 943 | |
| 944 | default: |
| 945 | continue; |
| 946 | } |
| 947 | |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 948 | src = eth_hdr(skb)->h_source; |
Satish Ashok | bc8c20a | 2015-07-13 05:28:37 -0700 | [diff] [blame] | 949 | if ((type == IGMPV3_CHANGE_TO_INCLUDE || |
| 950 | type == IGMPV3_MODE_IS_INCLUDE) && |
Nikolay Aleksandrov | e57f618 | 2019-07-02 15:00:18 +0300 | [diff] [blame] | 951 | nsrcs == 0) { |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 952 | br_ip4_multicast_leave_group(br, port, group, vid, src); |
Satish Ashok | bc8c20a | 2015-07-13 05:28:37 -0700 | [diff] [blame] | 953 | } else { |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 954 | err = br_ip4_multicast_add_group(br, port, group, vid, |
| 955 | src); |
Satish Ashok | bc8c20a | 2015-07-13 05:28:37 -0700 | [diff] [blame] | 956 | if (err) |
| 957 | break; |
| 958 | } |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | return err; |
| 962 | } |
| 963 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 964 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 965 | static int br_ip6_multicast_mld2_report(struct net_bridge *br, |
| 966 | struct net_bridge_port *port, |
Vlad Yasevich | 0649909 | 2013-10-28 15:45:07 -0400 | [diff] [blame] | 967 | struct sk_buff *skb, |
| 968 | u16 vid) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 969 | { |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 970 | unsigned int nsrcs_offset; |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 971 | const unsigned char *src; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 972 | struct icmp6hdr *icmp6h; |
| 973 | struct mld2_grec *grec; |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 974 | unsigned int grec_len; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 975 | int i; |
| 976 | int len; |
| 977 | int num; |
| 978 | int err = 0; |
| 979 | |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 980 | if (!ipv6_mc_may_pull(skb, sizeof(*icmp6h))) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 981 | return -EINVAL; |
| 982 | |
| 983 | icmp6h = icmp6_hdr(skb); |
| 984 | num = ntohs(icmp6h->icmp6_dataun.un_data16[1]); |
Linus Lüssing | c2d4fbd | 2015-09-11 18:39:48 +0200 | [diff] [blame] | 985 | len = skb_transport_offset(skb) + sizeof(*icmp6h); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 986 | |
| 987 | for (i = 0; i < num; i++) { |
Nikolay Aleksandrov | e57f618 | 2019-07-02 15:00:18 +0300 | [diff] [blame] | 988 | __be16 *_nsrcs, __nsrcs; |
| 989 | u16 nsrcs; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 990 | |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 991 | nsrcs_offset = len + offsetof(struct mld2_grec, grec_nsrcs); |
| 992 | |
| 993 | if (skb_transport_offset(skb) + ipv6_transport_len(skb) < |
| 994 | nsrcs_offset + sizeof(_nsrcs)) |
| 995 | return -EINVAL; |
| 996 | |
Nikolay Aleksandrov | e57f618 | 2019-07-02 15:00:18 +0300 | [diff] [blame] | 997 | _nsrcs = skb_header_pointer(skb, nsrcs_offset, |
| 998 | sizeof(__nsrcs), &__nsrcs); |
| 999 | if (!_nsrcs) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1000 | return -EINVAL; |
| 1001 | |
Nikolay Aleksandrov | e57f618 | 2019-07-02 15:00:18 +0300 | [diff] [blame] | 1002 | nsrcs = ntohs(*_nsrcs); |
| 1003 | grec_len = struct_size(grec, grec_src, nsrcs); |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1004 | |
| 1005 | if (!ipv6_mc_may_pull(skb, len + grec_len)) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1006 | return -EINVAL; |
| 1007 | |
| 1008 | grec = (struct mld2_grec *)(skb->data + len); |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1009 | len += grec_len; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1010 | |
| 1011 | /* We treat these as MLDv1 reports for now. */ |
| 1012 | switch (grec->grec_type) { |
| 1013 | case MLD2_MODE_IS_INCLUDE: |
| 1014 | case MLD2_MODE_IS_EXCLUDE: |
| 1015 | case MLD2_CHANGE_TO_INCLUDE: |
| 1016 | case MLD2_CHANGE_TO_EXCLUDE: |
| 1017 | case MLD2_ALLOW_NEW_SOURCES: |
| 1018 | case MLD2_BLOCK_OLD_SOURCES: |
| 1019 | break; |
| 1020 | |
| 1021 | default: |
| 1022 | continue; |
| 1023 | } |
| 1024 | |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1025 | src = eth_hdr(skb)->h_source; |
Satish Ashok | bc8c20a | 2015-07-13 05:28:37 -0700 | [diff] [blame] | 1026 | if ((grec->grec_type == MLD2_CHANGE_TO_INCLUDE || |
| 1027 | grec->grec_type == MLD2_MODE_IS_INCLUDE) && |
Nikolay Aleksandrov | e57f618 | 2019-07-02 15:00:18 +0300 | [diff] [blame] | 1028 | nsrcs == 0) { |
Satish Ashok | bc8c20a | 2015-07-13 05:28:37 -0700 | [diff] [blame] | 1029 | br_ip6_multicast_leave_group(br, port, &grec->grec_mca, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1030 | vid, src); |
Satish Ashok | bc8c20a | 2015-07-13 05:28:37 -0700 | [diff] [blame] | 1031 | } else { |
| 1032 | err = br_ip6_multicast_add_group(br, port, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1033 | &grec->grec_mca, vid, |
| 1034 | src); |
Davide Caratti | 9264251 | 2016-08-31 14:16:44 +0200 | [diff] [blame] | 1035 | if (err) |
Satish Ashok | bc8c20a | 2015-07-13 05:28:37 -0700 | [diff] [blame] | 1036 | break; |
| 1037 | } |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | return err; |
| 1041 | } |
| 1042 | #endif |
| 1043 | |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1044 | static bool br_ip4_multicast_select_querier(struct net_bridge *br, |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1045 | struct net_bridge_port *port, |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1046 | __be32 saddr) |
| 1047 | { |
| 1048 | if (!timer_pending(&br->ip4_own_query.timer) && |
| 1049 | !timer_pending(&br->ip4_other_query.timer)) |
| 1050 | goto update; |
| 1051 | |
| 1052 | if (!br->ip4_querier.addr.u.ip4) |
| 1053 | goto update; |
| 1054 | |
| 1055 | if (ntohl(saddr) <= ntohl(br->ip4_querier.addr.u.ip4)) |
| 1056 | goto update; |
| 1057 | |
| 1058 | return false; |
| 1059 | |
| 1060 | update: |
| 1061 | br->ip4_querier.addr.u.ip4 = saddr; |
| 1062 | |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1063 | /* update protected by general multicast_lock by caller */ |
| 1064 | rcu_assign_pointer(br->ip4_querier.port, port); |
| 1065 | |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1066 | return true; |
| 1067 | } |
| 1068 | |
| 1069 | #if IS_ENABLED(CONFIG_IPV6) |
| 1070 | static bool br_ip6_multicast_select_querier(struct net_bridge *br, |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1071 | struct net_bridge_port *port, |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1072 | struct in6_addr *saddr) |
| 1073 | { |
| 1074 | if (!timer_pending(&br->ip6_own_query.timer) && |
| 1075 | !timer_pending(&br->ip6_other_query.timer)) |
| 1076 | goto update; |
| 1077 | |
| 1078 | if (ipv6_addr_cmp(saddr, &br->ip6_querier.addr.u.ip6) <= 0) |
| 1079 | goto update; |
| 1080 | |
| 1081 | return false; |
| 1082 | |
| 1083 | update: |
| 1084 | br->ip6_querier.addr.u.ip6 = *saddr; |
| 1085 | |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1086 | /* update protected by general multicast_lock by caller */ |
| 1087 | rcu_assign_pointer(br->ip6_querier.port, port); |
| 1088 | |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1089 | return true; |
| 1090 | } |
| 1091 | #endif |
| 1092 | |
| 1093 | static bool br_multicast_select_querier(struct net_bridge *br, |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1094 | struct net_bridge_port *port, |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1095 | struct br_ip *saddr) |
| 1096 | { |
| 1097 | switch (saddr->proto) { |
| 1098 | case htons(ETH_P_IP): |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1099 | return br_ip4_multicast_select_querier(br, port, saddr->u.ip4); |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1100 | #if IS_ENABLED(CONFIG_IPV6) |
| 1101 | case htons(ETH_P_IPV6): |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1102 | return br_ip6_multicast_select_querier(br, port, &saddr->u.ip6); |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1103 | #endif |
| 1104 | } |
| 1105 | |
| 1106 | return false; |
| 1107 | } |
| 1108 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1109 | static void |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1110 | br_multicast_update_query_timer(struct net_bridge *br, |
| 1111 | struct bridge_mcast_other_query *query, |
| 1112 | unsigned long max_delay) |
Linus Lüssing | b00589a | 2013-08-01 01:06:20 +0200 | [diff] [blame] | 1113 | { |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1114 | if (!timer_pending(&query->timer)) |
| 1115 | query->delay_time = jiffies + max_delay; |
Linus Lüssing | b00589a | 2013-08-01 01:06:20 +0200 | [diff] [blame] | 1116 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1117 | mod_timer(&query->timer, jiffies + br->multicast_querier_interval); |
Linus Lüssing | b00589a | 2013-08-01 01:06:20 +0200 | [diff] [blame] | 1118 | } |
| 1119 | |
Nogah Frankel | 6d54964 | 2017-02-09 14:54:42 +0100 | [diff] [blame] | 1120 | static void br_port_mc_router_state_change(struct net_bridge_port *p, |
| 1121 | bool is_mc_router) |
| 1122 | { |
| 1123 | struct switchdev_attr attr = { |
| 1124 | .orig_dev = p->dev, |
| 1125 | .id = SWITCHDEV_ATTR_ID_PORT_MROUTER, |
| 1126 | .flags = SWITCHDEV_F_DEFER, |
| 1127 | .u.mrouter = is_mc_router, |
| 1128 | }; |
| 1129 | |
| 1130 | switchdev_port_attr_set(p->dev, &attr); |
| 1131 | } |
| 1132 | |
stephen hemminger | 7e80c12 | 2010-04-27 15:01:04 +0000 | [diff] [blame] | 1133 | /* |
Cong Wang | 7c77602 | 2013-06-21 15:37:25 +0800 | [diff] [blame] | 1134 | * Add port to router_list |
stephen hemminger | 7e80c12 | 2010-04-27 15:01:04 +0000 | [diff] [blame] | 1135 | * list is maintained ordered by pointer value |
| 1136 | * and locked by br->multicast_lock and RCU |
| 1137 | */ |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1138 | static void br_multicast_add_router(struct net_bridge *br, |
| 1139 | struct net_bridge_port *port) |
| 1140 | { |
stephen hemminger | dcdca2c | 2010-04-27 07:13:11 +0000 | [diff] [blame] | 1141 | struct net_bridge_port *p; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1142 | struct hlist_node *slot = NULL; |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1143 | |
Nikolay Aleksandrov | 1a040ea | 2015-06-09 10:23:57 -0700 | [diff] [blame] | 1144 | if (!hlist_unhashed(&port->rlist)) |
| 1145 | return; |
| 1146 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1147 | hlist_for_each_entry(p, &br->router_list, rlist) { |
stephen hemminger | 7e80c12 | 2010-04-27 15:01:04 +0000 | [diff] [blame] | 1148 | if ((unsigned long) port >= (unsigned long) p) |
| 1149 | break; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1150 | slot = &p->rlist; |
stephen hemminger | dcdca2c | 2010-04-27 07:13:11 +0000 | [diff] [blame] | 1151 | } |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1152 | |
stephen hemminger | 7e80c12 | 2010-04-27 15:01:04 +0000 | [diff] [blame] | 1153 | if (slot) |
Ken Helias | 1d02328 | 2014-08-06 16:09:16 -0700 | [diff] [blame] | 1154 | hlist_add_behind_rcu(&port->rlist, slot); |
stephen hemminger | dcdca2c | 2010-04-27 07:13:11 +0000 | [diff] [blame] | 1155 | else |
| 1156 | hlist_add_head_rcu(&port->rlist, &br->router_list); |
Satish Ashok | 949f1e3 | 2015-07-23 05:00:53 -0700 | [diff] [blame] | 1157 | br_rtr_notify(br->dev, port, RTM_NEWMDB); |
Nogah Frankel | 6d54964 | 2017-02-09 14:54:42 +0100 | [diff] [blame] | 1158 | br_port_mc_router_state_change(port, true); |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1161 | static void br_multicast_mark_router(struct net_bridge *br, |
| 1162 | struct net_bridge_port *port) |
| 1163 | { |
| 1164 | unsigned long now = jiffies; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1165 | |
| 1166 | if (!port) { |
Yotam Gigi | 7704142 | 2017-10-09 11:15:31 +0200 | [diff] [blame] | 1167 | if (br->multicast_router == MDB_RTR_TYPE_TEMP_QUERY) { |
| 1168 | if (!timer_pending(&br->multicast_router_timer)) |
| 1169 | br_mc_router_state_change(br, true); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1170 | mod_timer(&br->multicast_router_timer, |
| 1171 | now + br->multicast_querier_interval); |
Yotam Gigi | 7704142 | 2017-10-09 11:15:31 +0200 | [diff] [blame] | 1172 | } |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1173 | return; |
| 1174 | } |
| 1175 | |
Nikolay Aleksandrov | a55d824 | 2016-02-26 21:20:03 +0100 | [diff] [blame] | 1176 | if (port->multicast_router == MDB_RTR_TYPE_DISABLED || |
| 1177 | port->multicast_router == MDB_RTR_TYPE_PERM) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1178 | return; |
| 1179 | |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1180 | br_multicast_add_router(br, port); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1181 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1182 | mod_timer(&port->multicast_router_timer, |
| 1183 | now + br->multicast_querier_interval); |
| 1184 | } |
| 1185 | |
| 1186 | static void br_multicast_query_received(struct net_bridge *br, |
| 1187 | struct net_bridge_port *port, |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1188 | struct bridge_mcast_other_query *query, |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1189 | struct br_ip *saddr, |
Linus Lüssing | b00589a | 2013-08-01 01:06:20 +0200 | [diff] [blame] | 1190 | unsigned long max_delay) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1191 | { |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1192 | if (!br_multicast_select_querier(br, port, saddr)) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1193 | return; |
| 1194 | |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1195 | br_multicast_update_query_timer(br, query, max_delay); |
Hangbin Liu | 278e214 | 2019-02-22 21:22:32 +0800 | [diff] [blame] | 1196 | br_multicast_mark_router(br, port); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1197 | } |
| 1198 | |
zhong jiang | 9c2e955 | 2018-08-06 11:07:23 +0800 | [diff] [blame] | 1199 | static void br_ip4_multicast_query(struct net_bridge *br, |
| 1200 | struct net_bridge_port *port, |
| 1201 | struct sk_buff *skb, |
| 1202 | u16 vid) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1203 | { |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1204 | unsigned int transport_len = ip_transport_len(skb); |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1205 | const struct iphdr *iph = ip_hdr(skb); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1206 | struct igmphdr *ih = igmp_hdr(skb); |
| 1207 | struct net_bridge_mdb_entry *mp; |
| 1208 | struct igmpv3_query *ih3; |
| 1209 | struct net_bridge_port_group *p; |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 1210 | struct net_bridge_port_group __rcu **pp; |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1211 | struct br_ip saddr; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1212 | unsigned long max_delay; |
| 1213 | unsigned long now = jiffies; |
| 1214 | __be32 group; |
| 1215 | |
| 1216 | spin_lock(&br->multicast_lock); |
| 1217 | if (!netif_running(br->dev) || |
| 1218 | (port && port->state == BR_STATE_DISABLED)) |
| 1219 | goto out; |
| 1220 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1221 | group = ih->group; |
| 1222 | |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1223 | if (transport_len == sizeof(*ih)) { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1224 | max_delay = ih->code * (HZ / IGMP_TIMER_SCALE); |
| 1225 | |
| 1226 | if (!max_delay) { |
| 1227 | max_delay = 10 * HZ; |
| 1228 | group = 0; |
| 1229 | } |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1230 | } else if (transport_len >= sizeof(*ih3)) { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1231 | ih3 = igmpv3_query_hdr(skb); |
| 1232 | if (ih3->nsrcs) |
YOSHIFUJI Hideaki | bec68ff | 2010-03-13 12:27:21 -0800 | [diff] [blame] | 1233 | goto out; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1234 | |
YOSHIFUJI Hideaki / 吉藤英明 | 0ba8c9e | 2010-03-15 19:27:00 +0000 | [diff] [blame] | 1235 | max_delay = ih3->code ? |
| 1236 | IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1; |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 1237 | } else { |
Linus Lüssing | 9ed973c | 2014-03-10 22:25:24 +0100 | [diff] [blame] | 1238 | goto out; |
| 1239 | } |
| 1240 | |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1241 | if (!group) { |
| 1242 | saddr.proto = htons(ETH_P_IP); |
| 1243 | saddr.u.ip4 = iph->saddr; |
Linus Lüssing | b00589a | 2013-08-01 01:06:20 +0200 | [diff] [blame] | 1244 | |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1245 | br_multicast_query_received(br, port, &br->ip4_other_query, |
| 1246 | &saddr, max_delay); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1247 | goto out; |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1248 | } |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1249 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 1250 | mp = br_mdb_ip4_get(br, group, vid); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1251 | if (!mp) |
| 1252 | goto out; |
| 1253 | |
| 1254 | max_delay *= br->multicast_last_member_count; |
| 1255 | |
Andrew Lunn | ff0fd34 | 2017-11-09 23:10:57 +0100 | [diff] [blame] | 1256 | if (mp->host_joined && |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1257 | (timer_pending(&mp->timer) ? |
| 1258 | time_after(mp->timer.expires, now + max_delay) : |
| 1259 | try_to_del_timer_sync(&mp->timer) >= 0)) |
| 1260 | mod_timer(&mp->timer, now + max_delay); |
| 1261 | |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 1262 | for (pp = &mp->ports; |
| 1263 | (p = mlock_dereference(*pp, br)) != NULL; |
| 1264 | pp = &p->next) { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1265 | if (timer_pending(&p->timer) ? |
| 1266 | time_after(p->timer.expires, now + max_delay) : |
| 1267 | try_to_del_timer_sync(&p->timer) >= 0) |
Herbert Xu | 24f9cdc | 2011-02-11 12:42:07 +0000 | [diff] [blame] | 1268 | mod_timer(&p->timer, now + max_delay); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
| 1271 | out: |
| 1272 | spin_unlock(&br->multicast_lock); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1273 | } |
| 1274 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 1275 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1276 | static int br_ip6_multicast_query(struct net_bridge *br, |
| 1277 | struct net_bridge_port *port, |
Vlad Yasevich | 0649909 | 2013-10-28 15:45:07 -0400 | [diff] [blame] | 1278 | struct sk_buff *skb, |
| 1279 | u16 vid) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1280 | { |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1281 | unsigned int transport_len = ipv6_transport_len(skb); |
stephen hemminger | eca2a43 | 2012-12-13 06:51:28 +0000 | [diff] [blame] | 1282 | struct mld_msg *mld; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1283 | struct net_bridge_mdb_entry *mp; |
| 1284 | struct mld2_query *mld2q; |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 1285 | struct net_bridge_port_group *p; |
| 1286 | struct net_bridge_port_group __rcu **pp; |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1287 | struct br_ip saddr; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1288 | unsigned long max_delay; |
| 1289 | unsigned long now = jiffies; |
Linus Lüssing | 856ce5d | 2016-05-04 17:25:02 +0200 | [diff] [blame] | 1290 | unsigned int offset = skb_transport_offset(skb); |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1291 | const struct in6_addr *group = NULL; |
Linus Lüssing | 9ed973c | 2014-03-10 22:25:24 +0100 | [diff] [blame] | 1292 | bool is_general_query; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1293 | int err = 0; |
| 1294 | |
| 1295 | spin_lock(&br->multicast_lock); |
| 1296 | if (!netif_running(br->dev) || |
| 1297 | (port && port->state == BR_STATE_DISABLED)) |
| 1298 | goto out; |
| 1299 | |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1300 | if (transport_len == sizeof(*mld)) { |
Linus Lüssing | 856ce5d | 2016-05-04 17:25:02 +0200 | [diff] [blame] | 1301 | if (!pskb_may_pull(skb, offset + sizeof(*mld))) { |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1302 | err = -EINVAL; |
| 1303 | goto out; |
| 1304 | } |
| 1305 | mld = (struct mld_msg *) icmp6_hdr(skb); |
Li RongQing | 4715213 | 2012-07-09 23:56:12 +0000 | [diff] [blame] | 1306 | max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay)); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1307 | if (max_delay) |
| 1308 | group = &mld->mld_mca; |
Linus Lüssing | 248ba8e | 2013-08-06 00:32:05 +0200 | [diff] [blame] | 1309 | } else { |
Linus Lüssing | 856ce5d | 2016-05-04 17:25:02 +0200 | [diff] [blame] | 1310 | if (!pskb_may_pull(skb, offset + sizeof(*mld2q))) { |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1311 | err = -EINVAL; |
| 1312 | goto out; |
| 1313 | } |
| 1314 | mld2q = (struct mld2_query *)icmp6_hdr(skb); |
| 1315 | if (!mld2q->mld2q_nsrcs) |
| 1316 | group = &mld2q->mld2q_mca; |
Daniel Borkmann | e3f5b17 | 2013-09-04 00:19:39 +0200 | [diff] [blame] | 1317 | |
| 1318 | max_delay = max(msecs_to_jiffies(mldv2_mrc(mld2q)), 1UL); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1319 | } |
| 1320 | |
Linus Lüssing | 9ed973c | 2014-03-10 22:25:24 +0100 | [diff] [blame] | 1321 | is_general_query = group && ipv6_addr_any(group); |
| 1322 | |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1323 | if (is_general_query) { |
| 1324 | saddr.proto = htons(ETH_P_IPV6); |
Nikolay Aleksandrov | 3b26a5d | 2019-07-02 15:00:19 +0300 | [diff] [blame^] | 1325 | saddr.u.ip6 = ipv6_hdr(skb)->saddr; |
Linus Lüssing | b00589a | 2013-08-01 01:06:20 +0200 | [diff] [blame] | 1326 | |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1327 | br_multicast_query_received(br, port, &br->ip6_other_query, |
| 1328 | &saddr, max_delay); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1329 | goto out; |
Linus Lüssing | 6c03ee8 | 2014-06-12 01:41:23 +0200 | [diff] [blame] | 1330 | } else if (!group) { |
| 1331 | goto out; |
Linus Lüssing | dc4eb53 | 2014-06-07 18:26:27 +0200 | [diff] [blame] | 1332 | } |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1333 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 1334 | mp = br_mdb_ip6_get(br, group, vid); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1335 | if (!mp) |
| 1336 | goto out; |
| 1337 | |
| 1338 | max_delay *= br->multicast_last_member_count; |
Andrew Lunn | ff0fd34 | 2017-11-09 23:10:57 +0100 | [diff] [blame] | 1339 | if (mp->host_joined && |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1340 | (timer_pending(&mp->timer) ? |
| 1341 | time_after(mp->timer.expires, now + max_delay) : |
| 1342 | try_to_del_timer_sync(&mp->timer) >= 0)) |
| 1343 | mod_timer(&mp->timer, now + max_delay); |
| 1344 | |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 1345 | for (pp = &mp->ports; |
| 1346 | (p = mlock_dereference(*pp, br)) != NULL; |
| 1347 | pp = &p->next) { |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1348 | if (timer_pending(&p->timer) ? |
| 1349 | time_after(p->timer.expires, now + max_delay) : |
| 1350 | try_to_del_timer_sync(&p->timer) >= 0) |
Herbert Xu | 24f9cdc | 2011-02-11 12:42:07 +0000 | [diff] [blame] | 1351 | mod_timer(&p->timer, now + max_delay); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | out: |
| 1355 | spin_unlock(&br->multicast_lock); |
| 1356 | return err; |
| 1357 | } |
| 1358 | #endif |
| 1359 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1360 | static void |
| 1361 | br_multicast_leave_group(struct net_bridge *br, |
| 1362 | struct net_bridge_port *port, |
| 1363 | struct br_ip *group, |
| 1364 | struct bridge_mcast_other_query *other_query, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1365 | struct bridge_mcast_own_query *own_query, |
| 1366 | const unsigned char *src) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1367 | { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1368 | struct net_bridge_mdb_entry *mp; |
| 1369 | struct net_bridge_port_group *p; |
| 1370 | unsigned long now; |
| 1371 | unsigned long time; |
| 1372 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1373 | spin_lock(&br->multicast_lock); |
| 1374 | if (!netif_running(br->dev) || |
Satish Ashok | 544586f | 2015-07-28 03:28:27 -0700 | [diff] [blame] | 1375 | (port && port->state == BR_STATE_DISABLED)) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1376 | goto out; |
| 1377 | |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 1378 | mp = br_mdb_ip_get(br, group); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1379 | if (!mp) |
| 1380 | goto out; |
| 1381 | |
Satish Ashok | 544586f | 2015-07-28 03:28:27 -0700 | [diff] [blame] | 1382 | if (port && (port->flags & BR_MULTICAST_FAST_LEAVE)) { |
| 1383 | struct net_bridge_port_group __rcu **pp; |
| 1384 | |
| 1385 | for (pp = &mp->ports; |
| 1386 | (p = mlock_dereference(*pp, br)) != NULL; |
| 1387 | pp = &p->next) { |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1388 | if (!br_port_group_equal(p, port, src)) |
Satish Ashok | 544586f | 2015-07-28 03:28:27 -0700 | [diff] [blame] | 1389 | continue; |
| 1390 | |
| 1391 | rcu_assign_pointer(*pp, p->next); |
| 1392 | hlist_del_init(&p->mglist); |
| 1393 | del_timer(&p->timer); |
Nikolay Aleksandrov | 4329596 | 2018-12-05 15:14:25 +0200 | [diff] [blame] | 1394 | kfree_rcu(p, rcu); |
Elad Raz | 45ebcce | 2016-04-21 12:52:45 +0200 | [diff] [blame] | 1395 | br_mdb_notify(br->dev, port, group, RTM_DELMDB, |
| 1396 | p->flags); |
Satish Ashok | 544586f | 2015-07-28 03:28:27 -0700 | [diff] [blame] | 1397 | |
Andrew Lunn | ff0fd34 | 2017-11-09 23:10:57 +0100 | [diff] [blame] | 1398 | if (!mp->ports && !mp->host_joined && |
Satish Ashok | 544586f | 2015-07-28 03:28:27 -0700 | [diff] [blame] | 1399 | netif_running(br->dev)) |
| 1400 | mod_timer(&mp->timer, jiffies); |
| 1401 | } |
| 1402 | goto out; |
| 1403 | } |
| 1404 | |
| 1405 | if (timer_pending(&other_query->timer)) |
| 1406 | goto out; |
| 1407 | |
Nikolay Aleksandrov | 675779a | 2018-09-26 17:01:04 +0300 | [diff] [blame] | 1408 | if (br_opt_get(br, BROPT_MULTICAST_QUERIER)) { |
Cong Wang | 6b7df11 | 2013-05-21 21:52:56 +0000 | [diff] [blame] | 1409 | __br_multicast_send_query(br, port, &mp->addr); |
| 1410 | |
| 1411 | time = jiffies + br->multicast_last_member_count * |
| 1412 | br->multicast_last_member_interval; |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1413 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1414 | mod_timer(&own_query->timer, time); |
Cong Wang | 6b7df11 | 2013-05-21 21:52:56 +0000 | [diff] [blame] | 1415 | |
| 1416 | for (p = mlock_dereference(mp->ports, br); |
| 1417 | p != NULL; |
| 1418 | p = mlock_dereference(p->next, br)) { |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1419 | if (!br_port_group_equal(p, port, src)) |
Cong Wang | 6b7df11 | 2013-05-21 21:52:56 +0000 | [diff] [blame] | 1420 | continue; |
| 1421 | |
| 1422 | if (!hlist_unhashed(&p->mglist) && |
| 1423 | (timer_pending(&p->timer) ? |
| 1424 | time_after(p->timer.expires, time) : |
| 1425 | try_to_del_timer_sync(&p->timer) >= 0)) { |
| 1426 | mod_timer(&p->timer, time); |
| 1427 | } |
| 1428 | |
| 1429 | break; |
| 1430 | } |
| 1431 | } |
| 1432 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1433 | now = jiffies; |
| 1434 | time = now + br->multicast_last_member_count * |
| 1435 | br->multicast_last_member_interval; |
| 1436 | |
| 1437 | if (!port) { |
Andrew Lunn | ff0fd34 | 2017-11-09 23:10:57 +0100 | [diff] [blame] | 1438 | if (mp->host_joined && |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1439 | (timer_pending(&mp->timer) ? |
| 1440 | time_after(mp->timer.expires, time) : |
| 1441 | try_to_del_timer_sync(&mp->timer) >= 0)) { |
| 1442 | mod_timer(&mp->timer, time); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1443 | } |
Linus Lüssing | 454594f | 2013-10-20 00:58:57 +0200 | [diff] [blame] | 1444 | |
| 1445 | goto out; |
| 1446 | } |
| 1447 | |
| 1448 | for (p = mlock_dereference(mp->ports, br); |
| 1449 | p != NULL; |
| 1450 | p = mlock_dereference(p->next, br)) { |
| 1451 | if (p->port != port) |
| 1452 | continue; |
| 1453 | |
| 1454 | if (!hlist_unhashed(&p->mglist) && |
| 1455 | (timer_pending(&p->timer) ? |
| 1456 | time_after(p->timer.expires, time) : |
| 1457 | try_to_del_timer_sync(&p->timer) >= 0)) { |
| 1458 | mod_timer(&p->timer, time); |
| 1459 | } |
| 1460 | |
| 1461 | break; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1462 | } |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1463 | out: |
| 1464 | spin_unlock(&br->multicast_lock); |
| 1465 | } |
| 1466 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 1467 | static void br_ip4_multicast_leave_group(struct net_bridge *br, |
| 1468 | struct net_bridge_port *port, |
Vlad Yasevich | b0e9a30 | 2013-02-13 12:00:17 +0000 | [diff] [blame] | 1469 | __be32 group, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1470 | __u16 vid, |
| 1471 | const unsigned char *src) |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 1472 | { |
| 1473 | struct br_ip br_group; |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1474 | struct bridge_mcast_own_query *own_query; |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 1475 | |
| 1476 | if (ipv4_is_local_multicast(group)) |
| 1477 | return; |
| 1478 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1479 | own_query = port ? &port->ip4_own_query : &br->ip4_own_query; |
| 1480 | |
Nikolay Aleksandrov | 1515a63f | 2019-04-03 23:27:24 +0300 | [diff] [blame] | 1481 | memset(&br_group, 0, sizeof(br_group)); |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 1482 | br_group.u.ip4 = group; |
| 1483 | br_group.proto = htons(ETH_P_IP); |
Vlad Yasevich | b0e9a30 | 2013-02-13 12:00:17 +0000 | [diff] [blame] | 1484 | br_group.vid = vid; |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 1485 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1486 | br_multicast_leave_group(br, port, &br_group, &br->ip4_other_query, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1487 | own_query, src); |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 1488 | } |
| 1489 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 1490 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1491 | static void br_ip6_multicast_leave_group(struct net_bridge *br, |
| 1492 | struct net_bridge_port *port, |
Vlad Yasevich | b0e9a30 | 2013-02-13 12:00:17 +0000 | [diff] [blame] | 1493 | const struct in6_addr *group, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1494 | __u16 vid, |
| 1495 | const unsigned char *src) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1496 | { |
| 1497 | struct br_ip br_group; |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1498 | struct bridge_mcast_own_query *own_query; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1499 | |
Linus Lüssing | 3c3769e | 2013-09-04 02:13:39 +0200 | [diff] [blame] | 1500 | if (ipv6_addr_is_ll_all_nodes(group)) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1501 | return; |
| 1502 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1503 | own_query = port ? &port->ip6_own_query : &br->ip6_own_query; |
| 1504 | |
Nikolay Aleksandrov | 1515a63f | 2019-04-03 23:27:24 +0300 | [diff] [blame] | 1505 | memset(&br_group, 0, sizeof(br_group)); |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 1506 | br_group.u.ip6 = *group; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1507 | br_group.proto = htons(ETH_P_IPV6); |
Vlad Yasevich | b0e9a30 | 2013-02-13 12:00:17 +0000 | [diff] [blame] | 1508 | br_group.vid = vid; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1509 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1510 | br_multicast_leave_group(br, port, &br_group, &br->ip6_other_query, |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1511 | own_query, src); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1512 | } |
| 1513 | #endif |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 1514 | |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 1515 | static void br_multicast_err_count(const struct net_bridge *br, |
| 1516 | const struct net_bridge_port *p, |
| 1517 | __be16 proto) |
| 1518 | { |
| 1519 | struct bridge_mcast_stats __percpu *stats; |
| 1520 | struct bridge_mcast_stats *pstats; |
| 1521 | |
Nikolay Aleksandrov | 675779a | 2018-09-26 17:01:04 +0300 | [diff] [blame] | 1522 | if (!br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 1523 | return; |
| 1524 | |
| 1525 | if (p) |
| 1526 | stats = p->mcast_stats; |
| 1527 | else |
| 1528 | stats = br->mcast_stats; |
| 1529 | if (WARN_ON(!stats)) |
| 1530 | return; |
| 1531 | |
| 1532 | pstats = this_cpu_ptr(stats); |
| 1533 | |
| 1534 | u64_stats_update_begin(&pstats->syncp); |
| 1535 | switch (proto) { |
| 1536 | case htons(ETH_P_IP): |
| 1537 | pstats->mstats.igmp_parse_errors++; |
| 1538 | break; |
| 1539 | #if IS_ENABLED(CONFIG_IPV6) |
| 1540 | case htons(ETH_P_IPV6): |
| 1541 | pstats->mstats.mld_parse_errors++; |
| 1542 | break; |
| 1543 | #endif |
| 1544 | } |
| 1545 | u64_stats_update_end(&pstats->syncp); |
| 1546 | } |
| 1547 | |
Nikolay Aleksandrov | 91b02d3 | 2016-10-31 13:21:05 +0100 | [diff] [blame] | 1548 | static void br_multicast_pim(struct net_bridge *br, |
| 1549 | struct net_bridge_port *port, |
| 1550 | const struct sk_buff *skb) |
| 1551 | { |
| 1552 | unsigned int offset = skb_transport_offset(skb); |
| 1553 | struct pimhdr *pimhdr, _pimhdr; |
| 1554 | |
| 1555 | pimhdr = skb_header_pointer(skb, offset, sizeof(_pimhdr), &_pimhdr); |
| 1556 | if (!pimhdr || pim_hdr_version(pimhdr) != PIM_VERSION || |
| 1557 | pim_hdr_type(pimhdr) != PIM_TYPE_HELLO) |
| 1558 | return; |
| 1559 | |
| 1560 | br_multicast_mark_router(br, port); |
| 1561 | } |
| 1562 | |
Linus Lüssing | 4b3087c | 2019-01-21 07:26:28 +0100 | [diff] [blame] | 1563 | static int br_ip4_multicast_mrd_rcv(struct net_bridge *br, |
| 1564 | struct net_bridge_port *port, |
| 1565 | struct sk_buff *skb) |
| 1566 | { |
| 1567 | if (ip_hdr(skb)->protocol != IPPROTO_IGMP || |
| 1568 | igmp_hdr(skb)->type != IGMP_MRDISC_ADV) |
| 1569 | return -ENOMSG; |
| 1570 | |
| 1571 | br_multicast_mark_router(br, port); |
| 1572 | |
| 1573 | return 0; |
| 1574 | } |
| 1575 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1576 | static int br_multicast_ipv4_rcv(struct net_bridge *br, |
| 1577 | struct net_bridge_port *port, |
Vlad Yasevich | 0649909 | 2013-10-28 15:45:07 -0400 | [diff] [blame] | 1578 | struct sk_buff *skb, |
| 1579 | u16 vid) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1580 | { |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1581 | const unsigned char *src; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1582 | struct igmphdr *ih; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1583 | int err; |
| 1584 | |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1585 | err = ip_mc_check_igmp(skb); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1586 | |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 1587 | if (err == -ENOMSG) { |
Nikolay Aleksandrov | 91b02d3 | 2016-10-31 13:21:05 +0100 | [diff] [blame] | 1588 | if (!ipv4_is_local_multicast(ip_hdr(skb)->daddr)) { |
Herbert Xu | bd4265f | 2011-06-23 02:39:12 +0000 | [diff] [blame] | 1589 | BR_INPUT_SKB_CB(skb)->mrouters_only = 1; |
Nikolay Aleksandrov | 91b02d3 | 2016-10-31 13:21:05 +0100 | [diff] [blame] | 1590 | } else if (pim_ipv4_all_pim_routers(ip_hdr(skb)->daddr)) { |
| 1591 | if (ip_hdr(skb)->protocol == IPPROTO_PIM) |
| 1592 | br_multicast_pim(br, port, skb); |
Linus Lüssing | 4b3087c | 2019-01-21 07:26:28 +0100 | [diff] [blame] | 1593 | } else if (ipv4_is_all_snoopers(ip_hdr(skb)->daddr)) { |
Li RongQing | 08e7162 | 2019-02-19 10:17:09 +0800 | [diff] [blame] | 1594 | br_ip4_multicast_mrd_rcv(br, port, skb); |
Nikolay Aleksandrov | 91b02d3 | 2016-10-31 13:21:05 +0100 | [diff] [blame] | 1595 | } |
Linus Lüssing | 4b3087c | 2019-01-21 07:26:28 +0100 | [diff] [blame] | 1596 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1597 | return 0; |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 1598 | } else if (err < 0) { |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 1599 | br_multicast_err_count(br, port, skb->protocol); |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 1600 | return err; |
Herbert Xu | bd4265f | 2011-06-23 02:39:12 +0000 | [diff] [blame] | 1601 | } |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1602 | |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 1603 | ih = igmp_hdr(skb); |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1604 | src = eth_hdr(skb)->h_source; |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 1605 | BR_INPUT_SKB_CB(skb)->igmp = ih->type; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1606 | |
| 1607 | switch (ih->type) { |
| 1608 | case IGMP_HOST_MEMBERSHIP_REPORT: |
| 1609 | case IGMPV2_HOST_MEMBERSHIP_REPORT: |
Fernando Luis Vázquez Cao | 62b2bcb | 2011-06-13 15:04:43 +0000 | [diff] [blame] | 1610 | BR_INPUT_SKB_CB(skb)->mrouters_only = 1; |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1611 | err = br_ip4_multicast_add_group(br, port, ih->group, vid, src); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1612 | break; |
| 1613 | case IGMPV3_HOST_MEMBERSHIP_REPORT: |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1614 | err = br_ip4_multicast_igmp3_report(br, port, skb, vid); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1615 | break; |
| 1616 | case IGMP_HOST_MEMBERSHIP_QUERY: |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1617 | br_ip4_multicast_query(br, port, skb, vid); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1618 | break; |
| 1619 | case IGMP_HOST_LEAVE_MESSAGE: |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1620 | br_ip4_multicast_leave_group(br, port, ih->group, vid, src); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1621 | break; |
| 1622 | } |
| 1623 | |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 1624 | br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp, |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 1625 | BR_MCAST_DIR_RX); |
| 1626 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1627 | return err; |
| 1628 | } |
| 1629 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 1630 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Lüssing | 4b3087c | 2019-01-21 07:26:28 +0100 | [diff] [blame] | 1631 | static int br_ip6_multicast_mrd_rcv(struct net_bridge *br, |
| 1632 | struct net_bridge_port *port, |
| 1633 | struct sk_buff *skb) |
| 1634 | { |
| 1635 | int ret; |
| 1636 | |
| 1637 | if (ipv6_hdr(skb)->nexthdr != IPPROTO_ICMPV6) |
| 1638 | return -ENOMSG; |
| 1639 | |
| 1640 | ret = ipv6_mc_check_icmpv6(skb); |
| 1641 | if (ret < 0) |
| 1642 | return ret; |
| 1643 | |
| 1644 | if (icmp6_hdr(skb)->icmp6_type != ICMPV6_MRDISC_ADV) |
| 1645 | return -ENOMSG; |
| 1646 | |
| 1647 | br_multicast_mark_router(br, port); |
| 1648 | |
| 1649 | return 0; |
| 1650 | } |
| 1651 | |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1652 | static int br_multicast_ipv6_rcv(struct net_bridge *br, |
| 1653 | struct net_bridge_port *port, |
Vlad Yasevich | 0649909 | 2013-10-28 15:45:07 -0400 | [diff] [blame] | 1654 | struct sk_buff *skb, |
| 1655 | u16 vid) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1656 | { |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1657 | const unsigned char *src; |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 1658 | struct mld_msg *mld; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1659 | int err; |
| 1660 | |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1661 | err = ipv6_mc_check_mld(skb); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1662 | |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 1663 | if (err == -ENOMSG) { |
| 1664 | if (!ipv6_addr_is_ll_all_nodes(&ipv6_hdr(skb)->daddr)) |
| 1665 | BR_INPUT_SKB_CB(skb)->mrouters_only = 1; |
Linus Lüssing | 4b3087c | 2019-01-21 07:26:28 +0100 | [diff] [blame] | 1666 | |
| 1667 | if (ipv6_addr_is_all_snoopers(&ipv6_hdr(skb)->daddr)) { |
| 1668 | err = br_ip6_multicast_mrd_rcv(br, port, skb); |
| 1669 | |
| 1670 | if (err < 0 && err != -ENOMSG) { |
| 1671 | br_multicast_err_count(br, port, skb->protocol); |
| 1672 | return err; |
| 1673 | } |
| 1674 | } |
| 1675 | |
Linus Lüssing | 8fad9c3 | 2013-09-04 02:13:38 +0200 | [diff] [blame] | 1676 | return 0; |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 1677 | } else if (err < 0) { |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 1678 | br_multicast_err_count(br, port, skb->protocol); |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 1679 | return err; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1680 | } |
| 1681 | |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 1682 | mld = (struct mld_msg *)skb_transport_header(skb); |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 1683 | BR_INPUT_SKB_CB(skb)->igmp = mld->mld_type; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1684 | |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 1685 | switch (mld->mld_type) { |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1686 | case ICMPV6_MGM_REPORT: |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1687 | src = eth_hdr(skb)->h_source; |
Fernando Luis Vázquez Cao | fc2af6c | 2011-06-13 15:06:58 +0000 | [diff] [blame] | 1688 | BR_INPUT_SKB_CB(skb)->mrouters_only = 1; |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1689 | err = br_ip6_multicast_add_group(br, port, &mld->mld_mca, vid, |
| 1690 | src); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1691 | break; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1692 | case ICMPV6_MLD2_REPORT: |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1693 | err = br_ip6_multicast_mld2_report(br, port, skb, vid); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1694 | break; |
| 1695 | case ICMPV6_MGM_QUERY: |
Linus Lüssing | ba5ea614 | 2019-01-21 07:26:25 +0100 | [diff] [blame] | 1696 | err = br_ip6_multicast_query(br, port, skb, vid); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1697 | break; |
| 1698 | case ICMPV6_MGM_REDUCTION: |
Felix Fietkau | 6db6f0e | 2017-01-21 21:01:32 +0100 | [diff] [blame] | 1699 | src = eth_hdr(skb)->h_source; |
| 1700 | br_ip6_multicast_leave_group(br, port, &mld->mld_mca, vid, src); |
Linus Lüssing | 9afd85c | 2015-05-02 14:01:07 +0200 | [diff] [blame] | 1701 | break; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1702 | } |
| 1703 | |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 1704 | br_multicast_count(br, port, skb, BR_INPUT_SKB_CB(skb)->igmp, |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 1705 | BR_MCAST_DIR_RX); |
| 1706 | |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1707 | return err; |
| 1708 | } |
| 1709 | #endif |
| 1710 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1711 | int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port, |
Vlad Yasevich | 0649909 | 2013-10-28 15:45:07 -0400 | [diff] [blame] | 1712 | struct sk_buff *skb, u16 vid) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1713 | { |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 1714 | int ret = 0; |
| 1715 | |
YOSHIFUJI Hideaki / 吉藤英明 | 1fafc7a | 2010-04-25 08:06:40 +0000 | [diff] [blame] | 1716 | BR_INPUT_SKB_CB(skb)->igmp = 0; |
| 1717 | BR_INPUT_SKB_CB(skb)->mrouters_only = 0; |
| 1718 | |
Nikolay Aleksandrov | 13cefad | 2018-09-26 17:01:03 +0300 | [diff] [blame] | 1719 | if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1720 | return 0; |
| 1721 | |
| 1722 | switch (skb->protocol) { |
| 1723 | case htons(ETH_P_IP): |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 1724 | ret = br_multicast_ipv4_rcv(br, port, skb, vid); |
| 1725 | break; |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 1726 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1727 | case htons(ETH_P_IPV6): |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 1728 | ret = br_multicast_ipv6_rcv(br, port, skb, vid); |
| 1729 | break; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1730 | #endif |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 1733 | return ret; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1736 | static void br_multicast_query_expired(struct net_bridge *br, |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1737 | struct bridge_mcast_own_query *query, |
| 1738 | struct bridge_mcast_querier *querier) |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1739 | { |
| 1740 | spin_lock(&br->multicast_lock); |
| 1741 | if (query->startup_sent < br->multicast_startup_query_count) |
| 1742 | query->startup_sent++; |
| 1743 | |
Eric Dumazet | 71d9f61 | 2015-05-28 04:42:54 -0700 | [diff] [blame] | 1744 | RCU_INIT_POINTER(querier->port, NULL); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1745 | br_multicast_send_query(br, NULL, query); |
| 1746 | spin_unlock(&br->multicast_lock); |
| 1747 | } |
| 1748 | |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 1749 | static void br_ip4_multicast_query_expired(struct timer_list *t) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1750 | { |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 1751 | struct net_bridge *br = from_timer(br, t, ip4_own_query.timer); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1752 | |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1753 | br_multicast_query_expired(br, &br->ip4_own_query, &br->ip4_querier); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1756 | #if IS_ENABLED(CONFIG_IPV6) |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 1757 | static void br_ip6_multicast_query_expired(struct timer_list *t) |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1758 | { |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 1759 | struct net_bridge *br = from_timer(br, t, ip6_own_query.timer); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1760 | |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1761 | br_multicast_query_expired(br, &br->ip6_own_query, &br->ip6_querier); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1762 | } |
| 1763 | #endif |
| 1764 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1765 | void br_multicast_init(struct net_bridge *br) |
| 1766 | { |
Nikolay Aleksandrov | d08c6bc | 2018-12-05 15:14:27 +0200 | [diff] [blame] | 1767 | br->hash_max = BR_MULTICAST_DEFAULT_HASH_MAX; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1768 | |
Nikolay Aleksandrov | 7f0aec7 | 2016-02-26 21:20:01 +0100 | [diff] [blame] | 1769 | br->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1770 | br->multicast_last_member_count = 2; |
| 1771 | br->multicast_startup_query_count = 2; |
| 1772 | |
| 1773 | br->multicast_last_member_interval = HZ; |
| 1774 | br->multicast_query_response_interval = 10 * HZ; |
| 1775 | br->multicast_startup_query_interval = 125 * HZ / 4; |
| 1776 | br->multicast_query_interval = 125 * HZ; |
| 1777 | br->multicast_querier_interval = 255 * HZ; |
| 1778 | br->multicast_membership_interval = 260 * HZ; |
| 1779 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1780 | br->ip4_other_query.delay_time = 0; |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1781 | br->ip4_querier.port = NULL; |
Nikolay Aleksandrov | aa2ae3e | 2016-11-21 13:03:25 +0100 | [diff] [blame] | 1782 | br->multicast_igmp_version = 2; |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1783 | #if IS_ENABLED(CONFIG_IPV6) |
Nikolay Aleksandrov | aa2ae3e | 2016-11-21 13:03:25 +0100 | [diff] [blame] | 1784 | br->multicast_mld_version = 1; |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1785 | br->ip6_other_query.delay_time = 0; |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 1786 | br->ip6_querier.port = NULL; |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1787 | #endif |
Ido Schimmel | 6919622a | 2018-10-01 11:57:01 +0300 | [diff] [blame] | 1788 | br_opt_toggle(br, BROPT_MULTICAST_ENABLED, true); |
Nikolay Aleksandrov | 675779a | 2018-09-26 17:01:04 +0300 | [diff] [blame] | 1789 | br_opt_toggle(br, BROPT_HAS_IPV6_ADDR, true); |
Linus Lüssing | b00589a | 2013-08-01 01:06:20 +0200 | [diff] [blame] | 1790 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1791 | spin_lock_init(&br->multicast_lock); |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 1792 | timer_setup(&br->multicast_router_timer, |
| 1793 | br_multicast_local_router_expired, 0); |
| 1794 | timer_setup(&br->ip4_other_query.timer, |
| 1795 | br_ip4_multicast_querier_expired, 0); |
| 1796 | timer_setup(&br->ip4_own_query.timer, |
| 1797 | br_ip4_multicast_query_expired, 0); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1798 | #if IS_ENABLED(CONFIG_IPV6) |
Allen Pais | 88c1f37 | 2017-11-03 11:51:10 +0530 | [diff] [blame] | 1799 | timer_setup(&br->ip6_other_query.timer, |
| 1800 | br_ip6_multicast_querier_expired, 0); |
| 1801 | timer_setup(&br->ip6_own_query.timer, |
| 1802 | br_ip6_multicast_query_expired, 0); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1803 | #endif |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 1804 | INIT_HLIST_HEAD(&br->mdb_list); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
Linus Lüssing | 4effd28 | 2019-01-21 07:26:27 +0100 | [diff] [blame] | 1807 | static void br_ip4_multicast_join_snoopers(struct net_bridge *br) |
| 1808 | { |
| 1809 | struct in_device *in_dev = in_dev_get(br->dev); |
| 1810 | |
| 1811 | if (!in_dev) |
| 1812 | return; |
| 1813 | |
Florian Fainelli | 9fb2080 | 2019-02-01 20:20:52 -0800 | [diff] [blame] | 1814 | __ip_mc_inc_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC); |
Linus Lüssing | 4effd28 | 2019-01-21 07:26:27 +0100 | [diff] [blame] | 1815 | in_dev_put(in_dev); |
| 1816 | } |
| 1817 | |
| 1818 | #if IS_ENABLED(CONFIG_IPV6) |
| 1819 | static void br_ip6_multicast_join_snoopers(struct net_bridge *br) |
| 1820 | { |
| 1821 | struct in6_addr addr; |
| 1822 | |
| 1823 | ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a)); |
| 1824 | ipv6_dev_mc_inc(br->dev, &addr); |
| 1825 | } |
| 1826 | #else |
| 1827 | static inline void br_ip6_multicast_join_snoopers(struct net_bridge *br) |
| 1828 | { |
| 1829 | } |
| 1830 | #endif |
| 1831 | |
| 1832 | static void br_multicast_join_snoopers(struct net_bridge *br) |
| 1833 | { |
| 1834 | br_ip4_multicast_join_snoopers(br); |
| 1835 | br_ip6_multicast_join_snoopers(br); |
| 1836 | } |
| 1837 | |
| 1838 | static void br_ip4_multicast_leave_snoopers(struct net_bridge *br) |
| 1839 | { |
| 1840 | struct in_device *in_dev = in_dev_get(br->dev); |
| 1841 | |
| 1842 | if (WARN_ON(!in_dev)) |
| 1843 | return; |
| 1844 | |
Florian Fainelli | 9fb2080 | 2019-02-01 20:20:52 -0800 | [diff] [blame] | 1845 | __ip_mc_dec_group(in_dev, htonl(INADDR_ALLSNOOPERS_GROUP), GFP_ATOMIC); |
Linus Lüssing | 4effd28 | 2019-01-21 07:26:27 +0100 | [diff] [blame] | 1846 | in_dev_put(in_dev); |
| 1847 | } |
| 1848 | |
| 1849 | #if IS_ENABLED(CONFIG_IPV6) |
| 1850 | static void br_ip6_multicast_leave_snoopers(struct net_bridge *br) |
| 1851 | { |
| 1852 | struct in6_addr addr; |
| 1853 | |
| 1854 | ipv6_addr_set(&addr, htonl(0xff020000), 0, 0, htonl(0x6a)); |
| 1855 | ipv6_dev_mc_dec(br->dev, &addr); |
| 1856 | } |
| 1857 | #else |
| 1858 | static inline void br_ip6_multicast_leave_snoopers(struct net_bridge *br) |
| 1859 | { |
| 1860 | } |
| 1861 | #endif |
| 1862 | |
| 1863 | static void br_multicast_leave_snoopers(struct net_bridge *br) |
| 1864 | { |
| 1865 | br_ip4_multicast_leave_snoopers(br); |
| 1866 | br_ip6_multicast_leave_snoopers(br); |
| 1867 | } |
| 1868 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1869 | static void __br_multicast_open(struct net_bridge *br, |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1870 | struct bridge_mcast_own_query *query) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1871 | { |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1872 | query->startup_sent = 0; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1873 | |
Nikolay Aleksandrov | 13cefad | 2018-09-26 17:01:03 +0300 | [diff] [blame] | 1874 | if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1875 | return; |
| 1876 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1877 | mod_timer(&query->timer, jiffies); |
| 1878 | } |
| 1879 | |
| 1880 | void br_multicast_open(struct net_bridge *br) |
| 1881 | { |
Linus Lüssing | 4effd28 | 2019-01-21 07:26:27 +0100 | [diff] [blame] | 1882 | if (br_opt_get(br, BROPT_MULTICAST_ENABLED)) |
| 1883 | br_multicast_join_snoopers(br); |
| 1884 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1885 | __br_multicast_open(br, &br->ip4_own_query); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1886 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1887 | __br_multicast_open(br, &br->ip6_own_query); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1888 | #endif |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | void br_multicast_stop(struct net_bridge *br) |
| 1892 | { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1893 | del_timer_sync(&br->multicast_router_timer); |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1894 | del_timer_sync(&br->ip4_other_query.timer); |
| 1895 | del_timer_sync(&br->ip4_own_query.timer); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1896 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 1897 | del_timer_sync(&br->ip6_other_query.timer); |
| 1898 | del_timer_sync(&br->ip6_own_query.timer); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 1899 | #endif |
Linus Lüssing | 4effd28 | 2019-01-21 07:26:27 +0100 | [diff] [blame] | 1900 | |
| 1901 | if (br_opt_get(br, BROPT_MULTICAST_ENABLED)) |
| 1902 | br_multicast_leave_snoopers(br); |
Satish Ashok | e10177a | 2015-07-15 07:16:51 -0700 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | void br_multicast_dev_del(struct net_bridge *br) |
| 1906 | { |
Satish Ashok | e10177a | 2015-07-15 07:16:51 -0700 | [diff] [blame] | 1907 | struct net_bridge_mdb_entry *mp; |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 1908 | struct hlist_node *tmp; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1909 | |
| 1910 | spin_lock_bh(&br->multicast_lock); |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 1911 | hlist_for_each_entry_safe(mp, tmp, &br->mdb_list, mdb_node) { |
| 1912 | del_timer(&mp->timer); |
| 1913 | rhashtable_remove_fast(&br->mdb_hash_tbl, &mp->rhnode, |
| 1914 | br_mdb_rht_params); |
| 1915 | hlist_del_rcu(&mp->mdb_node); |
Nikolay Aleksandrov | 4329596 | 2018-12-05 15:14:25 +0200 | [diff] [blame] | 1916 | kfree_rcu(mp, rcu); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1917 | } |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1918 | spin_unlock_bh(&br->multicast_lock); |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 1919 | |
Nikolay Aleksandrov | 4329596 | 2018-12-05 15:14:25 +0200 | [diff] [blame] | 1920 | rcu_barrier(); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1921 | } |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1922 | |
| 1923 | int br_multicast_set_router(struct net_bridge *br, unsigned long val) |
| 1924 | { |
Linus Lüssing | 6ae4ae8 | 2015-05-23 03:12:34 +0200 | [diff] [blame] | 1925 | int err = -EINVAL; |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1926 | |
| 1927 | spin_lock_bh(&br->multicast_lock); |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1928 | |
| 1929 | switch (val) { |
Nikolay Aleksandrov | 7f0aec7 | 2016-02-26 21:20:01 +0100 | [diff] [blame] | 1930 | case MDB_RTR_TYPE_DISABLED: |
| 1931 | case MDB_RTR_TYPE_PERM: |
Yotam Gigi | 7704142 | 2017-10-09 11:15:31 +0200 | [diff] [blame] | 1932 | br_mc_router_state_change(br, val == MDB_RTR_TYPE_PERM); |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1933 | del_timer(&br->multicast_router_timer); |
Yotam Gigi | 7704142 | 2017-10-09 11:15:31 +0200 | [diff] [blame] | 1934 | br->multicast_router = val; |
| 1935 | err = 0; |
| 1936 | break; |
Nikolay Aleksandrov | 7f0aec7 | 2016-02-26 21:20:01 +0100 | [diff] [blame] | 1937 | case MDB_RTR_TYPE_TEMP_QUERY: |
Yotam Gigi | 7704142 | 2017-10-09 11:15:31 +0200 | [diff] [blame] | 1938 | if (br->multicast_router != MDB_RTR_TYPE_TEMP_QUERY) |
| 1939 | br_mc_router_state_change(br, false); |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1940 | br->multicast_router = val; |
| 1941 | err = 0; |
| 1942 | break; |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1943 | } |
| 1944 | |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1945 | spin_unlock_bh(&br->multicast_lock); |
| 1946 | |
| 1947 | return err; |
| 1948 | } |
| 1949 | |
Nikolay Aleksandrov | 7f0aec7 | 2016-02-26 21:20:01 +0100 | [diff] [blame] | 1950 | static void __del_port_router(struct net_bridge_port *p) |
| 1951 | { |
| 1952 | if (hlist_unhashed(&p->rlist)) |
| 1953 | return; |
| 1954 | hlist_del_init_rcu(&p->rlist); |
| 1955 | br_rtr_notify(p->br->dev, p, RTM_DELMDB); |
Nogah Frankel | 6d54964 | 2017-02-09 14:54:42 +0100 | [diff] [blame] | 1956 | br_port_mc_router_state_change(p, false); |
Nogah Frankel | f12e7d9 | 2017-02-09 14:54:41 +0100 | [diff] [blame] | 1957 | |
| 1958 | /* don't allow timer refresh */ |
| 1959 | if (p->multicast_router == MDB_RTR_TYPE_TEMP) |
| 1960 | p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; |
Nikolay Aleksandrov | 7f0aec7 | 2016-02-26 21:20:01 +0100 | [diff] [blame] | 1961 | } |
| 1962 | |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1963 | int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val) |
| 1964 | { |
| 1965 | struct net_bridge *br = p->br; |
Nikolay Aleksandrov | a55d824 | 2016-02-26 21:20:03 +0100 | [diff] [blame] | 1966 | unsigned long now = jiffies; |
Linus Lüssing | 6ae4ae8 | 2015-05-23 03:12:34 +0200 | [diff] [blame] | 1967 | int err = -EINVAL; |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1968 | |
| 1969 | spin_lock(&br->multicast_lock); |
Nikolay Aleksandrov | 4950cfd | 2016-02-26 21:20:02 +0100 | [diff] [blame] | 1970 | if (p->multicast_router == val) { |
Nikolay Aleksandrov | a55d824 | 2016-02-26 21:20:03 +0100 | [diff] [blame] | 1971 | /* Refresh the temp router port timer */ |
| 1972 | if (p->multicast_router == MDB_RTR_TYPE_TEMP) |
| 1973 | mod_timer(&p->multicast_router_timer, |
| 1974 | now + br->multicast_querier_interval); |
Nikolay Aleksandrov | 4950cfd | 2016-02-26 21:20:02 +0100 | [diff] [blame] | 1975 | err = 0; |
| 1976 | goto unlock; |
| 1977 | } |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1978 | switch (val) { |
Nikolay Aleksandrov | 7f0aec7 | 2016-02-26 21:20:01 +0100 | [diff] [blame] | 1979 | case MDB_RTR_TYPE_DISABLED: |
| 1980 | p->multicast_router = MDB_RTR_TYPE_DISABLED; |
| 1981 | __del_port_router(p); |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1982 | del_timer(&p->multicast_router_timer); |
Nikolay Aleksandrov | 7f0aec7 | 2016-02-26 21:20:01 +0100 | [diff] [blame] | 1983 | break; |
| 1984 | case MDB_RTR_TYPE_TEMP_QUERY: |
| 1985 | p->multicast_router = MDB_RTR_TYPE_TEMP_QUERY; |
| 1986 | __del_port_router(p); |
| 1987 | break; |
| 1988 | case MDB_RTR_TYPE_PERM: |
| 1989 | p->multicast_router = MDB_RTR_TYPE_PERM; |
| 1990 | del_timer(&p->multicast_router_timer); |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1991 | br_multicast_add_router(br, p); |
| 1992 | break; |
Nikolay Aleksandrov | a55d824 | 2016-02-26 21:20:03 +0100 | [diff] [blame] | 1993 | case MDB_RTR_TYPE_TEMP: |
| 1994 | p->multicast_router = MDB_RTR_TYPE_TEMP; |
| 1995 | br_multicast_mark_router(br, p); |
| 1996 | break; |
Nikolay Aleksandrov | 7f0aec7 | 2016-02-26 21:20:01 +0100 | [diff] [blame] | 1997 | default: |
| 1998 | goto unlock; |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1999 | } |
Nikolay Aleksandrov | 7f0aec7 | 2016-02-26 21:20:01 +0100 | [diff] [blame] | 2000 | err = 0; |
| 2001 | unlock: |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 2002 | spin_unlock(&br->multicast_lock); |
| 2003 | |
| 2004 | return err; |
| 2005 | } |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 2006 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 2007 | static void br_multicast_start_querier(struct net_bridge *br, |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 2008 | struct bridge_mcast_own_query *query) |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 2009 | { |
| 2010 | struct net_bridge_port *port; |
Herbert Xu | 7485721 | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 2011 | |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 2012 | __br_multicast_open(br, query); |
Herbert Xu | 7485721 | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 2013 | |
Nikolay Aleksandrov | c5b493c | 2019-04-11 15:08:25 +0300 | [diff] [blame] | 2014 | rcu_read_lock(); |
| 2015 | list_for_each_entry_rcu(port, &br->port_list, list) { |
Herbert Xu | 7485721 | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 2016 | if (port->state == BR_STATE_DISABLED || |
| 2017 | port->state == BR_STATE_BLOCKING) |
| 2018 | continue; |
| 2019 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 2020 | if (query == &br->ip4_own_query) |
| 2021 | br_multicast_enable(&port->ip4_own_query); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 2022 | #if IS_ENABLED(CONFIG_IPV6) |
| 2023 | else |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 2024 | br_multicast_enable(&port->ip6_own_query); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 2025 | #endif |
Herbert Xu | 7485721 | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 2026 | } |
Nikolay Aleksandrov | c5b493c | 2019-04-11 15:08:25 +0300 | [diff] [blame] | 2027 | rcu_read_unlock(); |
Herbert Xu | 7485721 | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | int br_multicast_toggle(struct net_bridge *br, unsigned long val) |
| 2031 | { |
Nikolay Aleksandrov | 7cb3f92 | 2016-10-18 18:09:48 +0200 | [diff] [blame] | 2032 | struct net_bridge_port *port; |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 2033 | |
Andrey Vagin | ef5e0d8 | 2011-11-10 05:48:03 +0000 | [diff] [blame] | 2034 | spin_lock_bh(&br->multicast_lock); |
Nikolay Aleksandrov | 13cefad | 2018-09-26 17:01:03 +0300 | [diff] [blame] | 2035 | if (!!br_opt_get(br, BROPT_MULTICAST_ENABLED) == !!val) |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 2036 | goto unlock; |
| 2037 | |
Nikolay Aleksandrov | 13cefad | 2018-09-26 17:01:03 +0300 | [diff] [blame] | 2038 | br_mc_disabled_update(br->dev, val); |
| 2039 | br_opt_toggle(br, BROPT_MULTICAST_ENABLED, !!val); |
Linus Lüssing | 4effd28 | 2019-01-21 07:26:27 +0100 | [diff] [blame] | 2040 | if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) { |
| 2041 | br_multicast_leave_snoopers(br); |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 2042 | goto unlock; |
Linus Lüssing | 4effd28 | 2019-01-21 07:26:27 +0100 | [diff] [blame] | 2043 | } |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 2044 | |
Herbert Xu | 3a7fda0 | 2010-07-29 00:45:30 +0000 | [diff] [blame] | 2045 | if (!netif_running(br->dev)) |
| 2046 | goto unlock; |
| 2047 | |
Nikolay Aleksandrov | 7cb3f92 | 2016-10-18 18:09:48 +0200 | [diff] [blame] | 2048 | br_multicast_open(br); |
| 2049 | list_for_each_entry(port, &br->port_list, list) |
| 2050 | __br_multicast_enable_port(port); |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 2051 | |
| 2052 | unlock: |
Andrey Vagin | ef5e0d8 | 2011-11-10 05:48:03 +0000 | [diff] [blame] | 2053 | spin_unlock_bh(&br->multicast_lock); |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 2054 | |
YueHaibing | a26d94b | 2018-12-17 17:46:23 +0800 | [diff] [blame] | 2055 | return 0; |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 2056 | } |
Herbert Xu | b195167 | 2010-02-27 19:41:51 +0000 | [diff] [blame] | 2057 | |
Ido Schimmel | 9341b98 | 2017-05-26 08:37:24 +0200 | [diff] [blame] | 2058 | bool br_multicast_enabled(const struct net_device *dev) |
| 2059 | { |
| 2060 | struct net_bridge *br = netdev_priv(dev); |
| 2061 | |
Nikolay Aleksandrov | 13cefad | 2018-09-26 17:01:03 +0300 | [diff] [blame] | 2062 | return !!br_opt_get(br, BROPT_MULTICAST_ENABLED); |
Ido Schimmel | 9341b98 | 2017-05-26 08:37:24 +0200 | [diff] [blame] | 2063 | } |
| 2064 | EXPORT_SYMBOL_GPL(br_multicast_enabled); |
| 2065 | |
Yotam Gigi | 0912bda | 2017-10-09 11:15:32 +0200 | [diff] [blame] | 2066 | bool br_multicast_router(const struct net_device *dev) |
| 2067 | { |
| 2068 | struct net_bridge *br = netdev_priv(dev); |
| 2069 | bool is_router; |
| 2070 | |
| 2071 | spin_lock_bh(&br->multicast_lock); |
| 2072 | is_router = br_multicast_is_router(br); |
| 2073 | spin_unlock_bh(&br->multicast_lock); |
| 2074 | return is_router; |
| 2075 | } |
| 2076 | EXPORT_SYMBOL_GPL(br_multicast_router); |
| 2077 | |
Herbert Xu | c5c2326 | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 2078 | int br_multicast_set_querier(struct net_bridge *br, unsigned long val) |
| 2079 | { |
Linus Lüssing | b00589a | 2013-08-01 01:06:20 +0200 | [diff] [blame] | 2080 | unsigned long max_delay; |
| 2081 | |
Herbert Xu | c5c2326 | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 2082 | val = !!val; |
| 2083 | |
| 2084 | spin_lock_bh(&br->multicast_lock); |
Nikolay Aleksandrov | 675779a | 2018-09-26 17:01:04 +0300 | [diff] [blame] | 2085 | if (br_opt_get(br, BROPT_MULTICAST_QUERIER) == val) |
Herbert Xu | c5c2326 | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 2086 | goto unlock; |
| 2087 | |
Nikolay Aleksandrov | 675779a | 2018-09-26 17:01:04 +0300 | [diff] [blame] | 2088 | br_opt_toggle(br, BROPT_MULTICAST_QUERIER, !!val); |
Linus Lüssing | b00589a | 2013-08-01 01:06:20 +0200 | [diff] [blame] | 2089 | if (!val) |
| 2090 | goto unlock; |
| 2091 | |
| 2092 | max_delay = br->multicast_query_response_interval; |
Linus Lüssing | b00589a | 2013-08-01 01:06:20 +0200 | [diff] [blame] | 2093 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 2094 | if (!timer_pending(&br->ip4_other_query.timer)) |
| 2095 | br->ip4_other_query.delay_time = jiffies + max_delay; |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 2096 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 2097 | br_multicast_start_querier(br, &br->ip4_own_query); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 2098 | |
| 2099 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 2100 | if (!timer_pending(&br->ip6_other_query.timer)) |
| 2101 | br->ip6_other_query.delay_time = jiffies + max_delay; |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 2102 | |
Linus Lüssing | 90010b3 | 2014-06-07 18:26:26 +0200 | [diff] [blame] | 2103 | br_multicast_start_querier(br, &br->ip6_own_query); |
Linus Lüssing | cc0fdd8 | 2013-08-30 17:28:17 +0200 | [diff] [blame] | 2104 | #endif |
Herbert Xu | c5c2326 | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 2105 | |
| 2106 | unlock: |
| 2107 | spin_unlock_bh(&br->multicast_lock); |
| 2108 | |
| 2109 | return 0; |
| 2110 | } |
| 2111 | |
Nikolay Aleksandrov | 5e92358 | 2016-11-21 13:03:24 +0100 | [diff] [blame] | 2112 | int br_multicast_set_igmp_version(struct net_bridge *br, unsigned long val) |
| 2113 | { |
| 2114 | /* Currently we support only version 2 and 3 */ |
| 2115 | switch (val) { |
| 2116 | case 2: |
| 2117 | case 3: |
| 2118 | break; |
| 2119 | default: |
| 2120 | return -EINVAL; |
| 2121 | } |
| 2122 | |
| 2123 | spin_lock_bh(&br->multicast_lock); |
| 2124 | br->multicast_igmp_version = val; |
| 2125 | spin_unlock_bh(&br->multicast_lock); |
| 2126 | |
| 2127 | return 0; |
| 2128 | } |
| 2129 | |
Nikolay Aleksandrov | aa2ae3e | 2016-11-21 13:03:25 +0100 | [diff] [blame] | 2130 | #if IS_ENABLED(CONFIG_IPV6) |
| 2131 | int br_multicast_set_mld_version(struct net_bridge *br, unsigned long val) |
| 2132 | { |
| 2133 | /* Currently we support version 1 and 2 */ |
| 2134 | switch (val) { |
| 2135 | case 1: |
| 2136 | case 2: |
| 2137 | break; |
| 2138 | default: |
| 2139 | return -EINVAL; |
| 2140 | } |
| 2141 | |
| 2142 | spin_lock_bh(&br->multicast_lock); |
| 2143 | br->multicast_mld_version = val; |
| 2144 | spin_unlock_bh(&br->multicast_lock); |
| 2145 | |
| 2146 | return 0; |
| 2147 | } |
| 2148 | #endif |
| 2149 | |
Linus Lüssing | 07f8ac4 | 2014-06-07 18:26:28 +0200 | [diff] [blame] | 2150 | /** |
| 2151 | * br_multicast_list_adjacent - Returns snooped multicast addresses |
| 2152 | * @dev: The bridge port adjacent to which to retrieve addresses |
| 2153 | * @br_ip_list: The list to store found, snooped multicast IP addresses in |
| 2154 | * |
| 2155 | * Creates a list of IP addresses (struct br_ip_list) sensed by the multicast |
| 2156 | * snooping feature on all bridge ports of dev's bridge device, excluding |
| 2157 | * the addresses from dev itself. |
| 2158 | * |
| 2159 | * Returns the number of items added to br_ip_list. |
| 2160 | * |
| 2161 | * Notes: |
| 2162 | * - br_ip_list needs to be initialized by caller |
| 2163 | * - br_ip_list might contain duplicates in the end |
| 2164 | * (needs to be taken care of by caller) |
| 2165 | * - br_ip_list needs to be freed by caller |
| 2166 | */ |
| 2167 | int br_multicast_list_adjacent(struct net_device *dev, |
| 2168 | struct list_head *br_ip_list) |
| 2169 | { |
| 2170 | struct net_bridge *br; |
| 2171 | struct net_bridge_port *port; |
| 2172 | struct net_bridge_port_group *group; |
| 2173 | struct br_ip_list *entry; |
| 2174 | int count = 0; |
| 2175 | |
| 2176 | rcu_read_lock(); |
Julian Wiedmann | 35f861e | 2019-03-29 14:38:19 +0100 | [diff] [blame] | 2177 | if (!br_ip_list || !netif_is_bridge_port(dev)) |
Linus Lüssing | 07f8ac4 | 2014-06-07 18:26:28 +0200 | [diff] [blame] | 2178 | goto unlock; |
| 2179 | |
| 2180 | port = br_port_get_rcu(dev); |
| 2181 | if (!port || !port->br) |
| 2182 | goto unlock; |
| 2183 | |
| 2184 | br = port->br; |
| 2185 | |
| 2186 | list_for_each_entry_rcu(port, &br->port_list, list) { |
| 2187 | if (!port->dev || port->dev == dev) |
| 2188 | continue; |
| 2189 | |
| 2190 | hlist_for_each_entry_rcu(group, &port->mglist, mglist) { |
| 2191 | entry = kmalloc(sizeof(*entry), GFP_ATOMIC); |
| 2192 | if (!entry) |
| 2193 | goto unlock; |
| 2194 | |
| 2195 | entry->addr = group->addr; |
| 2196 | list_add(&entry->list, br_ip_list); |
| 2197 | count++; |
| 2198 | } |
| 2199 | } |
| 2200 | |
| 2201 | unlock: |
| 2202 | rcu_read_unlock(); |
| 2203 | return count; |
| 2204 | } |
| 2205 | EXPORT_SYMBOL_GPL(br_multicast_list_adjacent); |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 2206 | |
| 2207 | /** |
Linus Lüssing | c34963e | 2014-07-07 05:41:17 +0200 | [diff] [blame] | 2208 | * br_multicast_has_querier_anywhere - Checks for a querier on a bridge |
| 2209 | * @dev: The bridge port providing the bridge on which to check for a querier |
| 2210 | * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 |
| 2211 | * |
| 2212 | * Checks whether the given interface has a bridge on top and if so returns |
| 2213 | * true if a valid querier exists anywhere on the bridged link layer. |
| 2214 | * Otherwise returns false. |
| 2215 | */ |
| 2216 | bool br_multicast_has_querier_anywhere(struct net_device *dev, int proto) |
| 2217 | { |
| 2218 | struct net_bridge *br; |
| 2219 | struct net_bridge_port *port; |
| 2220 | struct ethhdr eth; |
| 2221 | bool ret = false; |
| 2222 | |
| 2223 | rcu_read_lock(); |
Julian Wiedmann | 35f861e | 2019-03-29 14:38:19 +0100 | [diff] [blame] | 2224 | if (!netif_is_bridge_port(dev)) |
Linus Lüssing | c34963e | 2014-07-07 05:41:17 +0200 | [diff] [blame] | 2225 | goto unlock; |
| 2226 | |
| 2227 | port = br_port_get_rcu(dev); |
| 2228 | if (!port || !port->br) |
| 2229 | goto unlock; |
| 2230 | |
| 2231 | br = port->br; |
| 2232 | |
| 2233 | memset(ð, 0, sizeof(eth)); |
| 2234 | eth.h_proto = htons(proto); |
| 2235 | |
| 2236 | ret = br_multicast_querier_exists(br, ð); |
| 2237 | |
| 2238 | unlock: |
| 2239 | rcu_read_unlock(); |
| 2240 | return ret; |
| 2241 | } |
| 2242 | EXPORT_SYMBOL_GPL(br_multicast_has_querier_anywhere); |
| 2243 | |
| 2244 | /** |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 2245 | * br_multicast_has_querier_adjacent - Checks for a querier behind a bridge port |
| 2246 | * @dev: The bridge port adjacent to which to check for a querier |
| 2247 | * @proto: The protocol family to check for: IGMP -> ETH_P_IP, MLD -> ETH_P_IPV6 |
| 2248 | * |
| 2249 | * Checks whether the given interface has a bridge on top and if so returns |
| 2250 | * true if a selected querier is behind one of the other ports of this |
| 2251 | * bridge. Otherwise returns false. |
| 2252 | */ |
| 2253 | bool br_multicast_has_querier_adjacent(struct net_device *dev, int proto) |
| 2254 | { |
| 2255 | struct net_bridge *br; |
| 2256 | struct net_bridge_port *port; |
| 2257 | bool ret = false; |
| 2258 | |
| 2259 | rcu_read_lock(); |
Julian Wiedmann | 35f861e | 2019-03-29 14:38:19 +0100 | [diff] [blame] | 2260 | if (!netif_is_bridge_port(dev)) |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 2261 | goto unlock; |
| 2262 | |
| 2263 | port = br_port_get_rcu(dev); |
| 2264 | if (!port || !port->br) |
| 2265 | goto unlock; |
| 2266 | |
| 2267 | br = port->br; |
| 2268 | |
| 2269 | switch (proto) { |
| 2270 | case ETH_P_IP: |
| 2271 | if (!timer_pending(&br->ip4_other_query.timer) || |
| 2272 | rcu_dereference(br->ip4_querier.port) == port) |
| 2273 | goto unlock; |
| 2274 | break; |
Linus Lüssing | 3993c4e | 2014-06-12 01:41:24 +0200 | [diff] [blame] | 2275 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 2276 | case ETH_P_IPV6: |
| 2277 | if (!timer_pending(&br->ip6_other_query.timer) || |
| 2278 | rcu_dereference(br->ip6_querier.port) == port) |
| 2279 | goto unlock; |
| 2280 | break; |
Linus Lüssing | 3993c4e | 2014-06-12 01:41:24 +0200 | [diff] [blame] | 2281 | #endif |
Linus Lüssing | 2cd4143 | 2014-06-07 18:26:29 +0200 | [diff] [blame] | 2282 | default: |
| 2283 | goto unlock; |
| 2284 | } |
| 2285 | |
| 2286 | ret = true; |
| 2287 | unlock: |
| 2288 | rcu_read_unlock(); |
| 2289 | return ret; |
| 2290 | } |
| 2291 | EXPORT_SYMBOL_GPL(br_multicast_has_querier_adjacent); |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2292 | |
| 2293 | static void br_mcast_stats_add(struct bridge_mcast_stats __percpu *stats, |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 2294 | const struct sk_buff *skb, u8 type, u8 dir) |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2295 | { |
| 2296 | struct bridge_mcast_stats *pstats = this_cpu_ptr(stats); |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 2297 | __be16 proto = skb->protocol; |
| 2298 | unsigned int t_len; |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2299 | |
| 2300 | u64_stats_update_begin(&pstats->syncp); |
| 2301 | switch (proto) { |
| 2302 | case htons(ETH_P_IP): |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 2303 | t_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb); |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2304 | switch (type) { |
| 2305 | case IGMP_HOST_MEMBERSHIP_REPORT: |
| 2306 | pstats->mstats.igmp_v1reports[dir]++; |
| 2307 | break; |
| 2308 | case IGMPV2_HOST_MEMBERSHIP_REPORT: |
| 2309 | pstats->mstats.igmp_v2reports[dir]++; |
| 2310 | break; |
| 2311 | case IGMPV3_HOST_MEMBERSHIP_REPORT: |
| 2312 | pstats->mstats.igmp_v3reports[dir]++; |
| 2313 | break; |
| 2314 | case IGMP_HOST_MEMBERSHIP_QUERY: |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 2315 | if (t_len != sizeof(struct igmphdr)) { |
| 2316 | pstats->mstats.igmp_v3queries[dir]++; |
| 2317 | } else { |
| 2318 | unsigned int offset = skb_transport_offset(skb); |
| 2319 | struct igmphdr *ih, _ihdr; |
| 2320 | |
| 2321 | ih = skb_header_pointer(skb, offset, |
| 2322 | sizeof(_ihdr), &_ihdr); |
| 2323 | if (!ih) |
| 2324 | break; |
| 2325 | if (!ih->code) |
| 2326 | pstats->mstats.igmp_v1queries[dir]++; |
| 2327 | else |
| 2328 | pstats->mstats.igmp_v2queries[dir]++; |
| 2329 | } |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2330 | break; |
| 2331 | case IGMP_HOST_LEAVE_MESSAGE: |
| 2332 | pstats->mstats.igmp_leaves[dir]++; |
| 2333 | break; |
| 2334 | } |
| 2335 | break; |
| 2336 | #if IS_ENABLED(CONFIG_IPV6) |
| 2337 | case htons(ETH_P_IPV6): |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 2338 | t_len = ntohs(ipv6_hdr(skb)->payload_len) + |
| 2339 | sizeof(struct ipv6hdr); |
| 2340 | t_len -= skb_network_header_len(skb); |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2341 | switch (type) { |
| 2342 | case ICMPV6_MGM_REPORT: |
| 2343 | pstats->mstats.mld_v1reports[dir]++; |
| 2344 | break; |
| 2345 | case ICMPV6_MLD2_REPORT: |
| 2346 | pstats->mstats.mld_v2reports[dir]++; |
| 2347 | break; |
| 2348 | case ICMPV6_MGM_QUERY: |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 2349 | if (t_len != sizeof(struct mld_msg)) |
| 2350 | pstats->mstats.mld_v2queries[dir]++; |
| 2351 | else |
| 2352 | pstats->mstats.mld_v1queries[dir]++; |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2353 | break; |
| 2354 | case ICMPV6_MGM_REDUCTION: |
| 2355 | pstats->mstats.mld_leaves[dir]++; |
| 2356 | break; |
| 2357 | } |
| 2358 | break; |
| 2359 | #endif /* CONFIG_IPV6 */ |
| 2360 | } |
| 2361 | u64_stats_update_end(&pstats->syncp); |
| 2362 | } |
| 2363 | |
| 2364 | void br_multicast_count(struct net_bridge *br, const struct net_bridge_port *p, |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 2365 | const struct sk_buff *skb, u8 type, u8 dir) |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2366 | { |
| 2367 | struct bridge_mcast_stats __percpu *stats; |
| 2368 | |
| 2369 | /* if multicast_disabled is true then igmp type can't be set */ |
Nikolay Aleksandrov | 675779a | 2018-09-26 17:01:04 +0300 | [diff] [blame] | 2370 | if (!type || !br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2371 | return; |
| 2372 | |
| 2373 | if (p) |
| 2374 | stats = p->mcast_stats; |
| 2375 | else |
| 2376 | stats = br->mcast_stats; |
| 2377 | if (WARN_ON(!stats)) |
| 2378 | return; |
| 2379 | |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 2380 | br_mcast_stats_add(stats, skb, type, dir); |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2381 | } |
| 2382 | |
| 2383 | int br_multicast_init_stats(struct net_bridge *br) |
| 2384 | { |
| 2385 | br->mcast_stats = netdev_alloc_pcpu_stats(struct bridge_mcast_stats); |
| 2386 | if (!br->mcast_stats) |
| 2387 | return -ENOMEM; |
| 2388 | |
| 2389 | return 0; |
| 2390 | } |
| 2391 | |
Ido Schimmel | b6fe044 | 2017-04-10 14:59:27 +0300 | [diff] [blame] | 2392 | void br_multicast_uninit_stats(struct net_bridge *br) |
| 2393 | { |
| 2394 | free_percpu(br->mcast_stats); |
| 2395 | } |
| 2396 | |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2397 | static void mcast_stats_add_dir(u64 *dst, u64 *src) |
| 2398 | { |
| 2399 | dst[BR_MCAST_DIR_RX] += src[BR_MCAST_DIR_RX]; |
| 2400 | dst[BR_MCAST_DIR_TX] += src[BR_MCAST_DIR_TX]; |
| 2401 | } |
| 2402 | |
| 2403 | void br_multicast_get_stats(const struct net_bridge *br, |
| 2404 | const struct net_bridge_port *p, |
| 2405 | struct br_mcast_stats *dest) |
| 2406 | { |
| 2407 | struct bridge_mcast_stats __percpu *stats; |
| 2408 | struct br_mcast_stats tdst; |
| 2409 | int i; |
| 2410 | |
| 2411 | memset(dest, 0, sizeof(*dest)); |
| 2412 | if (p) |
| 2413 | stats = p->mcast_stats; |
| 2414 | else |
| 2415 | stats = br->mcast_stats; |
| 2416 | if (WARN_ON(!stats)) |
| 2417 | return; |
| 2418 | |
| 2419 | memset(&tdst, 0, sizeof(tdst)); |
| 2420 | for_each_possible_cpu(i) { |
| 2421 | struct bridge_mcast_stats *cpu_stats = per_cpu_ptr(stats, i); |
| 2422 | struct br_mcast_stats temp; |
| 2423 | unsigned int start; |
| 2424 | |
| 2425 | do { |
| 2426 | start = u64_stats_fetch_begin_irq(&cpu_stats->syncp); |
| 2427 | memcpy(&temp, &cpu_stats->mstats, sizeof(temp)); |
| 2428 | } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start)); |
| 2429 | |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 2430 | mcast_stats_add_dir(tdst.igmp_v1queries, temp.igmp_v1queries); |
| 2431 | mcast_stats_add_dir(tdst.igmp_v2queries, temp.igmp_v2queries); |
| 2432 | mcast_stats_add_dir(tdst.igmp_v3queries, temp.igmp_v3queries); |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2433 | mcast_stats_add_dir(tdst.igmp_leaves, temp.igmp_leaves); |
| 2434 | mcast_stats_add_dir(tdst.igmp_v1reports, temp.igmp_v1reports); |
| 2435 | mcast_stats_add_dir(tdst.igmp_v2reports, temp.igmp_v2reports); |
| 2436 | mcast_stats_add_dir(tdst.igmp_v3reports, temp.igmp_v3reports); |
| 2437 | tdst.igmp_parse_errors += temp.igmp_parse_errors; |
| 2438 | |
Nikolay Aleksandrov | a65056e | 2016-07-06 12:12:21 -0700 | [diff] [blame] | 2439 | mcast_stats_add_dir(tdst.mld_v1queries, temp.mld_v1queries); |
| 2440 | mcast_stats_add_dir(tdst.mld_v2queries, temp.mld_v2queries); |
Nikolay Aleksandrov | 1080ab9 | 2016-06-28 16:57:06 +0200 | [diff] [blame] | 2441 | mcast_stats_add_dir(tdst.mld_leaves, temp.mld_leaves); |
| 2442 | mcast_stats_add_dir(tdst.mld_v1reports, temp.mld_v1reports); |
| 2443 | mcast_stats_add_dir(tdst.mld_v2reports, temp.mld_v2reports); |
| 2444 | tdst.mld_parse_errors += temp.mld_parse_errors; |
| 2445 | } |
| 2446 | memcpy(dest, &tdst, sizeof(*dest)); |
| 2447 | } |
Nikolay Aleksandrov | 19e3a9c | 2018-12-05 15:14:24 +0200 | [diff] [blame] | 2448 | |
| 2449 | int br_mdb_hash_init(struct net_bridge *br) |
| 2450 | { |
| 2451 | return rhashtable_init(&br->mdb_hash_tbl, &br_mdb_rht_params); |
| 2452 | } |
| 2453 | |
| 2454 | void br_mdb_hash_fini(struct net_bridge *br) |
| 2455 | { |
| 2456 | rhashtable_destroy(&br->mdb_hash_tbl); |
| 2457 | } |