blob: 1d80f34a139c339a2d20e2a0c5756d85150cd62a [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 * Spanning tree protocol; generic parts
4 * Linux ethernet bridge
5 *
6 * Authors:
7 * Lennert Buytenhek <buytenh@gnu.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9#include <linux/kernel.h>
Franck Bui-Huu82524742008-05-12 21:21:05 +020010#include <linux/rculist.h>
Scott Feldman38dcf352014-11-28 14:34:20 +010011#include <net/switchdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13#include "br_private.h"
14#include "br_private_stp.h"
15
16/* since time values in bpdu are in jiffies and then scaled (1/256)
Joakim Tjernlundaaca7352012-03-01 08:12:18 +000017 * before sending, make sure that is at least one STP tick.
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 */
Joakim Tjernlundaaca7352012-03-01 08:12:18 +000019#define MESSAGE_AGE_INCR ((HZ / 256) + 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Jan Engelhardt36cbd3d2009-08-05 10:42:58 -070021static const char *const br_port_state_names[] = {
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090022 [BR_STATE_DISABLED] = "disabled",
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 [BR_STATE_LISTENING] = "listening",
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090024 [BR_STATE_LEARNING] = "learning",
25 [BR_STATE_FORWARDING] = "forwarding",
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 [BR_STATE_BLOCKING] = "blocking",
27};
28
Florian Fainelli775dd692014-09-30 16:13:19 -070029void br_set_state(struct net_bridge_port *p, unsigned int state)
30{
Scott Feldman35636062015-05-10 09:47:51 -070031 struct switchdev_attr attr = {
Ido Schimmel6ff64f62015-12-15 16:03:35 +010032 .orig_dev = p->dev,
Jiri Pirko1f868392015-10-01 11:03:42 +020033 .id = SWITCHDEV_ATTR_ID_PORT_STP_STATE,
Jiri Pirko0bc05d52015-10-14 19:40:50 +020034 .flags = SWITCHDEV_F_DEFER,
Scott Feldman42275bd2015-05-13 11:16:50 -070035 .u.stp_state = state,
Scott Feldman35636062015-05-10 09:47:51 -070036 };
Scott Feldman38dcf352014-11-28 14:34:20 +010037 int err;
38
Horatiu Vultur419dba8a2020-04-26 15:22:08 +020039 /* Don't change the state of the ports if they are driven by a different
40 * protocol.
41 */
42 if (p->flags & BR_MRP_AWARE)
43 return;
44
Florian Fainelli775dd692014-09-30 16:13:19 -070045 p->state = state;
Vladimir Olteandcbdf132021-02-13 22:43:17 +020046 err = switchdev_port_attr_set(p->dev, &attr, NULL);
Ido Schimmelbbe14f52015-11-13 13:06:12 +020047 if (err && err != -EOPNOTSUPP)
Scott Feldman38dcf352014-11-28 14:34:20 +010048 br_warn(p->br, "error setting offload STP state on port %u(%s)\n",
49 (unsigned int) p->port_no, p->dev->name);
Vivien Didelot7c25b162016-02-16 10:09:51 -050050 else
51 br_info(p->br, "port %u(%s) entered %s state\n",
52 (unsigned int) p->port_no, p->dev->name,
53 br_port_state_names[p->state]);
Vivien Didelotde179962019-12-11 20:07:10 -050054
55 if (p->br->stp_enabled == BR_KERNEL_STP) {
56 switch (p->state) {
57 case BR_STATE_BLOCKING:
58 p->stp_xstats.transition_blk++;
59 break;
60 case BR_STATE_FORWARDING:
61 p->stp_xstats.transition_fwd++;
62 break;
63 }
64 }
Florian Fainelli775dd692014-09-30 16:13:19 -070065}
66
Vladimir Olteanc0e715b2021-03-23 01:51:42 +020067u8 br_port_get_stp_state(const struct net_device *dev)
68{
69 struct net_bridge_port *p;
70
71 ASSERT_RTNL();
72
73 p = br_port_get_rtnl(dev);
74 if (!p)
75 return BR_STATE_DISABLED;
76
77 return p->state;
78}
79EXPORT_SYMBOL_GPL(br_port_get_stp_state);
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/* called under bridge lock */
82struct net_bridge_port *br_get_port(struct net_bridge *br, u16 port_no)
83{
84 struct net_bridge_port *p;
85
Madhuparna Bhowmik33c4acb2020-02-19 20:47:46 +053086 list_for_each_entry_rcu(p, &br->port_list, list,
87 lockdep_is_held(&br->lock)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 if (p->port_no == port_no)
89 return p;
90 }
91
92 return NULL;
93}
94
95/* called under bridge lock */
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090096static int br_should_become_root_port(const struct net_bridge_port *p,
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 u16 root_port)
98{
99 struct net_bridge *br;
100 struct net_bridge_port *rp;
101 int t;
102
103 br = p->br;
104 if (p->state == BR_STATE_DISABLED ||
105 br_is_designated_port(p))
106 return 0;
107
108 if (memcmp(&br->bridge_id, &p->designated_root, 8) <= 0)
109 return 0;
110
111 if (!root_port)
112 return 1;
113
114 rp = br_get_port(br, root_port);
115
116 t = memcmp(&p->designated_root, &rp->designated_root, 8);
117 if (t < 0)
118 return 1;
119 else if (t > 0)
120 return 0;
121
122 if (p->designated_cost + p->path_cost <
123 rp->designated_cost + rp->path_cost)
124 return 1;
125 else if (p->designated_cost + p->path_cost >
126 rp->designated_cost + rp->path_cost)
127 return 0;
128
129 t = memcmp(&p->designated_bridge, &rp->designated_bridge, 8);
130 if (t < 0)
131 return 1;
132 else if (t > 0)
133 return 0;
134
135 if (p->designated_port < rp->designated_port)
136 return 1;
137 else if (p->designated_port > rp->designated_port)
138 return 0;
139
140 if (p->port_id < rp->port_id)
141 return 1;
142
143 return 0;
144}
145
stephen hemminger1007dd12012-11-13 07:53:08 +0000146static void br_root_port_block(const struct net_bridge *br,
147 struct net_bridge_port *p)
148{
149
150 br_notice(br, "port %u(%s) tried to become root port (blocked)",
151 (unsigned int) p->port_no, p->dev->name);
152
Florian Fainelli775dd692014-09-30 16:13:19 -0700153 br_set_state(p, BR_STATE_LISTENING);
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200154 br_ifinfo_notify(RTM_NEWLINK, NULL, p);
stephen hemminger1007dd12012-11-13 07:53:08 +0000155
156 if (br->forward_delay > 0)
157 mod_timer(&p->forward_delay_timer, jiffies + br->forward_delay);
158}
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160/* called under bridge lock */
161static void br_root_selection(struct net_bridge *br)
162{
163 struct net_bridge_port *p;
164 u16 root_port = 0;
165
166 list_for_each_entry(p, &br->port_list, list) {
stephen hemminger1007dd12012-11-13 07:53:08 +0000167 if (!br_should_become_root_port(p, root_port))
168 continue;
169
170 if (p->flags & BR_ROOT_BLOCK)
171 br_root_port_block(br, p);
172 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 root_port = p->port_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175
176 br->root_port = root_port;
177
178 if (!root_port) {
179 br->designated_root = br->bridge_id;
180 br->root_path_cost = 0;
181 } else {
182 p = br_get_port(br, root_port);
183 br->designated_root = p->designated_root;
184 br->root_path_cost = p->designated_cost + p->path_cost;
185 }
186}
187
188/* called under bridge lock */
189void br_become_root_bridge(struct net_bridge *br)
190{
191 br->max_age = br->bridge_max_age;
192 br->hello_time = br->bridge_hello_time;
193 br->forward_delay = br->bridge_forward_delay;
194 br_topology_change_detection(br);
195 del_timer(&br->tcn_timer);
196
197 if (br->dev->flags & IFF_UP) {
198 br_config_bpdu_generation(br);
199 mod_timer(&br->hello_timer, jiffies + br->hello_time);
200 }
201}
202
203/* called under bridge lock */
204void br_transmit_config(struct net_bridge_port *p)
205{
206 struct br_config_bpdu bpdu;
207 struct net_bridge *br;
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 if (timer_pending(&p->hold_timer)) {
210 p->config_pending = 1;
211 return;
212 }
213
214 br = p->br;
215
216 bpdu.topology_change = br->topology_change;
217 bpdu.topology_change_ack = p->topology_change_ack;
218 bpdu.root = br->designated_root;
219 bpdu.root_path_cost = br->root_path_cost;
220 bpdu.bridge_id = br->bridge_id;
221 bpdu.port_id = p->port_id;
222 if (br_is_root_bridge(br))
223 bpdu.message_age = 0;
224 else {
225 struct net_bridge_port *root
226 = br_get_port(br, br->root_port);
stephen hemminger0c031502011-07-22 07:47:06 +0000227 bpdu.message_age = (jiffies - root->designated_age)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 + MESSAGE_AGE_INCR;
229 }
230 bpdu.max_age = br->max_age;
231 bpdu.hello_time = br->hello_time;
232 bpdu.forward_delay = br->forward_delay;
233
234 if (bpdu.message_age < br->max_age) {
235 br_send_config_bpdu(p, &bpdu);
236 p->topology_change_ack = 0;
237 p->config_pending = 0;
Nikolay Aleksandrov76b91c32015-07-23 11:01:05 -0700238 if (p->br->stp_enabled == BR_KERNEL_STP)
239 mod_timer(&p->hold_timer,
240 round_jiffies(jiffies + BR_HOLD_TIME));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242}
243
244/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000245static void br_record_config_information(struct net_bridge_port *p,
246 const struct br_config_bpdu *bpdu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 p->designated_root = bpdu->root;
249 p->designated_cost = bpdu->root_path_cost;
250 p->designated_bridge = bpdu->bridge_id;
251 p->designated_port = bpdu->port_id;
Joakim Tjernlund709e1b52012-03-01 08:12:19 +0000252 p->designated_age = jiffies - bpdu->message_age;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900254 mod_timer(&p->message_age_timer, jiffies
Chris Healy9a062012013-09-11 21:37:47 -0700255 + (bpdu->max_age - bpdu->message_age));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000259static void br_record_config_timeout_values(struct net_bridge *br,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 const struct br_config_bpdu *bpdu)
261{
262 br->max_age = bpdu->max_age;
263 br->hello_time = bpdu->hello_time;
264 br->forward_delay = bpdu->forward_delay;
Vivien Didelot8384b5f2016-12-10 13:44:28 -0500265 __br_set_topology_change(br, bpdu->topology_change);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268/* called under bridge lock */
269void br_transmit_tcn(struct net_bridge *br)
270{
stephen hemminger91bc0332013-04-30 05:29:27 +0000271 struct net_bridge_port *p;
272
273 p = br_get_port(br, br->root_port);
274 if (p)
275 br_send_tcn_bpdu(p);
276 else
277 br_notice(br, "root port %u not found for topology notice\n",
278 br->root_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281/* called under bridge lock */
282static int br_should_become_designated_port(const struct net_bridge_port *p)
283{
284 struct net_bridge *br;
285 int t;
286
287 br = p->br;
288 if (br_is_designated_port(p))
289 return 1;
290
291 if (memcmp(&p->designated_root, &br->designated_root, 8))
292 return 1;
293
294 if (br->root_path_cost < p->designated_cost)
295 return 1;
296 else if (br->root_path_cost > p->designated_cost)
297 return 0;
298
299 t = memcmp(&br->bridge_id, &p->designated_bridge, 8);
300 if (t < 0)
301 return 1;
302 else if (t > 0)
303 return 0;
304
305 if (p->port_id < p->designated_port)
306 return 1;
307
308 return 0;
309}
310
311/* called under bridge lock */
312static void br_designated_port_selection(struct net_bridge *br)
313{
314 struct net_bridge_port *p;
315
316 list_for_each_entry(p, &br->port_list, list) {
317 if (p->state != BR_STATE_DISABLED &&
318 br_should_become_designated_port(p))
319 br_become_designated_port(p);
320
321 }
322}
323
324/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000325static int br_supersedes_port_info(const struct net_bridge_port *p,
326 const struct br_config_bpdu *bpdu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 int t;
329
330 t = memcmp(&bpdu->root, &p->designated_root, 8);
331 if (t < 0)
332 return 1;
333 else if (t > 0)
334 return 0;
335
336 if (bpdu->root_path_cost < p->designated_cost)
337 return 1;
338 else if (bpdu->root_path_cost > p->designated_cost)
339 return 0;
340
341 t = memcmp(&bpdu->bridge_id, &p->designated_bridge, 8);
342 if (t < 0)
343 return 1;
344 else if (t > 0)
345 return 0;
346
347 if (memcmp(&bpdu->bridge_id, &p->br->bridge_id, 8))
348 return 1;
349
350 if (bpdu->port_id <= p->designated_port)
351 return 1;
352
353 return 0;
354}
355
356/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000357static void br_topology_change_acknowledged(struct net_bridge *br)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 br->topology_change_detected = 0;
360 del_timer(&br->tcn_timer);
361}
362
363/* called under bridge lock */
364void br_topology_change_detection(struct net_bridge *br)
365{
366 int isroot = br_is_root_bridge(br);
367
Stephen Hemminger4f0611a2009-05-15 06:11:58 +0000368 if (br->stp_enabled != BR_KERNEL_STP)
369 return;
370
stephen hemminger28a16c92010-05-10 09:31:09 +0000371 br_info(br, "topology change detected, %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 isroot ? "propagating" : "sending tcn bpdu");
373
374 if (isroot) {
Vivien Didelot8384b5f2016-12-10 13:44:28 -0500375 __br_set_topology_change(br, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 mod_timer(&br->topology_change_timer, jiffies
377 + br->bridge_forward_delay + br->bridge_max_age);
378 } else if (!br->topology_change_detected) {
379 br_transmit_tcn(br);
380 mod_timer(&br->tcn_timer, jiffies + br->bridge_hello_time);
381 }
382
383 br->topology_change_detected = 1;
384}
385
386/* called under bridge lock */
387void br_config_bpdu_generation(struct net_bridge *br)
388{
389 struct net_bridge_port *p;
390
391 list_for_each_entry(p, &br->port_list, list) {
392 if (p->state != BR_STATE_DISABLED &&
393 br_is_designated_port(p))
394 br_transmit_config(p);
395 }
396}
397
398/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000399static void br_reply(struct net_bridge_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 br_transmit_config(p);
402}
403
404/* called under bridge lock */
405void br_configuration_update(struct net_bridge *br)
406{
407 br_root_selection(br);
408 br_designated_port_selection(br);
409}
410
411/* called under bridge lock */
412void br_become_designated_port(struct net_bridge_port *p)
413{
414 struct net_bridge *br;
415
416 br = p->br;
417 p->designated_root = br->designated_root;
418 p->designated_cost = br->root_path_cost;
419 p->designated_bridge = br->bridge_id;
420 p->designated_port = p->port_id;
421}
422
423
424/* called under bridge lock */
425static void br_make_blocking(struct net_bridge_port *p)
426{
427 if (p->state != BR_STATE_DISABLED &&
428 p->state != BR_STATE_BLOCKING) {
429 if (p->state == BR_STATE_FORWARDING ||
430 p->state == BR_STATE_LEARNING)
431 br_topology_change_detection(p->br);
432
Florian Fainelli775dd692014-09-30 16:13:19 -0700433 br_set_state(p, BR_STATE_BLOCKING);
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200434 br_ifinfo_notify(RTM_NEWLINK, NULL, p);
stephen hemminger4ecb9612011-07-22 07:47:09 +0000435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 del_timer(&p->forward_delay_timer);
437 }
438}
439
440/* called under bridge lock */
441static void br_make_forwarding(struct net_bridge_port *p)
442{
Stephen Hemmingeref647f12008-08-05 18:42:51 -0700443 struct net_bridge *br = p->br;
Stephen Hemminger9cde0702007-03-21 14:22:44 -0700444
Stephen Hemmingeref647f12008-08-05 18:42:51 -0700445 if (p->state != BR_STATE_BLOCKING)
446 return;
447
stephen hemmingera461c022011-03-10 05:57:04 +0000448 if (br->stp_enabled == BR_NO_STP || br->forward_delay == 0) {
Florian Fainelli775dd692014-09-30 16:13:19 -0700449 br_set_state(p, BR_STATE_FORWARDING);
Stephen Hemmingeref647f12008-08-05 18:42:51 -0700450 br_topology_change_detection(br);
451 del_timer(&p->forward_delay_timer);
stephen hemminger160d73b2011-07-22 07:47:10 +0000452 } else if (br->stp_enabled == BR_KERNEL_STP)
Florian Fainelli775dd692014-09-30 16:13:19 -0700453 br_set_state(p, BR_STATE_LISTENING);
Stephen Hemmingeref647f12008-08-05 18:42:51 -0700454 else
Florian Fainelli775dd692014-09-30 16:13:19 -0700455 br_set_state(p, BR_STATE_LEARNING);
Stephen Hemmingeref647f12008-08-05 18:42:51 -0700456
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200457 br_ifinfo_notify(RTM_NEWLINK, NULL, p);
Stephen Hemmingeref647f12008-08-05 18:42:51 -0700458
459 if (br->forward_delay != 0)
460 mod_timer(&p->forward_delay_timer, jiffies + br->forward_delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
463/* called under bridge lock */
464void br_port_state_selection(struct net_bridge *br)
465{
466 struct net_bridge_port *p;
stephen hemminger1faa4352011-03-07 08:34:06 +0000467 unsigned int liveports = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 list_for_each_entry(p, &br->port_list, list) {
stephen hemminger1faa4352011-03-07 08:34:06 +0000470 if (p->state == BR_STATE_DISABLED)
471 continue;
472
Vitalii Demianetsb03b6dd2011-11-25 00:16:37 +0000473 /* Don't change port states if userspace is handling STP */
474 if (br->stp_enabled != BR_USER_STP) {
475 if (p->port_no == br->root_port) {
476 p->config_pending = 0;
477 p->topology_change_ack = 0;
478 br_make_forwarding(p);
479 } else if (br_is_designated_port(p)) {
480 del_timer(&p->message_age_timer);
481 br_make_forwarding(p);
482 } else {
483 p->config_pending = 0;
484 p->topology_change_ack = 0;
485 br_make_blocking(p);
486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488
Nikolay Aleksandrov7ce42de2015-06-19 01:45:50 -0700489 if (p->state != BR_STATE_BLOCKING)
490 br_multicast_enable_port(p);
Nikolay Aleksandrov9aa66382015-06-23 04:47:44 -0700491 /* Multicast is not disabled for the port when it goes in
492 * blocking state because the timers will expire and stop by
493 * themselves without sending more queries.
494 */
stephen hemminger1faa4352011-03-07 08:34:06 +0000495 if (p->state == BR_STATE_FORWARDING)
496 ++liveports;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
stephen hemminger1faa4352011-03-07 08:34:06 +0000498
499 if (liveports == 0)
500 netif_carrier_off(br->dev);
501 else
502 netif_carrier_on(br->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000506static void br_topology_change_acknowledge(struct net_bridge_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
508 p->topology_change_ack = 1;
509 br_transmit_config(p);
510}
511
512/* called under bridge lock */
stephen hemminger160d73b2011-07-22 07:47:10 +0000513void br_received_config_bpdu(struct net_bridge_port *p,
514 const struct br_config_bpdu *bpdu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 struct net_bridge *br;
517 int was_root;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900518
Vivien Didelotde179962019-12-11 20:07:10 -0500519 p->stp_xstats.rx_bpdu++;
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 br = p->br;
522 was_root = br_is_root_bridge(br);
523
524 if (br_supersedes_port_info(p, bpdu)) {
525 br_record_config_information(p, bpdu);
526 br_configuration_update(br);
527 br_port_state_selection(br);
528
529 if (!br_is_root_bridge(br) && was_root) {
530 del_timer(&br->hello_timer);
531 if (br->topology_change_detected) {
532 del_timer(&br->topology_change_timer);
533 br_transmit_tcn(br);
534
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900535 mod_timer(&br->tcn_timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 jiffies + br->bridge_hello_time);
537 }
538 }
539
540 if (p->port_no == br->root_port) {
541 br_record_config_timeout_values(br, bpdu);
542 br_config_bpdu_generation(br);
543 if (bpdu->topology_change_ack)
544 br_topology_change_acknowledged(br);
545 }
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900546 } else if (br_is_designated_port(p)) {
547 br_reply(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549}
550
551/* called under bridge lock */
552void br_received_tcn_bpdu(struct net_bridge_port *p)
553{
Vivien Didelotde179962019-12-11 20:07:10 -0500554 p->stp_xstats.rx_tcn++;
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (br_is_designated_port(p)) {
stephen hemminger28a16c92010-05-10 09:31:09 +0000557 br_info(p->br, "port %u(%s) received tcn bpdu\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000558 (unsigned int) p->port_no, p->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 br_topology_change_detection(p->br);
561 br_topology_change_acknowledge(p);
562 }
563}
stephen hemminger14f98f22011-04-04 14:03:33 +0000564
565/* Change bridge STP parameter */
566int br_set_hello_time(struct net_bridge *br, unsigned long val)
567{
568 unsigned long t = clock_t_to_jiffies(val);
569
570 if (t < BR_MIN_HELLO_TIME || t > BR_MAX_HELLO_TIME)
571 return -ERANGE;
572
573 spin_lock_bh(&br->lock);
574 br->bridge_hello_time = t;
575 if (br_is_root_bridge(br))
576 br->hello_time = br->bridge_hello_time;
577 spin_unlock_bh(&br->lock);
578 return 0;
579}
580
581int br_set_max_age(struct net_bridge *br, unsigned long val)
582{
583 unsigned long t = clock_t_to_jiffies(val);
584
585 if (t < BR_MIN_MAX_AGE || t > BR_MAX_MAX_AGE)
586 return -ERANGE;
587
588 spin_lock_bh(&br->lock);
589 br->bridge_max_age = t;
590 if (br_is_root_bridge(br))
591 br->max_age = br->bridge_max_age;
592 spin_unlock_bh(&br->lock);
593 return 0;
594
595}
596
Vivien Didelot82dd4332016-12-10 13:44:27 -0500597/* called under bridge lock */
598int __set_ageing_time(struct net_device *dev, unsigned long t)
599{
600 struct switchdev_attr attr = {
601 .orig_dev = dev,
602 .id = SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME,
603 .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP | SWITCHDEV_F_DEFER,
604 .u.ageing_time = jiffies_to_clock_t(t),
605 };
606 int err;
607
Vladimir Olteandcbdf132021-02-13 22:43:17 +0200608 err = switchdev_port_attr_set(dev, &attr, NULL);
Vivien Didelot82dd4332016-12-10 13:44:27 -0500609 if (err && err != -EOPNOTSUPP)
610 return err;
611
612 return 0;
613}
614
Stephen Hemminger4c656c12016-03-08 12:59:35 -0800615/* Set time interval that dynamic forwarding entries live
616 * For pure software bridge, allow values outside the 802.1
617 * standard specification for special cases:
Menglong Dongefb5b332021-01-07 18:53:32 -0800618 * 0 - entry never ages (all permanent)
619 * 1 - entry disappears (no persistence)
Stephen Hemminger4c656c12016-03-08 12:59:35 -0800620 *
621 * Offloaded switch entries maybe more restrictive
622 */
Vivien Didelot9e0b27f2016-07-21 12:42:19 -0400623int br_set_ageing_time(struct net_bridge *br, clock_t ageing_time)
Scott Feldmanc62987b2015-10-08 19:23:19 -0700624{
Scott Feldmanc62987b2015-10-08 19:23:19 -0700625 unsigned long t = clock_t_to_jiffies(ageing_time);
626 int err;
627
Vivien Didelot82dd4332016-12-10 13:44:27 -0500628 err = __set_ageing_time(br->dev, t);
629 if (err)
Scott Feldmanc62987b2015-10-08 19:23:19 -0700630 return err;
631
Vivien Didelot34d8acd2016-12-10 13:44:29 -0500632 spin_lock_bh(&br->lock);
633 br->bridge_ageing_time = t;
Scott Feldmanc62987b2015-10-08 19:23:19 -0700634 br->ageing_time = t;
Vivien Didelot34d8acd2016-12-10 13:44:29 -0500635 spin_unlock_bh(&br->lock);
636
Nikolay Aleksandrovf7cdee82017-02-04 18:05:07 +0100637 mod_delayed_work(system_long_wq, &br->gc_work, 0);
Scott Feldmanc62987b2015-10-08 19:23:19 -0700638
639 return 0;
640}
641
Vladimir Olteanbdf123b2021-06-27 14:54:26 +0300642clock_t br_get_ageing_time(const struct net_device *br_dev)
Vladimir Olteanf1d42ea2021-03-23 01:51:43 +0200643{
Vladimir Olteanbdf123b2021-06-27 14:54:26 +0300644 const struct net_bridge *br;
Vladimir Olteanf1d42ea2021-03-23 01:51:43 +0200645
646 if (!netif_is_bridge_master(br_dev))
647 return 0;
648
649 br = netdev_priv(br_dev);
650
651 return jiffies_to_clock_t(br->ageing_time);
652}
653EXPORT_SYMBOL_GPL(br_get_ageing_time);
654
Vivien Didelot8384b5f2016-12-10 13:44:28 -0500655/* called under bridge lock */
656void __br_set_topology_change(struct net_bridge *br, unsigned char val)
657{
Vivien Didelot34d8acd2016-12-10 13:44:29 -0500658 unsigned long t;
659 int err;
660
661 if (br->stp_enabled == BR_KERNEL_STP && br->topology_change != val) {
662 /* On topology change, set the bridge ageing time to twice the
663 * forward delay. Otherwise, restore its default ageing time.
664 */
665
666 if (val) {
667 t = 2 * br->forward_delay;
668 br_debug(br, "decreasing ageing time to %lu\n", t);
669 } else {
670 t = br->bridge_ageing_time;
671 br_debug(br, "restoring ageing time to %lu\n", t);
672 }
673
674 err = __set_ageing_time(br->dev, t);
675 if (err)
676 br_warn(br, "error offloading ageing time\n");
677 else
678 br->ageing_time = t;
679 }
680
Vivien Didelot8384b5f2016-12-10 13:44:28 -0500681 br->topology_change = val;
682}
683
Herbert Xube4f1542013-09-12 17:12:05 +1000684void __br_set_forward_delay(struct net_bridge *br, unsigned long t)
stephen hemminger14f98f22011-04-04 14:03:33 +0000685{
stephen hemminger14f98f22011-04-04 14:03:33 +0000686 br->bridge_forward_delay = t;
687 if (br_is_root_bridge(br))
688 br->forward_delay = br->bridge_forward_delay;
Herbert Xube4f1542013-09-12 17:12:05 +1000689}
690
691int br_set_forward_delay(struct net_bridge *br, unsigned long val)
692{
693 unsigned long t = clock_t_to_jiffies(val);
Vlad Yasevich8a921262015-11-10 06:15:32 -0500694 int err = -ERANGE;
Herbert Xube4f1542013-09-12 17:12:05 +1000695
696 spin_lock_bh(&br->lock);
Vlad Yasevich8a921262015-11-10 06:15:32 -0500697 if (br->stp_enabled != BR_NO_STP &&
698 (t < BR_MIN_FORWARD_DELAY || t > BR_MAX_FORWARD_DELAY))
699 goto unlock;
700
Herbert Xube4f1542013-09-12 17:12:05 +1000701 __br_set_forward_delay(br, t);
Vlad Yasevich8a921262015-11-10 06:15:32 -0500702 err = 0;
703
704unlock:
stephen hemminger14f98f22011-04-04 14:03:33 +0000705 spin_unlock_bh(&br->lock);
Vlad Yasevich8a921262015-11-10 06:15:32 -0500706 return err;
stephen hemminger14f98f22011-04-04 14:03:33 +0000707}