1 /* 2 * Userspace interface 3 * Linux ethernet bridge 4 * 5 * Authors: 6 * Lennert Buytenhek <buytenh@gnu.org> 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License 10 * as published by the Free Software Foundation; either version 11 * 2 of the License, or (at your option) any later version. 12 */ 13 14 #include <linux/kernel.h> 15 #include <linux/netdevice.h> 16 #include <linux/ethtool.h> 17 #include <linux/if_arp.h> 18 #include <linux/module.h> 19 #include <linux/init.h> 20 #include <linux/rtnetlink.h> 21 #include <linux/if_ether.h> 22 #include <linux/slab.h> 23 #include <net/sock.h> 24 25 #include "br_private.h" 26 27 /* 28 * Determine initial path cost based on speed. 29 * using recommendations from 802.1d standard 30 * 31 * Since driver might sleep need to not be holding any locks. 32 */ 33 static int port_cost(struct net_device *dev) 34 { 35 if (dev->ethtool_ops && dev->ethtool_ops->get_settings) { 36 struct ethtool_cmd ecmd = { .cmd = ETHTOOL_GSET, }; 37 38 if (!dev->ethtool_ops->get_settings(dev, &ecmd)) { 39 switch(ecmd.speed) { 40 case SPEED_10000: 41 return 2; 42 case SPEED_1000: 43 return 4; 44 case SPEED_100: 45 return 19; 46 case SPEED_10: 47 return 100; 48 } 49 } 50 } 51 52 /* Old silly heuristics based on name */ 53 if (!strncmp(dev->name, "lec", 3)) 54 return 7; 55 56 if (!strncmp(dev->name, "plip", 4)) 57 return 2500; 58 59 return 100; /* assume old 10Mbps */ 60 } 61 62 63 /* 64 * Check for port carrier transistions. 65 * Called from work queue to allow for calling functions that 66 * might sleep (such as speed check), and to debounce. 67 */ 68 void br_port_carrier_check(struct net_bridge_port *p) 69 { 70 struct net_device *dev = p->dev; 71 struct net_bridge *br = p->br; 72 73 if (netif_carrier_ok(dev)) 74 p->path_cost = port_cost(dev); 75 76 if (netif_running(br->dev)) { 77 spin_lock_bh(&br->lock); 78 if (netif_carrier_ok(dev)) { 79 if (p->state == BR_STATE_DISABLED) 80 br_stp_enable_port(p); 81 } else { 82 if (p->state != BR_STATE_DISABLED) 83 br_stp_disable_port(p); 84 } 85 spin_unlock_bh(&br->lock); 86 } 87 } 88 89 static void release_nbp(struct kobject *kobj) 90 { 91 struct net_bridge_port *p 92 = container_of(kobj, struct net_bridge_port, kobj); 93 kfree(p); 94 } 95 96 static struct kobj_type brport_ktype = { 97 #ifdef CONFIG_SYSFS 98 .sysfs_ops = &brport_sysfs_ops, 99 #endif 100 .release = release_nbp, 101 }; 102 103 static void destroy_nbp(struct net_bridge_port *p) 104 { 105 struct net_device *dev = p->dev; 106 107 p->br = NULL; 108 p->dev = NULL; 109 dev_put(dev); 110 111 kobject_put(&p->kobj); 112 } 113 114 static void destroy_nbp_rcu(struct rcu_head *head) 115 { 116 struct net_bridge_port *p = 117 container_of(head, struct net_bridge_port, rcu); 118 destroy_nbp(p); 119 } 120 121 /* Delete port(interface) from bridge is done in two steps. 122 * via RCU. First step, marks device as down. That deletes 123 * all the timers and stops new packets from flowing through. 124 * 125 * Final cleanup doesn't occur until after all CPU's finished 126 * processing packets. 127 * 128 * Protected from multiple admin operations by RTNL mutex 129 */ 130 static void del_nbp(struct net_bridge_port *p) 131 { 132 struct net_bridge *br = p->br; 133 struct net_device *dev = p->dev; 134 135 sysfs_remove_link(br->ifobj, dev->name); 136 137 dev_set_promiscuity(dev, -1); 138 139 spin_lock_bh(&br->lock); 140 br_stp_disable_port(p); 141 spin_unlock_bh(&br->lock); 142 143 br_ifinfo_notify(RTM_DELLINK, p); 144 145 br_fdb_delete_by_port(br, p, 1); 146 147 list_del_rcu(&p->list); 148 149 rcu_assign_pointer(dev->br_port, NULL); 150 151 br_multicast_del_port(p); 152 153 kobject_uevent(&p->kobj, KOBJ_REMOVE); 154 kobject_del(&p->kobj); 155 156 call_rcu(&p->rcu, destroy_nbp_rcu); 157 } 158 159 /* called with RTNL */ 160 static void del_br(struct net_bridge *br, struct list_head *head) 161 { 162 struct net_bridge_port *p, *n; 163 164 list_for_each_entry_safe(p, n, &br->port_list, list) { 165 del_nbp(p); 166 } 167 168 del_timer_sync(&br->gc_timer); 169 170 br_sysfs_delbr(br->dev); 171 unregister_netdevice_queue(br->dev, head); 172 } 173 174 static struct net_device *new_bridge_dev(struct net *net, const char *name) 175 { 176 struct net_bridge *br; 177 struct net_device *dev; 178 179 dev = alloc_netdev(sizeof(struct net_bridge), name, 180 br_dev_setup); 181 182 if (!dev) 183 return NULL; 184 dev_net_set(dev, net); 185 186 br = netdev_priv(dev); 187 br->dev = dev; 188 189 spin_lock_init(&br->lock); 190 INIT_LIST_HEAD(&br->port_list); 191 spin_lock_init(&br->hash_lock); 192 193 br->bridge_id.prio[0] = 0x80; 194 br->bridge_id.prio[1] = 0x00; 195 196 memcpy(br->group_addr, br_group_address, ETH_ALEN); 197 198 br->feature_mask = dev->features; 199 br->stp_enabled = BR_NO_STP; 200 br->designated_root = br->bridge_id; 201 br->root_path_cost = 0; 202 br->root_port = 0; 203 br->bridge_max_age = br->max_age = 20 * HZ; 204 br->bridge_hello_time = br->hello_time = 2 * HZ; 205 br->bridge_forward_delay = br->forward_delay = 15 * HZ; 206 br->topology_change = 0; 207 br->topology_change_detected = 0; 208 br->ageing_time = 300 * HZ; 209 210 br_netfilter_rtable_init(br); 211 212 br_stp_timer_init(br); 213 br_multicast_init(br); 214 215 return dev; 216 } 217 218 /* find an available port number */ 219 static int find_portno(struct net_bridge *br) 220 { 221 int index; 222 struct net_bridge_port *p; 223 unsigned long *inuse; 224 225 inuse = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long), 226 GFP_KERNEL); 227 if (!inuse) 228 return -ENOMEM; 229 230 set_bit(0, inuse); /* zero is reserved */ 231 list_for_each_entry(p, &br->port_list, list) { 232 set_bit(p->port_no, inuse); 233 } 234 index = find_first_zero_bit(inuse, BR_MAX_PORTS); 235 kfree(inuse); 236 237 return (index >= BR_MAX_PORTS) ? -EXFULL : index; 238 } 239 240 /* called with RTNL but without bridge lock */ 241 static struct net_bridge_port *new_nbp(struct net_bridge *br, 242 struct net_device *dev) 243 { 244 int index; 245 struct net_bridge_port *p; 246 247 index = find_portno(br); 248 if (index < 0) 249 return ERR_PTR(index); 250 251 p = kzalloc(sizeof(*p), GFP_KERNEL); 252 if (p == NULL) 253 return ERR_PTR(-ENOMEM); 254 255 p->br = br; 256 dev_hold(dev); 257 p->dev = dev; 258 p->path_cost = port_cost(dev); 259 p->priority = 0x8000 >> BR_PORT_BITS; 260 p->port_no = index; 261 p->flags = 0; 262 br_init_port(p); 263 p->state = BR_STATE_DISABLED; 264 br_stp_port_timer_init(p); 265 br_multicast_add_port(p); 266 267 return p; 268 } 269 270 static struct device_type br_type = { 271 .name = "bridge", 272 }; 273 274 int br_add_bridge(struct net *net, const char *name) 275 { 276 struct net_device *dev; 277 int ret; 278 279 dev = new_bridge_dev(net, name); 280 if (!dev) 281 return -ENOMEM; 282 283 rtnl_lock(); 284 if (strchr(dev->name, '%')) { 285 ret = dev_alloc_name(dev, dev->name); 286 if (ret < 0) 287 goto out_free; 288 } 289 290 SET_NETDEV_DEVTYPE(dev, &br_type); 291 292 ret = register_netdevice(dev); 293 if (ret) 294 goto out_free; 295 296 ret = br_sysfs_addbr(dev); 297 if (ret) 298 unregister_netdevice(dev); 299 out: 300 rtnl_unlock(); 301 return ret; 302 303 out_free: 304 free_netdev(dev); 305 goto out; 306 } 307 308 int br_del_bridge(struct net *net, const char *name) 309 { 310 struct net_device *dev; 311 int ret = 0; 312 313 rtnl_lock(); 314 dev = __dev_get_by_name(net, name); 315 if (dev == NULL) 316 ret = -ENXIO; /* Could not find device */ 317 318 else if (!(dev->priv_flags & IFF_EBRIDGE)) { 319 /* Attempt to delete non bridge device! */ 320 ret = -EPERM; 321 } 322 323 else if (dev->flags & IFF_UP) { 324 /* Not shutdown yet. */ 325 ret = -EBUSY; 326 } 327 328 else 329 del_br(netdev_priv(dev), NULL); 330 331 rtnl_unlock(); 332 return ret; 333 } 334 335 /* MTU of the bridge pseudo-device: ETH_DATA_LEN or the minimum of the ports */ 336 int br_min_mtu(const struct net_bridge *br) 337 { 338 const struct net_bridge_port *p; 339 int mtu = 0; 340 341 ASSERT_RTNL(); 342 343 if (list_empty(&br->port_list)) 344 mtu = ETH_DATA_LEN; 345 else { 346 list_for_each_entry(p, &br->port_list, list) { 347 if (!mtu || p->dev->mtu < mtu) 348 mtu = p->dev->mtu; 349 } 350 } 351 return mtu; 352 } 353 354 /* 355 * Recomputes features using slave's features 356 */ 357 void br_features_recompute(struct net_bridge *br) 358 { 359 struct net_bridge_port *p; 360 unsigned long features, mask; 361 362 features = mask = br->feature_mask; 363 if (list_empty(&br->port_list)) 364 goto done; 365 366 features &= ~NETIF_F_ONE_FOR_ALL; 367 368 list_for_each_entry(p, &br->port_list, list) { 369 features = netdev_increment_features(features, 370 p->dev->features, mask); 371 } 372 373 done: 374 br->dev->features = netdev_fix_features(features, NULL); 375 } 376 377 /* called with RTNL */ 378 int br_add_if(struct net_bridge *br, struct net_device *dev) 379 { 380 struct net_bridge_port *p; 381 int err = 0; 382 383 /* Don't allow bridging non-ethernet like devices */ 384 if ((dev->flags & IFF_LOOPBACK) || 385 dev->type != ARPHRD_ETHER || dev->addr_len != ETH_ALEN) 386 return -EINVAL; 387 388 /* No bridging of bridges */ 389 if (dev->netdev_ops->ndo_start_xmit == br_dev_xmit) 390 return -ELOOP; 391 392 /* Device is already being bridged */ 393 if (dev->br_port != NULL) 394 return -EBUSY; 395 396 /* No bridging devices that dislike that (e.g. wireless) */ 397 if (dev->priv_flags & IFF_DONT_BRIDGE) 398 return -EOPNOTSUPP; 399 400 p = new_nbp(br, dev); 401 if (IS_ERR(p)) 402 return PTR_ERR(p); 403 404 err = dev_set_promiscuity(dev, 1); 405 if (err) 406 goto put_back; 407 408 err = kobject_init_and_add(&p->kobj, &brport_ktype, &(dev->dev.kobj), 409 SYSFS_BRIDGE_PORT_ATTR); 410 if (err) 411 goto err0; 412 413 err = br_fdb_insert(br, p, dev->dev_addr); 414 if (err) 415 goto err1; 416 417 err = br_sysfs_addif(p); 418 if (err) 419 goto err2; 420 421 rcu_assign_pointer(dev->br_port, p); 422 dev_disable_lro(dev); 423 424 list_add_rcu(&p->list, &br->port_list); 425 426 spin_lock_bh(&br->lock); 427 br_stp_recalculate_bridge_id(br); 428 br_features_recompute(br); 429 430 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) && 431 (br->dev->flags & IFF_UP)) 432 br_stp_enable_port(p); 433 spin_unlock_bh(&br->lock); 434 435 br_ifinfo_notify(RTM_NEWLINK, p); 436 437 dev_set_mtu(br->dev, br_min_mtu(br)); 438 439 kobject_uevent(&p->kobj, KOBJ_ADD); 440 441 return 0; 442 err2: 443 br_fdb_delete_by_port(br, p, 1); 444 err1: 445 kobject_put(&p->kobj); 446 p = NULL; /* kobject_put frees */ 447 err0: 448 dev_set_promiscuity(dev, -1); 449 put_back: 450 dev_put(dev); 451 kfree(p); 452 return err; 453 } 454 455 /* called with RTNL */ 456 int br_del_if(struct net_bridge *br, struct net_device *dev) 457 { 458 struct net_bridge_port *p = dev->br_port; 459 460 if (!p || p->br != br) 461 return -EINVAL; 462 463 del_nbp(p); 464 465 spin_lock_bh(&br->lock); 466 br_stp_recalculate_bridge_id(br); 467 br_features_recompute(br); 468 spin_unlock_bh(&br->lock); 469 470 return 0; 471 } 472 473 void __net_exit br_net_exit(struct net *net) 474 { 475 struct net_device *dev; 476 LIST_HEAD(list); 477 478 rtnl_lock(); 479 for_each_netdev(net, dev) 480 if (dev->priv_flags & IFF_EBRIDGE) 481 del_br(netdev_priv(dev), &list); 482 483 unregister_netdevice_many(&list); 484 rtnl_unlock(); 485 486 } 487