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