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> |
Elad Raz | f1fecb1 | 2016-01-10 21:06:23 +0100 | [diff] [blame] | 10 | #include <net/switchdev.h> |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 11 | #if IS_ENABLED(CONFIG_IPV6) |
| 12 | #include <net/ipv6.h> |
Linus Lüssing | 3c3769e | 2013-09-04 02:13:39 +0200 | [diff] [blame] | 13 | #include <net/addrconf.h> |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 14 | #endif |
| 15 | |
| 16 | #include "br_private.h" |
| 17 | |
| 18 | static int br_rports_fill_info(struct sk_buff *skb, struct netlink_callback *cb, |
| 19 | struct net_device *dev) |
| 20 | { |
| 21 | struct net_bridge *br = netdev_priv(dev); |
| 22 | struct net_bridge_port *p; |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 23 | struct nlattr *nest; |
| 24 | |
| 25 | if (!br->multicast_router || hlist_empty(&br->router_list)) |
| 26 | return 0; |
| 27 | |
| 28 | nest = nla_nest_start(skb, MDBA_ROUTER); |
| 29 | if (nest == NULL) |
| 30 | return -EMSGSIZE; |
| 31 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 32 | hlist_for_each_entry_rcu(p, &br->router_list, rlist) { |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 33 | if (p && nla_put_u32(skb, MDBA_ROUTER_PORT, p->dev->ifindex)) |
| 34 | goto fail; |
| 35 | } |
| 36 | |
| 37 | nla_nest_end(skb, nest); |
| 38 | return 0; |
| 39 | fail: |
| 40 | nla_nest_cancel(skb, nest); |
| 41 | return -EMSGSIZE; |
| 42 | } |
| 43 | |
| 44 | static int br_mdb_fill_info(struct sk_buff *skb, struct netlink_callback *cb, |
| 45 | struct net_device *dev) |
| 46 | { |
| 47 | struct net_bridge *br = netdev_priv(dev); |
| 48 | struct net_bridge_mdb_htable *mdb; |
| 49 | struct nlattr *nest, *nest2; |
| 50 | int i, err = 0; |
| 51 | int idx = 0, s_idx = cb->args[1]; |
| 52 | |
| 53 | if (br->multicast_disabled) |
| 54 | return 0; |
| 55 | |
| 56 | mdb = rcu_dereference(br->mdb); |
| 57 | if (!mdb) |
| 58 | return 0; |
| 59 | |
| 60 | nest = nla_nest_start(skb, MDBA_MDB); |
| 61 | if (nest == NULL) |
| 62 | return -EMSGSIZE; |
| 63 | |
| 64 | for (i = 0; i < mdb->max; i++) { |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 65 | struct net_bridge_mdb_entry *mp; |
stephen hemminger | 762a3d8 | 2013-08-04 17:19:38 -0700 | [diff] [blame] | 66 | struct net_bridge_port_group *p; |
| 67 | struct net_bridge_port_group __rcu **pp; |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 68 | struct net_bridge_port *port; |
| 69 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 70 | hlist_for_each_entry_rcu(mp, &mdb->mhash[i], hlist[mdb->ver]) { |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 71 | if (idx < s_idx) |
| 72 | goto skip; |
| 73 | |
| 74 | nest2 = nla_nest_start(skb, MDBA_MDB_ENTRY); |
| 75 | if (nest2 == NULL) { |
| 76 | err = -EMSGSIZE; |
| 77 | goto out; |
| 78 | } |
| 79 | |
| 80 | for (pp = &mp->ports; |
| 81 | (p = rcu_dereference(*pp)) != NULL; |
| 82 | pp = &p->next) { |
| 83 | port = p->port; |
| 84 | if (port) { |
| 85 | struct br_mdb_entry e; |
Mathias Krause | c085c49 | 2013-03-09 05:52:19 +0000 | [diff] [blame] | 86 | memset(&e, 0, sizeof(e)); |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 87 | e.ifindex = port->dev->ifindex; |
Amerigo Wang | ccb1c31 | 2012-12-14 22:09:51 +0000 | [diff] [blame] | 88 | e.state = p->state; |
Nikolay Aleksandrov | 74fe61f | 2015-07-10 08:02:08 -0700 | [diff] [blame] | 89 | e.vid = p->addr.vid; |
Vlad Yasevich | 09d7cf7 | 2012-12-18 11:54:08 +0000 | [diff] [blame] | 90 | if (p->addr.proto == htons(ETH_P_IP)) |
| 91 | e.addr.u.ip4 = p->addr.u.ip4; |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 92 | #if IS_ENABLED(CONFIG_IPV6) |
Vlad Yasevich | 09d7cf7 | 2012-12-18 11:54:08 +0000 | [diff] [blame] | 93 | if (p->addr.proto == htons(ETH_P_IPV6)) |
| 94 | e.addr.u.ip6 = p->addr.u.ip6; |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 95 | #endif |
| 96 | e.addr.proto = p->addr.proto; |
| 97 | if (nla_put(skb, MDBA_MDB_ENTRY_INFO, sizeof(e), &e)) { |
| 98 | nla_nest_cancel(skb, nest2); |
| 99 | err = -EMSGSIZE; |
| 100 | goto out; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | nla_nest_end(skb, nest2); |
| 105 | skip: |
| 106 | idx++; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | out: |
| 111 | cb->args[1] = idx; |
| 112 | nla_nest_end(skb, nest); |
| 113 | return err; |
| 114 | } |
| 115 | |
| 116 | static int br_mdb_dump(struct sk_buff *skb, struct netlink_callback *cb) |
| 117 | { |
| 118 | struct net_device *dev; |
| 119 | struct net *net = sock_net(skb->sk); |
| 120 | struct nlmsghdr *nlh = NULL; |
| 121 | int idx = 0, s_idx; |
| 122 | |
| 123 | s_idx = cb->args[0]; |
| 124 | |
| 125 | rcu_read_lock(); |
| 126 | |
Cong Wang | 2ce297f | 2012-12-10 02:15:35 +0000 | [diff] [blame] | 127 | /* In theory this could be wrapped to 0... */ |
| 128 | cb->seq = net->dev_base_seq + br_mdb_rehash_seq; |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 129 | |
| 130 | for_each_netdev_rcu(net, dev) { |
| 131 | if (dev->priv_flags & IFF_EBRIDGE) { |
| 132 | struct br_port_msg *bpm; |
| 133 | |
| 134 | if (idx < s_idx) |
| 135 | goto skip; |
| 136 | |
| 137 | nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, |
| 138 | cb->nlh->nlmsg_seq, RTM_GETMDB, |
| 139 | sizeof(*bpm), NLM_F_MULTI); |
| 140 | if (nlh == NULL) |
| 141 | break; |
| 142 | |
| 143 | bpm = nlmsg_data(nlh); |
Mathias Krause | c085c49 | 2013-03-09 05:52:19 +0000 | [diff] [blame] | 144 | memset(bpm, 0, sizeof(*bpm)); |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 145 | bpm->ifindex = dev->ifindex; |
| 146 | if (br_mdb_fill_info(skb, cb, dev) < 0) |
| 147 | goto out; |
| 148 | if (br_rports_fill_info(skb, cb, dev) < 0) |
| 149 | goto out; |
| 150 | |
| 151 | cb->args[1] = 0; |
| 152 | nlmsg_end(skb, nlh); |
| 153 | skip: |
| 154 | idx++; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | out: |
| 159 | if (nlh) |
| 160 | nlmsg_end(skb, nlh); |
| 161 | rcu_read_unlock(); |
| 162 | cb->args[0] = idx; |
| 163 | return skb->len; |
| 164 | } |
| 165 | |
Cong Wang | 37a393b | 2012-12-11 22:23:07 +0000 | [diff] [blame] | 166 | static int nlmsg_populate_mdb_fill(struct sk_buff *skb, |
| 167 | struct net_device *dev, |
| 168 | struct br_mdb_entry *entry, u32 pid, |
| 169 | u32 seq, int type, unsigned int flags) |
| 170 | { |
| 171 | struct nlmsghdr *nlh; |
| 172 | struct br_port_msg *bpm; |
| 173 | struct nlattr *nest, *nest2; |
| 174 | |
Nicolas Dichtel | 8219967 | 2015-04-28 18:33:48 +0200 | [diff] [blame] | 175 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*bpm), 0); |
Cong Wang | 37a393b | 2012-12-11 22:23:07 +0000 | [diff] [blame] | 176 | if (!nlh) |
| 177 | return -EMSGSIZE; |
| 178 | |
| 179 | bpm = nlmsg_data(nlh); |
Mathias Krause | c085c49 | 2013-03-09 05:52:19 +0000 | [diff] [blame] | 180 | memset(bpm, 0, sizeof(*bpm)); |
Cong Wang | 37a393b | 2012-12-11 22:23:07 +0000 | [diff] [blame] | 181 | bpm->family = AF_BRIDGE; |
| 182 | bpm->ifindex = dev->ifindex; |
| 183 | nest = nla_nest_start(skb, MDBA_MDB); |
| 184 | if (nest == NULL) |
| 185 | goto cancel; |
| 186 | nest2 = nla_nest_start(skb, MDBA_MDB_ENTRY); |
| 187 | if (nest2 == NULL) |
| 188 | goto end; |
| 189 | |
| 190 | if (nla_put(skb, MDBA_MDB_ENTRY_INFO, sizeof(*entry), entry)) |
| 191 | goto end; |
| 192 | |
| 193 | nla_nest_end(skb, nest2); |
| 194 | nla_nest_end(skb, nest); |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 195 | nlmsg_end(skb, nlh); |
| 196 | return 0; |
Cong Wang | 37a393b | 2012-12-11 22:23:07 +0000 | [diff] [blame] | 197 | |
| 198 | end: |
| 199 | nla_nest_end(skb, nest); |
| 200 | cancel: |
| 201 | nlmsg_cancel(skb, nlh); |
| 202 | return -EMSGSIZE; |
| 203 | } |
| 204 | |
| 205 | static inline size_t rtnl_mdb_nlmsg_size(void) |
| 206 | { |
| 207 | return NLMSG_ALIGN(sizeof(struct br_port_msg)) |
| 208 | + nla_total_size(sizeof(struct br_mdb_entry)); |
| 209 | } |
| 210 | |
| 211 | static void __br_mdb_notify(struct net_device *dev, struct br_mdb_entry *entry, |
| 212 | int type) |
| 213 | { |
Elad Raz | f1fecb1 | 2016-01-10 21:06:23 +0100 | [diff] [blame] | 214 | struct switchdev_obj_port_mdb mdb = { |
| 215 | .obj = { |
| 216 | .id = SWITCHDEV_OBJ_ID_PORT_MDB, |
| 217 | .flags = SWITCHDEV_F_DEFER, |
| 218 | }, |
| 219 | .vid = entry->vid, |
| 220 | }; |
| 221 | struct net_device *port_dev; |
Cong Wang | 37a393b | 2012-12-11 22:23:07 +0000 | [diff] [blame] | 222 | struct net *net = dev_net(dev); |
| 223 | struct sk_buff *skb; |
| 224 | int err = -ENOBUFS; |
| 225 | |
Elad Raz | f1fecb1 | 2016-01-10 21:06:23 +0100 | [diff] [blame] | 226 | port_dev = __dev_get_by_index(net, entry->ifindex); |
| 227 | if (entry->addr.proto == htons(ETH_P_IP)) |
| 228 | ip_eth_mc_map(entry->addr.u.ip4, mdb.addr); |
| 229 | #if IS_ENABLED(CONFIG_IPV6) |
| 230 | else |
| 231 | ipv6_eth_mc_map(&entry->addr.u.ip6, mdb.addr); |
| 232 | #endif |
| 233 | |
| 234 | mdb.obj.orig_dev = port_dev; |
| 235 | if (port_dev && type == RTM_NEWMDB) |
| 236 | switchdev_port_obj_add(port_dev, &mdb.obj); |
| 237 | else if (port_dev && type == RTM_DELMDB) |
| 238 | switchdev_port_obj_del(port_dev, &mdb.obj); |
| 239 | |
Cong Wang | 37a393b | 2012-12-11 22:23:07 +0000 | [diff] [blame] | 240 | skb = nlmsg_new(rtnl_mdb_nlmsg_size(), GFP_ATOMIC); |
| 241 | if (!skb) |
| 242 | goto errout; |
| 243 | |
| 244 | err = nlmsg_populate_mdb_fill(skb, dev, entry, 0, 0, type, NTF_SELF); |
| 245 | if (err < 0) { |
| 246 | kfree_skb(skb); |
| 247 | goto errout; |
| 248 | } |
| 249 | |
| 250 | rtnl_notify(skb, net, 0, RTNLGRP_MDB, NULL, GFP_ATOMIC); |
| 251 | return; |
| 252 | errout: |
| 253 | rtnl_set_sk_err(net, RTNLGRP_MDB, err); |
| 254 | } |
| 255 | |
| 256 | void br_mdb_notify(struct net_device *dev, struct net_bridge_port *port, |
Nikolay Aleksandrov | 09cf021 | 2015-07-09 03:11:10 -0700 | [diff] [blame] | 257 | struct br_ip *group, int type, u8 state) |
Cong Wang | 37a393b | 2012-12-11 22:23:07 +0000 | [diff] [blame] | 258 | { |
| 259 | struct br_mdb_entry entry; |
| 260 | |
Mathias Krause | c085c49 | 2013-03-09 05:52:19 +0000 | [diff] [blame] | 261 | memset(&entry, 0, sizeof(entry)); |
Cong Wang | 37a393b | 2012-12-11 22:23:07 +0000 | [diff] [blame] | 262 | entry.ifindex = port->dev->ifindex; |
| 263 | entry.addr.proto = group->proto; |
| 264 | entry.addr.u.ip4 = group->u.ip4; |
| 265 | #if IS_ENABLED(CONFIG_IPV6) |
| 266 | entry.addr.u.ip6 = group->u.ip6; |
| 267 | #endif |
Nikolay Aleksandrov | 09cf021 | 2015-07-09 03:11:10 -0700 | [diff] [blame] | 268 | entry.state = state; |
Nikolay Aleksandrov | 74fe61f | 2015-07-10 08:02:08 -0700 | [diff] [blame] | 269 | entry.vid = group->vid; |
Cong Wang | 37a393b | 2012-12-11 22:23:07 +0000 | [diff] [blame] | 270 | __br_mdb_notify(dev, &entry, type); |
| 271 | } |
| 272 | |
Satish Ashok | 949f1e3 | 2015-07-23 05:00:53 -0700 | [diff] [blame] | 273 | static int nlmsg_populate_rtr_fill(struct sk_buff *skb, |
| 274 | struct net_device *dev, |
| 275 | int ifindex, u32 pid, |
| 276 | u32 seq, int type, unsigned int flags) |
| 277 | { |
| 278 | struct br_port_msg *bpm; |
| 279 | struct nlmsghdr *nlh; |
| 280 | struct nlattr *nest; |
| 281 | |
| 282 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*bpm), NLM_F_MULTI); |
| 283 | if (!nlh) |
| 284 | return -EMSGSIZE; |
| 285 | |
| 286 | bpm = nlmsg_data(nlh); |
| 287 | memset(bpm, 0, sizeof(*bpm)); |
| 288 | bpm->family = AF_BRIDGE; |
| 289 | bpm->ifindex = dev->ifindex; |
| 290 | nest = nla_nest_start(skb, MDBA_ROUTER); |
| 291 | if (!nest) |
| 292 | goto cancel; |
| 293 | |
| 294 | if (nla_put_u32(skb, MDBA_ROUTER_PORT, ifindex)) |
| 295 | goto end; |
| 296 | |
| 297 | nla_nest_end(skb, nest); |
| 298 | nlmsg_end(skb, nlh); |
| 299 | return 0; |
| 300 | |
| 301 | end: |
| 302 | nla_nest_end(skb, nest); |
| 303 | cancel: |
| 304 | nlmsg_cancel(skb, nlh); |
| 305 | return -EMSGSIZE; |
| 306 | } |
| 307 | |
| 308 | static inline size_t rtnl_rtr_nlmsg_size(void) |
| 309 | { |
| 310 | return NLMSG_ALIGN(sizeof(struct br_port_msg)) |
| 311 | + nla_total_size(sizeof(__u32)); |
| 312 | } |
| 313 | |
| 314 | void br_rtr_notify(struct net_device *dev, struct net_bridge_port *port, |
| 315 | int type) |
| 316 | { |
| 317 | struct net *net = dev_net(dev); |
| 318 | struct sk_buff *skb; |
| 319 | int err = -ENOBUFS; |
| 320 | int ifindex; |
| 321 | |
| 322 | ifindex = port ? port->dev->ifindex : 0; |
| 323 | skb = nlmsg_new(rtnl_rtr_nlmsg_size(), GFP_ATOMIC); |
| 324 | if (!skb) |
| 325 | goto errout; |
| 326 | |
| 327 | err = nlmsg_populate_rtr_fill(skb, dev, ifindex, 0, 0, type, NTF_SELF); |
| 328 | if (err < 0) { |
| 329 | kfree_skb(skb); |
| 330 | goto errout; |
| 331 | } |
| 332 | |
| 333 | rtnl_notify(skb, net, 0, RTNLGRP_MDB, NULL, GFP_ATOMIC); |
| 334 | return; |
| 335 | |
| 336 | errout: |
| 337 | rtnl_set_sk_err(net, RTNLGRP_MDB, err); |
| 338 | } |
| 339 | |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 340 | static bool is_valid_mdb_entry(struct br_mdb_entry *entry) |
| 341 | { |
| 342 | if (entry->ifindex == 0) |
| 343 | return false; |
| 344 | |
| 345 | if (entry->addr.proto == htons(ETH_P_IP)) { |
| 346 | if (!ipv4_is_multicast(entry->addr.u.ip4)) |
| 347 | return false; |
| 348 | if (ipv4_is_local_multicast(entry->addr.u.ip4)) |
| 349 | return false; |
| 350 | #if IS_ENABLED(CONFIG_IPV6) |
| 351 | } else if (entry->addr.proto == htons(ETH_P_IPV6)) { |
Linus Lüssing | 3c3769e | 2013-09-04 02:13:39 +0200 | [diff] [blame] | 352 | if (ipv6_addr_is_ll_all_nodes(&entry->addr.u.ip6)) |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 353 | return false; |
| 354 | #endif |
| 355 | } else |
| 356 | return false; |
Amerigo Wang | ccb1c31 | 2012-12-14 22:09:51 +0000 | [diff] [blame] | 357 | if (entry->state != MDB_PERMANENT && entry->state != MDB_TEMPORARY) |
| 358 | return false; |
Nikolay Aleksandrov | 74fe61f | 2015-07-10 08:02:08 -0700 | [diff] [blame] | 359 | if (entry->vid >= VLAN_VID_MASK) |
| 360 | return false; |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 361 | |
| 362 | return true; |
| 363 | } |
| 364 | |
| 365 | static int br_mdb_parse(struct sk_buff *skb, struct nlmsghdr *nlh, |
| 366 | struct net_device **pdev, struct br_mdb_entry **pentry) |
| 367 | { |
| 368 | struct net *net = sock_net(skb->sk); |
| 369 | struct br_mdb_entry *entry; |
| 370 | struct br_port_msg *bpm; |
| 371 | struct nlattr *tb[MDBA_SET_ENTRY_MAX+1]; |
| 372 | struct net_device *dev; |
| 373 | int err; |
| 374 | |
Nicolas Dichtel | 12d8725 | 2015-01-15 16:29:12 +0100 | [diff] [blame] | 375 | err = nlmsg_parse(nlh, sizeof(*bpm), tb, MDBA_SET_ENTRY_MAX, NULL); |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 376 | if (err < 0) |
| 377 | return err; |
| 378 | |
| 379 | bpm = nlmsg_data(nlh); |
| 380 | if (bpm->ifindex == 0) { |
| 381 | pr_info("PF_BRIDGE: br_mdb_parse() with invalid ifindex\n"); |
| 382 | return -EINVAL; |
| 383 | } |
| 384 | |
| 385 | dev = __dev_get_by_index(net, bpm->ifindex); |
| 386 | if (dev == NULL) { |
| 387 | pr_info("PF_BRIDGE: br_mdb_parse() with unknown ifindex\n"); |
| 388 | return -ENODEV; |
| 389 | } |
| 390 | |
| 391 | if (!(dev->priv_flags & IFF_EBRIDGE)) { |
| 392 | pr_info("PF_BRIDGE: br_mdb_parse() with non-bridge\n"); |
| 393 | return -EOPNOTSUPP; |
| 394 | } |
| 395 | |
| 396 | *pdev = dev; |
| 397 | |
| 398 | if (!tb[MDBA_SET_ENTRY] || |
| 399 | nla_len(tb[MDBA_SET_ENTRY]) != sizeof(struct br_mdb_entry)) { |
| 400 | pr_info("PF_BRIDGE: br_mdb_parse() with invalid attr\n"); |
| 401 | return -EINVAL; |
| 402 | } |
| 403 | |
| 404 | entry = nla_data(tb[MDBA_SET_ENTRY]); |
| 405 | if (!is_valid_mdb_entry(entry)) { |
| 406 | pr_info("PF_BRIDGE: br_mdb_parse() with invalid entry\n"); |
| 407 | return -EINVAL; |
| 408 | } |
| 409 | |
| 410 | *pentry = entry; |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | 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] | 415 | struct br_ip *group, unsigned char state) |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 416 | { |
| 417 | struct net_bridge_mdb_entry *mp; |
| 418 | struct net_bridge_port_group *p; |
| 419 | struct net_bridge_port_group __rcu **pp; |
| 420 | struct net_bridge_mdb_htable *mdb; |
Satish Ashok | f7e2965 | 2015-07-06 05:53:35 -0700 | [diff] [blame] | 421 | unsigned long now = jiffies; |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 422 | int err; |
| 423 | |
| 424 | mdb = mlock_dereference(br->mdb, br); |
| 425 | mp = br_mdb_ip_get(mdb, group); |
| 426 | if (!mp) { |
| 427 | mp = br_multicast_new_group(br, port, group); |
| 428 | err = PTR_ERR(mp); |
| 429 | if (IS_ERR(mp)) |
| 430 | return err; |
| 431 | } |
| 432 | |
| 433 | for (pp = &mp->ports; |
| 434 | (p = mlock_dereference(*pp, br)) != NULL; |
| 435 | pp = &p->next) { |
| 436 | if (p->port == port) |
| 437 | return -EEXIST; |
| 438 | if ((unsigned long)p->port < (unsigned long)port) |
| 439 | break; |
| 440 | } |
| 441 | |
Amerigo Wang | ccb1c31 | 2012-12-14 22:09:51 +0000 | [diff] [blame] | 442 | p = br_multicast_new_port_group(port, group, *pp, state); |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 443 | if (unlikely(!p)) |
| 444 | return -ENOMEM; |
| 445 | rcu_assign_pointer(*pp, p); |
Satish Ashok | f7e2965 | 2015-07-06 05:53:35 -0700 | [diff] [blame] | 446 | if (state == MDB_TEMPORARY) |
| 447 | mod_timer(&p->timer, now + br->multicast_membership_interval); |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 448 | |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | |
| 452 | static int __br_mdb_add(struct net *net, struct net_bridge *br, |
| 453 | struct br_mdb_entry *entry) |
| 454 | { |
| 455 | struct br_ip ip; |
| 456 | struct net_device *dev; |
| 457 | struct net_bridge_port *p; |
| 458 | int ret; |
| 459 | |
| 460 | if (!netif_running(br->dev) || br->multicast_disabled) |
| 461 | return -EINVAL; |
| 462 | |
| 463 | dev = __dev_get_by_index(net, entry->ifindex); |
| 464 | if (!dev) |
| 465 | return -ENODEV; |
| 466 | |
| 467 | p = br_port_get_rtnl(dev); |
| 468 | if (!p || p->br != br || p->state == BR_STATE_DISABLED) |
| 469 | return -EINVAL; |
| 470 | |
Nikolay Aleksandrov | f1158b7 | 2015-07-07 15:55:56 +0200 | [diff] [blame] | 471 | memset(&ip, 0, sizeof(ip)); |
Nikolay Aleksandrov | 74fe61f | 2015-07-10 08:02:08 -0700 | [diff] [blame] | 472 | ip.vid = entry->vid; |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 473 | ip.proto = entry->addr.proto; |
| 474 | if (ip.proto == htons(ETH_P_IP)) |
| 475 | ip.u.ip4 = entry->addr.u.ip4; |
| 476 | #if IS_ENABLED(CONFIG_IPV6) |
| 477 | else |
| 478 | ip.u.ip6 = entry->addr.u.ip6; |
| 479 | #endif |
| 480 | |
| 481 | spin_lock_bh(&br->multicast_lock); |
Amerigo Wang | ccb1c31 | 2012-12-14 22:09:51 +0000 | [diff] [blame] | 482 | ret = br_mdb_add_group(br, p, &ip, entry->state); |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 483 | spin_unlock_bh(&br->multicast_lock); |
| 484 | return ret; |
| 485 | } |
| 486 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 487 | static int br_mdb_add(struct sk_buff *skb, struct nlmsghdr *nlh) |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 488 | { |
| 489 | struct net *net = sock_net(skb->sk); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 490 | struct net_bridge_vlan_group *vg; |
Satish Ashok | e44deb2 | 2015-08-03 13:29:16 +0200 | [diff] [blame] | 491 | struct net_device *dev, *pdev; |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 492 | struct br_mdb_entry *entry; |
Satish Ashok | e44deb2 | 2015-08-03 13:29:16 +0200 | [diff] [blame] | 493 | struct net_bridge_port *p; |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 494 | struct net_bridge_vlan *v; |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 495 | struct net_bridge *br; |
| 496 | int err; |
| 497 | |
| 498 | err = br_mdb_parse(skb, nlh, &dev, &entry); |
| 499 | if (err < 0) |
| 500 | return err; |
| 501 | |
| 502 | br = netdev_priv(dev); |
| 503 | |
Satish Ashok | e44deb2 | 2015-08-03 13:29:16 +0200 | [diff] [blame] | 504 | /* If vlan filtering is enabled and VLAN is not specified |
| 505 | * install mdb entry on all vlans configured on the port. |
| 506 | */ |
| 507 | pdev = __dev_get_by_index(net, entry->ifindex); |
| 508 | if (!pdev) |
| 509 | return -ENODEV; |
| 510 | |
| 511 | p = br_port_get_rtnl(pdev); |
| 512 | if (!p || p->br != br || p->state == BR_STATE_DISABLED) |
| 513 | return -EINVAL; |
| 514 | |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 515 | vg = nbp_vlan_group(p); |
| 516 | if (br_vlan_enabled(br) && vg && entry->vid == 0) { |
| 517 | list_for_each_entry(v, &vg->vlan_list, vlist) { |
| 518 | entry->vid = v->vid; |
Satish Ashok | e44deb2 | 2015-08-03 13:29:16 +0200 | [diff] [blame] | 519 | err = __br_mdb_add(net, br, entry); |
| 520 | if (err) |
| 521 | break; |
| 522 | __br_mdb_notify(dev, entry, RTM_NEWMDB); |
| 523 | } |
| 524 | } else { |
| 525 | err = __br_mdb_add(net, br, entry); |
| 526 | if (!err) |
| 527 | __br_mdb_notify(dev, entry, RTM_NEWMDB); |
| 528 | } |
| 529 | |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 530 | return err; |
| 531 | } |
| 532 | |
| 533 | static int __br_mdb_del(struct net_bridge *br, struct br_mdb_entry *entry) |
| 534 | { |
| 535 | struct net_bridge_mdb_htable *mdb; |
| 536 | struct net_bridge_mdb_entry *mp; |
| 537 | struct net_bridge_port_group *p; |
| 538 | struct net_bridge_port_group __rcu **pp; |
| 539 | struct br_ip ip; |
| 540 | int err = -EINVAL; |
| 541 | |
| 542 | if (!netif_running(br->dev) || br->multicast_disabled) |
| 543 | return -EINVAL; |
| 544 | |
Nikolay Aleksandrov | f1158b7 | 2015-07-07 15:55:56 +0200 | [diff] [blame] | 545 | memset(&ip, 0, sizeof(ip)); |
Nikolay Aleksandrov | 74fe61f | 2015-07-10 08:02:08 -0700 | [diff] [blame] | 546 | ip.vid = entry->vid; |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 547 | ip.proto = entry->addr.proto; |
Satish Ashok | 51ed7f3e | 2015-07-09 04:12:45 -0700 | [diff] [blame] | 548 | if (ip.proto == htons(ETH_P_IP)) |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 549 | ip.u.ip4 = entry->addr.u.ip4; |
| 550 | #if IS_ENABLED(CONFIG_IPV6) |
Satish Ashok | 51ed7f3e | 2015-07-09 04:12:45 -0700 | [diff] [blame] | 551 | else |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 552 | ip.u.ip6 = entry->addr.u.ip6; |
| 553 | #endif |
| 554 | |
| 555 | spin_lock_bh(&br->multicast_lock); |
| 556 | mdb = mlock_dereference(br->mdb, br); |
| 557 | |
| 558 | mp = br_mdb_ip_get(mdb, &ip); |
| 559 | if (!mp) |
| 560 | goto unlock; |
| 561 | |
| 562 | for (pp = &mp->ports; |
| 563 | (p = mlock_dereference(*pp, br)) != NULL; |
| 564 | pp = &p->next) { |
| 565 | if (!p->port || p->port->dev->ifindex != entry->ifindex) |
| 566 | continue; |
| 567 | |
| 568 | if (p->port->state == BR_STATE_DISABLED) |
| 569 | goto unlock; |
| 570 | |
Nikolay Aleksandrov | 7ae90a4 | 2015-07-28 13:10:44 +0200 | [diff] [blame] | 571 | entry->state = p->state; |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 572 | rcu_assign_pointer(*pp, p->next); |
| 573 | hlist_del_init(&p->mglist); |
| 574 | del_timer(&p->timer); |
| 575 | call_rcu_bh(&p->rcu, br_multicast_free_pg); |
| 576 | err = 0; |
| 577 | |
Linus Lüssing | 454594f | 2013-10-20 00:58:57 +0200 | [diff] [blame] | 578 | if (!mp->ports && !mp->mglist && |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 579 | netif_running(br->dev)) |
| 580 | mod_timer(&mp->timer, jiffies); |
| 581 | break; |
| 582 | } |
| 583 | |
| 584 | unlock: |
| 585 | spin_unlock_bh(&br->multicast_lock); |
| 586 | return err; |
| 587 | } |
| 588 | |
Thomas Graf | 661d296 | 2013-03-21 07:45:29 +0000 | [diff] [blame] | 589 | static int br_mdb_del(struct sk_buff *skb, struct nlmsghdr *nlh) |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 590 | { |
Satish Ashok | e44deb2 | 2015-08-03 13:29:16 +0200 | [diff] [blame] | 591 | struct net *net = sock_net(skb->sk); |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 592 | struct net_bridge_vlan_group *vg; |
Satish Ashok | e44deb2 | 2015-08-03 13:29:16 +0200 | [diff] [blame] | 593 | struct net_device *dev, *pdev; |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 594 | struct br_mdb_entry *entry; |
Satish Ashok | e44deb2 | 2015-08-03 13:29:16 +0200 | [diff] [blame] | 595 | struct net_bridge_port *p; |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 596 | struct net_bridge_vlan *v; |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 597 | struct net_bridge *br; |
| 598 | int err; |
| 599 | |
| 600 | err = br_mdb_parse(skb, nlh, &dev, &entry); |
| 601 | if (err < 0) |
| 602 | return err; |
| 603 | |
| 604 | br = netdev_priv(dev); |
| 605 | |
Satish Ashok | e44deb2 | 2015-08-03 13:29:16 +0200 | [diff] [blame] | 606 | /* If vlan filtering is enabled and VLAN is not specified |
| 607 | * delete mdb entry on all vlans configured on the port. |
| 608 | */ |
| 609 | pdev = __dev_get_by_index(net, entry->ifindex); |
| 610 | if (!pdev) |
| 611 | return -ENODEV; |
| 612 | |
| 613 | p = br_port_get_rtnl(pdev); |
| 614 | if (!p || p->br != br || p->state == BR_STATE_DISABLED) |
| 615 | return -EINVAL; |
| 616 | |
Nikolay Aleksandrov | 2594e90 | 2015-09-25 19:00:11 +0200 | [diff] [blame] | 617 | vg = nbp_vlan_group(p); |
| 618 | if (br_vlan_enabled(br) && vg && entry->vid == 0) { |
| 619 | list_for_each_entry(v, &vg->vlan_list, vlist) { |
| 620 | entry->vid = v->vid; |
Satish Ashok | e44deb2 | 2015-08-03 13:29:16 +0200 | [diff] [blame] | 621 | err = __br_mdb_del(br, entry); |
| 622 | if (!err) |
| 623 | __br_mdb_notify(dev, entry, RTM_DELMDB); |
| 624 | } |
| 625 | } else { |
| 626 | err = __br_mdb_del(br, entry); |
| 627 | if (!err) |
| 628 | __br_mdb_notify(dev, entry, RTM_DELMDB); |
| 629 | } |
| 630 | |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 631 | return err; |
| 632 | } |
| 633 | |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 634 | void br_mdb_init(void) |
| 635 | { |
| 636 | rtnl_register(PF_BRIDGE, RTM_GETMDB, NULL, br_mdb_dump, NULL); |
Cong Wang | cfd5675 | 2012-12-11 22:23:08 +0000 | [diff] [blame] | 637 | rtnl_register(PF_BRIDGE, RTM_NEWMDB, br_mdb_add, NULL, NULL); |
| 638 | rtnl_register(PF_BRIDGE, RTM_DELMDB, br_mdb_del, NULL, NULL); |
Cong Wang | ee07c6e | 2012-12-07 00:04:48 +0000 | [diff] [blame] | 639 | } |
Vlad Yasevich | 6323315 | 2012-12-19 09:13:48 +0000 | [diff] [blame] | 640 | |
| 641 | void br_mdb_uninit(void) |
| 642 | { |
| 643 | rtnl_unregister(PF_BRIDGE, RTM_GETMDB); |
| 644 | rtnl_unregister(PF_BRIDGE, RTM_NEWMDB); |
| 645 | rtnl_unregister(PF_BRIDGE, RTM_DELMDB); |
| 646 | } |