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