Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 1 | #include <linux/err.h> |
| 2 | #include <linux/igmp.h> |
| 3 | #include <linux/kernel.h> |
| 4 | #include <linux/netdevice.h> |
| 5 | #include <linux/rculist.h> |
| 6 | #include <linux/skbuff.h> |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 7 | #include <linux/if_ether.h> |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 8 | #include <net/ip.h> |
| 9 | #include <net/netlink.h> |
| 10 | #if IS_ENABLED(CONFIG_IPV6) |
| 11 | #include <net/ipv6.h> |
Linus Lüssing | 3c3769e | 2013-09-04 02:13:39 +0200 | [diff] [blame] | 12 | #include <net/addrconf.h> |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 13 | #endif |
| 14 | |
| 15 | #include "br_private.h" |
| 16 | |
| 17 | static int br_rports_fill_info(struct sk_buff *skb, struct netlink_callback *cb, |
| 18 | struct net_device *dev) |
| 19 | { |
| 20 | struct net_bridge *br = netdev_priv(dev); |
| 21 | struct net_bridge_port *p; |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 22 | struct nlattr *nest; |
| 23 | |
| 24 | if (!br->multicast_router || hlist_empty(&br->router_list)) |
| 25 | return 0; |
| 26 | |
| 27 | nest = nla_nest_start(skb, MDBA_ROUTER); |
| 28 | if (nest == NULL) |
| 29 | return -EMSGSIZE; |
| 30 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 31 | hlist_for_each_entry_rcu(p, &br->router_list, rlist) { |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 32 | if (p && nla_put_u32(skb, MDBA_ROUTER_PORT, p->dev->ifindex)) |
| 33 | goto fail; |
| 34 | } |
| 35 | |
| 36 | nla_nest_end(skb, nest); |
| 37 | return 0; |
| 38 | fail: |
| 39 | nla_nest_cancel(skb, nest); |
| 40 | return -EMSGSIZE; |
| 41 | } |
| 42 | |
| 43 | static int br_mdb_fill_info(struct sk_buff *skb, struct netlink_callback *cb, |
| 44 | struct net_device *dev) |
| 45 | { |
| 46 | struct net_bridge *br = netdev_priv(dev); |
| 47 | struct net_bridge_mdb_htable *mdb; |
| 48 | struct nlattr *nest, *nest2; |
| 49 | int i, err = 0; |
| 50 | int idx = 0, s_idx = cb->args[1]; |
| 51 | |
| 52 | if (br->multicast_disabled) |
| 53 | return 0; |
| 54 | |
| 55 | mdb = rcu_dereference(br->mdb); |
| 56 | if (!mdb) |
| 57 | return 0; |
| 58 | |
| 59 | nest = nla_nest_start(skb, MDBA_MDB); |
| 60 | if (nest == NULL) |
| 61 | return -EMSGSIZE; |
| 62 | |
| 63 | for (i = 0; i < mdb->max; i++) { |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 64 | struct net_bridge_mdb_entry *mp; |
stephen hemminger | 762a3d8 | 2013-08-04 17:19:38 -0700 | [diff] [blame] | 65 | struct net_bridge_port_group *p; |
| 66 | struct net_bridge_port_group __rcu **pp; |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 67 | struct net_bridge_port *port; |
| 68 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 69 | hlist_for_each_entry_rcu(mp, &mdb->mhash[i], hlist[mdb->ver]) { |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 70 | if (idx < s_idx) |
| 71 | goto skip; |
| 72 | |
| 73 | nest2 = nla_nest_start(skb, MDBA_MDB_ENTRY); |
| 74 | if (nest2 == NULL) { |
| 75 | err = -EMSGSIZE; |
| 76 | goto out; |
| 77 | } |
| 78 | |
| 79 | for (pp = &mp->ports; |
| 80 | (p = rcu_dereference(*pp)) != NULL; |
| 81 | pp = &p->next) { |
| 82 | port = p->port; |
| 83 | if (port) { |
| 84 | struct br_mdb_entry e; |
Mathias Krause | c085c49 | 2013-03-09 05:52:19 +0000 | [diff] [blame] | 85 | memset(&e, 0, sizeof(e)); |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 86 | e.ifindex = port->dev->ifindex; |
Amerigo Wang | ccb1c31 | 2012-12-14 22:09:51 +0000 | [diff] [blame] | 87 | e.state = p->state; |
Vlad Yasevich | 09d7cf7 | 2012-12-18 11:54:08 +0000 | [diff] [blame] | 88 | if (p->addr.proto == htons(ETH_P_IP)) |
| 89 | e.addr.u.ip4 = p->addr.u.ip4; |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 90 | #if IS_ENABLED(CONFIG_IPV6) |
Vlad Yasevich | 09d7cf7 | 2012-12-18 11:54:08 +0000 | [diff] [blame] | 91 | if (p->addr.proto == htons(ETH_P_IPV6)) |
| 92 | e.addr.u.ip6 = p->addr.u.ip6; |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 93 | #endif |
| 94 | e.addr.proto = p->addr.proto; |
| 95 | if (nla_put(skb, MDBA_MDB_ENTRY_INFO, sizeof(e), &e)) { |
| 96 | nla_nest_cancel(skb, nest2); |
| 97 | err = -EMSGSIZE; |
| 98 | goto out; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | nla_nest_end(skb, nest2); |
| 103 | skip: |
| 104 | idx++; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | out: |
| 109 | cb->args[1] = idx; |
| 110 | nla_nest_end(skb, nest); |
| 111 | return err; |
| 112 | } |
| 113 | |
| 114 | static int br_mdb_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 115 | { |
| 116 | struct net_device *dev; |
| 117 | struct net *net = sock_net(skb->sk); |
| 118 | struct nlmsghdr *nlh = NULL; |
| 119 | int idx = 0, s_idx; |
| 120 | |
| 121 | s_idx = cb->args[0]; |
| 122 | |
| 123 | rcu_read_lock(); |
| 124 | |
Cong Wang | 2ce297f | 2012-12-10 02:15:35 +0000 | [diff] [blame] | 125 | /* In theory this could be wrapped to 0... */ |
| 126 | cb->seq = net->dev_base_seq + br_mdb_rehash_seq; |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 127 | |
| 128 | for_each_netdev_rcu(net, dev) { |
| 129 | if (dev->priv_flags & IFF_EBRIDGE) { |
| 130 | struct br_port_msg *bpm; |
| 131 | |
| 132 | if (idx < s_idx) |
| 133 | goto skip; |
| 134 | |
| 135 | nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, |
| 136 | cb->nlh->nlmsg_seq, RTM_GETMDB, |
| 137 | sizeof(*bpm), NLM_F_MULTI); |
| 138 | if (nlh == NULL) |
| 139 | break; |
| 140 | |
| 141 | bpm = nlmsg_data(nlh); |
Mathias Krause | c085c49 | 2013-03-09 05:52:19 +0000 | [diff] [blame] | 142 | memset(bpm, 0, sizeof(*bpm)); |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 143 | bpm->ifindex = dev->ifindex; |
| 144 | if (br_mdb_fill_info(skb, cb, dev) < 0) |
| 145 | goto out; |
| 146 | if (br_rports_fill_info(skb, cb, dev) < 0) |
| 147 | goto out; |
| 148 | |
| 149 | cb->args[1] = 0; |
| 150 | nlmsg_end(skb, nlh); |
| 151 | skip: |
| 152 | idx++; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | out: |
| 157 | if (nlh) |
| 158 | nlmsg_end(skb, nlh); |
| 159 | rcu_read_unlock(); |
| 160 | cb->args[0] = idx; |
| 161 | return skb->len; |
| 162 | } |
| 163 | |
Cong Wang | 37a393b | 2012-12-11 22:23:07 +0000 | [diff] [blame] | 164 | static int nlmsg_populate_mdb_fill(struct sk_buff *skb, |
| 165 | struct net_device *dev, |
| 166 | struct br_mdb_entry *entry, u32 pid, |
| 167 | u32 seq, int type, unsigned int flags) |
| 168 | { |
| 169 | struct nlmsghdr *nlh; |
| 170 | struct br_port_msg *bpm; |
| 171 | struct nlattr *nest, *nest2; |
| 172 | |
Nicolas Dichtel | 8219967 | 2015-04-28 18:33:48 +0200 | [diff] [blame] | 173 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*bpm), 0); |
Cong Wang | 37a393b | 2012-12-11 22:23:07 +0000 | [diff] [blame] | 174 | if (!nlh) |
| 175 | return -EMSGSIZE; |
| 176 | |
| 177 | bpm = nlmsg_data(nlh); |
Mathias Krause | c085c49 | 2013-03-09 05:52:19 +0000 | [diff] [blame] | 178 | memset(bpm, 0, sizeof(*bpm)); |
Cong Wang | 37a393b | 2012-12-11 22:23:07 +0000 | [diff] [blame] | 179 | bpm->family = AF_BRIDGE; |
| 180 | bpm->ifindex = dev->ifindex; |
| 181 | nest = nla_nest_start(skb, MDBA_MDB); |
| 182 | if (nest == NULL) |
| 183 | goto cancel; |
| 184 | nest2 = nla_nest_start(skb, MDBA_MDB_ENTRY); |
| 185 | if (nest2 == NULL) |
| 186 | goto end; |
| 187 | |
| 188 | if (nla_put(skb, MDBA_MDB_ENTRY_INFO, sizeof(*entry), entry)) |
| 189 | goto end; |
| 190 | |
| 191 | nla_nest_end(skb, nest2); |
| 192 | nla_nest_end(skb, nest); |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 193 | nlmsg_end(skb, nlh); |
| 194 | return 0; |
Cong Wang | 37a393b | 2012-12-11 22:23:07 +0000 | [diff] [blame] | 195 | |
| 196 | end: |
| 197 | nla_nest_end(skb, nest); |
| 198 | cancel: |
| 199 | nlmsg_cancel(skb, nlh); |
| 200 | return -EMSGSIZE; |
| 201 | } |
| 202 | |
| 203 | static inline size_t rtnl_mdb_nlmsg_size(void) |
| 204 | { |
| 205 | return NLMSG_ALIGN(sizeof(struct br_port_msg)) |
| 206 | + nla_total_size(sizeof(struct br_mdb_entry)); |
| 207 | } |
| 208 | |
| 209 | static void __br_mdb_notify(struct net_device *dev, struct br_mdb_entry *entry, |
| 210 | int type) |
| 211 | { |
| 212 | struct net *net = dev_net(dev); |
| 213 | struct sk_buff *skb; |
| 214 | int err = -ENOBUFS; |
| 215 | |
| 216 | skb = nlmsg_new(rtnl_mdb_nlmsg_size(), GFP_ATOMIC); |
| 217 | if (!skb) |
| 218 | goto errout; |
| 219 | |
| 220 | err = nlmsg_populate_mdb_fill(skb, dev, entry, 0, 0, type, NTF_SELF); |
| 221 | if (err < 0) { |
| 222 | kfree_skb(skb); |
| 223 | goto errout; |
| 224 | } |
| 225 | |
| 226 | rtnl_notify(skb, net, 0, RTNLGRP_MDB, NULL, GFP_ATOMIC); |
| 227 | return; |
| 228 | errout: |
| 229 | rtnl_set_sk_err(net, RTNLGRP_MDB, err); |
| 230 | } |
| 231 | |
| 232 | void br_mdb_notify(struct net_device *dev, struct net_bridge_port *port, |
| 233 | struct br_ip *group, int type) |
| 234 | { |
| 235 | struct br_mdb_entry entry; |
| 236 | |
Mathias Krause | c085c49 | 2013-03-09 05:52:19 +0000 | [diff] [blame] | 237 | memset(&entry, 0, sizeof(entry)); |
Cong Wang | 37a393b | 2012-12-11 22:23:07 +0000 | [diff] [blame] | 238 | entry.ifindex = port->dev->ifindex; |
| 239 | entry.addr.proto = group->proto; |
| 240 | entry.addr.u.ip4 = group->u.ip4; |
| 241 | #if IS_ENABLED(CONFIG_IPV6) |
| 242 | entry.addr.u.ip6 = group->u.ip6; |
| 243 | #endif |
| 244 | __br_mdb_notify(dev, &entry, type); |
| 245 | } |
| 246 | |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 247 | static bool is_valid_mdb_entry(struct br_mdb_entry *entry) |
| 248 | { |
| 249 | if (entry->ifindex == 0) |
| 250 | return false; |
| 251 | |
| 252 | if (entry->addr.proto == htons(ETH_P_IP)) { |
| 253 | if (!ipv4_is_multicast(entry->addr.u.ip4)) |
| 254 | return false; |
| 255 | if (ipv4_is_local_multicast(entry->addr.u.ip4)) |
| 256 | return false; |
| 257 | #if IS_ENABLED(CONFIG_IPV6) |
| 258 | } else if (entry->addr.proto == htons(ETH_P_IPV6)) { |
Linus Lüssing | 3c3769e | 2013-09-04 02:13:39 +0200 | [diff] [blame] | 259 | if (ipv6_addr_is_ll_all_nodes(&entry->addr.u.ip6)) |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 260 | return false; |
| 261 | #endif |
| 262 | } else |
| 263 | return false; |
Amerigo Wang | ccb1c31 | 2012-12-14 22:09:51 +0000 | [diff] [blame] | 264 | if (entry->state != MDB_PERMANENT && entry->state != MDB_TEMPORARY) |
| 265 | return false; |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 266 | |
| 267 | return true; |
| 268 | } |
| 269 | |
| 270 | static int br_mdb_parse(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 271 | struct net_device **pdev, struct br_mdb_entry **pentry) |
| 272 | { |
| 273 | struct net *net = sock_net(skb->sk); |
| 274 | struct br_mdb_entry *entry; |
| 275 | struct br_port_msg *bpm; |
| 276 | struct nlattr *tb[MDBA_SET_ENTRY_MAX+1]; |
| 277 | struct net_device *dev; |
| 278 | int err; |
| 279 | |
Nicolas Dichtel | 12d8725 | 2015-01-15 16:29:12 +0100 | [diff] [blame] | 280 | err = nlmsg_parse(nlh, sizeof(*bpm), tb, MDBA_SET_ENTRY_MAX, NULL); |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 281 | if (err < 0) |
| 282 | return err; |
| 283 | |
| 284 | bpm = nlmsg_data(nlh); |
| 285 | if (bpm->ifindex == 0) { |
| 286 | pr_info("PF_BRIDGE: br_mdb_parse() with invalid ifindex\n"); |
| 287 | return -EINVAL; |
| 288 | } |
| 289 | |
| 290 | dev = __dev_get_by_index(net, bpm->ifindex); |
| 291 | if (dev == NULL) { |
| 292 | pr_info("PF_BRIDGE: br_mdb_parse() with unknown ifindex\n"); |
| 293 | return -ENODEV; |
| 294 | } |
| 295 | |
| 296 | if (!(dev->priv_flags & IFF_EBRIDGE)) { |
| 297 | pr_info("PF_BRIDGE: br_mdb_parse() with non-bridge\n"); |
| 298 | return -EOPNOTSUPP; |
| 299 | } |
| 300 | |
| 301 | *pdev = dev; |
| 302 | |
| 303 | if (!tb[MDBA_SET_ENTRY] || |
| 304 | nla_len(tb[MDBA_SET_ENTRY]) != sizeof(struct br_mdb_entry)) { |
| 305 | pr_info("PF_BRIDGE: br_mdb_parse() with invalid attr\n"); |
| 306 | return -EINVAL; |
| 307 | } |
| 308 | |
| 309 | entry = nla_data(tb[MDBA_SET_ENTRY]); |
| 310 | if (!is_valid_mdb_entry(entry)) { |
| 311 | pr_info("PF_BRIDGE: br_mdb_parse() with invalid entry\n"); |
| 312 | return -EINVAL; |
| 313 | } |
| 314 | |
| 315 | *pentry = entry; |
| 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | static int br_mdb_add_group(struct net_bridge *br, struct net_bridge_port *port, |
Amerigo Wang | ccb1c31 | 2012-12-14 22:09:51 +0000 | [diff] [blame] | 320 | struct br_ip *group, unsigned char state) |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 321 | { |
| 322 | struct net_bridge_mdb_entry *mp; |
| 323 | struct net_bridge_port_group *p; |
| 324 | struct net_bridge_port_group __rcu **pp; |
| 325 | struct net_bridge_mdb_htable *mdb; |
Satish Ashok | f7e2965 | 2015-07-06 05:53:35 -0700 | [diff] [blame] | 326 | unsigned long now = jiffies; |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 327 | int err; |
| 328 | |
| 329 | mdb = mlock_dereference(br->mdb, br); |
| 330 | mp = br_mdb_ip_get(mdb, group); |
| 331 | if (!mp) { |
| 332 | mp = br_multicast_new_group(br, port, group); |
| 333 | err = PTR_ERR(mp); |
| 334 | if (IS_ERR(mp)) |
| 335 | return err; |
| 336 | } |
| 337 | |
| 338 | for (pp = &mp->ports; |
| 339 | (p = mlock_dereference(*pp, br)) != NULL; |
| 340 | pp = &p->next) { |
| 341 | if (p->port == port) |
| 342 | return -EEXIST; |
| 343 | if ((unsigned long)p->port < (unsigned long)port) |
| 344 | break; |
| 345 | } |
| 346 | |
Amerigo Wang | ccb1c31 | 2012-12-14 22:09:51 +0000 | [diff] [blame] | 347 | p = br_multicast_new_port_group(port, group, *pp, state); |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 348 | if (unlikely(!p)) |
| 349 | return -ENOMEM; |
| 350 | rcu_assign_pointer(*pp, p); |
Satish Ashok | f7e2965 | 2015-07-06 05:53:35 -0700 | [diff] [blame] | 351 | if (state == MDB_TEMPORARY) |
| 352 | mod_timer(&p->timer, now + br->multicast_membership_interval); |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 353 | |
| 354 | br_mdb_notify(br->dev, port, group, RTM_NEWMDB); |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | static int __br_mdb_add(struct net *net, struct net_bridge *br, |
| 359 | struct br_mdb_entry *entry) |
| 360 | { |
| 361 | struct br_ip ip; |
| 362 | struct net_device *dev; |
| 363 | struct net_bridge_port *p; |
| 364 | int ret; |
| 365 | |
| 366 | if (!netif_running(br->dev) || br->multicast_disabled) |
| 367 | return -EINVAL; |
| 368 | |
| 369 | dev = __dev_get_by_index(net, entry->ifindex); |
| 370 | if (!dev) |
| 371 | return -ENODEV; |
| 372 | |
| 373 | p = br_port_get_rtnl(dev); |
| 374 | if (!p || p->br != br || p->state == BR_STATE_DISABLED) |
| 375 | return -EINVAL; |
| 376 | |
Nikolay Aleksandrov | f1158b7 | 2015-07-07 15:55:56 +0200 | [diff] [blame] | 377 | memset(&ip, 0, sizeof(ip)); |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 378 | ip.proto = entry->addr.proto; |
| 379 | if (ip.proto == htons(ETH_P_IP)) |
| 380 | ip.u.ip4 = entry->addr.u.ip4; |
| 381 | #if IS_ENABLED(CONFIG_IPV6) |
| 382 | else |
| 383 | ip.u.ip6 = entry->addr.u.ip6; |
| 384 | #endif |
| 385 | |
| 386 | spin_lock_bh(&br->multicast_lock); |
Amerigo Wang | ccb1c31 | 2012-12-14 22:09:51 +0000 | [diff] [blame] | 387 | ret = br_mdb_add_group(br, p, &ip, entry->state); |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 388 | spin_unlock_bh(&br->multicast_lock); |
| 389 | return ret; |
| 390 | } |
| 391 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 392 | static int br_mdb_add(struct sk_buff *skb, struct nlmsghdr *nlh) |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 393 | { |
| 394 | struct net *net = sock_net(skb->sk); |
| 395 | struct br_mdb_entry *entry; |
| 396 | struct net_device *dev; |
| 397 | struct net_bridge *br; |
| 398 | int err; |
| 399 | |
| 400 | err = br_mdb_parse(skb, nlh, &dev, &entry); |
| 401 | if (err < 0) |
| 402 | return err; |
| 403 | |
| 404 | br = netdev_priv(dev); |
| 405 | |
| 406 | err = __br_mdb_add(net, br, entry); |
| 407 | if (!err) |
| 408 | __br_mdb_notify(dev, entry, RTM_NEWMDB); |
| 409 | return err; |
| 410 | } |
| 411 | |
| 412 | static int __br_mdb_del(struct net_bridge *br, struct br_mdb_entry *entry) |
| 413 | { |
| 414 | struct net_bridge_mdb_htable *mdb; |
| 415 | struct net_bridge_mdb_entry *mp; |
| 416 | struct net_bridge_port_group *p; |
| 417 | struct net_bridge_port_group __rcu **pp; |
| 418 | struct br_ip ip; |
| 419 | int err = -EINVAL; |
| 420 | |
| 421 | if (!netif_running(br->dev) || br->multicast_disabled) |
| 422 | return -EINVAL; |
| 423 | |
Nikolay Aleksandrov | f1158b7 | 2015-07-07 15:55:56 +0200 | [diff] [blame] | 424 | memset(&ip, 0, sizeof(ip)); |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 425 | ip.proto = entry->addr.proto; |
Satish Ashok | 51ed7f3e | 2015-07-09 04:12:45 -0700 | [diff] [blame] | 426 | if (ip.proto == htons(ETH_P_IP)) |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 427 | ip.u.ip4 = entry->addr.u.ip4; |
| 428 | #if IS_ENABLED(CONFIG_IPV6) |
Satish Ashok | 51ed7f3e | 2015-07-09 04:12:45 -0700 | [diff] [blame] | 429 | else |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 430 | ip.u.ip6 = entry->addr.u.ip6; |
| 431 | #endif |
| 432 | |
| 433 | spin_lock_bh(&br->multicast_lock); |
| 434 | mdb = mlock_dereference(br->mdb, br); |
| 435 | |
| 436 | mp = br_mdb_ip_get(mdb, &ip); |
| 437 | if (!mp) |
| 438 | goto unlock; |
| 439 | |
| 440 | for (pp = &mp->ports; |
| 441 | (p = mlock_dereference(*pp, br)) != NULL; |
| 442 | pp = &p->next) { |
| 443 | if (!p->port || p->port->dev->ifindex != entry->ifindex) |
| 444 | continue; |
| 445 | |
| 446 | if (p->port->state == BR_STATE_DISABLED) |
| 447 | goto unlock; |
| 448 | |
| 449 | rcu_assign_pointer(*pp, p->next); |
| 450 | hlist_del_init(&p->mglist); |
| 451 | del_timer(&p->timer); |
| 452 | call_rcu_bh(&p->rcu, br_multicast_free_pg); |
| 453 | err = 0; |
| 454 | |
Linus Lüssing | 454594f | 2013-10-20 00:58:57 +0200 | [diff] [blame] | 455 | if (!mp->ports && !mp->mglist && |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 456 | netif_running(br->dev)) |
| 457 | mod_timer(&mp->timer, jiffies); |
| 458 | break; |
| 459 | } |
| 460 | |
| 461 | unlock: |
| 462 | spin_unlock_bh(&br->multicast_lock); |
| 463 | return err; |
| 464 | } |
| 465 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 466 | static int br_mdb_del(struct sk_buff *skb, struct nlmsghdr *nlh) |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 467 | { |
| 468 | struct net_device *dev; |
| 469 | struct br_mdb_entry *entry; |
| 470 | struct net_bridge *br; |
| 471 | int err; |
| 472 | |
| 473 | err = br_mdb_parse(skb, nlh, &dev, &entry); |
| 474 | if (err < 0) |
| 475 | return err; |
| 476 | |
| 477 | br = netdev_priv(dev); |
| 478 | |
| 479 | err = __br_mdb_del(br, entry); |
| 480 | if (!err) |
| 481 | __br_mdb_notify(dev, entry, RTM_DELMDB); |
| 482 | return err; |
| 483 | } |
| 484 | |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 485 | void br_mdb_init(void) |
| 486 | { |
| 487 | rtnl_register(PF_BRIDGE, RTM_GETMDB, NULL, br_mdb_dump, NULL); |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 488 | rtnl_register(PF_BRIDGE, RTM_NEWMDB, br_mdb_add, NULL, NULL); |
| 489 | rtnl_register(PF_BRIDGE, RTM_DELMDB, br_mdb_del, NULL, NULL); |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 490 | } |
Vlad Yasevich | 6323315 | 2012-12-19 09:13:48 +0000 | [diff] [blame] | 491 | |
| 492 | void br_mdb_uninit(void) |
| 493 | { |
| 494 | rtnl_unregister(PF_BRIDGE, RTM_GETMDB); |
| 495 | rtnl_unregister(PF_BRIDGE, RTM_NEWMDB); |
| 496 | rtnl_unregister(PF_BRIDGE, RTM_DELMDB); |
| 497 | } |