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