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