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