Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Spanning tree protocol; generic parts |
| 4 | * Linux ethernet bridge |
| 5 | * |
| 6 | * Authors: |
| 7 | * Lennert Buytenhek <buytenh@gnu.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | #include <linux/kernel.h> |
Franck Bui-Huu | 8252474 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 10 | #include <linux/rculist.h> |
Scott Feldman | 38dcf35 | 2014-11-28 14:34:20 +0100 | [diff] [blame] | 11 | #include <net/switchdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | |
| 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 Tjernlund | aaca735 | 2012-03-01 08:12:18 +0000 | [diff] [blame] | 17 | * before sending, make sure that is at least one STP tick. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | */ |
Joakim Tjernlund | aaca735 | 2012-03-01 08:12:18 +0000 | [diff] [blame] | 19 | #define MESSAGE_AGE_INCR ((HZ / 256) + 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Jan Engelhardt | 36cbd3d | 2009-08-05 10:42:58 -0700 | [diff] [blame] | 21 | static const char *const br_port_state_names[] = { |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 22 | [BR_STATE_DISABLED] = "disabled", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | [BR_STATE_LISTENING] = "listening", |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 24 | [BR_STATE_LEARNING] = "learning", |
| 25 | [BR_STATE_FORWARDING] = "forwarding", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | [BR_STATE_BLOCKING] = "blocking", |
| 27 | }; |
| 28 | |
Florian Fainelli | 775dd69 | 2014-09-30 16:13:19 -0700 | [diff] [blame] | 29 | void br_set_state(struct net_bridge_port *p, unsigned int state) |
| 30 | { |
Scott Feldman | 3563606 | 2015-05-10 09:47:51 -0700 | [diff] [blame] | 31 | struct switchdev_attr attr = { |
Ido Schimmel | 6ff64f6 | 2015-12-15 16:03:35 +0100 | [diff] [blame] | 32 | .orig_dev = p->dev, |
Jiri Pirko | 1f86839 | 2015-10-01 11:03:42 +0200 | [diff] [blame] | 33 | .id = SWITCHDEV_ATTR_ID_PORT_STP_STATE, |
Jiri Pirko | 0bc05d5 | 2015-10-14 19:40:50 +0200 | [diff] [blame] | 34 | .flags = SWITCHDEV_F_DEFER, |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame] | 35 | .u.stp_state = state, |
Scott Feldman | 3563606 | 2015-05-10 09:47:51 -0700 | [diff] [blame] | 36 | }; |
Scott Feldman | 38dcf35 | 2014-11-28 14:34:20 +0100 | [diff] [blame] | 37 | int err; |
| 38 | |
Horatiu Vultur | 419dba8a | 2020-04-26 15:22:08 +0200 | [diff] [blame] | 39 | /* 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 Fainelli | 775dd69 | 2014-09-30 16:13:19 -0700 | [diff] [blame] | 45 | p->state = state; |
Vladimir Oltean | dcbdf13 | 2021-02-13 22:43:17 +0200 | [diff] [blame^] | 46 | err = switchdev_port_attr_set(p->dev, &attr, NULL); |
Ido Schimmel | bbe14f5 | 2015-11-13 13:06:12 +0200 | [diff] [blame] | 47 | if (err && err != -EOPNOTSUPP) |
Scott Feldman | 38dcf35 | 2014-11-28 14:34:20 +0100 | [diff] [blame] | 48 | br_warn(p->br, "error setting offload STP state on port %u(%s)\n", |
| 49 | (unsigned int) p->port_no, p->dev->name); |
Vivien Didelot | 7c25b16 | 2016-02-16 10:09:51 -0500 | [diff] [blame] | 50 | 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 Didelot | de17996 | 2019-12-11 20:07:10 -0500 | [diff] [blame] | 54 | |
| 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 Fainelli | 775dd69 | 2014-09-30 16:13:19 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | /* called under bridge lock */ |
| 68 | struct net_bridge_port *br_get_port(struct net_bridge *br, u16 port_no) |
| 69 | { |
| 70 | struct net_bridge_port *p; |
| 71 | |
Madhuparna Bhowmik | 33c4acb | 2020-02-19 20:47:46 +0530 | [diff] [blame] | 72 | list_for_each_entry_rcu(p, &br->port_list, list, |
| 73 | lockdep_is_held(&br->lock)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | if (p->port_no == port_no) |
| 75 | return p; |
| 76 | } |
| 77 | |
| 78 | return NULL; |
| 79 | } |
| 80 | |
| 81 | /* called under bridge lock */ |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 82 | static int br_should_become_root_port(const struct net_bridge_port *p, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | u16 root_port) |
| 84 | { |
| 85 | struct net_bridge *br; |
| 86 | struct net_bridge_port *rp; |
| 87 | int t; |
| 88 | |
| 89 | br = p->br; |
| 90 | if (p->state == BR_STATE_DISABLED || |
| 91 | br_is_designated_port(p)) |
| 92 | return 0; |
| 93 | |
| 94 | if (memcmp(&br->bridge_id, &p->designated_root, 8) <= 0) |
| 95 | return 0; |
| 96 | |
| 97 | if (!root_port) |
| 98 | return 1; |
| 99 | |
| 100 | rp = br_get_port(br, root_port); |
| 101 | |
| 102 | t = memcmp(&p->designated_root, &rp->designated_root, 8); |
| 103 | if (t < 0) |
| 104 | return 1; |
| 105 | else if (t > 0) |
| 106 | return 0; |
| 107 | |
| 108 | if (p->designated_cost + p->path_cost < |
| 109 | rp->designated_cost + rp->path_cost) |
| 110 | return 1; |
| 111 | else if (p->designated_cost + p->path_cost > |
| 112 | rp->designated_cost + rp->path_cost) |
| 113 | return 0; |
| 114 | |
| 115 | t = memcmp(&p->designated_bridge, &rp->designated_bridge, 8); |
| 116 | if (t < 0) |
| 117 | return 1; |
| 118 | else if (t > 0) |
| 119 | return 0; |
| 120 | |
| 121 | if (p->designated_port < rp->designated_port) |
| 122 | return 1; |
| 123 | else if (p->designated_port > rp->designated_port) |
| 124 | return 0; |
| 125 | |
| 126 | if (p->port_id < rp->port_id) |
| 127 | return 1; |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
stephen hemminger | 1007dd1 | 2012-11-13 07:53:08 +0000 | [diff] [blame] | 132 | static void br_root_port_block(const struct net_bridge *br, |
| 133 | struct net_bridge_port *p) |
| 134 | { |
| 135 | |
| 136 | br_notice(br, "port %u(%s) tried to become root port (blocked)", |
| 137 | (unsigned int) p->port_no, p->dev->name); |
| 138 | |
Florian Fainelli | 775dd69 | 2014-09-30 16:13:19 -0700 | [diff] [blame] | 139 | br_set_state(p, BR_STATE_LISTENING); |
Nikolay Aleksandrov | 9289906 | 2017-11-01 12:18:13 +0200 | [diff] [blame] | 140 | br_ifinfo_notify(RTM_NEWLINK, NULL, p); |
stephen hemminger | 1007dd1 | 2012-11-13 07:53:08 +0000 | [diff] [blame] | 141 | |
| 142 | if (br->forward_delay > 0) |
| 143 | mod_timer(&p->forward_delay_timer, jiffies + br->forward_delay); |
| 144 | } |
| 145 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | /* called under bridge lock */ |
| 147 | static void br_root_selection(struct net_bridge *br) |
| 148 | { |
| 149 | struct net_bridge_port *p; |
| 150 | u16 root_port = 0; |
| 151 | |
| 152 | list_for_each_entry(p, &br->port_list, list) { |
stephen hemminger | 1007dd1 | 2012-11-13 07:53:08 +0000 | [diff] [blame] | 153 | if (!br_should_become_root_port(p, root_port)) |
| 154 | continue; |
| 155 | |
| 156 | if (p->flags & BR_ROOT_BLOCK) |
| 157 | br_root_port_block(br, p); |
| 158 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | root_port = p->port_no; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | br->root_port = root_port; |
| 163 | |
| 164 | if (!root_port) { |
| 165 | br->designated_root = br->bridge_id; |
| 166 | br->root_path_cost = 0; |
| 167 | } else { |
| 168 | p = br_get_port(br, root_port); |
| 169 | br->designated_root = p->designated_root; |
| 170 | br->root_path_cost = p->designated_cost + p->path_cost; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /* called under bridge lock */ |
| 175 | void br_become_root_bridge(struct net_bridge *br) |
| 176 | { |
| 177 | br->max_age = br->bridge_max_age; |
| 178 | br->hello_time = br->bridge_hello_time; |
| 179 | br->forward_delay = br->bridge_forward_delay; |
| 180 | br_topology_change_detection(br); |
| 181 | del_timer(&br->tcn_timer); |
| 182 | |
| 183 | if (br->dev->flags & IFF_UP) { |
| 184 | br_config_bpdu_generation(br); |
| 185 | mod_timer(&br->hello_timer, jiffies + br->hello_time); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /* called under bridge lock */ |
| 190 | void br_transmit_config(struct net_bridge_port *p) |
| 191 | { |
| 192 | struct br_config_bpdu bpdu; |
| 193 | struct net_bridge *br; |
| 194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | if (timer_pending(&p->hold_timer)) { |
| 196 | p->config_pending = 1; |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | br = p->br; |
| 201 | |
| 202 | bpdu.topology_change = br->topology_change; |
| 203 | bpdu.topology_change_ack = p->topology_change_ack; |
| 204 | bpdu.root = br->designated_root; |
| 205 | bpdu.root_path_cost = br->root_path_cost; |
| 206 | bpdu.bridge_id = br->bridge_id; |
| 207 | bpdu.port_id = p->port_id; |
| 208 | if (br_is_root_bridge(br)) |
| 209 | bpdu.message_age = 0; |
| 210 | else { |
| 211 | struct net_bridge_port *root |
| 212 | = br_get_port(br, br->root_port); |
stephen hemminger | 0c03150 | 2011-07-22 07:47:06 +0000 | [diff] [blame] | 213 | bpdu.message_age = (jiffies - root->designated_age) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | + MESSAGE_AGE_INCR; |
| 215 | } |
| 216 | bpdu.max_age = br->max_age; |
| 217 | bpdu.hello_time = br->hello_time; |
| 218 | bpdu.forward_delay = br->forward_delay; |
| 219 | |
| 220 | if (bpdu.message_age < br->max_age) { |
| 221 | br_send_config_bpdu(p, &bpdu); |
| 222 | p->topology_change_ack = 0; |
| 223 | p->config_pending = 0; |
Nikolay Aleksandrov | 76b91c3 | 2015-07-23 11:01:05 -0700 | [diff] [blame] | 224 | if (p->br->stp_enabled == BR_KERNEL_STP) |
| 225 | mod_timer(&p->hold_timer, |
| 226 | round_jiffies(jiffies + BR_HOLD_TIME)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| 230 | /* called under bridge lock */ |
stephen hemminger | 160d73b | 2011-07-22 07:47:10 +0000 | [diff] [blame] | 231 | static void br_record_config_information(struct net_bridge_port *p, |
| 232 | const struct br_config_bpdu *bpdu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | { |
| 234 | p->designated_root = bpdu->root; |
| 235 | p->designated_cost = bpdu->root_path_cost; |
| 236 | p->designated_bridge = bpdu->bridge_id; |
| 237 | p->designated_port = bpdu->port_id; |
Joakim Tjernlund | 709e1b5 | 2012-03-01 08:12:19 +0000 | [diff] [blame] | 238 | p->designated_age = jiffies - bpdu->message_age; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 240 | mod_timer(&p->message_age_timer, jiffies |
Chris Healy | 9a06201 | 2013-09-11 21:37:47 -0700 | [diff] [blame] | 241 | + (bpdu->max_age - bpdu->message_age)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /* called under bridge lock */ |
stephen hemminger | 160d73b | 2011-07-22 07:47:10 +0000 | [diff] [blame] | 245 | static void br_record_config_timeout_values(struct net_bridge *br, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | const struct br_config_bpdu *bpdu) |
| 247 | { |
| 248 | br->max_age = bpdu->max_age; |
| 249 | br->hello_time = bpdu->hello_time; |
| 250 | br->forward_delay = bpdu->forward_delay; |
Vivien Didelot | 8384b5f | 2016-12-10 13:44:28 -0500 | [diff] [blame] | 251 | __br_set_topology_change(br, bpdu->topology_change); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /* called under bridge lock */ |
| 255 | void br_transmit_tcn(struct net_bridge *br) |
| 256 | { |
stephen hemminger | 91bc033 | 2013-04-30 05:29:27 +0000 | [diff] [blame] | 257 | struct net_bridge_port *p; |
| 258 | |
| 259 | p = br_get_port(br, br->root_port); |
| 260 | if (p) |
| 261 | br_send_tcn_bpdu(p); |
| 262 | else |
| 263 | br_notice(br, "root port %u not found for topology notice\n", |
| 264 | br->root_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /* called under bridge lock */ |
| 268 | static int br_should_become_designated_port(const struct net_bridge_port *p) |
| 269 | { |
| 270 | struct net_bridge *br; |
| 271 | int t; |
| 272 | |
| 273 | br = p->br; |
| 274 | if (br_is_designated_port(p)) |
| 275 | return 1; |
| 276 | |
| 277 | if (memcmp(&p->designated_root, &br->designated_root, 8)) |
| 278 | return 1; |
| 279 | |
| 280 | if (br->root_path_cost < p->designated_cost) |
| 281 | return 1; |
| 282 | else if (br->root_path_cost > p->designated_cost) |
| 283 | return 0; |
| 284 | |
| 285 | t = memcmp(&br->bridge_id, &p->designated_bridge, 8); |
| 286 | if (t < 0) |
| 287 | return 1; |
| 288 | else if (t > 0) |
| 289 | return 0; |
| 290 | |
| 291 | if (p->port_id < p->designated_port) |
| 292 | return 1; |
| 293 | |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | /* called under bridge lock */ |
| 298 | static void br_designated_port_selection(struct net_bridge *br) |
| 299 | { |
| 300 | struct net_bridge_port *p; |
| 301 | |
| 302 | list_for_each_entry(p, &br->port_list, list) { |
| 303 | if (p->state != BR_STATE_DISABLED && |
| 304 | br_should_become_designated_port(p)) |
| 305 | br_become_designated_port(p); |
| 306 | |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /* called under bridge lock */ |
stephen hemminger | 160d73b | 2011-07-22 07:47:10 +0000 | [diff] [blame] | 311 | static int br_supersedes_port_info(const struct net_bridge_port *p, |
| 312 | const struct br_config_bpdu *bpdu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | { |
| 314 | int t; |
| 315 | |
| 316 | t = memcmp(&bpdu->root, &p->designated_root, 8); |
| 317 | if (t < 0) |
| 318 | return 1; |
| 319 | else if (t > 0) |
| 320 | return 0; |
| 321 | |
| 322 | if (bpdu->root_path_cost < p->designated_cost) |
| 323 | return 1; |
| 324 | else if (bpdu->root_path_cost > p->designated_cost) |
| 325 | return 0; |
| 326 | |
| 327 | t = memcmp(&bpdu->bridge_id, &p->designated_bridge, 8); |
| 328 | if (t < 0) |
| 329 | return 1; |
| 330 | else if (t > 0) |
| 331 | return 0; |
| 332 | |
| 333 | if (memcmp(&bpdu->bridge_id, &p->br->bridge_id, 8)) |
| 334 | return 1; |
| 335 | |
| 336 | if (bpdu->port_id <= p->designated_port) |
| 337 | return 1; |
| 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | /* called under bridge lock */ |
stephen hemminger | 160d73b | 2011-07-22 07:47:10 +0000 | [diff] [blame] | 343 | static void br_topology_change_acknowledged(struct net_bridge *br) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | { |
| 345 | br->topology_change_detected = 0; |
| 346 | del_timer(&br->tcn_timer); |
| 347 | } |
| 348 | |
| 349 | /* called under bridge lock */ |
| 350 | void br_topology_change_detection(struct net_bridge *br) |
| 351 | { |
| 352 | int isroot = br_is_root_bridge(br); |
| 353 | |
Stephen Hemminger | 4f0611a | 2009-05-15 06:11:58 +0000 | [diff] [blame] | 354 | if (br->stp_enabled != BR_KERNEL_STP) |
| 355 | return; |
| 356 | |
stephen hemminger | 28a16c9 | 2010-05-10 09:31:09 +0000 | [diff] [blame] | 357 | br_info(br, "topology change detected, %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | isroot ? "propagating" : "sending tcn bpdu"); |
| 359 | |
| 360 | if (isroot) { |
Vivien Didelot | 8384b5f | 2016-12-10 13:44:28 -0500 | [diff] [blame] | 361 | __br_set_topology_change(br, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | mod_timer(&br->topology_change_timer, jiffies |
| 363 | + br->bridge_forward_delay + br->bridge_max_age); |
| 364 | } else if (!br->topology_change_detected) { |
| 365 | br_transmit_tcn(br); |
| 366 | mod_timer(&br->tcn_timer, jiffies + br->bridge_hello_time); |
| 367 | } |
| 368 | |
| 369 | br->topology_change_detected = 1; |
| 370 | } |
| 371 | |
| 372 | /* called under bridge lock */ |
| 373 | void br_config_bpdu_generation(struct net_bridge *br) |
| 374 | { |
| 375 | struct net_bridge_port *p; |
| 376 | |
| 377 | list_for_each_entry(p, &br->port_list, list) { |
| 378 | if (p->state != BR_STATE_DISABLED && |
| 379 | br_is_designated_port(p)) |
| 380 | br_transmit_config(p); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | /* called under bridge lock */ |
stephen hemminger | 160d73b | 2011-07-22 07:47:10 +0000 | [diff] [blame] | 385 | static void br_reply(struct net_bridge_port *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | { |
| 387 | br_transmit_config(p); |
| 388 | } |
| 389 | |
| 390 | /* called under bridge lock */ |
| 391 | void br_configuration_update(struct net_bridge *br) |
| 392 | { |
| 393 | br_root_selection(br); |
| 394 | br_designated_port_selection(br); |
| 395 | } |
| 396 | |
| 397 | /* called under bridge lock */ |
| 398 | void br_become_designated_port(struct net_bridge_port *p) |
| 399 | { |
| 400 | struct net_bridge *br; |
| 401 | |
| 402 | br = p->br; |
| 403 | p->designated_root = br->designated_root; |
| 404 | p->designated_cost = br->root_path_cost; |
| 405 | p->designated_bridge = br->bridge_id; |
| 406 | p->designated_port = p->port_id; |
| 407 | } |
| 408 | |
| 409 | |
| 410 | /* called under bridge lock */ |
| 411 | static void br_make_blocking(struct net_bridge_port *p) |
| 412 | { |
| 413 | if (p->state != BR_STATE_DISABLED && |
| 414 | p->state != BR_STATE_BLOCKING) { |
| 415 | if (p->state == BR_STATE_FORWARDING || |
| 416 | p->state == BR_STATE_LEARNING) |
| 417 | br_topology_change_detection(p->br); |
| 418 | |
Florian Fainelli | 775dd69 | 2014-09-30 16:13:19 -0700 | [diff] [blame] | 419 | br_set_state(p, BR_STATE_BLOCKING); |
Nikolay Aleksandrov | 9289906 | 2017-11-01 12:18:13 +0200 | [diff] [blame] | 420 | br_ifinfo_notify(RTM_NEWLINK, NULL, p); |
stephen hemminger | 4ecb961 | 2011-07-22 07:47:09 +0000 | [diff] [blame] | 421 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | del_timer(&p->forward_delay_timer); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | /* called under bridge lock */ |
| 427 | static void br_make_forwarding(struct net_bridge_port *p) |
| 428 | { |
Stephen Hemminger | ef647f1 | 2008-08-05 18:42:51 -0700 | [diff] [blame] | 429 | struct net_bridge *br = p->br; |
Stephen Hemminger | 9cde070 | 2007-03-21 14:22:44 -0700 | [diff] [blame] | 430 | |
Stephen Hemminger | ef647f1 | 2008-08-05 18:42:51 -0700 | [diff] [blame] | 431 | if (p->state != BR_STATE_BLOCKING) |
| 432 | return; |
| 433 | |
stephen hemminger | a461c02 | 2011-03-10 05:57:04 +0000 | [diff] [blame] | 434 | if (br->stp_enabled == BR_NO_STP || br->forward_delay == 0) { |
Florian Fainelli | 775dd69 | 2014-09-30 16:13:19 -0700 | [diff] [blame] | 435 | br_set_state(p, BR_STATE_FORWARDING); |
Stephen Hemminger | ef647f1 | 2008-08-05 18:42:51 -0700 | [diff] [blame] | 436 | br_topology_change_detection(br); |
| 437 | del_timer(&p->forward_delay_timer); |
stephen hemminger | 160d73b | 2011-07-22 07:47:10 +0000 | [diff] [blame] | 438 | } else if (br->stp_enabled == BR_KERNEL_STP) |
Florian Fainelli | 775dd69 | 2014-09-30 16:13:19 -0700 | [diff] [blame] | 439 | br_set_state(p, BR_STATE_LISTENING); |
Stephen Hemminger | ef647f1 | 2008-08-05 18:42:51 -0700 | [diff] [blame] | 440 | else |
Florian Fainelli | 775dd69 | 2014-09-30 16:13:19 -0700 | [diff] [blame] | 441 | br_set_state(p, BR_STATE_LEARNING); |
Stephen Hemminger | ef647f1 | 2008-08-05 18:42:51 -0700 | [diff] [blame] | 442 | |
Nikolay Aleksandrov | 9289906 | 2017-11-01 12:18:13 +0200 | [diff] [blame] | 443 | br_ifinfo_notify(RTM_NEWLINK, NULL, p); |
Stephen Hemminger | ef647f1 | 2008-08-05 18:42:51 -0700 | [diff] [blame] | 444 | |
| 445 | if (br->forward_delay != 0) |
| 446 | mod_timer(&p->forward_delay_timer, jiffies + br->forward_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /* called under bridge lock */ |
| 450 | void br_port_state_selection(struct net_bridge *br) |
| 451 | { |
| 452 | struct net_bridge_port *p; |
stephen hemminger | 1faa435 | 2011-03-07 08:34:06 +0000 | [diff] [blame] | 453 | unsigned int liveports = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
| 455 | list_for_each_entry(p, &br->port_list, list) { |
stephen hemminger | 1faa435 | 2011-03-07 08:34:06 +0000 | [diff] [blame] | 456 | if (p->state == BR_STATE_DISABLED) |
| 457 | continue; |
| 458 | |
Vitalii Demianets | b03b6dd | 2011-11-25 00:16:37 +0000 | [diff] [blame] | 459 | /* Don't change port states if userspace is handling STP */ |
| 460 | if (br->stp_enabled != BR_USER_STP) { |
| 461 | if (p->port_no == br->root_port) { |
| 462 | p->config_pending = 0; |
| 463 | p->topology_change_ack = 0; |
| 464 | br_make_forwarding(p); |
| 465 | } else if (br_is_designated_port(p)) { |
| 466 | del_timer(&p->message_age_timer); |
| 467 | br_make_forwarding(p); |
| 468 | } else { |
| 469 | p->config_pending = 0; |
| 470 | p->topology_change_ack = 0; |
| 471 | br_make_blocking(p); |
| 472 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | } |
| 474 | |
Nikolay Aleksandrov | 7ce42de | 2015-06-19 01:45:50 -0700 | [diff] [blame] | 475 | if (p->state != BR_STATE_BLOCKING) |
| 476 | br_multicast_enable_port(p); |
Nikolay Aleksandrov | 9aa6638 | 2015-06-23 04:47:44 -0700 | [diff] [blame] | 477 | /* Multicast is not disabled for the port when it goes in |
| 478 | * blocking state because the timers will expire and stop by |
| 479 | * themselves without sending more queries. |
| 480 | */ |
stephen hemminger | 1faa435 | 2011-03-07 08:34:06 +0000 | [diff] [blame] | 481 | if (p->state == BR_STATE_FORWARDING) |
| 482 | ++liveports; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | } |
stephen hemminger | 1faa435 | 2011-03-07 08:34:06 +0000 | [diff] [blame] | 484 | |
| 485 | if (liveports == 0) |
| 486 | netif_carrier_off(br->dev); |
| 487 | else |
| 488 | netif_carrier_on(br->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | /* called under bridge lock */ |
stephen hemminger | 160d73b | 2011-07-22 07:47:10 +0000 | [diff] [blame] | 492 | static void br_topology_change_acknowledge(struct net_bridge_port *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | { |
| 494 | p->topology_change_ack = 1; |
| 495 | br_transmit_config(p); |
| 496 | } |
| 497 | |
| 498 | /* called under bridge lock */ |
stephen hemminger | 160d73b | 2011-07-22 07:47:10 +0000 | [diff] [blame] | 499 | void br_received_config_bpdu(struct net_bridge_port *p, |
| 500 | const struct br_config_bpdu *bpdu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | { |
| 502 | struct net_bridge *br; |
| 503 | int was_root; |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 504 | |
Vivien Didelot | de17996 | 2019-12-11 20:07:10 -0500 | [diff] [blame] | 505 | p->stp_xstats.rx_bpdu++; |
| 506 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | br = p->br; |
| 508 | was_root = br_is_root_bridge(br); |
| 509 | |
| 510 | if (br_supersedes_port_info(p, bpdu)) { |
| 511 | br_record_config_information(p, bpdu); |
| 512 | br_configuration_update(br); |
| 513 | br_port_state_selection(br); |
| 514 | |
| 515 | if (!br_is_root_bridge(br) && was_root) { |
| 516 | del_timer(&br->hello_timer); |
| 517 | if (br->topology_change_detected) { |
| 518 | del_timer(&br->topology_change_timer); |
| 519 | br_transmit_tcn(br); |
| 520 | |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 521 | mod_timer(&br->tcn_timer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | jiffies + br->bridge_hello_time); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | if (p->port_no == br->root_port) { |
| 527 | br_record_config_timeout_values(br, bpdu); |
| 528 | br_config_bpdu_generation(br); |
| 529 | if (bpdu->topology_change_ack) |
| 530 | br_topology_change_acknowledged(br); |
| 531 | } |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 532 | } else if (br_is_designated_port(p)) { |
| 533 | br_reply(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | |
| 537 | /* called under bridge lock */ |
| 538 | void br_received_tcn_bpdu(struct net_bridge_port *p) |
| 539 | { |
Vivien Didelot | de17996 | 2019-12-11 20:07:10 -0500 | [diff] [blame] | 540 | p->stp_xstats.rx_tcn++; |
| 541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | if (br_is_designated_port(p)) { |
stephen hemminger | 28a16c9 | 2010-05-10 09:31:09 +0000 | [diff] [blame] | 543 | br_info(p->br, "port %u(%s) received tcn bpdu\n", |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 544 | (unsigned int) p->port_no, p->dev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
| 546 | br_topology_change_detection(p->br); |
| 547 | br_topology_change_acknowledge(p); |
| 548 | } |
| 549 | } |
stephen hemminger | 14f98f2 | 2011-04-04 14:03:33 +0000 | [diff] [blame] | 550 | |
| 551 | /* Change bridge STP parameter */ |
| 552 | int br_set_hello_time(struct net_bridge *br, unsigned long val) |
| 553 | { |
| 554 | unsigned long t = clock_t_to_jiffies(val); |
| 555 | |
| 556 | if (t < BR_MIN_HELLO_TIME || t > BR_MAX_HELLO_TIME) |
| 557 | return -ERANGE; |
| 558 | |
| 559 | spin_lock_bh(&br->lock); |
| 560 | br->bridge_hello_time = t; |
| 561 | if (br_is_root_bridge(br)) |
| 562 | br->hello_time = br->bridge_hello_time; |
| 563 | spin_unlock_bh(&br->lock); |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | int br_set_max_age(struct net_bridge *br, unsigned long val) |
| 568 | { |
| 569 | unsigned long t = clock_t_to_jiffies(val); |
| 570 | |
| 571 | if (t < BR_MIN_MAX_AGE || t > BR_MAX_MAX_AGE) |
| 572 | return -ERANGE; |
| 573 | |
| 574 | spin_lock_bh(&br->lock); |
| 575 | br->bridge_max_age = t; |
| 576 | if (br_is_root_bridge(br)) |
| 577 | br->max_age = br->bridge_max_age; |
| 578 | spin_unlock_bh(&br->lock); |
| 579 | return 0; |
| 580 | |
| 581 | } |
| 582 | |
Vivien Didelot | 82dd433 | 2016-12-10 13:44:27 -0500 | [diff] [blame] | 583 | /* called under bridge lock */ |
| 584 | int __set_ageing_time(struct net_device *dev, unsigned long t) |
| 585 | { |
| 586 | struct switchdev_attr attr = { |
| 587 | .orig_dev = dev, |
| 588 | .id = SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME, |
| 589 | .flags = SWITCHDEV_F_SKIP_EOPNOTSUPP | SWITCHDEV_F_DEFER, |
| 590 | .u.ageing_time = jiffies_to_clock_t(t), |
| 591 | }; |
| 592 | int err; |
| 593 | |
Vladimir Oltean | dcbdf13 | 2021-02-13 22:43:17 +0200 | [diff] [blame^] | 594 | err = switchdev_port_attr_set(dev, &attr, NULL); |
Vivien Didelot | 82dd433 | 2016-12-10 13:44:27 -0500 | [diff] [blame] | 595 | if (err && err != -EOPNOTSUPP) |
| 596 | return err; |
| 597 | |
| 598 | return 0; |
| 599 | } |
| 600 | |
Stephen Hemminger | 4c656c1 | 2016-03-08 12:59:35 -0800 | [diff] [blame] | 601 | /* Set time interval that dynamic forwarding entries live |
| 602 | * For pure software bridge, allow values outside the 802.1 |
| 603 | * standard specification for special cases: |
Menglong Dong | efb5b33 | 2021-01-07 18:53:32 -0800 | [diff] [blame] | 604 | * 0 - entry never ages (all permanent) |
| 605 | * 1 - entry disappears (no persistence) |
Stephen Hemminger | 4c656c1 | 2016-03-08 12:59:35 -0800 | [diff] [blame] | 606 | * |
| 607 | * Offloaded switch entries maybe more restrictive |
| 608 | */ |
Vivien Didelot | 9e0b27f | 2016-07-21 12:42:19 -0400 | [diff] [blame] | 609 | int br_set_ageing_time(struct net_bridge *br, clock_t ageing_time) |
Scott Feldman | c62987b | 2015-10-08 19:23:19 -0700 | [diff] [blame] | 610 | { |
Scott Feldman | c62987b | 2015-10-08 19:23:19 -0700 | [diff] [blame] | 611 | unsigned long t = clock_t_to_jiffies(ageing_time); |
| 612 | int err; |
| 613 | |
Vivien Didelot | 82dd433 | 2016-12-10 13:44:27 -0500 | [diff] [blame] | 614 | err = __set_ageing_time(br->dev, t); |
| 615 | if (err) |
Scott Feldman | c62987b | 2015-10-08 19:23:19 -0700 | [diff] [blame] | 616 | return err; |
| 617 | |
Vivien Didelot | 34d8acd | 2016-12-10 13:44:29 -0500 | [diff] [blame] | 618 | spin_lock_bh(&br->lock); |
| 619 | br->bridge_ageing_time = t; |
Scott Feldman | c62987b | 2015-10-08 19:23:19 -0700 | [diff] [blame] | 620 | br->ageing_time = t; |
Vivien Didelot | 34d8acd | 2016-12-10 13:44:29 -0500 | [diff] [blame] | 621 | spin_unlock_bh(&br->lock); |
| 622 | |
Nikolay Aleksandrov | f7cdee8 | 2017-02-04 18:05:07 +0100 | [diff] [blame] | 623 | mod_delayed_work(system_long_wq, &br->gc_work, 0); |
Scott Feldman | c62987b | 2015-10-08 19:23:19 -0700 | [diff] [blame] | 624 | |
| 625 | return 0; |
| 626 | } |
| 627 | |
Vivien Didelot | 8384b5f | 2016-12-10 13:44:28 -0500 | [diff] [blame] | 628 | /* called under bridge lock */ |
| 629 | void __br_set_topology_change(struct net_bridge *br, unsigned char val) |
| 630 | { |
Vivien Didelot | 34d8acd | 2016-12-10 13:44:29 -0500 | [diff] [blame] | 631 | unsigned long t; |
| 632 | int err; |
| 633 | |
| 634 | if (br->stp_enabled == BR_KERNEL_STP && br->topology_change != val) { |
| 635 | /* On topology change, set the bridge ageing time to twice the |
| 636 | * forward delay. Otherwise, restore its default ageing time. |
| 637 | */ |
| 638 | |
| 639 | if (val) { |
| 640 | t = 2 * br->forward_delay; |
| 641 | br_debug(br, "decreasing ageing time to %lu\n", t); |
| 642 | } else { |
| 643 | t = br->bridge_ageing_time; |
| 644 | br_debug(br, "restoring ageing time to %lu\n", t); |
| 645 | } |
| 646 | |
| 647 | err = __set_ageing_time(br->dev, t); |
| 648 | if (err) |
| 649 | br_warn(br, "error offloading ageing time\n"); |
| 650 | else |
| 651 | br->ageing_time = t; |
| 652 | } |
| 653 | |
Vivien Didelot | 8384b5f | 2016-12-10 13:44:28 -0500 | [diff] [blame] | 654 | br->topology_change = val; |
| 655 | } |
| 656 | |
Herbert Xu | be4f154 | 2013-09-12 17:12:05 +1000 | [diff] [blame] | 657 | void __br_set_forward_delay(struct net_bridge *br, unsigned long t) |
stephen hemminger | 14f98f2 | 2011-04-04 14:03:33 +0000 | [diff] [blame] | 658 | { |
stephen hemminger | 14f98f2 | 2011-04-04 14:03:33 +0000 | [diff] [blame] | 659 | br->bridge_forward_delay = t; |
| 660 | if (br_is_root_bridge(br)) |
| 661 | br->forward_delay = br->bridge_forward_delay; |
Herbert Xu | be4f154 | 2013-09-12 17:12:05 +1000 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | int br_set_forward_delay(struct net_bridge *br, unsigned long val) |
| 665 | { |
| 666 | unsigned long t = clock_t_to_jiffies(val); |
Vlad Yasevich | 8a92126 | 2015-11-10 06:15:32 -0500 | [diff] [blame] | 667 | int err = -ERANGE; |
Herbert Xu | be4f154 | 2013-09-12 17:12:05 +1000 | [diff] [blame] | 668 | |
| 669 | spin_lock_bh(&br->lock); |
Vlad Yasevich | 8a92126 | 2015-11-10 06:15:32 -0500 | [diff] [blame] | 670 | if (br->stp_enabled != BR_NO_STP && |
| 671 | (t < BR_MIN_FORWARD_DELAY || t > BR_MAX_FORWARD_DELAY)) |
| 672 | goto unlock; |
| 673 | |
Herbert Xu | be4f154 | 2013-09-12 17:12:05 +1000 | [diff] [blame] | 674 | __br_set_forward_delay(br, t); |
Vlad Yasevich | 8a92126 | 2015-11-10 06:15:32 -0500 | [diff] [blame] | 675 | err = 0; |
| 676 | |
| 677 | unlock: |
stephen hemminger | 14f98f2 | 2011-04-04 14:03:33 +0000 | [diff] [blame] | 678 | spin_unlock_bh(&br->lock); |
Vlad Yasevich | 8a92126 | 2015-11-10 06:15:32 -0500 | [diff] [blame] | 679 | return err; |
stephen hemminger | 14f98f2 | 2011-04-04 14:03:33 +0000 | [diff] [blame] | 680 | } |