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