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> |
| 26 | #include <net/ip.h> |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 27 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 28 | #include <net/ipv6.h> |
| 29 | #include <net/mld.h> |
| 30 | #include <net/addrconf.h> |
David S. Miller | d4c4f07 | 2010-04-27 10:16:54 -0700 | [diff] [blame] | 31 | #include <net/ip6_checksum.h> |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 32 | #endif |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 33 | |
| 34 | #include "br_private.h" |
| 35 | |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 36 | #define mlock_dereference(X, br) \ |
| 37 | rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock)) |
| 38 | |
Herbert Xu | c83b8fa | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 39 | static void br_multicast_start_querier(struct net_bridge *br); |
| 40 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 41 | #if IS_ENABLED(CONFIG_IPV6) |
Linus Lüssing | e4de9f9 | 2011-02-15 13:19:21 +0000 | [diff] [blame] | 42 | static inline int ipv6_is_transient_multicast(const struct in6_addr *addr) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 43 | { |
Linus Lüssing | e4de9f9 | 2011-02-15 13:19:21 +0000 | [diff] [blame] | 44 | if (ipv6_addr_is_multicast(addr) && IPV6_ADDR_MC_FLAG_TRANSIENT(addr)) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 45 | return 1; |
| 46 | return 0; |
| 47 | } |
| 48 | #endif |
| 49 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 50 | static inline int br_ip_equal(const struct br_ip *a, const struct br_ip *b) |
| 51 | { |
| 52 | if (a->proto != b->proto) |
| 53 | return 0; |
| 54 | switch (a->proto) { |
| 55 | case htons(ETH_P_IP): |
| 56 | return a->u.ip4 == b->u.ip4; |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 57 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 58 | case htons(ETH_P_IPV6): |
| 59 | return ipv6_addr_equal(&a->u.ip6, &b->u.ip6); |
| 60 | #endif |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 61 | } |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static inline int __br_ip4_hash(struct net_bridge_mdb_htable *mdb, __be32 ip) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 66 | { |
Eric Dumazet | 0eae88f | 2010-04-20 19:06:52 -0700 | [diff] [blame] | 67 | return jhash_1word(mdb->secret, (__force u32)ip) & (mdb->max - 1); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 70 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 71 | static inline int __br_ip6_hash(struct net_bridge_mdb_htable *mdb, |
| 72 | const struct in6_addr *ip) |
| 73 | { |
| 74 | return jhash2((__force u32 *)ip->s6_addr32, 4, mdb->secret) & (mdb->max - 1); |
| 75 | } |
| 76 | #endif |
| 77 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 78 | static inline int br_ip_hash(struct net_bridge_mdb_htable *mdb, |
| 79 | struct br_ip *ip) |
| 80 | { |
| 81 | switch (ip->proto) { |
| 82 | case htons(ETH_P_IP): |
| 83 | return __br_ip4_hash(mdb, ip->u.ip4); |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 84 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 85 | case htons(ETH_P_IPV6): |
| 86 | return __br_ip6_hash(mdb, &ip->u.ip6); |
| 87 | #endif |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 88 | } |
| 89 | return 0; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | static struct net_bridge_mdb_entry *__br_mdb_ip_get( |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 93 | struct net_bridge_mdb_htable *mdb, struct br_ip *dst, int hash) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 94 | { |
| 95 | struct net_bridge_mdb_entry *mp; |
| 96 | struct hlist_node *p; |
| 97 | |
Herbert Xu | 49f5fcf | 2010-03-05 21:07:39 +0000 | [diff] [blame] | 98 | hlist_for_each_entry_rcu(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) { |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 99 | if (br_ip_equal(&mp->addr, dst)) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 100 | return mp; |
| 101 | } |
| 102 | |
| 103 | return NULL; |
| 104 | } |
| 105 | |
Herbert Xu | 7f285fa | 2010-07-05 14:50:08 +0000 | [diff] [blame] | 106 | static struct net_bridge_mdb_entry *br_mdb_ip_get( |
| 107 | struct net_bridge_mdb_htable *mdb, struct br_ip *dst) |
| 108 | { |
| 109 | if (!mdb) |
| 110 | return NULL; |
| 111 | |
| 112 | return __br_mdb_ip_get(mdb, dst, br_ip_hash(mdb, dst)); |
| 113 | } |
| 114 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 115 | static struct net_bridge_mdb_entry *br_mdb_ip4_get( |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 116 | struct net_bridge_mdb_htable *mdb, __be32 dst) |
| 117 | { |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 118 | struct br_ip br_dst; |
Herbert Xu | 0821ec5 | 2010-03-15 20:38:25 -0700 | [diff] [blame] | 119 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 120 | br_dst.u.ip4 = dst; |
| 121 | br_dst.proto = htons(ETH_P_IP); |
| 122 | |
Herbert Xu | 7f285fa | 2010-07-05 14:50:08 +0000 | [diff] [blame] | 123 | return br_mdb_ip_get(mdb, &br_dst); |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 124 | } |
| 125 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 126 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 127 | static struct net_bridge_mdb_entry *br_mdb_ip6_get( |
| 128 | struct net_bridge_mdb_htable *mdb, const struct in6_addr *dst) |
| 129 | { |
| 130 | struct br_ip br_dst; |
| 131 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 132 | br_dst.u.ip6 = *dst; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 133 | br_dst.proto = htons(ETH_P_IPV6); |
| 134 | |
Herbert Xu | 7f285fa | 2010-07-05 14:50:08 +0000 | [diff] [blame] | 135 | return br_mdb_ip_get(mdb, &br_dst); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 136 | } |
| 137 | #endif |
| 138 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 139 | struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br, |
| 140 | struct sk_buff *skb) |
| 141 | { |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 142 | struct net_bridge_mdb_htable *mdb = rcu_dereference(br->mdb); |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 143 | struct br_ip ip; |
| 144 | |
Herbert Xu | 7f285fa | 2010-07-05 14:50:08 +0000 | [diff] [blame] | 145 | if (br->multicast_disabled) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 146 | return NULL; |
| 147 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 148 | if (BR_INPUT_SKB_CB(skb)->igmp) |
| 149 | return NULL; |
| 150 | |
| 151 | ip.proto = skb->protocol; |
| 152 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 153 | switch (skb->protocol) { |
| 154 | case htons(ETH_P_IP): |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 155 | ip.u.ip4 = ip_hdr(skb)->daddr; |
| 156 | break; |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 157 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 158 | case htons(ETH_P_IPV6): |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 159 | ip.u.ip6 = ipv6_hdr(skb)->daddr; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 160 | break; |
| 161 | #endif |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 162 | default: |
| 163 | return NULL; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 164 | } |
| 165 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 166 | return br_mdb_ip_get(mdb, &ip); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static void br_mdb_free(struct rcu_head *head) |
| 170 | { |
| 171 | struct net_bridge_mdb_htable *mdb = |
| 172 | container_of(head, struct net_bridge_mdb_htable, rcu); |
| 173 | struct net_bridge_mdb_htable *old = mdb->old; |
| 174 | |
| 175 | mdb->old = NULL; |
| 176 | kfree(old->mhash); |
| 177 | kfree(old); |
| 178 | } |
| 179 | |
| 180 | static int br_mdb_copy(struct net_bridge_mdb_htable *new, |
| 181 | struct net_bridge_mdb_htable *old, |
| 182 | int elasticity) |
| 183 | { |
| 184 | struct net_bridge_mdb_entry *mp; |
| 185 | struct hlist_node *p; |
| 186 | int maxlen; |
| 187 | int len; |
| 188 | int i; |
| 189 | |
| 190 | for (i = 0; i < old->max; i++) |
| 191 | hlist_for_each_entry(mp, p, &old->mhash[i], hlist[old->ver]) |
| 192 | hlist_add_head(&mp->hlist[new->ver], |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 193 | &new->mhash[br_ip_hash(new, &mp->addr)]); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 194 | |
| 195 | if (!elasticity) |
| 196 | return 0; |
| 197 | |
| 198 | maxlen = 0; |
| 199 | for (i = 0; i < new->max; i++) { |
| 200 | len = 0; |
| 201 | hlist_for_each_entry(mp, p, &new->mhash[i], hlist[new->ver]) |
| 202 | len++; |
| 203 | if (len > maxlen) |
| 204 | maxlen = len; |
| 205 | } |
| 206 | |
| 207 | return maxlen > elasticity ? -EINVAL : 0; |
| 208 | } |
| 209 | |
| 210 | static void br_multicast_free_pg(struct rcu_head *head) |
| 211 | { |
| 212 | struct net_bridge_port_group *p = |
| 213 | container_of(head, struct net_bridge_port_group, rcu); |
| 214 | |
| 215 | kfree(p); |
| 216 | } |
| 217 | |
| 218 | static void br_multicast_free_group(struct rcu_head *head) |
| 219 | { |
| 220 | struct net_bridge_mdb_entry *mp = |
| 221 | container_of(head, struct net_bridge_mdb_entry, rcu); |
| 222 | |
| 223 | kfree(mp); |
| 224 | } |
| 225 | |
| 226 | static void br_multicast_group_expired(unsigned long data) |
| 227 | { |
| 228 | struct net_bridge_mdb_entry *mp = (void *)data; |
| 229 | struct net_bridge *br = mp->br; |
| 230 | struct net_bridge_mdb_htable *mdb; |
| 231 | |
| 232 | spin_lock(&br->multicast_lock); |
| 233 | if (!netif_running(br->dev) || timer_pending(&mp->timer)) |
| 234 | goto out; |
| 235 | |
Herbert Xu | 8a87017 | 2011-02-12 01:05:42 -0800 | [diff] [blame] | 236 | mp->mglist = false; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 237 | |
| 238 | if (mp->ports) |
| 239 | goto out; |
| 240 | |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 241 | mdb = mlock_dereference(br->mdb, br); |
| 242 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 243 | hlist_del_rcu(&mp->hlist[mdb->ver]); |
| 244 | mdb->size--; |
| 245 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 246 | call_rcu_bh(&mp->rcu, br_multicast_free_group); |
| 247 | |
| 248 | out: |
| 249 | spin_unlock(&br->multicast_lock); |
| 250 | } |
| 251 | |
| 252 | static void br_multicast_del_pg(struct net_bridge *br, |
| 253 | struct net_bridge_port_group *pg) |
| 254 | { |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 255 | struct net_bridge_mdb_htable *mdb; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 256 | struct net_bridge_mdb_entry *mp; |
| 257 | struct net_bridge_port_group *p; |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 258 | struct net_bridge_port_group __rcu **pp; |
| 259 | |
| 260 | mdb = mlock_dereference(br->mdb, br); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 261 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 262 | mp = br_mdb_ip_get(mdb, &pg->addr); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 263 | if (WARN_ON(!mp)) |
| 264 | return; |
| 265 | |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 266 | for (pp = &mp->ports; |
| 267 | (p = mlock_dereference(*pp, br)) != NULL; |
| 268 | pp = &p->next) { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 269 | if (p != pg) |
| 270 | continue; |
| 271 | |
stephen hemminger | 83f6a74 | 2010-04-27 15:01:06 +0000 | [diff] [blame] | 272 | rcu_assign_pointer(*pp, p->next); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 273 | hlist_del_init(&p->mglist); |
| 274 | del_timer(&p->timer); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 275 | call_rcu_bh(&p->rcu, br_multicast_free_pg); |
| 276 | |
Herbert Xu | 8a87017 | 2011-02-12 01:05:42 -0800 | [diff] [blame] | 277 | if (!mp->ports && !mp->mglist && |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 278 | netif_running(br->dev)) |
| 279 | mod_timer(&mp->timer, jiffies); |
| 280 | |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | WARN_ON(1); |
| 285 | } |
| 286 | |
| 287 | static void br_multicast_port_group_expired(unsigned long data) |
| 288 | { |
| 289 | struct net_bridge_port_group *pg = (void *)data; |
| 290 | struct net_bridge *br = pg->port->br; |
| 291 | |
| 292 | spin_lock(&br->multicast_lock); |
| 293 | if (!netif_running(br->dev) || timer_pending(&pg->timer) || |
| 294 | hlist_unhashed(&pg->mglist)) |
| 295 | goto out; |
| 296 | |
| 297 | br_multicast_del_pg(br, pg); |
| 298 | |
| 299 | out: |
| 300 | spin_unlock(&br->multicast_lock); |
| 301 | } |
| 302 | |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 303 | 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] | 304 | int elasticity) |
| 305 | { |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 306 | struct net_bridge_mdb_htable *old = rcu_dereference_protected(*mdbp, 1); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 307 | struct net_bridge_mdb_htable *mdb; |
| 308 | int err; |
| 309 | |
| 310 | mdb = kmalloc(sizeof(*mdb), GFP_ATOMIC); |
| 311 | if (!mdb) |
| 312 | return -ENOMEM; |
| 313 | |
| 314 | mdb->max = max; |
| 315 | mdb->old = old; |
| 316 | |
| 317 | mdb->mhash = kzalloc(max * sizeof(*mdb->mhash), GFP_ATOMIC); |
| 318 | if (!mdb->mhash) { |
| 319 | kfree(mdb); |
| 320 | return -ENOMEM; |
| 321 | } |
| 322 | |
| 323 | mdb->size = old ? old->size : 0; |
| 324 | mdb->ver = old ? old->ver ^ 1 : 0; |
| 325 | |
| 326 | if (!old || elasticity) |
| 327 | get_random_bytes(&mdb->secret, sizeof(mdb->secret)); |
| 328 | else |
| 329 | mdb->secret = old->secret; |
| 330 | |
| 331 | if (!old) |
| 332 | goto out; |
| 333 | |
| 334 | err = br_mdb_copy(mdb, old, elasticity); |
| 335 | if (err) { |
| 336 | kfree(mdb->mhash); |
| 337 | kfree(mdb); |
| 338 | return err; |
| 339 | } |
| 340 | |
| 341 | call_rcu_bh(&mdb->rcu, br_mdb_free); |
| 342 | |
| 343 | out: |
| 344 | rcu_assign_pointer(*mdbp, mdb); |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 349 | static struct sk_buff *br_ip4_multicast_alloc_query(struct net_bridge *br, |
| 350 | __be32 group) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 351 | { |
| 352 | struct sk_buff *skb; |
| 353 | struct igmphdr *ih; |
| 354 | struct ethhdr *eth; |
| 355 | struct iphdr *iph; |
| 356 | |
| 357 | skb = netdev_alloc_skb_ip_align(br->dev, sizeof(*eth) + sizeof(*iph) + |
| 358 | sizeof(*ih) + 4); |
| 359 | if (!skb) |
| 360 | goto out; |
| 361 | |
| 362 | skb->protocol = htons(ETH_P_IP); |
| 363 | |
| 364 | skb_reset_mac_header(skb); |
| 365 | eth = eth_hdr(skb); |
| 366 | |
| 367 | memcpy(eth->h_source, br->dev->dev_addr, 6); |
| 368 | eth->h_dest[0] = 1; |
| 369 | eth->h_dest[1] = 0; |
| 370 | eth->h_dest[2] = 0x5e; |
| 371 | eth->h_dest[3] = 0; |
| 372 | eth->h_dest[4] = 0; |
| 373 | eth->h_dest[5] = 1; |
| 374 | eth->h_proto = htons(ETH_P_IP); |
| 375 | skb_put(skb, sizeof(*eth)); |
| 376 | |
| 377 | skb_set_network_header(skb, skb->len); |
| 378 | iph = ip_hdr(skb); |
| 379 | |
| 380 | iph->version = 4; |
| 381 | iph->ihl = 6; |
| 382 | iph->tos = 0xc0; |
| 383 | iph->tot_len = htons(sizeof(*iph) + sizeof(*ih) + 4); |
| 384 | iph->id = 0; |
| 385 | iph->frag_off = htons(IP_DF); |
| 386 | iph->ttl = 1; |
| 387 | iph->protocol = IPPROTO_IGMP; |
| 388 | iph->saddr = 0; |
| 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 | |
| 436 | memcpy(eth->h_source, br->dev->dev_addr, 6); |
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 | |
| 472 | interval = ipv6_addr_any(group) ? br->multicast_last_member_interval : |
| 473 | br->multicast_query_response_interval; |
| 474 | |
| 475 | mldq->mld_type = ICMPV6_MGM_QUERY; |
| 476 | mldq->mld_code = 0; |
| 477 | mldq->mld_cksum = 0; |
| 478 | mldq->mld_maxdelay = htons((u16)jiffies_to_msecs(interval)); |
| 479 | mldq->mld_reserved = 0; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 480 | mldq->mld_mca = *group; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 481 | |
| 482 | /* checksum */ |
| 483 | mldq->mld_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, |
| 484 | sizeof(*mldq), IPPROTO_ICMPV6, |
| 485 | csum_partial(mldq, |
| 486 | sizeof(*mldq), 0)); |
| 487 | skb_put(skb, sizeof(*mldq)); |
| 488 | |
| 489 | __skb_pull(skb, sizeof(*eth)); |
| 490 | |
| 491 | out: |
| 492 | return skb; |
| 493 | } |
| 494 | #endif |
| 495 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 496 | static struct sk_buff *br_multicast_alloc_query(struct net_bridge *br, |
| 497 | struct br_ip *addr) |
| 498 | { |
| 499 | switch (addr->proto) { |
| 500 | case htons(ETH_P_IP): |
| 501 | return br_ip4_multicast_alloc_query(br, addr->u.ip4); |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 502 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 503 | case htons(ETH_P_IPV6): |
| 504 | return br_ip6_multicast_alloc_query(br, &addr->u.ip6); |
| 505 | #endif |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 506 | } |
| 507 | return NULL; |
| 508 | } |
| 509 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 510 | static struct net_bridge_mdb_entry *br_multicast_get_group( |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 511 | struct net_bridge *br, struct net_bridge_port *port, |
| 512 | struct br_ip *group, int hash) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 513 | { |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 514 | struct net_bridge_mdb_htable *mdb; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 515 | struct net_bridge_mdb_entry *mp; |
| 516 | struct hlist_node *p; |
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); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 523 | hlist_for_each_entry(mp, p, &mdb->mhash[hash], hlist[mdb->ver]) { |
| 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; |
| 543 | if (unlikely(max >= br->hash_max)) { |
stephen hemminger | 28a16c9 | 2010-05-10 09:31:09 +0000 | [diff] [blame] | 544 | br_warn(br, "Multicast hash table maximum " |
| 545 | "reached, disabling snooping: %s, %d\n", |
| 546 | port ? port->dev->name : br->dev->name, max); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 547 | err = -E2BIG; |
| 548 | disable: |
| 549 | br->multicast_disabled = 1; |
| 550 | goto err; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | if (max > mdb->max || elasticity) { |
| 555 | if (mdb->old) { |
| 556 | if (net_ratelimit()) |
stephen hemminger | 28a16c9 | 2010-05-10 09:31:09 +0000 | [diff] [blame] | 557 | br_info(br, "Multicast hash table " |
| 558 | "on fire: %s\n", |
| 559 | port ? port->dev->name : br->dev->name); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 560 | err = -EEXIST; |
| 561 | goto err; |
| 562 | } |
| 563 | |
| 564 | err = br_mdb_rehash(&br->mdb, max, elasticity); |
| 565 | if (err) { |
stephen hemminger | 28a16c9 | 2010-05-10 09:31:09 +0000 | [diff] [blame] | 566 | br_warn(br, "Cannot rehash multicast " |
| 567 | "hash table, disabling snooping: %s, %d, %d\n", |
| 568 | port ? port->dev->name : br->dev->name, |
| 569 | mdb->size, err); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 570 | goto disable; |
| 571 | } |
| 572 | |
| 573 | err = -EAGAIN; |
| 574 | goto err; |
| 575 | } |
| 576 | |
| 577 | return NULL; |
| 578 | |
| 579 | err: |
| 580 | mp = ERR_PTR(err); |
| 581 | return mp; |
| 582 | } |
| 583 | |
| 584 | static struct net_bridge_mdb_entry *br_multicast_new_group( |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 585 | struct net_bridge *br, struct net_bridge_port *port, |
| 586 | 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; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 623 | setup_timer(&mp->timer, br_multicast_group_expired, |
| 624 | (unsigned long)mp); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 625 | |
| 626 | hlist_add_head_rcu(&mp->hlist[mdb->ver], &mdb->mhash[hash]); |
| 627 | mdb->size++; |
| 628 | |
| 629 | out: |
| 630 | return mp; |
| 631 | } |
| 632 | |
| 633 | static int br_multicast_add_group(struct net_bridge *br, |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 634 | struct net_bridge_port *port, |
| 635 | struct br_ip *group) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 636 | { |
| 637 | struct net_bridge_mdb_entry *mp; |
| 638 | struct net_bridge_port_group *p; |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 639 | struct net_bridge_port_group __rcu **pp; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 640 | unsigned long now = jiffies; |
| 641 | int err; |
| 642 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 643 | spin_lock(&br->multicast_lock); |
| 644 | if (!netif_running(br->dev) || |
| 645 | (port && port->state == BR_STATE_DISABLED)) |
| 646 | goto out; |
| 647 | |
| 648 | mp = br_multicast_new_group(br, port, group); |
| 649 | err = PTR_ERR(mp); |
Tobias Klauser | 4c0833b | 2010-12-10 03:18:04 +0000 | [diff] [blame] | 650 | if (IS_ERR(mp)) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 651 | goto err; |
| 652 | |
| 653 | if (!port) { |
Herbert Xu | 8a87017 | 2011-02-12 01:05:42 -0800 | [diff] [blame] | 654 | mp->mglist = true; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 655 | mod_timer(&mp->timer, now + br->multicast_membership_interval); |
| 656 | goto out; |
| 657 | } |
| 658 | |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 659 | for (pp = &mp->ports; |
| 660 | (p = mlock_dereference(*pp, br)) != NULL; |
| 661 | pp = &p->next) { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 662 | if (p->port == port) |
| 663 | goto found; |
| 664 | if ((unsigned long)p->port < (unsigned long)port) |
| 665 | break; |
| 666 | } |
| 667 | |
| 668 | p = kzalloc(sizeof(*p), GFP_ATOMIC); |
| 669 | err = -ENOMEM; |
| 670 | if (unlikely(!p)) |
| 671 | goto err; |
| 672 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 673 | p->addr = *group; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 674 | p->port = port; |
| 675 | p->next = *pp; |
| 676 | hlist_add_head(&p->mglist, &port->mglist); |
| 677 | setup_timer(&p->timer, br_multicast_port_group_expired, |
| 678 | (unsigned long)p); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 679 | |
| 680 | rcu_assign_pointer(*pp, p); |
| 681 | |
| 682 | found: |
| 683 | mod_timer(&p->timer, now + br->multicast_membership_interval); |
| 684 | out: |
| 685 | err = 0; |
| 686 | |
| 687 | err: |
| 688 | spin_unlock(&br->multicast_lock); |
| 689 | return err; |
| 690 | } |
| 691 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 692 | static int br_ip4_multicast_add_group(struct net_bridge *br, |
| 693 | struct net_bridge_port *port, |
| 694 | __be32 group) |
| 695 | { |
| 696 | struct br_ip br_group; |
| 697 | |
| 698 | if (ipv4_is_local_multicast(group)) |
| 699 | return 0; |
| 700 | |
| 701 | br_group.u.ip4 = group; |
| 702 | br_group.proto = htons(ETH_P_IP); |
| 703 | |
| 704 | return br_multicast_add_group(br, port, &br_group); |
| 705 | } |
| 706 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 707 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 708 | static int br_ip6_multicast_add_group(struct net_bridge *br, |
| 709 | struct net_bridge_port *port, |
| 710 | const struct in6_addr *group) |
| 711 | { |
| 712 | struct br_ip br_group; |
| 713 | |
Linus Lüssing | e4de9f9 | 2011-02-15 13:19:21 +0000 | [diff] [blame] | 714 | if (!ipv6_is_transient_multicast(group)) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 715 | return 0; |
| 716 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 717 | br_group.u.ip6 = *group; |
Linus Lüssing | 9cc6e0c | 2011-02-15 13:19:17 +0000 | [diff] [blame] | 718 | br_group.proto = htons(ETH_P_IPV6); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 719 | |
| 720 | return br_multicast_add_group(br, port, &br_group); |
| 721 | } |
| 722 | #endif |
| 723 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 724 | static void br_multicast_router_expired(unsigned long data) |
| 725 | { |
| 726 | struct net_bridge_port *port = (void *)data; |
| 727 | struct net_bridge *br = port->br; |
| 728 | |
| 729 | spin_lock(&br->multicast_lock); |
| 730 | if (port->multicast_router != 1 || |
| 731 | timer_pending(&port->multicast_router_timer) || |
| 732 | hlist_unhashed(&port->rlist)) |
| 733 | goto out; |
| 734 | |
| 735 | hlist_del_init_rcu(&port->rlist); |
| 736 | |
| 737 | out: |
| 738 | spin_unlock(&br->multicast_lock); |
| 739 | } |
| 740 | |
| 741 | static void br_multicast_local_router_expired(unsigned long data) |
| 742 | { |
| 743 | } |
| 744 | |
Herbert Xu | c83b8fa | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 745 | static void br_multicast_querier_expired(unsigned long data) |
| 746 | { |
Herbert Xu | bb63f1f | 2012-04-30 00:22:56 +0000 | [diff] [blame] | 747 | struct net_bridge *br = (void *)data; |
Herbert Xu | c83b8fa | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 748 | |
| 749 | spin_lock(&br->multicast_lock); |
| 750 | if (!netif_running(br->dev) || br->multicast_disabled) |
| 751 | goto out; |
| 752 | |
| 753 | br_multicast_start_querier(br); |
| 754 | |
| 755 | out: |
| 756 | spin_unlock(&br->multicast_lock); |
| 757 | } |
| 758 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 759 | static void __br_multicast_send_query(struct net_bridge *br, |
| 760 | struct net_bridge_port *port, |
| 761 | struct br_ip *ip) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 762 | { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 763 | struct sk_buff *skb; |
| 764 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 765 | skb = br_multicast_alloc_query(br, ip); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 766 | if (!skb) |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 767 | return; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 768 | |
| 769 | if (port) { |
| 770 | __skb_push(skb, sizeof(struct ethhdr)); |
| 771 | skb->dev = port->dev; |
Jan Engelhardt | 713aefa | 2010-03-23 04:07:21 +0100 | [diff] [blame] | 772 | NF_HOOK(NFPROTO_BRIDGE, NF_BR_LOCAL_OUT, skb, NULL, skb->dev, |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 773 | dev_queue_xmit); |
| 774 | } else |
| 775 | netif_rx(skb); |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 776 | } |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 777 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 778 | static void br_multicast_send_query(struct net_bridge *br, |
| 779 | struct net_bridge_port *port, u32 sent) |
| 780 | { |
| 781 | unsigned long time; |
| 782 | struct br_ip br_group; |
| 783 | |
| 784 | if (!netif_running(br->dev) || br->multicast_disabled || |
Herbert Xu | c5c2326 | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 785 | !br->multicast_querier || |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 786 | timer_pending(&br->multicast_querier_timer)) |
| 787 | return; |
| 788 | |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 789 | memset(&br_group.u, 0, sizeof(br_group.u)); |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 790 | |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 791 | br_group.proto = htons(ETH_P_IP); |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 792 | __br_multicast_send_query(br, port, &br_group); |
| 793 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 794 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 795 | br_group.proto = htons(ETH_P_IPV6); |
| 796 | __br_multicast_send_query(br, port, &br_group); |
| 797 | #endif |
| 798 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 799 | time = jiffies; |
| 800 | time += sent < br->multicast_startup_query_count ? |
| 801 | br->multicast_startup_query_interval : |
| 802 | br->multicast_query_interval; |
| 803 | mod_timer(port ? &port->multicast_query_timer : |
| 804 | &br->multicast_query_timer, time); |
| 805 | } |
| 806 | |
| 807 | static void br_multicast_port_query_expired(unsigned long data) |
| 808 | { |
| 809 | struct net_bridge_port *port = (void *)data; |
| 810 | struct net_bridge *br = port->br; |
| 811 | |
| 812 | spin_lock(&br->multicast_lock); |
Dan Carpenter | 02a780c | 2010-03-06 01:14:09 +0000 | [diff] [blame] | 813 | if (port->state == BR_STATE_DISABLED || |
| 814 | port->state == BR_STATE_BLOCKING) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 815 | goto out; |
| 816 | |
| 817 | if (port->multicast_startup_queries_sent < |
| 818 | br->multicast_startup_query_count) |
| 819 | port->multicast_startup_queries_sent++; |
| 820 | |
| 821 | br_multicast_send_query(port->br, port, |
| 822 | port->multicast_startup_queries_sent); |
| 823 | |
| 824 | out: |
| 825 | spin_unlock(&br->multicast_lock); |
| 826 | } |
| 827 | |
| 828 | void br_multicast_add_port(struct net_bridge_port *port) |
| 829 | { |
| 830 | port->multicast_router = 1; |
| 831 | |
| 832 | setup_timer(&port->multicast_router_timer, br_multicast_router_expired, |
| 833 | (unsigned long)port); |
| 834 | setup_timer(&port->multicast_query_timer, |
| 835 | br_multicast_port_query_expired, (unsigned long)port); |
| 836 | } |
| 837 | |
| 838 | void br_multicast_del_port(struct net_bridge_port *port) |
| 839 | { |
| 840 | del_timer_sync(&port->multicast_router_timer); |
| 841 | } |
| 842 | |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 843 | static void __br_multicast_enable_port(struct net_bridge_port *port) |
| 844 | { |
| 845 | port->multicast_startup_queries_sent = 0; |
| 846 | |
| 847 | if (try_to_del_timer_sync(&port->multicast_query_timer) >= 0 || |
| 848 | del_timer(&port->multicast_query_timer)) |
| 849 | mod_timer(&port->multicast_query_timer, jiffies); |
| 850 | } |
| 851 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 852 | void br_multicast_enable_port(struct net_bridge_port *port) |
| 853 | { |
| 854 | struct net_bridge *br = port->br; |
| 855 | |
| 856 | spin_lock(&br->multicast_lock); |
| 857 | if (br->multicast_disabled || !netif_running(br->dev)) |
| 858 | goto out; |
| 859 | |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 860 | __br_multicast_enable_port(port); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 861 | |
| 862 | out: |
| 863 | spin_unlock(&br->multicast_lock); |
| 864 | } |
| 865 | |
| 866 | void br_multicast_disable_port(struct net_bridge_port *port) |
| 867 | { |
| 868 | struct net_bridge *br = port->br; |
| 869 | struct net_bridge_port_group *pg; |
| 870 | struct hlist_node *p, *n; |
| 871 | |
| 872 | spin_lock(&br->multicast_lock); |
| 873 | hlist_for_each_entry_safe(pg, p, n, &port->mglist, mglist) |
| 874 | br_multicast_del_pg(br, pg); |
| 875 | |
| 876 | if (!hlist_unhashed(&port->rlist)) |
| 877 | hlist_del_init_rcu(&port->rlist); |
| 878 | del_timer(&port->multicast_router_timer); |
| 879 | del_timer(&port->multicast_query_timer); |
| 880 | spin_unlock(&br->multicast_lock); |
| 881 | } |
| 882 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 883 | static int br_ip4_multicast_igmp3_report(struct net_bridge *br, |
| 884 | struct net_bridge_port *port, |
| 885 | struct sk_buff *skb) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 886 | { |
| 887 | struct igmpv3_report *ih; |
| 888 | struct igmpv3_grec *grec; |
| 889 | int i; |
| 890 | int len; |
| 891 | int num; |
| 892 | int type; |
| 893 | int err = 0; |
| 894 | __be32 group; |
| 895 | |
| 896 | if (!pskb_may_pull(skb, sizeof(*ih))) |
| 897 | return -EINVAL; |
| 898 | |
| 899 | ih = igmpv3_report_hdr(skb); |
| 900 | num = ntohs(ih->ngrec); |
| 901 | len = sizeof(*ih); |
| 902 | |
| 903 | for (i = 0; i < num; i++) { |
| 904 | len += sizeof(*grec); |
| 905 | if (!pskb_may_pull(skb, len)) |
| 906 | return -EINVAL; |
| 907 | |
Herbert Xu | fd218cf | 2010-04-07 21:20:47 -0700 | [diff] [blame] | 908 | grec = (void *)(skb->data + len - sizeof(*grec)); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 909 | group = grec->grec_mca; |
| 910 | type = grec->grec_type; |
| 911 | |
Eric Dumazet | 8eabf95 | 2010-04-20 03:20:05 +0000 | [diff] [blame] | 912 | len += ntohs(grec->grec_nsrcs) * 4; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 913 | if (!pskb_may_pull(skb, len)) |
| 914 | return -EINVAL; |
| 915 | |
| 916 | /* We treat this as an IGMPv2 report for now. */ |
| 917 | switch (type) { |
| 918 | case IGMPV3_MODE_IS_INCLUDE: |
| 919 | case IGMPV3_MODE_IS_EXCLUDE: |
| 920 | case IGMPV3_CHANGE_TO_INCLUDE: |
| 921 | case IGMPV3_CHANGE_TO_EXCLUDE: |
| 922 | case IGMPV3_ALLOW_NEW_SOURCES: |
| 923 | case IGMPV3_BLOCK_OLD_SOURCES: |
| 924 | break; |
| 925 | |
| 926 | default: |
| 927 | continue; |
| 928 | } |
| 929 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 930 | err = br_ip4_multicast_add_group(br, port, group); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 931 | if (err) |
| 932 | break; |
| 933 | } |
| 934 | |
| 935 | return err; |
| 936 | } |
| 937 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 938 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 939 | static int br_ip6_multicast_mld2_report(struct net_bridge *br, |
| 940 | struct net_bridge_port *port, |
| 941 | struct sk_buff *skb) |
| 942 | { |
| 943 | struct icmp6hdr *icmp6h; |
| 944 | struct mld2_grec *grec; |
| 945 | int i; |
| 946 | int len; |
| 947 | int num; |
| 948 | int err = 0; |
| 949 | |
| 950 | if (!pskb_may_pull(skb, sizeof(*icmp6h))) |
| 951 | return -EINVAL; |
| 952 | |
| 953 | icmp6h = icmp6_hdr(skb); |
| 954 | num = ntohs(icmp6h->icmp6_dataun.un_data16[1]); |
| 955 | len = sizeof(*icmp6h); |
| 956 | |
| 957 | for (i = 0; i < num; i++) { |
| 958 | __be16 *nsrcs, _nsrcs; |
| 959 | |
| 960 | nsrcs = skb_header_pointer(skb, |
| 961 | len + offsetof(struct mld2_grec, |
Linus Lüssing | 649e984 | 2011-02-15 13:19:18 +0000 | [diff] [blame] | 962 | grec_nsrcs), |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 963 | sizeof(_nsrcs), &_nsrcs); |
| 964 | if (!nsrcs) |
| 965 | return -EINVAL; |
| 966 | |
| 967 | if (!pskb_may_pull(skb, |
| 968 | len + sizeof(*grec) + |
Linus Lüssing | d41db9f | 2011-02-15 13:19:19 +0000 | [diff] [blame] | 969 | sizeof(struct in6_addr) * ntohs(*nsrcs))) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 970 | return -EINVAL; |
| 971 | |
| 972 | grec = (struct mld2_grec *)(skb->data + len); |
Linus Lüssing | d41db9f | 2011-02-15 13:19:19 +0000 | [diff] [blame] | 973 | len += sizeof(*grec) + |
| 974 | sizeof(struct in6_addr) * ntohs(*nsrcs); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 975 | |
| 976 | /* We treat these as MLDv1 reports for now. */ |
| 977 | switch (grec->grec_type) { |
| 978 | case MLD2_MODE_IS_INCLUDE: |
| 979 | case MLD2_MODE_IS_EXCLUDE: |
| 980 | case MLD2_CHANGE_TO_INCLUDE: |
| 981 | case MLD2_CHANGE_TO_EXCLUDE: |
| 982 | case MLD2_ALLOW_NEW_SOURCES: |
| 983 | case MLD2_BLOCK_OLD_SOURCES: |
| 984 | break; |
| 985 | |
| 986 | default: |
| 987 | continue; |
| 988 | } |
| 989 | |
| 990 | err = br_ip6_multicast_add_group(br, port, &grec->grec_mca); |
| 991 | if (!err) |
| 992 | break; |
| 993 | } |
| 994 | |
| 995 | return err; |
| 996 | } |
| 997 | #endif |
| 998 | |
stephen hemminger | 7e80c12 | 2010-04-27 15:01:04 +0000 | [diff] [blame] | 999 | /* |
| 1000 | * Add port to rotuer_list |
| 1001 | * list is maintained ordered by pointer value |
| 1002 | * and locked by br->multicast_lock and RCU |
| 1003 | */ |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1004 | static void br_multicast_add_router(struct net_bridge *br, |
| 1005 | struct net_bridge_port *port) |
| 1006 | { |
stephen hemminger | dcdca2c | 2010-04-27 07:13:11 +0000 | [diff] [blame] | 1007 | struct net_bridge_port *p; |
stephen hemminger | 7e80c12 | 2010-04-27 15:01:04 +0000 | [diff] [blame] | 1008 | struct hlist_node *n, *slot = NULL; |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1009 | |
David S. Miller | 709b932 | 2010-04-27 16:49:58 -0700 | [diff] [blame] | 1010 | hlist_for_each_entry(p, n, &br->router_list, rlist) { |
stephen hemminger | 7e80c12 | 2010-04-27 15:01:04 +0000 | [diff] [blame] | 1011 | if ((unsigned long) port >= (unsigned long) p) |
| 1012 | break; |
| 1013 | slot = n; |
stephen hemminger | dcdca2c | 2010-04-27 07:13:11 +0000 | [diff] [blame] | 1014 | } |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1015 | |
stephen hemminger | 7e80c12 | 2010-04-27 15:01:04 +0000 | [diff] [blame] | 1016 | if (slot) |
| 1017 | hlist_add_after_rcu(slot, &port->rlist); |
stephen hemminger | dcdca2c | 2010-04-27 07:13:11 +0000 | [diff] [blame] | 1018 | else |
| 1019 | hlist_add_head_rcu(&port->rlist, &br->router_list); |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1022 | static void br_multicast_mark_router(struct net_bridge *br, |
| 1023 | struct net_bridge_port *port) |
| 1024 | { |
| 1025 | unsigned long now = jiffies; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1026 | |
| 1027 | if (!port) { |
| 1028 | if (br->multicast_router == 1) |
| 1029 | mod_timer(&br->multicast_router_timer, |
| 1030 | now + br->multicast_querier_interval); |
| 1031 | return; |
| 1032 | } |
| 1033 | |
| 1034 | if (port->multicast_router != 1) |
| 1035 | return; |
| 1036 | |
| 1037 | if (!hlist_unhashed(&port->rlist)) |
| 1038 | goto timer; |
| 1039 | |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1040 | br_multicast_add_router(br, port); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1041 | |
| 1042 | timer: |
| 1043 | mod_timer(&port->multicast_router_timer, |
| 1044 | now + br->multicast_querier_interval); |
| 1045 | } |
| 1046 | |
| 1047 | static void br_multicast_query_received(struct net_bridge *br, |
| 1048 | struct net_bridge_port *port, |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 1049 | int saddr) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1050 | { |
| 1051 | if (saddr) |
| 1052 | mod_timer(&br->multicast_querier_timer, |
| 1053 | jiffies + br->multicast_querier_interval); |
| 1054 | else if (timer_pending(&br->multicast_querier_timer)) |
| 1055 | return; |
| 1056 | |
| 1057 | br_multicast_mark_router(br, port); |
| 1058 | } |
| 1059 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 1060 | static int br_ip4_multicast_query(struct net_bridge *br, |
| 1061 | struct net_bridge_port *port, |
| 1062 | struct sk_buff *skb) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1063 | { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1064 | const struct iphdr *iph = ip_hdr(skb); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1065 | struct igmphdr *ih = igmp_hdr(skb); |
| 1066 | struct net_bridge_mdb_entry *mp; |
| 1067 | struct igmpv3_query *ih3; |
| 1068 | struct net_bridge_port_group *p; |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 1069 | struct net_bridge_port_group __rcu **pp; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1070 | unsigned long max_delay; |
| 1071 | unsigned long now = jiffies; |
| 1072 | __be32 group; |
YOSHIFUJI Hideaki | bec68ff | 2010-03-13 12:27:21 -0800 | [diff] [blame] | 1073 | int err = 0; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1074 | |
| 1075 | spin_lock(&br->multicast_lock); |
| 1076 | if (!netif_running(br->dev) || |
| 1077 | (port && port->state == BR_STATE_DISABLED)) |
| 1078 | goto out; |
| 1079 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 1080 | br_multicast_query_received(br, port, !!iph->saddr); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1081 | |
| 1082 | group = ih->group; |
| 1083 | |
| 1084 | if (skb->len == sizeof(*ih)) { |
| 1085 | max_delay = ih->code * (HZ / IGMP_TIMER_SCALE); |
| 1086 | |
| 1087 | if (!max_delay) { |
| 1088 | max_delay = 10 * HZ; |
| 1089 | group = 0; |
| 1090 | } |
| 1091 | } else { |
YOSHIFUJI Hideaki | bec68ff | 2010-03-13 12:27:21 -0800 | [diff] [blame] | 1092 | if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) { |
| 1093 | err = -EINVAL; |
| 1094 | goto out; |
| 1095 | } |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1096 | |
| 1097 | ih3 = igmpv3_query_hdr(skb); |
| 1098 | if (ih3->nsrcs) |
YOSHIFUJI Hideaki | bec68ff | 2010-03-13 12:27:21 -0800 | [diff] [blame] | 1099 | goto out; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1100 | |
YOSHIFUJI Hideaki / 吉藤英明 | 0ba8c9e | 2010-03-15 19:27:00 +0000 | [diff] [blame] | 1101 | max_delay = ih3->code ? |
| 1102 | IGMPV3_MRC(ih3->code) * (HZ / IGMP_TIMER_SCALE) : 1; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | if (!group) |
| 1106 | goto out; |
| 1107 | |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 1108 | mp = br_mdb_ip4_get(mlock_dereference(br->mdb, br), group); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1109 | if (!mp) |
| 1110 | goto out; |
| 1111 | |
| 1112 | max_delay *= br->multicast_last_member_count; |
| 1113 | |
Herbert Xu | 8a87017 | 2011-02-12 01:05:42 -0800 | [diff] [blame] | 1114 | if (mp->mglist && |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1115 | (timer_pending(&mp->timer) ? |
| 1116 | time_after(mp->timer.expires, now + max_delay) : |
| 1117 | try_to_del_timer_sync(&mp->timer) >= 0)) |
| 1118 | mod_timer(&mp->timer, now + max_delay); |
| 1119 | |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 1120 | for (pp = &mp->ports; |
| 1121 | (p = mlock_dereference(*pp, br)) != NULL; |
| 1122 | pp = &p->next) { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1123 | if (timer_pending(&p->timer) ? |
| 1124 | time_after(p->timer.expires, now + max_delay) : |
| 1125 | try_to_del_timer_sync(&p->timer) >= 0) |
Herbert Xu | 24f9cdc | 2011-02-11 12:42:07 +0000 | [diff] [blame] | 1126 | mod_timer(&p->timer, now + max_delay); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | out: |
| 1130 | spin_unlock(&br->multicast_lock); |
YOSHIFUJI Hideaki | bec68ff | 2010-03-13 12:27:21 -0800 | [diff] [blame] | 1131 | return err; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1132 | } |
| 1133 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 1134 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1135 | static int br_ip6_multicast_query(struct net_bridge *br, |
| 1136 | struct net_bridge_port *port, |
| 1137 | struct sk_buff *skb) |
| 1138 | { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1139 | const struct ipv6hdr *ip6h = ipv6_hdr(skb); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1140 | struct mld_msg *mld = (struct mld_msg *) icmp6_hdr(skb); |
| 1141 | struct net_bridge_mdb_entry *mp; |
| 1142 | struct mld2_query *mld2q; |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 1143 | struct net_bridge_port_group *p; |
| 1144 | struct net_bridge_port_group __rcu **pp; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1145 | unsigned long max_delay; |
| 1146 | unsigned long now = jiffies; |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1147 | const struct in6_addr *group = NULL; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1148 | int err = 0; |
| 1149 | |
| 1150 | spin_lock(&br->multicast_lock); |
| 1151 | if (!netif_running(br->dev) || |
| 1152 | (port && port->state == BR_STATE_DISABLED)) |
| 1153 | goto out; |
| 1154 | |
| 1155 | br_multicast_query_received(br, port, !ipv6_addr_any(&ip6h->saddr)); |
| 1156 | |
| 1157 | if (skb->len == sizeof(*mld)) { |
| 1158 | if (!pskb_may_pull(skb, sizeof(*mld))) { |
| 1159 | err = -EINVAL; |
| 1160 | goto out; |
| 1161 | } |
| 1162 | mld = (struct mld_msg *) icmp6_hdr(skb); |
Li RongQing | 4715213 | 2012-07-09 23:56:12 +0000 | [diff] [blame] | 1163 | max_delay = msecs_to_jiffies(ntohs(mld->mld_maxdelay)); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1164 | if (max_delay) |
| 1165 | group = &mld->mld_mca; |
| 1166 | } else if (skb->len >= sizeof(*mld2q)) { |
| 1167 | if (!pskb_may_pull(skb, sizeof(*mld2q))) { |
| 1168 | err = -EINVAL; |
| 1169 | goto out; |
| 1170 | } |
| 1171 | mld2q = (struct mld2_query *)icmp6_hdr(skb); |
| 1172 | if (!mld2q->mld2q_nsrcs) |
| 1173 | group = &mld2q->mld2q_mca; |
| 1174 | max_delay = mld2q->mld2q_mrc ? MLDV2_MRC(mld2q->mld2q_mrc) : 1; |
| 1175 | } |
| 1176 | |
| 1177 | if (!group) |
| 1178 | goto out; |
| 1179 | |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 1180 | mp = br_mdb_ip6_get(mlock_dereference(br->mdb, br), group); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1181 | if (!mp) |
| 1182 | goto out; |
| 1183 | |
| 1184 | max_delay *= br->multicast_last_member_count; |
Herbert Xu | 8a87017 | 2011-02-12 01:05:42 -0800 | [diff] [blame] | 1185 | if (mp->mglist && |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1186 | (timer_pending(&mp->timer) ? |
| 1187 | time_after(mp->timer.expires, now + max_delay) : |
| 1188 | try_to_del_timer_sync(&mp->timer) >= 0)) |
| 1189 | mod_timer(&mp->timer, now + max_delay); |
| 1190 | |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 1191 | for (pp = &mp->ports; |
| 1192 | (p = mlock_dereference(*pp, br)) != NULL; |
| 1193 | pp = &p->next) { |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1194 | if (timer_pending(&p->timer) ? |
| 1195 | time_after(p->timer.expires, now + max_delay) : |
| 1196 | try_to_del_timer_sync(&p->timer) >= 0) |
Herbert Xu | 24f9cdc | 2011-02-11 12:42:07 +0000 | [diff] [blame] | 1197 | mod_timer(&p->timer, now + max_delay); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | out: |
| 1201 | spin_unlock(&br->multicast_lock); |
| 1202 | return err; |
| 1203 | } |
| 1204 | #endif |
| 1205 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1206 | static void br_multicast_leave_group(struct net_bridge *br, |
| 1207 | struct net_bridge_port *port, |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 1208 | struct br_ip *group) |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1209 | { |
| 1210 | struct net_bridge_mdb_htable *mdb; |
| 1211 | struct net_bridge_mdb_entry *mp; |
| 1212 | struct net_bridge_port_group *p; |
| 1213 | unsigned long now; |
| 1214 | unsigned long time; |
| 1215 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1216 | spin_lock(&br->multicast_lock); |
| 1217 | if (!netif_running(br->dev) || |
| 1218 | (port && port->state == BR_STATE_DISABLED) || |
| 1219 | timer_pending(&br->multicast_querier_timer)) |
| 1220 | goto out; |
| 1221 | |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 1222 | mdb = mlock_dereference(br->mdb, br); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1223 | mp = br_mdb_ip_get(mdb, group); |
| 1224 | if (!mp) |
| 1225 | goto out; |
| 1226 | |
| 1227 | now = jiffies; |
| 1228 | time = now + br->multicast_last_member_count * |
| 1229 | br->multicast_last_member_interval; |
| 1230 | |
| 1231 | if (!port) { |
Herbert Xu | 8a87017 | 2011-02-12 01:05:42 -0800 | [diff] [blame] | 1232 | if (mp->mglist && |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1233 | (timer_pending(&mp->timer) ? |
| 1234 | time_after(mp->timer.expires, time) : |
| 1235 | try_to_del_timer_sync(&mp->timer) >= 0)) { |
| 1236 | mod_timer(&mp->timer, time); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1237 | } |
| 1238 | |
| 1239 | goto out; |
| 1240 | } |
| 1241 | |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 1242 | for (p = mlock_dereference(mp->ports, br); |
| 1243 | p != NULL; |
| 1244 | p = mlock_dereference(p->next, br)) { |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1245 | if (p->port != port) |
| 1246 | continue; |
| 1247 | |
| 1248 | if (!hlist_unhashed(&p->mglist) && |
| 1249 | (timer_pending(&p->timer) ? |
| 1250 | time_after(p->timer.expires, time) : |
| 1251 | try_to_del_timer_sync(&p->timer) >= 0)) { |
| 1252 | mod_timer(&p->timer, time); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | break; |
| 1256 | } |
| 1257 | |
| 1258 | out: |
| 1259 | spin_unlock(&br->multicast_lock); |
| 1260 | } |
| 1261 | |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 1262 | static void br_ip4_multicast_leave_group(struct net_bridge *br, |
| 1263 | struct net_bridge_port *port, |
| 1264 | __be32 group) |
| 1265 | { |
| 1266 | struct br_ip br_group; |
| 1267 | |
| 1268 | if (ipv4_is_local_multicast(group)) |
| 1269 | return; |
| 1270 | |
| 1271 | br_group.u.ip4 = group; |
| 1272 | br_group.proto = htons(ETH_P_IP); |
| 1273 | |
| 1274 | br_multicast_leave_group(br, port, &br_group); |
| 1275 | } |
| 1276 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 1277 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1278 | static void br_ip6_multicast_leave_group(struct net_bridge *br, |
| 1279 | struct net_bridge_port *port, |
| 1280 | const struct in6_addr *group) |
| 1281 | { |
| 1282 | struct br_ip br_group; |
| 1283 | |
Linus Lüssing | e4de9f9 | 2011-02-15 13:19:21 +0000 | [diff] [blame] | 1284 | if (!ipv6_is_transient_multicast(group)) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1285 | return; |
| 1286 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 1287 | br_group.u.ip6 = *group; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1288 | br_group.proto = htons(ETH_P_IPV6); |
| 1289 | |
| 1290 | br_multicast_leave_group(br, port, &br_group); |
| 1291 | } |
| 1292 | #endif |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 1293 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1294 | static int br_multicast_ipv4_rcv(struct net_bridge *br, |
| 1295 | struct net_bridge_port *port, |
| 1296 | struct sk_buff *skb) |
| 1297 | { |
| 1298 | struct sk_buff *skb2 = skb; |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1299 | const struct iphdr *iph; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1300 | struct igmphdr *ih; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 1301 | unsigned int len; |
| 1302 | unsigned int offset; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1303 | int err; |
| 1304 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1305 | /* We treat OOM as packet loss for now. */ |
| 1306 | if (!pskb_may_pull(skb, sizeof(*iph))) |
| 1307 | return -EINVAL; |
| 1308 | |
| 1309 | iph = ip_hdr(skb); |
| 1310 | |
| 1311 | if (iph->ihl < 5 || iph->version != 4) |
| 1312 | return -EINVAL; |
| 1313 | |
| 1314 | if (!pskb_may_pull(skb, ip_hdrlen(skb))) |
| 1315 | return -EINVAL; |
| 1316 | |
| 1317 | iph = ip_hdr(skb); |
| 1318 | |
| 1319 | if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl))) |
| 1320 | return -EINVAL; |
| 1321 | |
Herbert Xu | bd4265f | 2011-06-23 02:39:12 +0000 | [diff] [blame] | 1322 | if (iph->protocol != IPPROTO_IGMP) { |
| 1323 | if ((iph->daddr & IGMP_LOCAL_GROUP_MASK) != IGMP_LOCAL_GROUP) |
| 1324 | BR_INPUT_SKB_CB(skb)->mrouters_only = 1; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1325 | return 0; |
Herbert Xu | bd4265f | 2011-06-23 02:39:12 +0000 | [diff] [blame] | 1326 | } |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1327 | |
| 1328 | len = ntohs(iph->tot_len); |
| 1329 | if (skb->len < len || len < ip_hdrlen(skb)) |
| 1330 | return -EINVAL; |
| 1331 | |
| 1332 | if (skb->len > len) { |
| 1333 | skb2 = skb_clone(skb, GFP_ATOMIC); |
| 1334 | if (!skb2) |
| 1335 | return -ENOMEM; |
| 1336 | |
| 1337 | err = pskb_trim_rcsum(skb2, len); |
| 1338 | if (err) |
YOSHIFUJI Hideaki / 吉藤英明 | 8440853 | 2010-03-15 19:26:56 +0000 | [diff] [blame] | 1339 | goto err_out; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | len -= ip_hdrlen(skb2); |
| 1343 | offset = skb_network_offset(skb2) + ip_hdrlen(skb2); |
| 1344 | __skb_pull(skb2, offset); |
| 1345 | skb_reset_transport_header(skb2); |
| 1346 | |
| 1347 | err = -EINVAL; |
| 1348 | if (!pskb_may_pull(skb2, sizeof(*ih))) |
| 1349 | goto out; |
| 1350 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1351 | switch (skb2->ip_summed) { |
| 1352 | case CHECKSUM_COMPLETE: |
| 1353 | if (!csum_fold(skb2->csum)) |
| 1354 | break; |
| 1355 | /* fall through */ |
| 1356 | case CHECKSUM_NONE: |
| 1357 | skb2->csum = 0; |
| 1358 | if (skb_checksum_complete(skb2)) |
YOSHIFUJI Hideaki / 吉藤英明 | 8440853 | 2010-03-15 19:26:56 +0000 | [diff] [blame] | 1359 | goto out; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | err = 0; |
| 1363 | |
| 1364 | BR_INPUT_SKB_CB(skb)->igmp = 1; |
| 1365 | ih = igmp_hdr(skb2); |
| 1366 | |
| 1367 | switch (ih->type) { |
| 1368 | case IGMP_HOST_MEMBERSHIP_REPORT: |
| 1369 | case IGMPV2_HOST_MEMBERSHIP_REPORT: |
Fernando Luis Vázquez Cao | 62b2bcb | 2011-06-13 15:04:43 +0000 | [diff] [blame] | 1370 | BR_INPUT_SKB_CB(skb)->mrouters_only = 1; |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 1371 | err = br_ip4_multicast_add_group(br, port, ih->group); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1372 | break; |
| 1373 | case IGMPV3_HOST_MEMBERSHIP_REPORT: |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 1374 | err = br_ip4_multicast_igmp3_report(br, port, skb2); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1375 | break; |
| 1376 | case IGMP_HOST_MEMBERSHIP_QUERY: |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 1377 | err = br_ip4_multicast_query(br, port, skb2); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1378 | break; |
| 1379 | case IGMP_HOST_LEAVE_MESSAGE: |
YOSHIFUJI Hideaki | 8ef2a9a | 2010-04-18 12:42:07 +0900 | [diff] [blame] | 1380 | br_ip4_multicast_leave_group(br, port, ih->group); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1381 | break; |
| 1382 | } |
| 1383 | |
| 1384 | out: |
| 1385 | __skb_push(skb2, offset); |
YOSHIFUJI Hideaki / 吉藤英明 | 8440853 | 2010-03-15 19:26:56 +0000 | [diff] [blame] | 1386 | err_out: |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1387 | if (skb2 != skb) |
| 1388 | kfree_skb(skb2); |
| 1389 | return err; |
| 1390 | } |
| 1391 | |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 1392 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1393 | static int br_multicast_ipv6_rcv(struct net_bridge *br, |
| 1394 | struct net_bridge_port *port, |
| 1395 | struct sk_buff *skb) |
| 1396 | { |
Tomas Winkler | 9d89081 | 2011-01-03 11:26:08 -0800 | [diff] [blame] | 1397 | struct sk_buff *skb2; |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1398 | const struct ipv6hdr *ip6h; |
Eric Dumazet | 22df133 | 2011-08-23 19:57:05 +0000 | [diff] [blame] | 1399 | u8 icmp6_type; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1400 | u8 nexthdr; |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 1401 | __be16 frag_off; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 1402 | unsigned int len; |
Kulikov Vasiliy | bb7a0bd | 2010-07-15 08:47:33 +0000 | [diff] [blame] | 1403 | int offset; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1404 | int err; |
| 1405 | |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1406 | if (!pskb_may_pull(skb, sizeof(*ip6h))) |
| 1407 | return -EINVAL; |
| 1408 | |
| 1409 | ip6h = ipv6_hdr(skb); |
| 1410 | |
| 1411 | /* |
| 1412 | * We're interested in MLD messages only. |
| 1413 | * - Version is 6 |
| 1414 | * - MLD has always Router Alert hop-by-hop option |
| 1415 | * - But we do not support jumbrograms. |
| 1416 | */ |
| 1417 | if (ip6h->version != 6 || |
| 1418 | ip6h->nexthdr != IPPROTO_HOPOPTS || |
| 1419 | ip6h->payload_len == 0) |
| 1420 | return 0; |
| 1421 | |
Linus Lüssing | ff9a57a | 2011-03-26 20:27:24 +0000 | [diff] [blame] | 1422 | len = ntohs(ip6h->payload_len) + sizeof(*ip6h); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1423 | if (skb->len < len) |
| 1424 | return -EINVAL; |
| 1425 | |
| 1426 | nexthdr = ip6h->nexthdr; |
Jesse Gross | 75f2811 | 2011-11-30 17:05:51 -0800 | [diff] [blame] | 1427 | offset = ipv6_skip_exthdr(skb, sizeof(*ip6h), &nexthdr, &frag_off); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1428 | |
| 1429 | if (offset < 0 || nexthdr != IPPROTO_ICMPV6) |
| 1430 | return 0; |
| 1431 | |
| 1432 | /* Okay, we found ICMPv6 header */ |
| 1433 | skb2 = skb_clone(skb, GFP_ATOMIC); |
| 1434 | if (!skb2) |
| 1435 | return -ENOMEM; |
| 1436 | |
Tomas Winkler | 9d89081 | 2011-01-03 11:26:08 -0800 | [diff] [blame] | 1437 | err = -EINVAL; |
| 1438 | if (!pskb_may_pull(skb2, offset + sizeof(struct icmp6hdr))) |
| 1439 | goto out; |
| 1440 | |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1441 | len -= offset - skb_network_offset(skb2); |
| 1442 | |
| 1443 | __skb_pull(skb2, offset); |
| 1444 | skb_reset_transport_header(skb2); |
stephen hemminger | fa2da8c | 2011-11-15 08:09:14 +0000 | [diff] [blame] | 1445 | skb_postpull_rcsum(skb2, skb_network_header(skb2), |
| 1446 | skb_network_header_len(skb2)); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1447 | |
Eric Dumazet | 22df133 | 2011-08-23 19:57:05 +0000 | [diff] [blame] | 1448 | icmp6_type = icmp6_hdr(skb2)->icmp6_type; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1449 | |
Eric Dumazet | 22df133 | 2011-08-23 19:57:05 +0000 | [diff] [blame] | 1450 | switch (icmp6_type) { |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1451 | case ICMPV6_MGM_QUERY: |
| 1452 | case ICMPV6_MGM_REPORT: |
| 1453 | case ICMPV6_MGM_REDUCTION: |
| 1454 | case ICMPV6_MLD2_REPORT: |
| 1455 | break; |
| 1456 | default: |
| 1457 | err = 0; |
| 1458 | goto out; |
| 1459 | } |
| 1460 | |
| 1461 | /* Okay, we found MLD message. Check further. */ |
| 1462 | if (skb2->len > len) { |
| 1463 | err = pskb_trim_rcsum(skb2, len); |
| 1464 | if (err) |
| 1465 | goto out; |
Yan, Zheng | 4b275d7 | 2011-08-23 22:54:33 +0000 | [diff] [blame] | 1466 | err = -EINVAL; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1467 | } |
| 1468 | |
Yan, Zheng | 4b275d7 | 2011-08-23 22:54:33 +0000 | [diff] [blame] | 1469 | ip6h = ipv6_hdr(skb2); |
| 1470 | |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1471 | switch (skb2->ip_summed) { |
| 1472 | case CHECKSUM_COMPLETE: |
Yan, Zheng | 4b275d7 | 2011-08-23 22:54:33 +0000 | [diff] [blame] | 1473 | if (!csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, skb2->len, |
| 1474 | IPPROTO_ICMPV6, skb2->csum)) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1475 | break; |
| 1476 | /*FALLTHROUGH*/ |
| 1477 | case CHECKSUM_NONE: |
Yan, Zheng | 4b275d7 | 2011-08-23 22:54:33 +0000 | [diff] [blame] | 1478 | skb2->csum = ~csum_unfold(csum_ipv6_magic(&ip6h->saddr, |
| 1479 | &ip6h->daddr, |
| 1480 | skb2->len, |
| 1481 | IPPROTO_ICMPV6, 0)); |
| 1482 | if (__skb_checksum_complete(skb2)) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1483 | goto out; |
| 1484 | } |
| 1485 | |
| 1486 | err = 0; |
| 1487 | |
| 1488 | BR_INPUT_SKB_CB(skb)->igmp = 1; |
| 1489 | |
Eric Dumazet | 22df133 | 2011-08-23 19:57:05 +0000 | [diff] [blame] | 1490 | switch (icmp6_type) { |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1491 | case ICMPV6_MGM_REPORT: |
| 1492 | { |
Tomas Winkler | 9d89081 | 2011-01-03 11:26:08 -0800 | [diff] [blame] | 1493 | struct mld_msg *mld; |
| 1494 | if (!pskb_may_pull(skb2, sizeof(*mld))) { |
| 1495 | err = -EINVAL; |
| 1496 | goto out; |
| 1497 | } |
| 1498 | mld = (struct mld_msg *)skb_transport_header(skb2); |
Fernando Luis Vázquez Cao | fc2af6c | 2011-06-13 15:06:58 +0000 | [diff] [blame] | 1499 | BR_INPUT_SKB_CB(skb)->mrouters_only = 1; |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1500 | err = br_ip6_multicast_add_group(br, port, &mld->mld_mca); |
| 1501 | break; |
| 1502 | } |
| 1503 | case ICMPV6_MLD2_REPORT: |
| 1504 | err = br_ip6_multicast_mld2_report(br, port, skb2); |
| 1505 | break; |
| 1506 | case ICMPV6_MGM_QUERY: |
| 1507 | err = br_ip6_multicast_query(br, port, skb2); |
| 1508 | break; |
| 1509 | case ICMPV6_MGM_REDUCTION: |
| 1510 | { |
Tomas Winkler | 9d89081 | 2011-01-03 11:26:08 -0800 | [diff] [blame] | 1511 | struct mld_msg *mld; |
| 1512 | if (!pskb_may_pull(skb2, sizeof(*mld))) { |
| 1513 | err = -EINVAL; |
| 1514 | goto out; |
| 1515 | } |
| 1516 | mld = (struct mld_msg *)skb_transport_header(skb2); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1517 | br_ip6_multicast_leave_group(br, port, &mld->mld_mca); |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | out: |
Tomas Winkler | 9d89081 | 2011-01-03 11:26:08 -0800 | [diff] [blame] | 1522 | kfree_skb(skb2); |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1523 | return err; |
| 1524 | } |
| 1525 | #endif |
| 1526 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1527 | int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port, |
| 1528 | struct sk_buff *skb) |
| 1529 | { |
YOSHIFUJI Hideaki / 吉藤英明 | 1fafc7a | 2010-04-25 08:06:40 +0000 | [diff] [blame] | 1530 | BR_INPUT_SKB_CB(skb)->igmp = 0; |
| 1531 | BR_INPUT_SKB_CB(skb)->mrouters_only = 0; |
| 1532 | |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1533 | if (br->multicast_disabled) |
| 1534 | return 0; |
| 1535 | |
| 1536 | switch (skb->protocol) { |
| 1537 | case htons(ETH_P_IP): |
| 1538 | return br_multicast_ipv4_rcv(br, port, skb); |
Eric Dumazet | dfd56b8 | 2011-12-10 09:48:31 +0000 | [diff] [blame] | 1539 | #if IS_ENABLED(CONFIG_IPV6) |
YOSHIFUJI Hideaki | 08b202b | 2010-04-23 01:54:22 +0900 | [diff] [blame] | 1540 | case htons(ETH_P_IPV6): |
| 1541 | return br_multicast_ipv6_rcv(br, port, skb); |
| 1542 | #endif |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | return 0; |
| 1546 | } |
| 1547 | |
| 1548 | static void br_multicast_query_expired(unsigned long data) |
| 1549 | { |
| 1550 | struct net_bridge *br = (void *)data; |
| 1551 | |
| 1552 | spin_lock(&br->multicast_lock); |
| 1553 | if (br->multicast_startup_queries_sent < |
| 1554 | br->multicast_startup_query_count) |
| 1555 | br->multicast_startup_queries_sent++; |
| 1556 | |
| 1557 | br_multicast_send_query(br, NULL, br->multicast_startup_queries_sent); |
| 1558 | |
| 1559 | spin_unlock(&br->multicast_lock); |
| 1560 | } |
| 1561 | |
| 1562 | void br_multicast_init(struct net_bridge *br) |
| 1563 | { |
| 1564 | br->hash_elasticity = 4; |
| 1565 | br->hash_max = 512; |
| 1566 | |
| 1567 | br->multicast_router = 1; |
Herbert Xu | c5c2326 | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 1568 | br->multicast_querier = 0; |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1569 | br->multicast_last_member_count = 2; |
| 1570 | br->multicast_startup_query_count = 2; |
| 1571 | |
| 1572 | br->multicast_last_member_interval = HZ; |
| 1573 | br->multicast_query_response_interval = 10 * HZ; |
| 1574 | br->multicast_startup_query_interval = 125 * HZ / 4; |
| 1575 | br->multicast_query_interval = 125 * HZ; |
| 1576 | br->multicast_querier_interval = 255 * HZ; |
| 1577 | br->multicast_membership_interval = 260 * HZ; |
| 1578 | |
| 1579 | spin_lock_init(&br->multicast_lock); |
| 1580 | setup_timer(&br->multicast_router_timer, |
| 1581 | br_multicast_local_router_expired, 0); |
| 1582 | setup_timer(&br->multicast_querier_timer, |
Herbert Xu | bb63f1f | 2012-04-30 00:22:56 +0000 | [diff] [blame] | 1583 | br_multicast_querier_expired, (unsigned long)br); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1584 | setup_timer(&br->multicast_query_timer, br_multicast_query_expired, |
| 1585 | (unsigned long)br); |
| 1586 | } |
| 1587 | |
| 1588 | void br_multicast_open(struct net_bridge *br) |
| 1589 | { |
| 1590 | br->multicast_startup_queries_sent = 0; |
| 1591 | |
| 1592 | if (br->multicast_disabled) |
| 1593 | return; |
| 1594 | |
| 1595 | mod_timer(&br->multicast_query_timer, jiffies); |
| 1596 | } |
| 1597 | |
| 1598 | void br_multicast_stop(struct net_bridge *br) |
| 1599 | { |
| 1600 | struct net_bridge_mdb_htable *mdb; |
| 1601 | struct net_bridge_mdb_entry *mp; |
| 1602 | struct hlist_node *p, *n; |
| 1603 | u32 ver; |
| 1604 | int i; |
| 1605 | |
| 1606 | del_timer_sync(&br->multicast_router_timer); |
| 1607 | del_timer_sync(&br->multicast_querier_timer); |
| 1608 | del_timer_sync(&br->multicast_query_timer); |
| 1609 | |
| 1610 | spin_lock_bh(&br->multicast_lock); |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 1611 | mdb = mlock_dereference(br->mdb, br); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1612 | if (!mdb) |
| 1613 | goto out; |
| 1614 | |
| 1615 | br->mdb = NULL; |
| 1616 | |
| 1617 | ver = mdb->ver; |
| 1618 | for (i = 0; i < mdb->max; i++) { |
| 1619 | hlist_for_each_entry_safe(mp, p, n, &mdb->mhash[i], |
| 1620 | hlist[ver]) { |
| 1621 | del_timer(&mp->timer); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1622 | call_rcu_bh(&mp->rcu, br_multicast_free_group); |
| 1623 | } |
| 1624 | } |
| 1625 | |
| 1626 | if (mdb->old) { |
| 1627 | spin_unlock_bh(&br->multicast_lock); |
Herbert Xu | 10cc2b5 | 2010-03-05 21:03:35 +0000 | [diff] [blame] | 1628 | rcu_barrier_bh(); |
Herbert Xu | eb1d164 | 2010-02-27 19:41:45 +0000 | [diff] [blame] | 1629 | spin_lock_bh(&br->multicast_lock); |
| 1630 | WARN_ON(mdb->old); |
| 1631 | } |
| 1632 | |
| 1633 | mdb->old = mdb; |
| 1634 | call_rcu_bh(&mdb->rcu, br_mdb_free); |
| 1635 | |
| 1636 | out: |
| 1637 | spin_unlock_bh(&br->multicast_lock); |
| 1638 | } |
Herbert Xu | 0909e11 | 2010-02-27 19:41:49 +0000 | [diff] [blame] | 1639 | |
| 1640 | int br_multicast_set_router(struct net_bridge *br, unsigned long val) |
| 1641 | { |
| 1642 | int err = -ENOENT; |
| 1643 | |
| 1644 | spin_lock_bh(&br->multicast_lock); |
| 1645 | if (!netif_running(br->dev)) |
| 1646 | goto unlock; |
| 1647 | |
| 1648 | switch (val) { |
| 1649 | case 0: |
| 1650 | case 2: |
| 1651 | del_timer(&br->multicast_router_timer); |
| 1652 | /* fall through */ |
| 1653 | case 1: |
| 1654 | br->multicast_router = val; |
| 1655 | err = 0; |
| 1656 | break; |
| 1657 | |
| 1658 | default: |
| 1659 | err = -EINVAL; |
| 1660 | break; |
| 1661 | } |
| 1662 | |
| 1663 | unlock: |
| 1664 | spin_unlock_bh(&br->multicast_lock); |
| 1665 | |
| 1666 | return err; |
| 1667 | } |
| 1668 | |
| 1669 | int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val) |
| 1670 | { |
| 1671 | struct net_bridge *br = p->br; |
| 1672 | int err = -ENOENT; |
| 1673 | |
| 1674 | spin_lock(&br->multicast_lock); |
| 1675 | if (!netif_running(br->dev) || p->state == BR_STATE_DISABLED) |
| 1676 | goto unlock; |
| 1677 | |
| 1678 | switch (val) { |
| 1679 | case 0: |
| 1680 | case 1: |
| 1681 | case 2: |
| 1682 | p->multicast_router = val; |
| 1683 | err = 0; |
| 1684 | |
| 1685 | if (val < 2 && !hlist_unhashed(&p->rlist)) |
| 1686 | hlist_del_init_rcu(&p->rlist); |
| 1687 | |
| 1688 | if (val == 1) |
| 1689 | break; |
| 1690 | |
| 1691 | del_timer(&p->multicast_router_timer); |
| 1692 | |
| 1693 | if (val == 0) |
| 1694 | break; |
| 1695 | |
| 1696 | br_multicast_add_router(br, p); |
| 1697 | break; |
| 1698 | |
| 1699 | default: |
| 1700 | err = -EINVAL; |
| 1701 | break; |
| 1702 | } |
| 1703 | |
| 1704 | unlock: |
| 1705 | spin_unlock(&br->multicast_lock); |
| 1706 | |
| 1707 | return err; |
| 1708 | } |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 1709 | |
Herbert Xu | 7485721 | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 1710 | static void br_multicast_start_querier(struct net_bridge *br) |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 1711 | { |
| 1712 | struct net_bridge_port *port; |
Herbert Xu | 7485721 | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 1713 | |
| 1714 | br_multicast_open(br); |
| 1715 | |
| 1716 | list_for_each_entry(port, &br->port_list, list) { |
| 1717 | if (port->state == BR_STATE_DISABLED || |
| 1718 | port->state == BR_STATE_BLOCKING) |
| 1719 | continue; |
| 1720 | |
| 1721 | __br_multicast_enable_port(port); |
| 1722 | } |
| 1723 | } |
| 1724 | |
| 1725 | int br_multicast_toggle(struct net_bridge *br, unsigned long val) |
| 1726 | { |
Herbert Xu | 3a7fda0 | 2010-07-29 00:45:30 +0000 | [diff] [blame] | 1727 | int err = 0; |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 1728 | struct net_bridge_mdb_htable *mdb; |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 1729 | |
Andrey Vagin | ef5e0d8 | 2011-11-10 05:48:03 +0000 | [diff] [blame] | 1730 | spin_lock_bh(&br->multicast_lock); |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 1731 | if (br->multicast_disabled == !val) |
| 1732 | goto unlock; |
| 1733 | |
| 1734 | br->multicast_disabled = !val; |
| 1735 | if (br->multicast_disabled) |
| 1736 | goto unlock; |
| 1737 | |
Herbert Xu | 3a7fda0 | 2010-07-29 00:45:30 +0000 | [diff] [blame] | 1738 | if (!netif_running(br->dev)) |
| 1739 | goto unlock; |
| 1740 | |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 1741 | mdb = mlock_dereference(br->mdb, br); |
| 1742 | if (mdb) { |
| 1743 | if (mdb->old) { |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 1744 | err = -EEXIST; |
| 1745 | rollback: |
| 1746 | br->multicast_disabled = !!val; |
| 1747 | goto unlock; |
| 1748 | } |
| 1749 | |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 1750 | err = br_mdb_rehash(&br->mdb, mdb->max, |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 1751 | br->hash_elasticity); |
| 1752 | if (err) |
| 1753 | goto rollback; |
| 1754 | } |
| 1755 | |
Herbert Xu | 7485721 | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 1756 | br_multicast_start_querier(br); |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 1757 | |
| 1758 | unlock: |
Andrey Vagin | ef5e0d8 | 2011-11-10 05:48:03 +0000 | [diff] [blame] | 1759 | spin_unlock_bh(&br->multicast_lock); |
Herbert Xu | 561f110 | 2010-02-27 19:41:50 +0000 | [diff] [blame] | 1760 | |
| 1761 | return err; |
| 1762 | } |
Herbert Xu | b195167 | 2010-02-27 19:41:51 +0000 | [diff] [blame] | 1763 | |
Herbert Xu | c5c2326 | 2012-04-13 02:37:42 +0000 | [diff] [blame] | 1764 | int br_multicast_set_querier(struct net_bridge *br, unsigned long val) |
| 1765 | { |
| 1766 | val = !!val; |
| 1767 | |
| 1768 | spin_lock_bh(&br->multicast_lock); |
| 1769 | if (br->multicast_querier == val) |
| 1770 | goto unlock; |
| 1771 | |
| 1772 | br->multicast_querier = val; |
| 1773 | if (val) |
| 1774 | br_multicast_start_querier(br); |
| 1775 | |
| 1776 | unlock: |
| 1777 | spin_unlock_bh(&br->multicast_lock); |
| 1778 | |
| 1779 | return 0; |
| 1780 | } |
| 1781 | |
Herbert Xu | b195167 | 2010-02-27 19:41:51 +0000 | [diff] [blame] | 1782 | int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val) |
| 1783 | { |
| 1784 | int err = -ENOENT; |
| 1785 | u32 old; |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 1786 | struct net_bridge_mdb_htable *mdb; |
Herbert Xu | b195167 | 2010-02-27 19:41:51 +0000 | [diff] [blame] | 1787 | |
| 1788 | spin_lock(&br->multicast_lock); |
| 1789 | if (!netif_running(br->dev)) |
| 1790 | goto unlock; |
| 1791 | |
| 1792 | err = -EINVAL; |
| 1793 | if (!is_power_of_2(val)) |
| 1794 | goto unlock; |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 1795 | |
| 1796 | mdb = mlock_dereference(br->mdb, br); |
| 1797 | if (mdb && val < mdb->size) |
Herbert Xu | b195167 | 2010-02-27 19:41:51 +0000 | [diff] [blame] | 1798 | goto unlock; |
| 1799 | |
| 1800 | err = 0; |
| 1801 | |
| 1802 | old = br->hash_max; |
| 1803 | br->hash_max = val; |
| 1804 | |
Eric Dumazet | e805168 | 2010-11-15 06:38:10 +0000 | [diff] [blame] | 1805 | if (mdb) { |
| 1806 | if (mdb->old) { |
Herbert Xu | b195167 | 2010-02-27 19:41:51 +0000 | [diff] [blame] | 1807 | err = -EEXIST; |
| 1808 | rollback: |
| 1809 | br->hash_max = old; |
| 1810 | goto unlock; |
| 1811 | } |
| 1812 | |
| 1813 | err = br_mdb_rehash(&br->mdb, br->hash_max, |
| 1814 | br->hash_elasticity); |
| 1815 | if (err) |
| 1816 | goto rollback; |
| 1817 | } |
| 1818 | |
| 1819 | unlock: |
| 1820 | spin_unlock(&br->multicast_lock); |
| 1821 | |
| 1822 | return err; |
| 1823 | } |