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