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