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 | |
Stephen Hemminger | 944c794 | 2010-08-24 13:08:10 +0000 | [diff] [blame] | 64 | /* Check for port carrier transistions. */ |
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 | |
| 175 | del_timer_sync(&br->gc_timer); |
| 176 | |
| 177 | br_sysfs_delbr(br->dev); |
Eric Dumazet | 8c56ba0 | 2009-10-28 05:35:35 +0000 | [diff] [blame] | 178 | unregister_netdevice_queue(br->dev, head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | /* find an available port number */ |
| 182 | static int find_portno(struct net_bridge *br) |
| 183 | { |
| 184 | int index; |
| 185 | struct net_bridge_port *p; |
| 186 | unsigned long *inuse; |
| 187 | |
Stephen Hemminger | 3b781fa | 2006-03-20 22:56:50 -0800 | [diff] [blame] | 188 | inuse = kcalloc(BITS_TO_LONGS(BR_MAX_PORTS), sizeof(unsigned long), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | GFP_KERNEL); |
| 190 | if (!inuse) |
| 191 | return -ENOMEM; |
| 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | set_bit(0, inuse); /* zero is reserved */ |
| 194 | list_for_each_entry(p, &br->port_list, list) { |
| 195 | set_bit(p->port_no, inuse); |
| 196 | } |
| 197 | index = find_first_zero_bit(inuse, BR_MAX_PORTS); |
| 198 | kfree(inuse); |
| 199 | |
| 200 | return (index >= BR_MAX_PORTS) ? -EXFULL : index; |
| 201 | } |
| 202 | |
Stephen Hemminger | 4433f42 | 2005-12-20 15:19:51 -0800 | [diff] [blame] | 203 | /* called with RTNL but without bridge lock */ |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 204 | static struct net_bridge_port *new_nbp(struct net_bridge *br, |
Stephen Hemminger | 4433f42 | 2005-12-20 15:19:51 -0800 | [diff] [blame] | 205 | struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | { |
| 207 | int index; |
| 208 | struct net_bridge_port *p; |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 209 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | index = find_portno(br); |
| 211 | if (index < 0) |
| 212 | return ERR_PTR(index); |
| 213 | |
Stephen Hemminger | cee4854 | 2006-03-20 22:57:03 -0800 | [diff] [blame] | 214 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | if (p == NULL) |
| 216 | return ERR_PTR(-ENOMEM); |
| 217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | p->br = br; |
| 219 | dev_hold(dev); |
| 220 | p->dev = dev; |
Stephen Hemminger | 4433f42 | 2005-12-20 15:19:51 -0800 | [diff] [blame] | 221 | p->path_cost = port_cost(dev); |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 222 | p->priority = 0x8000 >> BR_PORT_BITS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | p->port_no = index; |
Fischer, Anna | 3982d3d | 2009-08-13 06:55:16 +0000 | [diff] [blame] | 224 | p->flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | br_init_port(p); |
| 226 | p->state = BR_STATE_DISABLED; |
Stephen Hemminger | d32439c | 2006-03-03 17:15:34 -0800 | [diff] [blame] | 227 | br_stp_port_timer_init(p); |
Herbert Xu | 3fe2d7c | 2010-02-28 00:49:38 -0800 | [diff] [blame] | 228 | br_multicast_add_port(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | return p; |
| 231 | } |
| 232 | |
Alexey Dobriyan | 4aa678b | 2008-09-08 16:19:58 -0700 | [diff] [blame] | 233 | int br_add_bridge(struct net *net, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { |
| 235 | struct net_device *dev; |
Eric Dumazet | 11f3a6b | 2011-08-22 06:05:59 +0000 | [diff] [blame] | 236 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
stephen hemminger | bb900b2 | 2011-04-04 14:03:32 +0000 | [diff] [blame] | 238 | dev = alloc_netdev(sizeof(struct net_bridge), name, |
| 239 | br_dev_setup); |
| 240 | |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 241 | if (!dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | return -ENOMEM; |
| 243 | |
stephen hemminger | bb900b2 | 2011-04-04 14:03:32 +0000 | [diff] [blame] | 244 | dev_net_set(dev, net); |
stephen hemminger | 149ddd8 | 2012-06-26 05:48:45 +0000 | [diff] [blame] | 245 | dev->rtnl_link_ops = &br_link_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | |
Eric Dumazet | 11f3a6b | 2011-08-22 06:05:59 +0000 | [diff] [blame] | 247 | res = register_netdev(dev); |
| 248 | if (res) |
| 249 | free_netdev(dev); |
| 250 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Alexey Dobriyan | 4aa678b | 2008-09-08 16:19:58 -0700 | [diff] [blame] | 253 | int br_del_bridge(struct net *net, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | { |
| 255 | struct net_device *dev; |
| 256 | int ret = 0; |
| 257 | |
| 258 | rtnl_lock(); |
Alexey Dobriyan | 4aa678b | 2008-09-08 16:19:58 -0700 | [diff] [blame] | 259 | dev = __dev_get_by_name(net, name); |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 260 | if (dev == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | ret = -ENXIO; /* Could not find device */ |
| 262 | |
| 263 | else if (!(dev->priv_flags & IFF_EBRIDGE)) { |
| 264 | /* Attempt to delete non bridge device! */ |
| 265 | ret = -EPERM; |
| 266 | } |
| 267 | |
| 268 | else if (dev->flags & IFF_UP) { |
| 269 | /* Not shutdown yet. */ |
| 270 | ret = -EBUSY; |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 271 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 273 | else |
stephen hemminger | 1ce5cce | 2011-10-06 11:19:41 +0000 | [diff] [blame] | 274 | br_dev_delete(dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | |
| 276 | rtnl_unlock(); |
| 277 | return ret; |
| 278 | } |
| 279 | |
Kris Katterjohn | 46f25df | 2006-01-05 16:35:42 -0800 | [diff] [blame] | 280 | /* 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] | 281 | int br_min_mtu(const struct net_bridge *br) |
| 282 | { |
| 283 | const struct net_bridge_port *p; |
| 284 | int mtu = 0; |
| 285 | |
| 286 | ASSERT_RTNL(); |
| 287 | |
| 288 | if (list_empty(&br->port_list)) |
Kris Katterjohn | 46f25df | 2006-01-05 16:35:42 -0800 | [diff] [blame] | 289 | mtu = ETH_DATA_LEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | else { |
| 291 | list_for_each_entry(p, &br->port_list, list) { |
| 292 | if (!mtu || p->dev->mtu < mtu) |
| 293 | mtu = p->dev->mtu; |
| 294 | } |
| 295 | } |
| 296 | return mtu; |
| 297 | } |
| 298 | |
Stephen Hemminger | 81d3530 | 2005-05-29 14:15:17 -0700 | [diff] [blame] | 299 | /* |
| 300 | * Recomputes features using slave's features |
| 301 | */ |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 302 | netdev_features_t br_features_recompute(struct net_bridge *br, |
| 303 | netdev_features_t features) |
Stephen Hemminger | 81d3530 | 2005-05-29 14:15:17 -0700 | [diff] [blame] | 304 | { |
| 305 | struct net_bridge_port *p; |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 306 | netdev_features_t mask; |
Stephen Hemminger | 81d3530 | 2005-05-29 14:15:17 -0700 | [diff] [blame] | 307 | |
Herbert Xu | b63365a | 2008-10-23 01:11:29 -0700 | [diff] [blame] | 308 | if (list_empty(&br->port_list)) |
Michał Mirosław | c4d27ef | 2011-04-22 06:31:16 +0000 | [diff] [blame] | 309 | return features; |
Herbert Xu | b63365a | 2008-10-23 01:11:29 -0700 | [diff] [blame] | 310 | |
Michał Mirosław | c4d27ef | 2011-04-22 06:31:16 +0000 | [diff] [blame] | 311 | mask = features; |
Herbert Xu | b63365a | 2008-10-23 01:11:29 -0700 | [diff] [blame] | 312 | features &= ~NETIF_F_ONE_FOR_ALL; |
Stephen Hemminger | 81d3530 | 2005-05-29 14:15:17 -0700 | [diff] [blame] | 313 | |
| 314 | list_for_each_entry(p, &br->port_list, list) { |
Herbert Xu | b63365a | 2008-10-23 01:11:29 -0700 | [diff] [blame] | 315 | features = netdev_increment_features(features, |
| 316 | p->dev->features, mask); |
Stephen Hemminger | 81d3530 | 2005-05-29 14:15:17 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Michał Mirosław | c4d27ef | 2011-04-22 06:31:16 +0000 | [diff] [blame] | 319 | return features; |
Stephen Hemminger | 81d3530 | 2005-05-29 14:15:17 -0700 | [diff] [blame] | 320 | } |
| 321 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | /* called with RTNL */ |
| 323 | int br_add_if(struct net_bridge *br, struct net_device *dev) |
| 324 | { |
| 325 | struct net_bridge_port *p; |
| 326 | int err = 0; |
stephen hemminger | edf947f | 2011-03-24 13:24:01 +0000 | [diff] [blame] | 327 | bool changed_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
Stephen Hemminger | 1056bd5 | 2009-11-05 20:46:52 -0800 | [diff] [blame] | 329 | /* Don't allow bridging non-ethernet like devices */ |
| 330 | if ((dev->flags & IFF_LOOPBACK) || |
stephen hemminger | 77f9859 | 2011-09-30 14:37:26 +0000 | [diff] [blame] | 331 | dev->type != ARPHRD_ETHER || dev->addr_len != ETH_ALEN || |
| 332 | !is_valid_ether_addr(dev->dev_addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | return -EINVAL; |
| 334 | |
Stephen Hemminger | 1056bd5 | 2009-11-05 20:46:52 -0800 | [diff] [blame] | 335 | /* No bridging of bridges */ |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 336 | if (dev->netdev_ops->ndo_start_xmit == br_dev_xmit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | return -ELOOP; |
| 338 | |
Stephen Hemminger | 1056bd5 | 2009-11-05 20:46:52 -0800 | [diff] [blame] | 339 | /* Device is already being bridged */ |
Jiri Pirko | f350a0a8 | 2010-06-15 06:50:45 +0000 | [diff] [blame] | 340 | if (br_port_exists(dev)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | return -EBUSY; |
| 342 | |
Johannes Berg | ad4bb6f | 2009-11-19 00:56:30 +0100 | [diff] [blame] | 343 | /* No bridging devices that dislike that (e.g. wireless) */ |
| 344 | if (dev->priv_flags & IFF_DONT_BRIDGE) |
| 345 | return -EOPNOTSUPP; |
| 346 | |
Stephen Hemminger | bab1dee | 2006-02-09 17:10:12 -0800 | [diff] [blame] | 347 | p = new_nbp(br, dev); |
| 348 | if (IS_ERR(p)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | return PTR_ERR(p); |
| 350 | |
Amerigo Wang | bb8ed63 | 2011-05-19 21:39:11 +0000 | [diff] [blame] | 351 | call_netdevice_notifiers(NETDEV_JOIN, dev); |
| 352 | |
Wang Chen | bc3f907 | 2008-07-14 20:53:13 -0700 | [diff] [blame] | 353 | err = dev_set_promiscuity(dev, 1); |
| 354 | if (err) |
| 355 | goto put_back; |
| 356 | |
Greg Kroah-Hartman | e32cc73 | 2007-12-17 23:05:35 -0700 | [diff] [blame] | 357 | err = kobject_init_and_add(&p->kobj, &brport_ktype, &(dev->dev.kobj), |
| 358 | SYSFS_BRIDGE_PORT_ATTR); |
Stephen Hemminger | bab1dee | 2006-02-09 17:10:12 -0800 | [diff] [blame] | 359 | if (err) |
Stephen Hemminger | bab1dee | 2006-02-09 17:10:12 -0800 | [diff] [blame] | 360 | goto err1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | |
Stephen Hemminger | bab1dee | 2006-02-09 17:10:12 -0800 | [diff] [blame] | 362 | err = br_sysfs_addif(p); |
| 363 | if (err) |
| 364 | goto err2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
Amerigo Wang | 47be03a2 | 2012-08-10 01:24:37 +0000 | [diff] [blame] | 366 | if (br_netpoll_info(br) && ((err = br_netpoll_enable(p, GFP_KERNEL)))) |
Herbert Xu | 91d2c34 | 2010-06-10 16:12:50 +0000 | [diff] [blame] | 367 | goto err3; |
| 368 | |
Jiri Pirko | 74fdd93 | 2013-01-03 22:48:54 +0000 | [diff] [blame] | 369 | err = netdev_master_upper_dev_link(dev, br->dev); |
Jiri Pirko | ab95bfe | 2010-06-01 21:52:08 +0000 | [diff] [blame] | 370 | if (err) |
Gao feng | 9b1536c | 2012-12-19 23:41:43 +0000 | [diff] [blame] | 371 | goto err4; |
Jiri Pirko | f350a0a8 | 2010-06-15 06:50:45 +0000 | [diff] [blame] | 372 | |
Jiri Pirko | afc6151 | 2011-02-13 09:33:42 +0000 | [diff] [blame] | 373 | err = netdev_rx_handler_register(dev, br_handle_frame, p); |
| 374 | if (err) |
Gao feng | 9b1536c | 2012-12-19 23:41:43 +0000 | [diff] [blame] | 375 | goto err5; |
Jiri Pirko | afc6151 | 2011-02-13 09:33:42 +0000 | [diff] [blame] | 376 | |
Jiri Pirko | f350a0a8 | 2010-06-15 06:50:45 +0000 | [diff] [blame] | 377 | dev->priv_flags |= IFF_BRIDGE_PORT; |
Jiri Pirko | ab95bfe | 2010-06-01 21:52:08 +0000 | [diff] [blame] | 378 | |
Ben Hutchings | 0187bdf | 2008-06-19 16:15:47 -0700 | [diff] [blame] | 379 | dev_disable_lro(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
Stephen Hemminger | bab1dee | 2006-02-09 17:10:12 -0800 | [diff] [blame] | 381 | list_add_rcu(&p->list, &br->port_list); |
| 382 | |
Michał Mirosław | c4d27ef | 2011-04-22 06:31:16 +0000 | [diff] [blame] | 383 | netdev_update_features(br->dev); |
| 384 | |
Stephen Hemminger | bab1dee | 2006-02-09 17:10:12 -0800 | [diff] [blame] | 385 | spin_lock_bh(&br->lock); |
stephen hemminger | edf947f | 2011-03-24 13:24:01 +0000 | [diff] [blame] | 386 | changed_addr = br_stp_recalculate_bridge_id(br); |
Aji Srinivas | de79059 | 2007-03-07 16:10:53 -0800 | [diff] [blame] | 387 | |
stephen hemminger | 576eb62 | 2012-12-28 18:15:22 +0000 | [diff] [blame] | 388 | if (netif_running(dev) && netif_oper_up(dev) && |
Aji Srinivas | de79059 | 2007-03-07 16:10:53 -0800 | [diff] [blame] | 389 | (br->dev->flags & IFF_UP)) |
| 390 | br_stp_enable_port(p); |
Stephen Hemminger | bab1dee | 2006-02-09 17:10:12 -0800 | [diff] [blame] | 391 | spin_unlock_bh(&br->lock); |
| 392 | |
Stephen Hemminger | b86c450 | 2007-03-22 14:08:46 -0700 | [diff] [blame] | 393 | br_ifinfo_notify(RTM_NEWLINK, p); |
| 394 | |
stephen hemminger | edf947f | 2011-03-24 13:24:01 +0000 | [diff] [blame] | 395 | if (changed_addr) |
stephen hemminger | 56139fc | 2011-07-22 07:47:08 +0000 | [diff] [blame] | 396 | call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev); |
stephen hemminger | edf947f | 2011-03-24 13:24:01 +0000 | [diff] [blame] | 397 | |
Stephen Hemminger | bab1dee | 2006-02-09 17:10:12 -0800 | [diff] [blame] | 398 | dev_set_mtu(br->dev, br_min_mtu(br)); |
Stephen Hemminger | 269def7 | 2007-02-22 01:10:18 -0800 | [diff] [blame] | 399 | |
Vlad Yasevich | bc9a25d | 2013-02-13 12:00:19 +0000 | [diff] [blame] | 400 | if (br_fdb_insert(br, p, dev->dev_addr, 0)) |
stephen hemminger | 77f9859 | 2011-09-30 14:37:26 +0000 | [diff] [blame] | 401 | netdev_err(dev, "failed insert local address bridge forwarding table\n"); |
| 402 | |
Stephen Hemminger | bab1dee | 2006-02-09 17:10:12 -0800 | [diff] [blame] | 403 | kobject_uevent(&p->kobj, KOBJ_ADD); |
| 404 | |
| 405 | return 0; |
Jiri Pirko | afc6151 | 2011-02-13 09:33:42 +0000 | [diff] [blame] | 406 | |
Gao feng | 9b1536c | 2012-12-19 23:41:43 +0000 | [diff] [blame] | 407 | err5: |
Jiri Pirko | 74fdd93 | 2013-01-03 22:48:54 +0000 | [diff] [blame] | 408 | netdev_upper_dev_unlink(dev, br->dev); |
Gao feng | 9b1536c | 2012-12-19 23:41:43 +0000 | [diff] [blame] | 409 | err4: |
| 410 | br_netpoll_disable(p); |
Herbert Xu | 91d2c34 | 2010-06-10 16:12:50 +0000 | [diff] [blame] | 411 | err3: |
| 412 | sysfs_remove_link(br->ifobj, p->dev->name); |
Stephen Hemminger | bab1dee | 2006-02-09 17:10:12 -0800 | [diff] [blame] | 413 | err2: |
Xiaotian Feng | c587aea | 2009-07-23 23:06:32 +0000 | [diff] [blame] | 414 | kobject_put(&p->kobj); |
Jeff Hansen | 30df94f | 2009-09-28 12:54:25 -0700 | [diff] [blame] | 415 | p = NULL; /* kobject_put frees */ |
stephen hemminger | 77f9859 | 2011-09-30 14:37:26 +0000 | [diff] [blame] | 416 | err1: |
Wang Chen | bc3f907 | 2008-07-14 20:53:13 -0700 | [diff] [blame] | 417 | dev_set_promiscuity(dev, -1); |
Volodymyr G Lukiianyk | 43af853 | 2008-04-29 03:17:42 -0700 | [diff] [blame] | 418 | put_back: |
| 419 | dev_put(dev); |
Wang Chen | bc3f907 | 2008-07-14 20:53:13 -0700 | [diff] [blame] | 420 | kfree(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | return err; |
| 422 | } |
| 423 | |
| 424 | /* called with RTNL */ |
| 425 | int br_del_if(struct net_bridge *br, struct net_device *dev) |
| 426 | { |
Jiri Pirko | f350a0a8 | 2010-06-15 06:50:45 +0000 | [diff] [blame] | 427 | struct net_bridge_port *p; |
Andrei Warkentin | 9be6dd6 | 2011-08-05 11:04:10 +0000 | [diff] [blame] | 428 | bool changed_addr; |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 429 | |
Eric Dumazet | ec1e561 | 2010-11-15 06:38:14 +0000 | [diff] [blame] | 430 | p = br_port_get_rtnl(dev); |
stephen hemminger | b5ed54e | 2010-11-15 06:38:13 +0000 | [diff] [blame] | 431 | if (!p || p->br != br) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | return -EINVAL; |
| 433 | |
Amerigo Wang | d30362c | 2012-08-10 01:24:43 +0000 | [diff] [blame] | 434 | /* Since more than one interface can be attached to a bridge, |
| 435 | * there still maybe an alternate path for netconsole to use; |
| 436 | * therefore there is no reason for a NETDEV_RELEASE event. |
| 437 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | del_nbp(p); |
| 439 | |
| 440 | spin_lock_bh(&br->lock); |
Andrei Warkentin | 9be6dd6 | 2011-08-05 11:04:10 +0000 | [diff] [blame] | 441 | changed_addr = br_stp_recalculate_bridge_id(br); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | spin_unlock_bh(&br->lock); |
| 443 | |
Andrei Warkentin | 9be6dd6 | 2011-08-05 11:04:10 +0000 | [diff] [blame] | 444 | if (changed_addr) |
| 445 | call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev); |
| 446 | |
Michał Mirosław | c4d27ef | 2011-04-22 06:31:16 +0000 | [diff] [blame] | 447 | netdev_update_features(br->dev); |
| 448 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | return 0; |
| 450 | } |
| 451 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 452 | void __net_exit br_net_exit(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | { |
Patrick McHardy | ab1b204 | 2008-07-03 03:53:42 -0700 | [diff] [blame] | 454 | struct net_device *dev; |
Eric Dumazet | 8c56ba0 | 2009-10-28 05:35:35 +0000 | [diff] [blame] | 455 | LIST_HEAD(list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | |
| 457 | rtnl_lock(); |
Eric Dumazet | 8c56ba0 | 2009-10-28 05:35:35 +0000 | [diff] [blame] | 458 | for_each_netdev(net, dev) |
| 459 | if (dev->priv_flags & IFF_EBRIDGE) |
stephen hemminger | 1ce5cce | 2011-10-06 11:19:41 +0000 | [diff] [blame] | 460 | br_dev_delete(dev, &list); |
Eric Dumazet | 8c56ba0 | 2009-10-28 05:35:35 +0000 | [diff] [blame] | 461 | |
| 462 | unregister_netdevice_many(&list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | rtnl_unlock(); |
| 464 | |
| 465 | } |