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