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