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