blob: 67c60240b7139f6fe1f2ac9abf4b236bb3bff4a3 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Userspace interface
4 * Linux ethernet bridge
5 *
6 * Authors:
7 * Lennert Buytenhek <buytenh@gnu.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
10#include <linux/kernel.h>
11#include <linux/netdevice.h>
stephen hemminger77f98592011-09-30 14:37:26 +000012#include <linux/etherdevice.h>
WANG Congc06ee962010-05-06 00:48:24 -070013#include <linux/netpoll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/ethtool.h>
15#include <linux/if_arp.h>
16#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/rtnetlink.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080019#include <linux/if_ether.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020021#include <net/dsa.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <net/sock.h>
Vlad Yasevich407af322013-02-13 12:00:12 +000023#include <linux/if_vlan.h>
Jiri Pirko56607382015-10-14 19:40:53 +020024#include <net/switchdev.h>
Tyler Hicks705e0de2018-07-20 21:56:54 +000025#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include "br_private.h"
28
29/*
30 * Determine initial path cost based on speed.
31 * using recommendations from 802.1d standard
32 *
Matthew Wilcox61a44b92007-07-31 14:00:02 -070033 * Since driver might sleep need to not be holding any locks.
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 */
Stephen Hemminger4433f422005-12-20 15:19:51 -080035static int port_cost(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
David Decotigny702b26a2016-02-24 10:58:09 -080037 struct ethtool_link_ksettings ecmd;
Stephen Hemmingerb4a488d2007-08-30 22:16:22 -070038
David Decotigny702b26a2016-02-24 10:58:09 -080039 if (!__ethtool_get_link_ksettings(dev, &ecmd)) {
40 switch (ecmd.base.speed) {
Jiri Pirkofa3df922011-09-01 03:29:38 +000041 case SPEED_10000:
42 return 2;
43 case SPEED_1000:
44 return 4;
45 case SPEED_100:
46 return 19;
47 case SPEED_10:
48 return 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 }
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
Stephen Hemminger4433f422005-12-20 15:19:51 -080062
tanxiaojun1a81a2e2013-12-16 21:32:46 +080063/* Check for port carrier transitions. */
Nikolay Aleksandrovfaa1cd82018-05-03 13:47:24 +030064void br_port_carrier_check(struct net_bridge_port *p, bool *notified)
Stephen Hemminger4433f422005-12-20 15:19:51 -080065{
Stephen Hemminger269def72007-02-22 01:10:18 -080066 struct net_device *dev = p->dev;
67 struct net_bridge *br = p->br;
Stephen Hemmingerb3f1be42006-02-09 17:08:52 -080068
stephen hemminger8f3359b2013-04-13 14:06:07 +000069 if (!(p->flags & BR_ADMIN_COST) &&
70 netif_running(dev) && netif_oper_up(dev))
Stephen Hemminger6e86b8902006-03-03 17:14:51 -080071 p->path_cost = port_cost(dev);
Stephen Hemminger4433f422005-12-20 15:19:51 -080072
Nikolay Aleksandrovfaa1cd82018-05-03 13:47:24 +030073 *notified = false;
stephen hemmingeraa7c6e52010-08-24 13:12:56 +000074 if (!netif_running(br->dev))
75 return;
76
77 spin_lock_bh(&br->lock);
stephen hemminger576eb622012-12-28 18:15:22 +000078 if (netif_running(dev) && netif_oper_up(dev)) {
Nikolay Aleksandrovfaa1cd82018-05-03 13:47:24 +030079 if (p->state == BR_STATE_DISABLED) {
stephen hemmingeraa7c6e52010-08-24 13:12:56 +000080 br_stp_enable_port(p);
Nikolay Aleksandrovfaa1cd82018-05-03 13:47:24 +030081 *notified = true;
82 }
stephen hemmingeraa7c6e52010-08-24 13:12:56 +000083 } else {
Nikolay Aleksandrovfaa1cd82018-05-03 13:47:24 +030084 if (p->state != BR_STATE_DISABLED) {
stephen hemmingeraa7c6e52010-08-24 13:12:56 +000085 br_stp_disable_port(p);
Nikolay Aleksandrovfaa1cd82018-05-03 13:47:24 +030086 *notified = true;
87 }
Stephen Hemminger4433f422005-12-20 15:19:51 -080088 }
stephen hemmingeraa7c6e52010-08-24 13:12:56 +000089 spin_unlock_bh(&br->lock);
Stephen Hemminger4433f422005-12-20 15:19:51 -080090}
91
Vlad Yasevich2796d0c2014-05-16 09:59:20 -040092static void br_port_set_promisc(struct net_bridge_port *p)
93{
94 int err = 0;
95
96 if (br_promisc_port(p))
97 return;
98
99 err = dev_set_promiscuity(p->dev, 1);
100 if (err)
101 return;
102
103 br_fdb_unsync_static(p->br, p);
104 p->flags |= BR_PROMISC;
105}
106
107static void br_port_clear_promisc(struct net_bridge_port *p)
108{
109 int err;
110
111 /* Check if the port is already non-promisc or if it doesn't
112 * support UNICAST filtering. Without unicast filtering support
113 * we'll end up re-enabling promisc mode anyway, so just check for
114 * it here.
115 */
116 if (!br_promisc_port(p) || !(p->dev->priv_flags & IFF_UNICAST_FLT))
117 return;
118
119 /* Since we'll be clearing the promisc mode, program the port
120 * first so that we don't have interruption in traffic.
121 */
122 err = br_fdb_sync_static(p->br, p);
123 if (err)
124 return;
125
126 dev_set_promiscuity(p->dev, -1);
127 p->flags &= ~BR_PROMISC;
128}
129
130/* When a port is added or removed or when certain port flags
131 * change, this function is called to automatically manage
132 * promiscuity setting of all the bridge ports. We are always called
133 * under RTNL so can skip using rcu primitives.
134 */
135void br_manage_promisc(struct net_bridge *br)
136{
137 struct net_bridge_port *p;
138 bool set_all = false;
139
140 /* If vlan filtering is disabled or bridge interface is placed
141 * into promiscuous mode, place all ports in promiscuous mode.
142 */
Ido Schimmel1f514452017-05-26 08:37:23 +0200143 if ((br->dev->flags & IFF_PROMISC) || !br_vlan_enabled(br->dev))
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400144 set_all = true;
145
146 list_for_each_entry(p, &br->port_list, list) {
147 if (set_all) {
148 br_port_set_promisc(p);
149 } else {
150 /* If the number of auto-ports is <= 1, then all other
151 * ports will have their output configuration
152 * statically specified through fdbs. Since ingress
153 * on the auto-port becomes forwarding/egress to other
154 * ports and egress configuration is statically known,
155 * we can say that ingress configuration of the
156 * auto-port is also statically known.
157 * This lets us disable promiscuous mode and write
158 * this config to hw.
159 */
Toshiaki Makitae0a47d12014-06-05 20:53:32 +0900160 if (br->auto_cnt == 0 ||
161 (br->auto_cnt == 1 && br_auto_port(p)))
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400162 br_port_clear_promisc(p);
163 else
164 br_port_set_promisc(p);
165 }
166 }
167}
168
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300169int nbp_backup_change(struct net_bridge_port *p,
170 struct net_device *backup_dev)
171{
172 struct net_bridge_port *old_backup = rtnl_dereference(p->backup_port);
173 struct net_bridge_port *backup_p = NULL;
174
175 ASSERT_RTNL();
176
177 if (backup_dev) {
Julian Wiedmann35f861e2019-03-29 14:38:19 +0100178 if (!netif_is_bridge_port(backup_dev))
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300179 return -ENOENT;
180
181 backup_p = br_port_get_rtnl(backup_dev);
182 if (backup_p->br != p->br)
183 return -EINVAL;
184 }
185
186 if (p == backup_p)
187 return -EINVAL;
188
189 if (old_backup == backup_p)
190 return 0;
191
192 /* if the backup link is already set, clear it */
193 if (old_backup)
194 old_backup->backup_redirected_cnt--;
195
196 if (backup_p)
197 backup_p->backup_redirected_cnt++;
198 rcu_assign_pointer(p->backup_port, backup_p);
199
200 return 0;
201}
202
203static void nbp_backup_clear(struct net_bridge_port *p)
204{
205 nbp_backup_change(p, NULL);
206 if (p->backup_redirected_cnt) {
207 struct net_bridge_port *cur_p;
208
209 list_for_each_entry(cur_p, &p->br->port_list, list) {
210 struct net_bridge_port *backup_p;
211
212 backup_p = rtnl_dereference(cur_p->backup_port);
213 if (backup_p == p)
214 nbp_backup_change(cur_p, NULL);
215 }
216 }
217
218 WARN_ON(rcu_access_pointer(p->backup_port) || p->backup_redirected_cnt);
219}
220
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400221static void nbp_update_port_count(struct net_bridge *br)
222{
223 struct net_bridge_port *p;
224 u32 cnt = 0;
225
226 list_for_each_entry(p, &br->port_list, list) {
227 if (br_auto_port(p))
228 cnt++;
229 }
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400230 if (br->auto_cnt != cnt) {
231 br->auto_cnt = cnt;
232 br_manage_promisc(br);
233 }
234}
235
236static void nbp_delete_promisc(struct net_bridge_port *p)
237{
stephen hemminger025559e2014-05-16 20:46:17 -0700238 /* If port is currently promiscuous, unset promiscuity.
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400239 * Otherwise, it is a static port so remove all addresses
240 * from it.
241 */
242 dev_set_allmulti(p->dev, -1);
243 if (br_promisc_port(p))
244 dev_set_promiscuity(p->dev, -1);
245 else
246 br_fdb_unsync_static(p->br, p);
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400247}
248
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800249static void release_nbp(struct kobject *kobj)
250{
251 struct net_bridge_port *p
252 = container_of(kobj, struct net_bridge_port, kobj);
253 kfree(p);
254}
255
Tyler Hicks705e0de2018-07-20 21:56:54 +0000256static void brport_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
257{
258 struct net_bridge_port *p = kobj_to_brport(kobj);
259
260 net_ns_get_ownership(dev_net(p->dev), uid, gid);
261}
262
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800263static struct kobj_type brport_ktype = {
264#ifdef CONFIG_SYSFS
265 .sysfs_ops = &brport_sysfs_ops,
266#endif
267 .release = release_nbp,
Tyler Hicks705e0de2018-07-20 21:56:54 +0000268 .get_ownership = brport_get_ownership,
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800269};
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271static void destroy_nbp(struct net_bridge_port *p)
272{
273 struct net_device *dev = p->dev;
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 p->br = NULL;
276 p->dev = NULL;
277 dev_put(dev);
278
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800279 kobject_put(&p->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282static void destroy_nbp_rcu(struct rcu_head *head)
283{
284 struct net_bridge_port *p =
285 container_of(head, struct net_bridge_port, rcu);
286 destroy_nbp(p);
287}
288
Paolo Abeni45493d42016-02-26 10:45:38 +0100289static unsigned get_max_headroom(struct net_bridge *br)
290{
291 unsigned max_headroom = 0;
292 struct net_bridge_port *p;
293
294 list_for_each_entry(p, &br->port_list, list) {
295 unsigned dev_headroom = netdev_get_fwd_headroom(p->dev);
296
297 if (dev_headroom > max_headroom)
298 max_headroom = dev_headroom;
299 }
300
301 return max_headroom;
302}
303
304static void update_headroom(struct net_bridge *br, int new_hr)
305{
306 struct net_bridge_port *p;
307
308 list_for_each_entry(p, &br->port_list, list)
309 netdev_set_rx_headroom(p->dev, new_hr);
310
311 br->dev->needed_headroom = new_hr;
312}
313
Stephen Hemminger3f4cfc22006-01-31 17:44:07 -0800314/* Delete port(interface) from bridge is done in two steps.
315 * via RCU. First step, marks device as down. That deletes
316 * all the timers and stops new packets from flowing through.
317 *
318 * Final cleanup doesn't occur until after all CPU's finished
319 * processing packets.
320 *
321 * Protected from multiple admin operations by RTNL mutex
322 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323static void del_nbp(struct net_bridge_port *p)
324{
325 struct net_bridge *br = p->br;
326 struct net_device *dev = p->dev;
327
Randy Dunlapb3bcb722010-05-18 12:26:27 -0700328 sysfs_remove_link(br->ifobj, p->dev->name);
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800329
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400330 nbp_delete_promisc(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 spin_lock_bh(&br->lock);
333 br_stp_disable_port(p);
334 spin_unlock_bh(&br->lock);
335
Horatiu Vultur65369932020-04-26 15:22:07 +0200336 br_mrp_port_del(br, p);
Henrik Bjoernlund86a14b72020-10-27 10:02:45 +0000337 br_cfm_port_del(br, p);
Horatiu Vultur65369932020-04-26 15:22:07 +0200338
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200339 br_ifinfo_notify(RTM_DELLINK, NULL, p);
Stephen Hemmingerb86c4502007-03-22 14:08:46 -0700340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 list_del_rcu(&p->list);
Paolo Abeni45493d42016-02-26 10:45:38 +0100342 if (netdev_get_fwd_headroom(dev) == br->dev->needed_headroom)
343 update_headroom(br, get_max_headroom(br));
344 netdev_reset_rx_headroom(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Nikolay Aleksandrovf409d0e2015-10-12 21:47:05 +0200346 nbp_vlan_flush(p);
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -0700347 br_fdb_delete_by_port(br, p, 0, 1);
Jiri Pirko56607382015-10-14 19:40:53 +0200348 switchdev_deferred_process();
Nikolay Aleksandrov2756f682018-07-23 11:16:59 +0300349 nbp_backup_clear(p);
Jiri Pirko56607382015-10-14 19:40:53 +0200350
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400351 nbp_update_port_count(br);
352
Jiri Pirko0f495792014-09-05 15:51:28 +0200353 netdev_upper_dev_unlink(dev, br->dev);
354
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000355 dev->priv_flags &= ~IFF_BRIDGE_PORT;
356
Jiri Pirkoab95bfe2010-06-01 21:52:08 +0000357 netdev_rx_handler_unregister(dev);
Stephen Hemmingerb3f1be42006-02-09 17:08:52 -0800358
Herbert Xu3fe2d7c2010-02-28 00:49:38 -0800359 br_multicast_del_port(p);
360
Stephen Hemminger125a12c2006-03-03 17:16:15 -0800361 kobject_uevent(&p->kobj, KOBJ_REMOVE);
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800362 kobject_del(&p->kobj);
363
Herbert Xu91d2c342010-06-10 16:12:50 +0000364 br_netpoll_disable(p);
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 call_rcu(&p->rcu, destroy_nbp_rcu);
367}
368
stephen hemminger1ce5cce2011-10-06 11:19:41 +0000369/* Delete bridge device */
370void br_dev_delete(struct net_device *dev, struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
stephen hemminger1ce5cce2011-10-06 11:19:41 +0000372 struct net_bridge *br = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 struct net_bridge_port *p, *n;
374
375 list_for_each_entry_safe(p, n, &br->port_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 del_nbp(p);
377 }
378
Roopa Prabhu821f1b22017-10-06 22:12:37 -0700379 br_recalculate_neigh_suppress_enabled(br);
380
Nikolay Aleksandrov1ea2d022015-06-23 05:28:16 -0700381 br_fdb_delete_by_port(br, NULL, 0, 1);
Ding Tianhongf8730422013-12-07 22:12:05 +0800382
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100383 cancel_delayed_work_sync(&br->gc_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 br_sysfs_delbr(br->dev);
Eric Dumazet8c56ba02009-10-28 05:35:35 +0000386 unregister_netdevice_queue(br->dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389/* find an available port number */
390static int find_portno(struct net_bridge *br)
391{
392 int index;
393 struct net_bridge_port *p;
394 unsigned long *inuse;
395
Andy Shevchenko459479d2018-08-30 13:33:18 +0300396 inuse = bitmap_zalloc(BR_MAX_PORTS, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 if (!inuse)
398 return -ENOMEM;
399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 set_bit(0, inuse); /* zero is reserved */
401 list_for_each_entry(p, &br->port_list, list) {
402 set_bit(p->port_no, inuse);
403 }
404 index = find_first_zero_bit(inuse, BR_MAX_PORTS);
Andy Shevchenko459479d2018-08-30 13:33:18 +0300405 bitmap_free(inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 return (index >= BR_MAX_PORTS) ? -EXFULL : index;
408}
409
Stephen Hemminger4433f422005-12-20 15:19:51 -0800410/* called with RTNL but without bridge lock */
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900411static struct net_bridge_port *new_nbp(struct net_bridge *br,
Stephen Hemminger4433f422005-12-20 15:19:51 -0800412 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct net_bridge_port *p;
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200415 int index, err;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 index = find_portno(br);
418 if (index < 0)
419 return ERR_PTR(index);
420
Stephen Hemmingercee48542006-03-20 22:57:03 -0800421 p = kzalloc(sizeof(*p), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (p == NULL)
423 return ERR_PTR(-ENOMEM);
424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 p->br = br;
426 dev_hold(dev);
427 p->dev = dev;
Stephen Hemminger4433f422005-12-20 15:19:51 -0800428 p->path_cost = port_cost(dev);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900429 p->priority = 0x8000 >> BR_PORT_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 p->port_no = index;
Mike Manning99f906e2017-04-26 14:48:09 +0100431 p->flags = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 br_init_port(p);
Florian Fainelli775dd692014-09-30 16:13:19 -0700433 br_set_state(p, BR_STATE_DISABLED);
Stephen Hemmingerd32439c2006-03-03 17:15:34 -0800434 br_stp_port_timer_init(p);
Nikolay Aleksandrov1080ab92016-06-28 16:57:06 +0200435 err = br_multicast_add_port(p);
436 if (err) {
437 dev_put(dev);
438 kfree(p);
439 p = ERR_PTR(err);
440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 return p;
443}
444
Alexey Dobriyan4aa678b2008-09-08 16:19:58 -0700445int br_add_bridge(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 struct net_device *dev;
Eric Dumazet11f3a6b2011-08-22 06:05:59 +0000448 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Tom Gundersenc835a672014-07-14 16:37:24 +0200450 dev = alloc_netdev(sizeof(struct net_bridge), name, NET_NAME_UNKNOWN,
stephen hemmingerbb900b22011-04-04 14:03:32 +0000451 br_dev_setup);
452
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900453 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return -ENOMEM;
455
stephen hemmingerbb900b22011-04-04 14:03:32 +0000456 dev_net_set(dev, net);
stephen hemminger149ddd82012-06-26 05:48:45 +0000457 dev->rtnl_link_ops = &br_link_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Nikolay Aleksandrov893b1952021-08-05 11:29:01 +0300459 res = register_netdevice(dev);
Eric Dumazet11f3a6b2011-08-22 06:05:59 +0000460 if (res)
461 free_netdev(dev);
462 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Alexey Dobriyan4aa678b2008-09-08 16:19:58 -0700465int br_del_bridge(struct net *net, const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 struct net_device *dev;
468 int ret = 0;
469
Alexey Dobriyan4aa678b2008-09-08 16:19:58 -0700470 dev = __dev_get_by_name(net, name);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900471 if (dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 ret = -ENXIO; /* Could not find device */
473
474 else if (!(dev->priv_flags & IFF_EBRIDGE)) {
475 /* Attempt to delete non bridge device! */
476 ret = -EPERM;
477 }
478
479 else if (dev->flags & IFF_UP) {
480 /* Not shutdown yet. */
481 ret = -EBUSY;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900484 else
stephen hemminger1ce5cce2011-10-06 11:19:41 +0000485 br_dev_delete(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return ret;
488}
489
Nikolay Aleksandrov804b8542018-03-30 13:46:19 +0300490/* MTU of the bridge pseudo-device: ETH_DATA_LEN or the minimum of the ports */
491static int br_mtu_min(const struct net_bridge *br)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 const struct net_bridge_port *p;
Nikolay Aleksandrovf40aa232018-03-30 13:46:18 +0300494 int ret_mtu = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Nikolay Aleksandrov804b8542018-03-30 13:46:19 +0300496 list_for_each_entry(p, &br->port_list, list)
497 if (!ret_mtu || ret_mtu > p->dev->mtu)
Nikolay Aleksandrovf40aa232018-03-30 13:46:18 +0300498 ret_mtu = p->dev->mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Nikolay Aleksandrovf40aa232018-03-30 13:46:18 +0300500 return ret_mtu ? ret_mtu : ETH_DATA_LEN;
Chas Williams419d14a2018-03-22 11:34:06 -0400501}
502
Nikolay Aleksandrov804b8542018-03-30 13:46:19 +0300503void br_mtu_auto_adjust(struct net_bridge *br)
504{
505 ASSERT_RTNL();
506
507 /* if the bridge MTU was manually configured don't mess with it */
Nikolay Aleksandrov3341d912018-09-26 17:01:06 +0300508 if (br_opt_get(br, BROPT_MTU_SET_BY_USER))
Nikolay Aleksandrov804b8542018-03-30 13:46:19 +0300509 return;
510
511 /* change to the minimum MTU and clear the flag which was set by
512 * the bridge ndo_change_mtu callback
513 */
514 dev_set_mtu(br->dev, br_mtu_min(br));
Nikolay Aleksandrov3341d912018-09-26 17:01:06 +0300515 br_opt_toggle(br, BROPT_MTU_SET_BY_USER, false);
Nikolay Aleksandrov804b8542018-03-30 13:46:19 +0300516}
517
Eric Dumazetae74f102016-03-21 09:55:11 -0700518static void br_set_gso_limits(struct net_bridge *br)
519{
520 unsigned int gso_max_size = GSO_MAX_SIZE;
521 u16 gso_max_segs = GSO_MAX_SEGS;
522 const struct net_bridge_port *p;
523
524 list_for_each_entry(p, &br->port_list, list) {
525 gso_max_size = min(gso_max_size, p->dev->gso_max_size);
526 gso_max_segs = min(gso_max_segs, p->dev->gso_max_segs);
527 }
528 br->dev->gso_max_size = gso_max_size;
529 br->dev->gso_max_segs = gso_max_segs;
530}
531
Stephen Hemminger81d35302005-05-29 14:15:17 -0700532/*
533 * Recomputes features using slave's features
534 */
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000535netdev_features_t br_features_recompute(struct net_bridge *br,
536 netdev_features_t features)
Stephen Hemminger81d35302005-05-29 14:15:17 -0700537{
538 struct net_bridge_port *p;
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000539 netdev_features_t mask;
Stephen Hemminger81d35302005-05-29 14:15:17 -0700540
Herbert Xub63365a2008-10-23 01:11:29 -0700541 if (list_empty(&br->port_list))
Michał Mirosławc4d27ef2011-04-22 06:31:16 +0000542 return features;
Herbert Xub63365a2008-10-23 01:11:29 -0700543
Michał Mirosławc4d27ef2011-04-22 06:31:16 +0000544 mask = features;
Herbert Xub63365a2008-10-23 01:11:29 -0700545 features &= ~NETIF_F_ONE_FOR_ALL;
Stephen Hemminger81d35302005-05-29 14:15:17 -0700546
547 list_for_each_entry(p, &br->port_list, list) {
Herbert Xub63365a2008-10-23 01:11:29 -0700548 features = netdev_increment_features(features,
549 p->dev->features, mask);
Stephen Hemminger81d35302005-05-29 14:15:17 -0700550 }
Toshiaki Makitaf902e882015-01-09 14:16:40 +0900551 features = netdev_add_tso_features(features, mask);
Stephen Hemminger81d35302005-05-29 14:15:17 -0700552
Michał Mirosławc4d27ef2011-04-22 06:31:16 +0000553 return features;
Stephen Hemminger81d35302005-05-29 14:15:17 -0700554}
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556/* called with RTNL */
David Ahernca752be2017-10-04 17:48:50 -0700557int br_add_if(struct net_bridge *br, struct net_device *dev,
558 struct netlink_ext_ack *extack)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 struct net_bridge_port *p;
561 int err = 0;
Paolo Abeni45493d42016-02-26 10:45:38 +0100562 unsigned br_hr, dev_hr;
Wolfgang Bumillera019abd2021-07-02 14:07:36 +0200563 bool changed_addr, fdb_synced = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Vladimir Oltean9eb8eff2020-05-10 19:37:40 +0300565 /* Don't allow bridging non-ethernet like devices. */
Stephen Hemminger1056bd52009-11-05 20:46:52 -0800566 if ((dev->flags & IFF_LOOPBACK) ||
stephen hemminger77f98592011-09-30 14:37:26 +0000567 dev->type != ARPHRD_ETHER || dev->addr_len != ETH_ALEN ||
Vladimir Oltean9eb8eff2020-05-10 19:37:40 +0300568 !is_valid_ether_addr(dev->dev_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return -EINVAL;
570
Vladimir Oltean9eb8eff2020-05-10 19:37:40 +0300571 /* Also don't allow bridging of net devices that are DSA masters, since
572 * the bridge layer rx_handler prevents the DSA fake ethertype handler
573 * to be invoked, so we don't get the chance to strip off and parse the
574 * DSA switch tag protocol header (the bridge layer just returns
575 * RX_HANDLER_CONSUMED, stopping RX processing for these frames).
576 * The only case where that would not be an issue is when bridging can
577 * already be offloaded, such as when the DSA master is itself a DSA
578 * or plain switchdev port, and is bridged only with other ports from
579 * the same hardware device.
580 */
581 if (netdev_uses_dsa(dev)) {
582 list_for_each_entry(p, &br->port_list, list) {
583 if (!netdev_port_same_parent_id(dev, p->dev)) {
584 NL_SET_ERR_MSG(extack,
585 "Cannot do software bridging with a DSA master");
586 return -EINVAL;
587 }
588 }
589 }
590
Stephen Hemminger1056bd52009-11-05 20:46:52 -0800591 /* No bridging of bridges */
David Ahernca752be2017-10-04 17:48:50 -0700592 if (dev->netdev_ops->ndo_start_xmit == br_dev_xmit) {
593 NL_SET_ERR_MSG(extack,
594 "Can not enslave a bridge to a bridge");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return -ELOOP;
David Ahernca752be2017-10-04 17:48:50 -0700596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Hangbin Liue8238fc2018-04-27 20:59:24 +0800598 /* Device has master upper dev */
599 if (netdev_master_upper_dev_get(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return -EBUSY;
601
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100602 /* No bridging devices that dislike that (e.g. wireless) */
David Ahernca752be2017-10-04 17:48:50 -0700603 if (dev->priv_flags & IFF_DONT_BRIDGE) {
604 NL_SET_ERR_MSG(extack,
605 "Device does not allow enslaving to a bridge");
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100606 return -EOPNOTSUPP;
David Ahernca752be2017-10-04 17:48:50 -0700607 }
Johannes Bergad4bb6f2009-11-19 00:56:30 +0100608
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800609 p = new_nbp(br, dev);
610 if (IS_ERR(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return PTR_ERR(p);
612
Amerigo Wangbb8ed632011-05-19 21:39:11 +0000613 call_netdevice_notifiers(NETDEV_JOIN, dev);
614
Vlad Yasevich2796d0c2014-05-16 09:59:20 -0400615 err = dev_set_allmulti(dev, 1);
Tobin C. Hardingbdfad5a2019-05-10 12:52:12 +1000616 if (err) {
617 kfree(p); /* kobject not yet init'd, manually free */
618 goto err1;
619 }
Wang Chenbc3f9072008-07-14 20:53:13 -0700620
Greg Kroah-Hartmane32cc732007-12-17 23:05:35 -0700621 err = kobject_init_and_add(&p->kobj, &brport_ktype, &(dev->dev.kobj),
622 SYSFS_BRIDGE_PORT_ATTR);
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800623 if (err)
Tobin C. Hardingbdfad5a2019-05-10 12:52:12 +1000624 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800626 err = br_sysfs_addif(p);
627 if (err)
628 goto err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
Eric W. Biedermana8779ec2014-03-27 15:36:38 -0700630 err = br_netpoll_enable(p);
stephen hemminger93d8bf92013-07-24 11:51:41 -0700631 if (err)
Herbert Xu91d2c342010-06-10 16:12:50 +0000632 goto err3;
633
Vladimir Oltean9eb8eff2020-05-10 19:37:40 +0300634 err = netdev_rx_handler_register(dev, br_get_rx_handler(dev), p);
Jiri Pirkoab95bfe2010-06-01 21:52:08 +0000635 if (err)
Gao feng9b1536c2012-12-19 23:41:43 +0000636 goto err4;
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000637
Jiri Pirko0f495792014-09-05 15:51:28 +0200638 dev->priv_flags |= IFF_BRIDGE_PORT;
639
David Ahernca752be2017-10-04 17:48:50 -0700640 err = netdev_master_upper_dev_link(dev, br->dev, NULL, NULL, extack);
Jiri Pirkoafc61512011-02-13 09:33:42 +0000641 if (err)
Gao feng9b1536c2012-12-19 23:41:43 +0000642 goto err5;
Jiri Pirkoafc61512011-02-13 09:33:42 +0000643
Ben Hutchings0187bdf2008-06-19 16:15:47 -0700644 dev_disable_lro(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800646 list_add_rcu(&p->list, &br->port_list);
647
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400648 nbp_update_port_count(br);
Wolfgang Bumillera019abd2021-07-02 14:07:36 +0200649 if (!br_promisc_port(p) && (p->dev->priv_flags & IFF_UNICAST_FLT)) {
650 /* When updating the port count we also update all ports'
651 * promiscuous mode.
652 * A port leaving promiscuous mode normally gets the bridge's
653 * fdb synced to the unicast filter (if supported), however,
654 * `br_port_clear_promisc` does not distinguish between
655 * non-promiscuous ports and *new* ports, so we need to
656 * sync explicitly here.
657 */
658 fdb_synced = br_fdb_sync_static(br, p) == 0;
659 if (!fdb_synced)
660 netdev_err(dev, "failed to sync bridge static fdb addresses to this port\n");
661 }
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400662
Michał Mirosławc4d27ef2011-04-22 06:31:16 +0000663 netdev_update_features(br->dev);
664
Paolo Abeni45493d42016-02-26 10:45:38 +0100665 br_hr = br->dev->needed_headroom;
666 dev_hr = netdev_get_fwd_headroom(dev);
667 if (br_hr < dev_hr)
668 update_headroom(br, dev_hr);
669 else
670 netdev_set_rx_headroom(dev, br_hr);
Florian Fainellifd094802013-08-27 12:03:53 +0100671
Toshiaki Makitaa4b816d2014-02-07 16:48:21 +0900672 if (br_fdb_insert(br, p, dev->dev_addr, 0))
673 netdev_err(dev, "failed insert local address bridge forwarding table\n");
674
Petr Machataca935da2018-12-13 11:54:37 +0000675 if (br->dev->addr_assign_type != NET_ADDR_SET) {
676 /* Ask for permission to use this MAC address now, even if we
677 * don't end up choosing it below.
678 */
679 err = dev_pre_changeaddr_notify(br->dev, dev->dev_addr, extack);
680 if (err)
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300681 goto err6;
Petr Machataca935da2018-12-13 11:54:37 +0000682 }
683
Petr Machata169327d2018-12-12 17:02:50 +0000684 err = nbp_vlan_init(p, extack);
Elad Raz08474cc2016-01-06 13:01:04 +0100685 if (err) {
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400686 netdev_err(dev, "failed to initialize vlan filtering on this port\n");
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300687 goto err6;
Elad Raz08474cc2016-01-06 13:01:04 +0100688 }
Vlad Yasevich5be5a2d2014-10-03 11:29:18 -0400689
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800690 spin_lock_bh(&br->lock);
stephen hemmingeredf947f2011-03-24 13:24:01 +0000691 changed_addr = br_stp_recalculate_bridge_id(br);
Aji Srinivasde790592007-03-07 16:10:53 -0800692
stephen hemminger576eb622012-12-28 18:15:22 +0000693 if (netif_running(dev) && netif_oper_up(dev) &&
Aji Srinivasde790592007-03-07 16:10:53 -0800694 (br->dev->flags & IFF_UP))
695 br_stp_enable_port(p);
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800696 spin_unlock_bh(&br->lock);
697
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200698 br_ifinfo_notify(RTM_NEWLINK, NULL, p);
Stephen Hemmingerb86c4502007-03-22 14:08:46 -0700699
stephen hemmingeredf947f2011-03-24 13:24:01 +0000700 if (changed_addr)
stephen hemminger56139fc2011-07-22 07:47:08 +0000701 call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
stephen hemmingeredf947f2011-03-24 13:24:01 +0000702
Nikolay Aleksandrov804b8542018-03-30 13:46:19 +0300703 br_mtu_auto_adjust(br);
Eric Dumazetae74f102016-03-21 09:55:11 -0700704 br_set_gso_limits(br);
Stephen Hemminger269def72007-02-22 01:10:18 -0800705
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800706 kobject_uevent(&p->kobj, KOBJ_ADD);
707
708 return 0;
Jiri Pirkoafc61512011-02-13 09:33:42 +0000709
Vladimir Oltean2f5dc002021-07-21 19:24:01 +0300710err6:
Wolfgang Bumillera019abd2021-07-02 14:07:36 +0200711 if (fdb_synced)
712 br_fdb_unsync_static(br, p);
Elad Raz08474cc2016-01-06 13:01:04 +0100713 list_del_rcu(&p->list);
714 br_fdb_delete_by_port(br, p, 0, 1);
715 nbp_update_port_count(br);
716 netdev_upper_dev_unlink(dev, br->dev);
Gao feng9b1536c2012-12-19 23:41:43 +0000717err5:
Jiri Pirko0f495792014-09-05 15:51:28 +0200718 dev->priv_flags &= ~IFF_BRIDGE_PORT;
719 netdev_rx_handler_unregister(dev);
Gao feng9b1536c2012-12-19 23:41:43 +0000720err4:
721 br_netpoll_disable(p);
Herbert Xu91d2c342010-06-10 16:12:50 +0000722err3:
723 sysfs_remove_link(br->ifobj, p->dev->name);
Stephen Hemmingerbab1dee2006-02-09 17:10:12 -0800724err2:
Xiaotian Fengc587aea2009-07-23 23:06:32 +0000725 kobject_put(&p->kobj);
wangweidong019ee792014-05-29 10:15:30 +0800726 dev_set_allmulti(dev, -1);
Tobin C. Hardingbdfad5a2019-05-10 12:52:12 +1000727err1:
Volodymyr G Lukiianyk43af8532008-04-29 03:17:42 -0700728 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return err;
730}
731
732/* called with RTNL */
733int br_del_if(struct net_bridge *br, struct net_device *dev)
734{
Jiri Pirkof350a0a82010-06-15 06:50:45 +0000735 struct net_bridge_port *p;
Andrei Warkentin9be6dd62011-08-05 11:04:10 +0000736 bool changed_addr;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900737
Eric Dumazetec1e5612010-11-15 06:38:14 +0000738 p = br_port_get_rtnl(dev);
stephen hemmingerb5ed54e2010-11-15 06:38:13 +0000739 if (!p || p->br != br)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return -EINVAL;
741
Amerigo Wangd30362c2012-08-10 01:24:43 +0000742 /* Since more than one interface can be attached to a bridge,
743 * there still maybe an alternate path for netconsole to use;
744 * therefore there is no reason for a NETDEV_RELEASE event.
745 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 del_nbp(p);
747
Nikolay Aleksandrov804b8542018-03-30 13:46:19 +0300748 br_mtu_auto_adjust(br);
Eric Dumazetae74f102016-03-21 09:55:11 -0700749 br_set_gso_limits(br);
Venkat Venkatsubra4c906c22015-03-13 07:08:22 -0700750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 spin_lock_bh(&br->lock);
Andrei Warkentin9be6dd62011-08-05 11:04:10 +0000752 changed_addr = br_stp_recalculate_bridge_id(br);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 spin_unlock_bh(&br->lock);
754
Andrei Warkentin9be6dd62011-08-05 11:04:10 +0000755 if (changed_addr)
756 call_netdevice_notifiers(NETDEV_CHANGEADDR, br->dev);
757
Michał Mirosławc4d27ef2011-04-22 06:31:16 +0000758 netdev_update_features(br->dev);
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 return 0;
761}
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400762
763void br_port_flags_change(struct net_bridge_port *p, unsigned long mask)
764{
765 struct net_bridge *br = p->br;
766
767 if (mask & BR_AUTO_MASK)
768 nbp_update_port_count(br);
Roopa Prabhu821f1b22017-10-06 22:12:37 -0700769
770 if (mask & BR_NEIGH_SUPPRESS)
771 br_recalculate_neigh_suppress_enabled(br);
Vlad Yaseviche028e4b2014-05-16 09:59:16 -0400772}
Ido Schimmel085ddc82018-11-21 08:02:41 +0000773
774bool br_port_flag_is_set(const struct net_device *dev, unsigned long flag)
775{
776 struct net_bridge_port *p;
777
778 p = br_port_get_rtnl_rcu(dev);
779 if (!p)
780 return false;
781
782 return p->flags & flag;
783}
784EXPORT_SYMBOL_GPL(br_port_flag_is_set);