Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Userspace interface |
| 3 | * Linux ethernet bridge |
| 4 | * |
| 5 | * Authors: |
| 6 | * Lennert Buytenhek <buytenh@gnu.org> |
| 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
stephen hemminger | 77f9859 | 2011-09-30 14:37:26 +0000 | [diff] [blame] | 16 | #include <linux/etherdevice.h> |
WANG Cong | c06ee96 | 2010-05-06 00:48:24 -0700 | [diff] [blame] | 17 | #include <linux/netpoll.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/ethtool.h> |
| 19 | #include <linux/if_arp.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/rtnetlink.h> |
Kris Katterjohn | 46f25df | 2006-01-05 16:35:42 -0800 | [diff] [blame] | 23 | #include <linux/if_ether.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <net/sock.h> |
Vlad Yasevich | 407af32 | 2013-02-13 12:00:12 +0000 | [diff] [blame] | 26 | #include <linux/if_vlan.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | #include "br_private.h" |
| 29 | |
| 30 | /* |
| 31 | * Determine initial path cost based on speed. |
| 32 | * using recommendations from 802.1d standard |
| 33 | * |
Matthew Wilcox | 61a44b9 | 2007-07-31 14:00:02 -0700 | [diff] [blame] | 34 | * Since driver might sleep need to not be holding any locks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | */ |
Stephen Hemminger | 4433f42 | 2005-12-20 15:19:51 -0800 | [diff] [blame] | 36 | static int port_cost(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | { |
Jiri Pirko | fa3df92 | 2011-09-01 03:29:38 +0000 | [diff] [blame] | 38 | struct ethtool_cmd ecmd; |
Stephen Hemminger | b4a488d | 2007-08-30 22:16:22 -0700 | [diff] [blame] | 39 | |
Jiri Pirko | 4bc71cb | 2011-09-03 03:34:30 +0000 | [diff] [blame] | 40 | if (!__ethtool_get_settings(dev, &ecmd)) { |
Jiri Pirko | fa3df92 | 2011-09-01 03:29:38 +0000 | [diff] [blame] | 41 | switch (ethtool_cmd_speed(&ecmd)) { |
| 42 | case SPEED_10000: |
| 43 | return 2; |
| 44 | case SPEED_1000: |
| 45 | return 4; |
| 46 | case SPEED_100: |
| 47 | return 19; |
| 48 | case SPEED_10: |
| 49 | return 100; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | |
| 53 | /* Old silly heuristics based on name */ |
| 54 | if (!strncmp(dev->name, "lec", 3)) |
| 55 | return 7; |
| 56 | |
| 57 | if (!strncmp(dev->name, "plip", 4)) |
| 58 | return 2500; |
| 59 | |
| 60 | return 100; /* assume old 10Mbps */ |
| 61 | } |
| 62 | |
Stephen Hemminger | 4433f42 | 2005-12-20 15:19:51 -0800 | [diff] [blame] | 63 | |
tanxiaojun | 1a81a2e | 2013-12-16 21:32:46 +0800 | [diff] [blame] | 64 | /* Check for port carrier transitions. */ |
Stephen Hemminger | 269def7 | 2007-02-22 01:10:18 -0800 | [diff] [blame] | 65 | void br_port_carrier_check(struct net_bridge_port *p) |
Stephen Hemminger | 4433f42 | 2005-12-20 15:19:51 -0800 | [diff] [blame] | 66 | { |
Stephen Hemminger | 269def7 | 2007-02-22 01:10:18 -0800 | [diff] [blame] | 67 | struct net_device *dev = p->dev; |
| 68 | struct net_bridge *br = p->br; |
Stephen Hemminger | b3f1be4 | 2006-02-09 17:08:52 -0800 | [diff] [blame] | 69 | |
stephen hemminger | 8f3359b | 2013-04-13 14:06:07 +0000 | [diff] [blame] | 70 | if (!(p->flags & BR_ADMIN_COST) && |
| 71 | netif_running(dev) && netif_oper_up(dev)) |
Stephen Hemminger | 6e86b89 | 2006-03-03 17:14:51 -0800 | [diff] [blame] | 72 | p->path_cost = port_cost(dev); |
Stephen Hemminger | 4433f42 | 2005-12-20 15:19:51 -0800 | [diff] [blame] | 73 | |
stephen hemminger | aa7c6e5 | 2010-08-24 13:12:56 +0000 | [diff] [blame] | 74 | if (!netif_running(br->dev)) |
| 75 | return; |
| 76 | |
| 77 | spin_lock_bh(&br->lock); |
stephen hemminger | 576eb62 | 2012-12-28 18:15:22 +0000 | [diff] [blame] | 78 | if (netif_running(dev) && netif_oper_up(dev)) { |
stephen hemminger | aa7c6e5 | 2010-08-24 13:12:56 +0000 | [diff] [blame] | 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); |
Stephen Hemminger | 4433f42 | 2005-12-20 15:19:51 -0800 | [diff] [blame] | 84 | } |
stephen hemminger | aa7c6e5 | 2010-08-24 13:12:56 +0000 | [diff] [blame] | 85 | spin_unlock_bh(&br->lock); |
Stephen Hemminger | 4433f42 | 2005-12-20 15:19:51 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Stephen Hemminger | bab1dee | 2006-02-09 17:10:12 -0800 | [diff] [blame] | 88 | static void release_nbp(struct kobject *kobj) |
| 89 | { |
| 90 | struct net_bridge_port *p |
| 91 | = container_of(kobj, struct net_bridge_port, kobj); |
| 92 | kfree(p); |
| 93 | } |
| 94 | |
| 95 | static struct kobj_type brport_ktype = { |
| 96 | #ifdef CONFIG_SYSFS |
| 97 | .sysfs_ops = &brport_sysfs_ops, |
| 98 | #endif |
| 99 | .release = release_nbp, |
| 100 | }; |
| 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | static void destroy_nbp(struct net_bridge_port *p) |
| 103 | { |
| 104 | struct net_device *dev = p->dev; |
| 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | p->br = NULL; |
| 107 | p->dev = NULL; |
| 108 | dev_put(dev); |
| 109 | |
Stephen Hemminger | bab1dee | 2006-02-09 17:10:12 -0800 | [diff] [blame] | 110 | kobject_put(&p->kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static void destroy_nbp_rcu(struct rcu_head *head) |
| 114 | { |
| 115 | struct net_bridge_port *p = |
| 116 | container_of(head, struct net_bridge_port, rcu); |
| 117 | destroy_nbp(p); |
| 118 | } |
| 119 | |
Stephen Hemminger | 3f4cfc2 | 2006-01-31 17:44:07 -0800 | [diff] [blame] | 120 | /* Delete port(interface) from bridge is done in two steps. |
| 121 | * via RCU. First step, marks device as down. That deletes |
| 122 | * all the timers and stops new packets from flowing through. |
| 123 | * |
| 124 | * Final cleanup doesn't occur until after all CPU's finished |
| 125 | * processing packets. |
| 126 | * |
| 127 | * Protected from multiple admin operations by RTNL mutex |
| 128 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | static void del_nbp(struct net_bridge_port *p) |
| 130 | { |
| 131 | struct net_bridge *br = p->br; |
| 132 | struct net_device *dev = p->dev; |
| 133 | |
Randy Dunlap | b3bcb72 | 2010-05-18 12:26:27 -0700 | [diff] [blame] | 134 | sysfs_remove_link(br->ifobj, p->dev->name); |
Stephen Hemminger | bab1dee | 2006-02-09 17:10:12 -0800 | [diff] [blame] | 135 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | dev_set_promiscuity(dev, -1); |
| 137 | |
| 138 | spin_lock_bh(&br->lock); |
| 139 | br_stp_disable_port(p); |
| 140 | spin_unlock_bh(&br->lock); |
| 141 | |
Stephen Hemminger | b86c450 | 2007-03-22 14:08:46 -0700 | [diff] [blame] | 142 | br_ifinfo_notify(RTM_DELLINK, p); |
| 143 | |
Vlad Yasevich | 243a2e6 | 2013-02-13 12:00:09 +0000 | [diff] [blame] | 144 | nbp_vlan_flush(p); |
Stephen Hemminger | 1a62069 | 2006-10-12 14:45:38 -0700 | [diff] [blame] | 145 | br_fdb_delete_by_port(br, p, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | list_del_rcu(&p->list); |
| 148 | |
Jiri Pirko | f350a0a8 | 2010-06-15 06:50:45 +0000 | [diff] [blame] | 149 | dev->priv_flags &= ~IFF_BRIDGE_PORT; |
| 150 | |
Jiri Pirko | ab95bfe | 2010-06-01 21:52:08 +0000 | [diff] [blame] | 151 | netdev_rx_handler_unregister(dev); |
Stephen Hemminger | b3f1be4 | 2006-02-09 17:08:52 -0800 | [diff] [blame] | 152 | |
Jiri Pirko | 74fdd93 | 2013-01-03 22:48:54 +0000 | [diff] [blame] | 153 | netdev_upper_dev_unlink(dev, br->dev); |
Jiri Pirko | afc6151 | 2011-02-13 09:33:42 +0000 | [diff] [blame] | 154 | |
Herbert Xu | 3fe2d7c | 2010-02-28 00:49:38 -0800 | [diff] [blame] | 155 | br_multicast_del_port(p); |
| 156 | |
Stephen Hemminger | 125a12c | 2006-03-03 17:16:15 -0800 | [diff] [blame] | 157 | kobject_uevent(&p->kobj, KOBJ_REMOVE); |
Stephen Hemminger | bab1dee | 2006-02-09 17:10:12 -0800 | [diff] [blame] | 158 | kobject_del(&p->kobj); |
| 159 | |
Herbert Xu | 91d2c34 | 2010-06-10 16:12:50 +0000 | [diff] [blame] | 160 | br_netpoll_disable(p); |
| 161 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | call_rcu(&p->rcu, destroy_nbp_rcu); |
| 163 | } |
| 164 | |
stephen hemminger | 1ce5cce | 2011-10-06 11:19:41 +0000 | [diff] [blame] | 165 | /* Delete bridge device */ |
| 166 | void br_dev_delete(struct net_device *dev, struct list_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | { |
stephen hemminger | 1ce5cce | 2011-10-06 11:19:41 +0000 | [diff] [blame] | 168 | struct net_bridge *br = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | struct net_bridge_port *p, *n; |
| 170 | |
| 171 | list_for_each_entry_safe(p, n, &br->port_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | del_nbp(p); |
| 173 | } |
| 174 | |
Ding Tianhong | f873042 | 2013-12-07 22:12:05 +0800 | [diff] [blame] | 175 | br_fdb_delete_by_port(br, NULL, 1); |
| 176 | |
Toshiaki Makita | b4e09b2 | 2013-11-13 17:26:14 +0900 | [diff] [blame] | 177 | br_vlan_flush(br); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | del_timer_sync(&br->gc_timer); |
| 179 | |
| 180 | br_sysfs_delbr(br->dev); |
Eric Dumazet | 8c56ba0 | 2009-10-28 05:35:35 +0000 | [diff] [blame] | 181 | unregister_netdevice_queue(br->dev, head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | /* find an available port number */ |
| 185 | static int find_portno(struct net_bridge *br) |
| 186 | { |
| 187 | int index; |
| 188 | struct net_bridge_port *p; |
| 189 | unsigned long *inuse; |
| 190 | |
Stephen Hemminger | 3b781fa | 2006-03-20 22:56:50 -0800 | [diff] [blame] | 191 | inuse = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | GFP_KERNEL); |
| 193 | if (!inuse) |
| 194 | return -ENOMEM; |
| 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | set_bit(0, inuse); /* zero is reserved */ |
| 197 | list_for_each_entry(p, &br->port_list, list) { |
| 198 | set_bit(p->port_no, inuse); |
| 199 | } |
| 200 | index = find_first_zero_bit(inuse, BR_MAX_PORTS); |
| 201 | kfree(inuse); |
| 202 | |
| 203 | return (index >= BR_MAX_PORTS) ? -EXFULL : index; |
| 204 | } |
| 205 | |
Stephen Hemminger | 4433f42 | 2005-12-20 15:19:51 -0800 | [diff] [blame] | 206 | /* called with RTNL but without bridge lock */ |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 207 | static struct net_bridge_port *new_nbp(struct net_bridge *br, |
Stephen Hemminger | 4433f42 | 2005-12-20 15:19:51 -0800 | [diff] [blame] | 208 | struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | { |
| 210 | int index; |
| 211 | struct net_bridge_port *p; |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | index = find_portno(br); |
| 214 | if (index < 0) |
| 215 | return ERR_PTR(index); |
| 216 | |
Stephen Hemminger | cee4854 | 2006-03-20 22:57:03 -0800 | [diff] [blame] | 217 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | if (p == NULL) |
| 219 | return ERR_PTR(-ENOMEM); |
| 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | p->br = br; |
| 222 | dev_hold(dev); |
| 223 | p->dev = dev; |
Stephen Hemminger | 4433f42 | 2005-12-20 15:19:51 -0800 | [diff] [blame] | 224 | p->path_cost = port_cost(dev); |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 225 | p->priority = 0x8000 >> BR_PORT_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | p->port_no = index; |
Vlad Yasevich | 867a594 | 2013-06-05 10:08:01 -0400 | [diff] [blame] | 227 | p->flags = BR_LEARNING | BR_FLOOD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | br_init_port(p); |
| 229 | p->state = BR_STATE_DISABLED; |
Stephen Hemminger | d32439c | 2006-03-03 17:15:34 -0800 | [diff] [blame] | 230 | br_stp_port_timer_init(p); |
Herbert Xu | 3fe2d7c | 2010-02-28 00:49:38 -0800 | [diff] [blame] | 231 | br_multicast_add_port(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
| 233 | return p; |
| 234 | } |
| 235 | |
Alexey Dobriyan | 4aa678b | 2008-09-08 16:19:58 -0700 | [diff] [blame] | 236 | int br_add_bridge(struct net *net, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | { |
| 238 | struct net_device *dev; |
Eric Dumazet | 11f3a6b | 2011-08-22 06:05:59 +0000 | [diff] [blame] | 239 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
stephen hemminger | bb900b2 | 2011-04-04 14:03:32 +0000 | [diff] [blame] | 241 | dev = alloc_netdev(sizeof(struct net_bridge), name, |
| 242 | br_dev_setup); |
| 243 | |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 244 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | return -ENOMEM; |
| 246 | |
stephen hemminger | bb900b2 | 2011-04-04 14:03:32 +0000 | [diff] [blame] | 247 | dev_net_set(dev, net); |
stephen hemminger | 149ddd8 | 2012-06-26 05:48:45 +0000 | [diff] [blame] | 248 | dev->rtnl_link_ops = &br_link_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
Eric Dumazet | 11f3a6b | 2011-08-22 06:05:59 +0000 | [diff] [blame] | 250 | res = register_netdev(dev); |
| 251 | if (res) |
| 252 | free_netdev(dev); |
| 253 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Alexey Dobriyan | 4aa678b | 2008-09-08 16:19:58 -0700 | [diff] [blame] | 256 | int br_del_bridge(struct net *net, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | { |
| 258 | struct net_device *dev; |
| 259 | int ret = 0; |
| 260 | |
| 261 | rtnl_lock(); |
Alexey Dobriyan | 4aa678b | 2008-09-08 16:19:58 -0700 | [diff] [blame] | 262 | dev = __dev_get_by_name(net, name); |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 263 | if (dev == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | ret = -ENXIO; /* Could not find device */ |
| 265 | |
| 266 | else if (!(dev->priv_flags & IFF_EBRIDGE)) { |
| 267 | /* Attempt to delete non bridge device! */ |
| 268 | ret = -EPERM; |
| 269 | } |
| 270 | |
| 271 | else if (dev->flags & IFF_UP) { |
| 272 | /* Not shutdown yet. */ |
| 273 | ret = -EBUSY; |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 274 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 276 | else |
stephen hemminger | 1ce5cce | 2011-10-06 11:19:41 +0000 | [diff] [blame] | 277 | br_dev_delete(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
| 279 | rtnl_unlock(); |
| 280 | return ret; |
| 281 | } |
| 282 | |
Kris Katterjohn | 46f25df | 2006-01-05 16:35:42 -0800 | [diff] [blame] | 283 | /* MTU of the bridge pseudo-device: ETH_DATA_LEN or the minimum of the ports */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | int br_min_mtu(const struct net_bridge *br) |
| 285 | { |
| 286 | const struct net_bridge_port *p; |
| 287 | int mtu = 0; |
| 288 | |
| 289 | ASSERT_RTNL(); |
| 290 | |
| 291 | if (list_empty(&br->port_list)) |
Kris Katterjohn | 46f25df | 2006-01-05 16:35:42 -0800 | [diff] [blame] | 292 | mtu = ETH_DATA_LEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | else { |
| 294 | list_for_each_entry(p, &br->port_list, list) { |
| 295 | if (!mtu || p->dev->mtu < mtu) |
| 296 | mtu = p->dev->mtu; |
| 297 | } |
| 298 | } |
| 299 | return mtu; |
| 300 | } |
| 301 | |
Stephen Hemminger | 81d3530 | 2005-05-29 14:15:17 -0700 | [diff] [blame] | 302 | /* |
| 303 | * Recomputes features using slave's features |
| 304 | */ |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 305 | netdev_features_t br_features_recompute(struct net_bridge *br, |
| 306 | netdev_features_t features) |
Stephen Hemminger | 81d3530 | 2005-05-29 14:15:17 -0700 | [diff] [blame] | 307 | { |
| 308 | struct net_bridge_port *p; |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 309 | netdev_features_t mask; |
Stephen Hemminger | 81d3530 | 2005-05-29 14:15:17 -0700 | [diff] [blame] | 310 | |
Herbert Xu | b63365a | 2008-10-23 01:11:29 -0700 | [diff] [blame] | 311 | if (list_empty(&br->port_list)) |
Michał Mirosław | c4d27ef | 2011-04-22 06:31:16 +0000 | [diff] [blame] | 312 | return features; |
Herbert Xu | b63365a | 2008-10-23 01:11:29 -0700 | [diff] [blame] | 313 | |
Michał Mirosław | c4d27ef | 2011-04-22 06:31:16 +0000 | [diff] [blame] | 314 | mask = features; |
Herbert Xu | b63365a | 2008-10-23 01:11:29 -0700 | [diff] [blame] | 315 | features &= ~NETIF_F_ONE_FOR_ALL; |
Stephen Hemminger | 81d3530 | 2005-05-29 14:15:17 -0700 | [diff] [blame] | 316 | |
| 317 | list_for_each_entry(p, &br->port_list, list) { |
Herbert Xu | b63365a | 2008-10-23 01:11:29 -0700 | [diff] [blame] | 318 | features = netdev_increment_features(features, |
| 319 | p->dev->features, mask); |
Stephen Hemminger | 81d3530 | 2005-05-29 14:15:17 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Michał Mirosław | c4d27ef | 2011-04-22 06:31:16 +0000 | [diff] [blame] | 322 | return features; |
Stephen Hemminger | 81d3530 | 2005-05-29 14:15:17 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | /* called with RTNL */ |
| 326 | int br_add_if(struct net_bridge *br, struct net_device *dev) |
| 327 | { |
| 328 | struct net_bridge_port *p; |
| 329 | int err = 0; |
stephen hemminger | edf947f | 2011-03-24 13:24:01 +0000 | [diff] [blame] | 330 | bool changed_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
Stephen Hemminger | 1056bd5 | 2009-11-05 20:46:52 -0800 | [diff] [blame] | 332 | /* Don't allow bridging non-ethernet like devices */ |
| 333 | if ((dev->flags & IFF_LOOPBACK) || |
stephen hemminger | 77f9859 | 2011-09-30 14:37:26 +0000 | [diff] [blame] | 334 | dev->type != ARPHRD_ETHER || dev->addr_len != ETH_ALEN || |
| 335 | !is_valid_ether_addr(dev->dev_addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | return -EINVAL; |
| 337 | |
Stephen Hemminger | 1056bd5 | 2009-11-05 20:46:52 -0800 | [diff] [blame] | 338 | /* No bridging of bridges */ |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 339 | if (dev->netdev_ops->ndo_start_xmit == br_dev_xmit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | return -ELOOP; |
| 341 | |
Stephen Hemminger | 1056bd5 | 2009-11-05 20:46:52 -0800 | [diff] [blame] | 342 | /* Device is already being bridged */ |
Jiri Pirko | f350a0a8 | 2010-06-15 06:50:45 +0000 | [diff] [blame] | 343 | if (br_port_exists(dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | return -EBUSY; |
| 345 | |
Johannes Berg | ad4bb6f | 2009-11-19 00:56:30 +0100 | [diff] [blame] | 346 | /* No bridging devices that dislike that (e.g. wireless) */ |
| 347 | if (dev->priv_flags & IFF_DONT_BRIDGE) |
| 348 | return -EOPNOTSUPP; |
| 349 | |
Stephen Hemminger | bab1dee | 2006-02-09 17:10:12 -0800 | [diff] [blame] | 350 | p = new_nbp(br, dev); |
| 351 | if (IS_ERR(p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | return PTR_ERR(p); |
| 353 | |
Amerigo Wang | bb8ed63 | 2011-05-19 21:39:11 +0000 | [diff] [blame] | 354 | call_netdevice_notifiers(NETDEV_JOIN, dev); |
| 355 | |
Wang Chen | bc3f907 | 2008-07-14 20:53:13 -0700 | [diff] [blame] | 356 | err = dev_set_promiscuity(dev, 1); |
| 357 | if (err) |
| 358 | goto put_back; |
| 359 | |
Greg Kroah-Hartman | e32cc73 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 360 | err = kobject_init_and_add(&p->kobj, &brport_ktype, &(dev->dev.kobj), |
| 361 | SYSFS_BRIDGE_PORT_ATTR); |
Stephen Hemminger | bab1dee | 2006-02-09 17:10:12 -0800 | [diff] [blame] | 362 | if (err) |
Stephen Hemminger | bab1dee | 2006-02-09 17:10:12 -0800 | [diff] [blame] | 363 | goto err1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | |
Stephen Hemminger | bab1dee | 2006-02-09 17:10:12 -0800 | [diff] [blame] | 365 | err = br_sysfs_addif(p); |
| 366 | if (err) |
| 367 | goto err2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
Eric W. Biederman | a8779ec | 2014-03-27 15:36:38 -0700 | [diff] [blame] | 369 | err = br_netpoll_enable(p); |
stephen hemminger | 93d8bf9 | 2013-07-24 11:51:41 -0700 | [diff] [blame] | 370 | if (err) |
Herbert Xu | 91d2c34 | 2010-06-10 16:12:50 +0000 | [diff] [blame] | 371 | goto err3; |
| 372 | |
Jiri Pirko | 74fdd93 | 2013-01-03 22:48:54 +0000 | [diff] [blame] | 373 | err = netdev_master_upper_dev_link(dev, br->dev); |
Jiri Pirko | ab95bfe | 2010-06-01 21:52:08 +0000 | [diff] [blame] | 374 | if (err) |
Gao feng | 9b1536c | 2012-12-19 23:41:43 +0000 | [diff] [blame] | 375 | goto err4; |
Jiri Pirko | f350a0a8 | 2010-06-15 06:50:45 +0000 | [diff] [blame] | 376 | |
Jiri Pirko | afc6151 | 2011-02-13 09:33:42 +0000 | [diff] [blame] | 377 | err = netdev_rx_handler_register(dev, br_handle_frame, p); |
| 378 | if (err) |
Gao feng | 9b1536c | 2012-12-19 23:41:43 +0000 | [diff] [blame] | 379 | goto err5; |
Jiri Pirko | afc6151 | 2011-02-13 09:33:42 +0000 | [diff] [blame] | 380 | |
Jiri Pirko | f350a0a8 | 2010-06-15 06:50:45 +0000 | [diff] [blame] | 381 | dev->priv_flags |= IFF_BRIDGE_PORT; |
Jiri Pirko | ab95bfe | 2010-06-01 21:52:08 +0000 | [diff] [blame] | 382 | |
Ben Hutchings | 0187bdf | 2008-06-19 16:15:47 -0700 | [diff] [blame] | 383 | dev_disable_lro(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
Stephen Hemminger | bab1dee | 2006-02-09 17:10:12 -0800 | [diff] [blame] | 385 | list_add_rcu(&p->list, &br->port_list); |
| 386 | |
Michał Mirosław | c4d27ef | 2011-04-22 06:31:16 +0000 | [diff] [blame] | 387 | netdev_update_features(br->dev); |
| 388 | |
Florian Fainelli | fd09480 | 2013-08-27 12:03:53 +0100 | [diff] [blame] | 389 | if (br->dev->needed_headroom < dev->needed_headroom) |
| 390 | br->dev->needed_headroom = dev->needed_headroom; |
| 391 | |
Toshiaki Makita | a4b816d | 2014-02-07 16:48:21 +0900 | [diff] [blame] | 392 | if (br_fdb_insert(br, p, dev->dev_addr, 0)) |
| 393 | netdev_err(dev, "failed insert local address bridge forwarding table\n"); |
| 394 | |
Stephen Hemminger | bab1dee | 2006-02-09 17:10:12 -0800 | [diff] [blame] | 395 | spin_lock_bh(&br->lock); |
stephen hemminger | edf947f | 2011-03-24 13:24:01 +0000 | [diff] [blame] | 396 | changed_addr = br_stp_recalculate_bridge_id(br); |
Aji Srinivas | de79059 | 2007-03-07 16:10:53 -0800 | [diff] [blame] | 397 | |
stephen hemminger | 576eb62 | 2012-12-28 18:15:22 +0000 | [diff] [blame] | 398 | if (netif_running(dev) && netif_oper_up(dev) && |
Aji Srinivas | de79059 | 2007-03-07 16:10:53 -0800 | [diff] [blame] | 399 | (br->dev->flags & IFF_UP)) |
| 400 | br_stp_enable_port(p); |
Stephen Hemminger | bab1dee | 2006-02-09 17:10:12 -0800 | [diff] [blame] | 401 | spin_unlock_bh(&br->lock); |
| 402 | |
Stephen Hemminger | b86c450 | 2007-03-22 14:08:46 -0700 | [diff] [blame] | 403 | br_ifinfo_notify(RTM_NEWLINK, p); |
| 404 | |
stephen hemminger | edf947f | 2011-03-24 13:24:01 +0000 | [diff] [blame] | 405 | if (changed_addr) |
stephen hemminger | 56139fc | 2011-07-22 07:47:08 +0000 | [diff] [blame] | 406 | call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev); |
stephen hemminger | edf947f | 2011-03-24 13:24:01 +0000 | [diff] [blame] | 407 | |
Stephen Hemminger | bab1dee | 2006-02-09 17:10:12 -0800 | [diff] [blame] | 408 | dev_set_mtu(br->dev, br_min_mtu(br)); |
Stephen Hemminger | 269def7 | 2007-02-22 01:10:18 -0800 | [diff] [blame] | 409 | |
Stephen Hemminger | bab1dee | 2006-02-09 17:10:12 -0800 | [diff] [blame] | 410 | kobject_uevent(&p->kobj, KOBJ_ADD); |
| 411 | |
| 412 | return 0; |
Jiri Pirko | afc6151 | 2011-02-13 09:33:42 +0000 | [diff] [blame] | 413 | |
Gao feng | 9b1536c | 2012-12-19 23:41:43 +0000 | [diff] [blame] | 414 | err5: |
Jiri Pirko | 74fdd93 | 2013-01-03 22:48:54 +0000 | [diff] [blame] | 415 | netdev_upper_dev_unlink(dev, br->dev); |
Gao feng | 9b1536c | 2012-12-19 23:41:43 +0000 | [diff] [blame] | 416 | err4: |
| 417 | br_netpoll_disable(p); |
Herbert Xu | 91d2c34 | 2010-06-10 16:12:50 +0000 | [diff] [blame] | 418 | err3: |
| 419 | sysfs_remove_link(br->ifobj, p->dev->name); |
Stephen Hemminger | bab1dee | 2006-02-09 17:10:12 -0800 | [diff] [blame] | 420 | err2: |
Xiaotian Feng | c587aea | 2009-07-23 23:06:32 +0000 | [diff] [blame] | 421 | kobject_put(&p->kobj); |
Jeff Hansen | 30df94f | 2009-09-28 12:54:25 -0700 | [diff] [blame] | 422 | p = NULL; /* kobject_put frees */ |
stephen hemminger | 77f9859 | 2011-09-30 14:37:26 +0000 | [diff] [blame] | 423 | err1: |
Wang Chen | bc3f907 | 2008-07-14 20:53:13 -0700 | [diff] [blame] | 424 | dev_set_promiscuity(dev, -1); |
Volodymyr G Lukiianyk | 43af853 | 2008-04-29 03:17:42 -0700 | [diff] [blame] | 425 | put_back: |
| 426 | dev_put(dev); |
Wang Chen | bc3f907 | 2008-07-14 20:53:13 -0700 | [diff] [blame] | 427 | kfree(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | return err; |
| 429 | } |
| 430 | |
| 431 | /* called with RTNL */ |
| 432 | int br_del_if(struct net_bridge *br, struct net_device *dev) |
| 433 | { |
Jiri Pirko | f350a0a8 | 2010-06-15 06:50:45 +0000 | [diff] [blame] | 434 | struct net_bridge_port *p; |
Andrei Warkentin | 9be6dd6 | 2011-08-05 11:04:10 +0000 | [diff] [blame] | 435 | bool changed_addr; |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 436 | |
Eric Dumazet | ec1e561 | 2010-11-15 06:38:14 +0000 | [diff] [blame] | 437 | p = br_port_get_rtnl(dev); |
stephen hemminger | b5ed54e | 2010-11-15 06:38:13 +0000 | [diff] [blame] | 438 | if (!p || p->br != br) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | return -EINVAL; |
| 440 | |
Amerigo Wang | d30362c | 2012-08-10 01:24:43 +0000 | [diff] [blame] | 441 | /* Since more than one interface can be attached to a bridge, |
| 442 | * there still maybe an alternate path for netconsole to use; |
| 443 | * therefore there is no reason for a NETDEV_RELEASE event. |
| 444 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | del_nbp(p); |
| 446 | |
| 447 | spin_lock_bh(&br->lock); |
Andrei Warkentin | 9be6dd6 | 2011-08-05 11:04:10 +0000 | [diff] [blame] | 448 | changed_addr = br_stp_recalculate_bridge_id(br); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | spin_unlock_bh(&br->lock); |
| 450 | |
Andrei Warkentin | 9be6dd6 | 2011-08-05 11:04:10 +0000 | [diff] [blame] | 451 | if (changed_addr) |
| 452 | call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev); |
| 453 | |
Michał Mirosław | c4d27ef | 2011-04-22 06:31:16 +0000 | [diff] [blame] | 454 | netdev_update_features(br->dev); |
| 455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | return 0; |
| 457 | } |