Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Spanning tree protocol; generic parts |
| 3 | * Linux ethernet bridge |
| 4 | * |
| 5 | * Authors: |
| 6 | * Lennert Buytenhek <buytenh@gnu.org> |
| 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version |
| 11 | * 2 of the License, or (at your option) any later version. |
| 12 | */ |
| 13 | #include <linux/kernel.h> |
Franck Bui-Huu | 8252474 | 2008-05-12 21:21:05 +0200 | [diff] [blame] | 14 | #include <linux/rculist.h> |
Scott Feldman | 38dcf35 | 2014-11-28 14:34:20 +0100 | [diff] [blame] | 15 | #include <net/switchdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | #include "br_private.h" |
| 18 | #include "br_private_stp.h" |
| 19 | |
| 20 | /* 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] | 21 | * before sending, make sure that is at least one STP tick. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | */ |
Joakim Tjernlund | aaca735 | 2012-03-01 08:12:18 +0000 | [diff] [blame] | 23 | #define MESSAGE_AGE_INCR ((HZ / 256) + 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Jan Engelhardt | 36cbd3d | 2009-08-05 10:42:58 -0700 | [diff] [blame] | 25 | static const char *const br_port_state_names[] = { |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 26 | [BR_STATE_DISABLED] = "disabled", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | [BR_STATE_LISTENING] = "listening", |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 28 | [BR_STATE_LEARNING] = "learning", |
| 29 | [BR_STATE_FORWARDING] = "forwarding", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | [BR_STATE_BLOCKING] = "blocking", |
| 31 | }; |
| 32 | |
| 33 | void br_log_state(const struct net_bridge_port *p) |
| 34 | { |
Paulius Zaleckas | d9e179e | 2012-03-06 22:25:14 +0000 | [diff] [blame] | 35 | br_info(p->br, "port %u(%s) entered %s state\n", |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 36 | (unsigned int) p->port_no, p->dev->name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | br_port_state_names[p->state]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Florian Fainelli | 775dd69 | 2014-09-30 16:13:19 -0700 | [diff] [blame] | 40 | void br_set_state(struct net_bridge_port *p, unsigned int state) |
| 41 | { |
Scott Feldman | 3563606 | 2015-05-10 09:47:51 -0700 | [diff] [blame] | 42 | struct switchdev_attr attr = { |
| 43 | .id = SWITCHDEV_ATTR_PORT_STP_STATE, |
Scott Feldman | 42275bd | 2015-05-13 11:16:50 -0700 | [diff] [blame^] | 44 | .u.stp_state = state, |
Scott Feldman | 3563606 | 2015-05-10 09:47:51 -0700 | [diff] [blame] | 45 | }; |
Scott Feldman | 38dcf35 | 2014-11-28 14:34:20 +0100 | [diff] [blame] | 46 | int err; |
| 47 | |
Florian Fainelli | 775dd69 | 2014-09-30 16:13:19 -0700 | [diff] [blame] | 48 | p->state = state; |
Scott Feldman | 3563606 | 2015-05-10 09:47:51 -0700 | [diff] [blame] | 49 | err = switchdev_port_attr_set(p->dev, &attr); |
Scott Feldman | 38dcf35 | 2014-11-28 14:34:20 +0100 | [diff] [blame] | 50 | if (err && err != -EOPNOTSUPP) |
| 51 | br_warn(p->br, "error setting offload STP state on port %u(%s)\n", |
| 52 | (unsigned int) p->port_no, p->dev->name); |
Florian Fainelli | 775dd69 | 2014-09-30 16:13:19 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | /* called under bridge lock */ |
| 56 | struct net_bridge_port *br_get_port(struct net_bridge *br, u16 port_no) |
| 57 | { |
| 58 | struct net_bridge_port *p; |
| 59 | |
| 60 | list_for_each_entry_rcu(p, &br->port_list, list) { |
| 61 | if (p->port_no == port_no) |
| 62 | return p; |
| 63 | } |
| 64 | |
| 65 | return NULL; |
| 66 | } |
| 67 | |
| 68 | /* called under bridge lock */ |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 69 | 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] | 70 | u16 root_port) |
| 71 | { |
| 72 | struct net_bridge *br; |
| 73 | struct net_bridge_port *rp; |
| 74 | int t; |
| 75 | |
| 76 | br = p->br; |
| 77 | if (p->state == BR_STATE_DISABLED || |
| 78 | br_is_designated_port(p)) |
| 79 | return 0; |
| 80 | |
| 81 | if (memcmp(&br->bridge_id, &p->designated_root, 8) <= 0) |
| 82 | return 0; |
| 83 | |
| 84 | if (!root_port) |
| 85 | return 1; |
| 86 | |
| 87 | rp = br_get_port(br, root_port); |
| 88 | |
| 89 | t = memcmp(&p->designated_root, &rp->designated_root, 8); |
| 90 | if (t < 0) |
| 91 | return 1; |
| 92 | else if (t > 0) |
| 93 | return 0; |
| 94 | |
| 95 | if (p->designated_cost + p->path_cost < |
| 96 | rp->designated_cost + rp->path_cost) |
| 97 | return 1; |
| 98 | else if (p->designated_cost + p->path_cost > |
| 99 | rp->designated_cost + rp->path_cost) |
| 100 | return 0; |
| 101 | |
| 102 | t = memcmp(&p->designated_bridge, &rp->designated_bridge, 8); |
| 103 | if (t < 0) |
| 104 | return 1; |
| 105 | else if (t > 0) |
| 106 | return 0; |
| 107 | |
| 108 | if (p->designated_port < rp->designated_port) |
| 109 | return 1; |
| 110 | else if (p->designated_port > rp->designated_port) |
| 111 | return 0; |
| 112 | |
| 113 | if (p->port_id < rp->port_id) |
| 114 | return 1; |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
stephen hemminger | 1007dd1 | 2012-11-13 07:53:08 +0000 | [diff] [blame] | 119 | static void br_root_port_block(const struct net_bridge *br, |
| 120 | struct net_bridge_port *p) |
| 121 | { |
| 122 | |
| 123 | br_notice(br, "port %u(%s) tried to become root port (blocked)", |
| 124 | (unsigned int) p->port_no, p->dev->name); |
| 125 | |
Florian Fainelli | 775dd69 | 2014-09-30 16:13:19 -0700 | [diff] [blame] | 126 | br_set_state(p, BR_STATE_LISTENING); |
stephen hemminger | 1007dd1 | 2012-11-13 07:53:08 +0000 | [diff] [blame] | 127 | br_log_state(p); |
| 128 | br_ifinfo_notify(RTM_NEWLINK, p); |
| 129 | |
| 130 | if (br->forward_delay > 0) |
| 131 | mod_timer(&p->forward_delay_timer, jiffies + br->forward_delay); |
| 132 | } |
| 133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | /* called under bridge lock */ |
| 135 | static void br_root_selection(struct net_bridge *br) |
| 136 | { |
| 137 | struct net_bridge_port *p; |
| 138 | u16 root_port = 0; |
| 139 | |
| 140 | list_for_each_entry(p, &br->port_list, list) { |
stephen hemminger | 1007dd1 | 2012-11-13 07:53:08 +0000 | [diff] [blame] | 141 | if (!br_should_become_root_port(p, root_port)) |
| 142 | continue; |
| 143 | |
| 144 | if (p->flags & BR_ROOT_BLOCK) |
| 145 | br_root_port_block(br, p); |
| 146 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | root_port = p->port_no; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | br->root_port = root_port; |
| 151 | |
| 152 | if (!root_port) { |
| 153 | br->designated_root = br->bridge_id; |
| 154 | br->root_path_cost = 0; |
| 155 | } else { |
| 156 | p = br_get_port(br, root_port); |
| 157 | br->designated_root = p->designated_root; |
| 158 | br->root_path_cost = p->designated_cost + p->path_cost; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /* called under bridge lock */ |
| 163 | void br_become_root_bridge(struct net_bridge *br) |
| 164 | { |
| 165 | br->max_age = br->bridge_max_age; |
| 166 | br->hello_time = br->bridge_hello_time; |
| 167 | br->forward_delay = br->bridge_forward_delay; |
| 168 | br_topology_change_detection(br); |
| 169 | del_timer(&br->tcn_timer); |
| 170 | |
| 171 | if (br->dev->flags & IFF_UP) { |
| 172 | br_config_bpdu_generation(br); |
| 173 | mod_timer(&br->hello_timer, jiffies + br->hello_time); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /* called under bridge lock */ |
| 178 | void br_transmit_config(struct net_bridge_port *p) |
| 179 | { |
| 180 | struct br_config_bpdu bpdu; |
| 181 | struct net_bridge *br; |
| 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | if (timer_pending(&p->hold_timer)) { |
| 184 | p->config_pending = 1; |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | br = p->br; |
| 189 | |
| 190 | bpdu.topology_change = br->topology_change; |
| 191 | bpdu.topology_change_ack = p->topology_change_ack; |
| 192 | bpdu.root = br->designated_root; |
| 193 | bpdu.root_path_cost = br->root_path_cost; |
| 194 | bpdu.bridge_id = br->bridge_id; |
| 195 | bpdu.port_id = p->port_id; |
| 196 | if (br_is_root_bridge(br)) |
| 197 | bpdu.message_age = 0; |
| 198 | else { |
| 199 | struct net_bridge_port *root |
| 200 | = br_get_port(br, br->root_port); |
stephen hemminger | 0c03150 | 2011-07-22 07:47:06 +0000 | [diff] [blame] | 201 | bpdu.message_age = (jiffies - root->designated_age) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | + MESSAGE_AGE_INCR; |
| 203 | } |
| 204 | bpdu.max_age = br->max_age; |
| 205 | bpdu.hello_time = br->hello_time; |
| 206 | bpdu.forward_delay = br->forward_delay; |
| 207 | |
| 208 | if (bpdu.message_age < br->max_age) { |
| 209 | br_send_config_bpdu(p, &bpdu); |
| 210 | p->topology_change_ack = 0; |
| 211 | p->config_pending = 0; |
Stephen Hemminger | 9a834b8 | 2007-05-31 01:21:39 -0700 | [diff] [blame] | 212 | mod_timer(&p->hold_timer, |
| 213 | round_jiffies(jiffies + BR_HOLD_TIME)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
| 217 | /* called under bridge lock */ |
stephen hemminger | 160d73b | 2011-07-22 07:47:10 +0000 | [diff] [blame] | 218 | static void br_record_config_information(struct net_bridge_port *p, |
| 219 | const struct br_config_bpdu *bpdu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | { |
| 221 | p->designated_root = bpdu->root; |
| 222 | p->designated_cost = bpdu->root_path_cost; |
| 223 | p->designated_bridge = bpdu->bridge_id; |
| 224 | p->designated_port = bpdu->port_id; |
Joakim Tjernlund | 709e1b5 | 2012-03-01 08:12:19 +0000 | [diff] [blame] | 225 | p->designated_age = jiffies - bpdu->message_age; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 227 | mod_timer(&p->message_age_timer, jiffies |
Chris Healy | 9a06201 | 2013-09-11 21:37:47 -0700 | [diff] [blame] | 228 | + (bpdu->max_age - bpdu->message_age)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /* called under bridge lock */ |
stephen hemminger | 160d73b | 2011-07-22 07:47:10 +0000 | [diff] [blame] | 232 | static void br_record_config_timeout_values(struct net_bridge *br, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | const struct br_config_bpdu *bpdu) |
| 234 | { |
| 235 | br->max_age = bpdu->max_age; |
| 236 | br->hello_time = bpdu->hello_time; |
| 237 | br->forward_delay = bpdu->forward_delay; |
| 238 | br->topology_change = bpdu->topology_change; |
| 239 | } |
| 240 | |
| 241 | /* called under bridge lock */ |
| 242 | void br_transmit_tcn(struct net_bridge *br) |
| 243 | { |
stephen hemminger | 91bc033 | 2013-04-30 05:29:27 +0000 | [diff] [blame] | 244 | struct net_bridge_port *p; |
| 245 | |
| 246 | p = br_get_port(br, br->root_port); |
| 247 | if (p) |
| 248 | br_send_tcn_bpdu(p); |
| 249 | else |
| 250 | br_notice(br, "root port %u not found for topology notice\n", |
| 251 | br->root_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /* called under bridge lock */ |
| 255 | static int br_should_become_designated_port(const struct net_bridge_port *p) |
| 256 | { |
| 257 | struct net_bridge *br; |
| 258 | int t; |
| 259 | |
| 260 | br = p->br; |
| 261 | if (br_is_designated_port(p)) |
| 262 | return 1; |
| 263 | |
| 264 | if (memcmp(&p->designated_root, &br->designated_root, 8)) |
| 265 | return 1; |
| 266 | |
| 267 | if (br->root_path_cost < p->designated_cost) |
| 268 | return 1; |
| 269 | else if (br->root_path_cost > p->designated_cost) |
| 270 | return 0; |
| 271 | |
| 272 | t = memcmp(&br->bridge_id, &p->designated_bridge, 8); |
| 273 | if (t < 0) |
| 274 | return 1; |
| 275 | else if (t > 0) |
| 276 | return 0; |
| 277 | |
| 278 | if (p->port_id < p->designated_port) |
| 279 | return 1; |
| 280 | |
| 281 | return 0; |
| 282 | } |
| 283 | |
| 284 | /* called under bridge lock */ |
| 285 | static void br_designated_port_selection(struct net_bridge *br) |
| 286 | { |
| 287 | struct net_bridge_port *p; |
| 288 | |
| 289 | list_for_each_entry(p, &br->port_list, list) { |
| 290 | if (p->state != BR_STATE_DISABLED && |
| 291 | br_should_become_designated_port(p)) |
| 292 | br_become_designated_port(p); |
| 293 | |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | /* called under bridge lock */ |
stephen hemminger | 160d73b | 2011-07-22 07:47:10 +0000 | [diff] [blame] | 298 | static int br_supersedes_port_info(const struct net_bridge_port *p, |
| 299 | const struct br_config_bpdu *bpdu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | { |
| 301 | int t; |
| 302 | |
| 303 | t = memcmp(&bpdu->root, &p->designated_root, 8); |
| 304 | if (t < 0) |
| 305 | return 1; |
| 306 | else if (t > 0) |
| 307 | return 0; |
| 308 | |
| 309 | if (bpdu->root_path_cost < p->designated_cost) |
| 310 | return 1; |
| 311 | else if (bpdu->root_path_cost > p->designated_cost) |
| 312 | return 0; |
| 313 | |
| 314 | t = memcmp(&bpdu->bridge_id, &p->designated_bridge, 8); |
| 315 | if (t < 0) |
| 316 | return 1; |
| 317 | else if (t > 0) |
| 318 | return 0; |
| 319 | |
| 320 | if (memcmp(&bpdu->bridge_id, &p->br->bridge_id, 8)) |
| 321 | return 1; |
| 322 | |
| 323 | if (bpdu->port_id <= p->designated_port) |
| 324 | return 1; |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | /* called under bridge lock */ |
stephen hemminger | 160d73b | 2011-07-22 07:47:10 +0000 | [diff] [blame] | 330 | static void br_topology_change_acknowledged(struct net_bridge *br) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | { |
| 332 | br->topology_change_detected = 0; |
| 333 | del_timer(&br->tcn_timer); |
| 334 | } |
| 335 | |
| 336 | /* called under bridge lock */ |
| 337 | void br_topology_change_detection(struct net_bridge *br) |
| 338 | { |
| 339 | int isroot = br_is_root_bridge(br); |
| 340 | |
Stephen Hemminger | 4f0611a | 2009-05-15 06:11:58 +0000 | [diff] [blame] | 341 | if (br->stp_enabled != BR_KERNEL_STP) |
| 342 | return; |
| 343 | |
stephen hemminger | 28a16c9 | 2010-05-10 09:31:09 +0000 | [diff] [blame] | 344 | br_info(br, "topology change detected, %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | isroot ? "propagating" : "sending tcn bpdu"); |
| 346 | |
| 347 | if (isroot) { |
| 348 | br->topology_change = 1; |
| 349 | mod_timer(&br->topology_change_timer, jiffies |
| 350 | + br->bridge_forward_delay + br->bridge_max_age); |
| 351 | } else if (!br->topology_change_detected) { |
| 352 | br_transmit_tcn(br); |
| 353 | mod_timer(&br->tcn_timer, jiffies + br->bridge_hello_time); |
| 354 | } |
| 355 | |
| 356 | br->topology_change_detected = 1; |
| 357 | } |
| 358 | |
| 359 | /* called under bridge lock */ |
| 360 | void br_config_bpdu_generation(struct net_bridge *br) |
| 361 | { |
| 362 | struct net_bridge_port *p; |
| 363 | |
| 364 | list_for_each_entry(p, &br->port_list, list) { |
| 365 | if (p->state != BR_STATE_DISABLED && |
| 366 | br_is_designated_port(p)) |
| 367 | br_transmit_config(p); |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | /* called under bridge lock */ |
stephen hemminger | 160d73b | 2011-07-22 07:47:10 +0000 | [diff] [blame] | 372 | static void br_reply(struct net_bridge_port *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | { |
| 374 | br_transmit_config(p); |
| 375 | } |
| 376 | |
| 377 | /* called under bridge lock */ |
| 378 | void br_configuration_update(struct net_bridge *br) |
| 379 | { |
| 380 | br_root_selection(br); |
| 381 | br_designated_port_selection(br); |
| 382 | } |
| 383 | |
| 384 | /* called under bridge lock */ |
| 385 | void br_become_designated_port(struct net_bridge_port *p) |
| 386 | { |
| 387 | struct net_bridge *br; |
| 388 | |
| 389 | br = p->br; |
| 390 | p->designated_root = br->designated_root; |
| 391 | p->designated_cost = br->root_path_cost; |
| 392 | p->designated_bridge = br->bridge_id; |
| 393 | p->designated_port = p->port_id; |
| 394 | } |
| 395 | |
| 396 | |
| 397 | /* called under bridge lock */ |
| 398 | static void br_make_blocking(struct net_bridge_port *p) |
| 399 | { |
| 400 | if (p->state != BR_STATE_DISABLED && |
| 401 | p->state != BR_STATE_BLOCKING) { |
| 402 | if (p->state == BR_STATE_FORWARDING || |
| 403 | p->state == BR_STATE_LEARNING) |
| 404 | br_topology_change_detection(p->br); |
| 405 | |
Florian Fainelli | 775dd69 | 2014-09-30 16:13:19 -0700 | [diff] [blame] | 406 | br_set_state(p, BR_STATE_BLOCKING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | br_log_state(p); |
stephen hemminger | 4ecb961 | 2011-07-22 07:47:09 +0000 | [diff] [blame] | 408 | br_ifinfo_notify(RTM_NEWLINK, p); |
| 409 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | del_timer(&p->forward_delay_timer); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | /* called under bridge lock */ |
| 415 | static void br_make_forwarding(struct net_bridge_port *p) |
| 416 | { |
Stephen Hemminger | ef647f1 | 2008-08-05 18:42:51 -0700 | [diff] [blame] | 417 | struct net_bridge *br = p->br; |
Stephen Hemminger | 9cde070 | 2007-03-21 14:22:44 -0700 | [diff] [blame] | 418 | |
Stephen Hemminger | ef647f1 | 2008-08-05 18:42:51 -0700 | [diff] [blame] | 419 | if (p->state != BR_STATE_BLOCKING) |
| 420 | return; |
| 421 | |
stephen hemminger | a461c02 | 2011-03-10 05:57:04 +0000 | [diff] [blame] | 422 | if (br->stp_enabled == BR_NO_STP || br->forward_delay == 0) { |
Florian Fainelli | 775dd69 | 2014-09-30 16:13:19 -0700 | [diff] [blame] | 423 | br_set_state(p, BR_STATE_FORWARDING); |
Stephen Hemminger | ef647f1 | 2008-08-05 18:42:51 -0700 | [diff] [blame] | 424 | br_topology_change_detection(br); |
| 425 | del_timer(&p->forward_delay_timer); |
stephen hemminger | 160d73b | 2011-07-22 07:47:10 +0000 | [diff] [blame] | 426 | } else if (br->stp_enabled == BR_KERNEL_STP) |
Florian Fainelli | 775dd69 | 2014-09-30 16:13:19 -0700 | [diff] [blame] | 427 | br_set_state(p, BR_STATE_LISTENING); |
Stephen Hemminger | ef647f1 | 2008-08-05 18:42:51 -0700 | [diff] [blame] | 428 | else |
Florian Fainelli | 775dd69 | 2014-09-30 16:13:19 -0700 | [diff] [blame] | 429 | br_set_state(p, BR_STATE_LEARNING); |
Stephen Hemminger | ef647f1 | 2008-08-05 18:42:51 -0700 | [diff] [blame] | 430 | |
Herbert Xu | 3fe2d7c | 2010-02-28 00:49:38 -0800 | [diff] [blame] | 431 | br_multicast_enable_port(p); |
Stephen Hemminger | ef647f1 | 2008-08-05 18:42:51 -0700 | [diff] [blame] | 432 | br_log_state(p); |
stephen hemminger | 4ecb961 | 2011-07-22 07:47:09 +0000 | [diff] [blame] | 433 | br_ifinfo_notify(RTM_NEWLINK, p); |
Stephen Hemminger | ef647f1 | 2008-08-05 18:42:51 -0700 | [diff] [blame] | 434 | |
| 435 | if (br->forward_delay != 0) |
| 436 | mod_timer(&p->forward_delay_timer, jiffies + br->forward_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /* called under bridge lock */ |
| 440 | void br_port_state_selection(struct net_bridge *br) |
| 441 | { |
| 442 | struct net_bridge_port *p; |
stephen hemminger | 1faa435 | 2011-03-07 08:34:06 +0000 | [diff] [blame] | 443 | unsigned int liveports = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | |
| 445 | list_for_each_entry(p, &br->port_list, list) { |
stephen hemminger | 1faa435 | 2011-03-07 08:34:06 +0000 | [diff] [blame] | 446 | if (p->state == BR_STATE_DISABLED) |
| 447 | continue; |
| 448 | |
Vitalii Demianets | b03b6dd | 2011-11-25 00:16:37 +0000 | [diff] [blame] | 449 | /* Don't change port states if userspace is handling STP */ |
| 450 | if (br->stp_enabled != BR_USER_STP) { |
| 451 | if (p->port_no == br->root_port) { |
| 452 | p->config_pending = 0; |
| 453 | p->topology_change_ack = 0; |
| 454 | br_make_forwarding(p); |
| 455 | } else if (br_is_designated_port(p)) { |
| 456 | del_timer(&p->message_age_timer); |
| 457 | br_make_forwarding(p); |
| 458 | } else { |
| 459 | p->config_pending = 0; |
| 460 | p->topology_change_ack = 0; |
| 461 | br_make_blocking(p); |
| 462 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | } |
| 464 | |
stephen hemminger | 1faa435 | 2011-03-07 08:34:06 +0000 | [diff] [blame] | 465 | if (p->state == BR_STATE_FORWARDING) |
| 466 | ++liveports; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | } |
stephen hemminger | 1faa435 | 2011-03-07 08:34:06 +0000 | [diff] [blame] | 468 | |
| 469 | if (liveports == 0) |
| 470 | netif_carrier_off(br->dev); |
| 471 | else |
| 472 | netif_carrier_on(br->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | /* called under bridge lock */ |
stephen hemminger | 160d73b | 2011-07-22 07:47:10 +0000 | [diff] [blame] | 476 | static void br_topology_change_acknowledge(struct net_bridge_port *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | { |
| 478 | p->topology_change_ack = 1; |
| 479 | br_transmit_config(p); |
| 480 | } |
| 481 | |
| 482 | /* called under bridge lock */ |
stephen hemminger | 160d73b | 2011-07-22 07:47:10 +0000 | [diff] [blame] | 483 | void br_received_config_bpdu(struct net_bridge_port *p, |
| 484 | const struct br_config_bpdu *bpdu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | { |
| 486 | struct net_bridge *br; |
| 487 | int was_root; |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 488 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | br = p->br; |
| 490 | was_root = br_is_root_bridge(br); |
| 491 | |
| 492 | if (br_supersedes_port_info(p, bpdu)) { |
| 493 | br_record_config_information(p, bpdu); |
| 494 | br_configuration_update(br); |
| 495 | br_port_state_selection(br); |
| 496 | |
| 497 | if (!br_is_root_bridge(br) && was_root) { |
| 498 | del_timer(&br->hello_timer); |
| 499 | if (br->topology_change_detected) { |
| 500 | del_timer(&br->topology_change_timer); |
| 501 | br_transmit_tcn(br); |
| 502 | |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 503 | mod_timer(&br->tcn_timer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | jiffies + br->bridge_hello_time); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | if (p->port_no == br->root_port) { |
| 509 | br_record_config_timeout_values(br, bpdu); |
| 510 | br_config_bpdu_generation(br); |
| 511 | if (bpdu->topology_change_ack) |
| 512 | br_topology_change_acknowledged(br); |
| 513 | } |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 514 | } else if (br_is_designated_port(p)) { |
| 515 | br_reply(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
| 519 | /* called under bridge lock */ |
| 520 | void br_received_tcn_bpdu(struct net_bridge_port *p) |
| 521 | { |
| 522 | if (br_is_designated_port(p)) { |
stephen hemminger | 28a16c9 | 2010-05-10 09:31:09 +0000 | [diff] [blame] | 523 | br_info(p->br, "port %u(%s) received tcn bpdu\n", |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 524 | (unsigned int) p->port_no, p->dev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | |
| 526 | br_topology_change_detection(p->br); |
| 527 | br_topology_change_acknowledge(p); |
| 528 | } |
| 529 | } |
stephen hemminger | 14f98f2 | 2011-04-04 14:03:33 +0000 | [diff] [blame] | 530 | |
| 531 | /* Change bridge STP parameter */ |
| 532 | int br_set_hello_time(struct net_bridge *br, unsigned long val) |
| 533 | { |
| 534 | unsigned long t = clock_t_to_jiffies(val); |
| 535 | |
| 536 | if (t < BR_MIN_HELLO_TIME || t > BR_MAX_HELLO_TIME) |
| 537 | return -ERANGE; |
| 538 | |
| 539 | spin_lock_bh(&br->lock); |
| 540 | br->bridge_hello_time = t; |
| 541 | if (br_is_root_bridge(br)) |
| 542 | br->hello_time = br->bridge_hello_time; |
| 543 | spin_unlock_bh(&br->lock); |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | int br_set_max_age(struct net_bridge *br, unsigned long val) |
| 548 | { |
| 549 | unsigned long t = clock_t_to_jiffies(val); |
| 550 | |
| 551 | if (t < BR_MIN_MAX_AGE || t > BR_MAX_MAX_AGE) |
| 552 | return -ERANGE; |
| 553 | |
| 554 | spin_lock_bh(&br->lock); |
| 555 | br->bridge_max_age = t; |
| 556 | if (br_is_root_bridge(br)) |
| 557 | br->max_age = br->bridge_max_age; |
| 558 | spin_unlock_bh(&br->lock); |
| 559 | return 0; |
| 560 | |
| 561 | } |
| 562 | |
Herbert Xu | be4f154 | 2013-09-12 17:12:05 +1000 | [diff] [blame] | 563 | void __br_set_forward_delay(struct net_bridge *br, unsigned long t) |
stephen hemminger | 14f98f2 | 2011-04-04 14:03:33 +0000 | [diff] [blame] | 564 | { |
stephen hemminger | 14f98f2 | 2011-04-04 14:03:33 +0000 | [diff] [blame] | 565 | br->bridge_forward_delay = t; |
| 566 | if (br_is_root_bridge(br)) |
| 567 | br->forward_delay = br->bridge_forward_delay; |
Herbert Xu | be4f154 | 2013-09-12 17:12:05 +1000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | int br_set_forward_delay(struct net_bridge *br, unsigned long val) |
| 571 | { |
| 572 | unsigned long t = clock_t_to_jiffies(val); |
| 573 | int err = -ERANGE; |
| 574 | |
| 575 | spin_lock_bh(&br->lock); |
| 576 | if (br->stp_enabled != BR_NO_STP && |
| 577 | (t < BR_MIN_FORWARD_DELAY || t > BR_MAX_FORWARD_DELAY)) |
| 578 | goto unlock; |
| 579 | |
| 580 | __br_set_forward_delay(br, t); |
| 581 | err = 0; |
| 582 | |
| 583 | unlock: |
stephen hemminger | 14f98f2 | 2011-04-04 14:03:33 +0000 | [diff] [blame] | 584 | spin_unlock_bh(&br->lock); |
Herbert Xu | be4f154 | 2013-09-12 17:12:05 +1000 | [diff] [blame] | 585 | return err; |
stephen hemminger | 14f98f2 | 2011-04-04 14:03:33 +0000 | [diff] [blame] | 586 | } |