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