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