sys/net/bridge/bridgestp.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 | /* * Copyright (c) 2000 Jason L. Wright (jason@thought.net) * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Jason L. Wright * 4. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * $OpenBSD: bridgestp.c,v 1.5 2001/03/22 03:48:29 jason Exp $ * $NetBSD: bridgestp.c,v 1.5 2003/11/28 08:56:48 keihan Exp $ * $FreeBSD: src/sys/net/bridgestp.c,v 1.7 2005/10/11 02:58:32 thompsa Exp $ */ /* * Implementation of the spanning tree protocol as defined in * ISO/IEC Final DIS 15802-3 (IEEE P802.1D/D17), May 25, 1998. * (In English: IEEE 802.1D, Draft 17, 1998) */ #include <sys/param.h> #include <sys/systm.h> #include <sys/malloc.h> /* for M_NOWAIT */ #include <sys/mbuf.h> #include <sys/socket.h> #include <sys/sockio.h> #include <sys/kernel.h> #include <sys/callout.h> #include <sys/proc.h> #include <sys/lock.h> #include <sys/thread.h> #include <sys/thread2.h> #include <sys/msgport2.h> #include <net/if.h> #include <net/if_dl.h> #include <net/if_types.h> #include <net/if_llc.h> #include <net/if_media.h> #include <netinet/in.h> #include <netinet/in_systm.h> #include <netinet/in_var.h> #include <netinet/if_ether.h> #include <net/bridge/if_bridgevar.h> /* BPDU message types */ #define BSTP_MSGTYPE_CFG 0x00 /* Configuration */ #define BSTP_MSGTYPE_TCN 0x80 /* Topology chg notification */ /* BPDU flags */ #define BSTP_FLAG_TC 0x01 /* Topology change */ #define BSTP_FLAG_TCA 0x80 /* Topology change ack */ #define BSTP_MESSAGE_AGE_INCR (1 * 256) /* in 256ths of a second */ #define BSTP_TICK_VAL (1 * 256) /* in 256ths of a second */ /* * Because BPDU's do not make nicely aligned structures, two different * declarations are used: bstp_?bpdu (wire representation, packed) and * bstp_*_unit (internal, nicely aligned version). */ /* configuration bridge protocol data unit */ struct bstp_cbpdu { uint8_t cbu_dsap; /* LLC: destination sap */ uint8_t cbu_ssap; /* LLC: source sap */ uint8_t cbu_ctl; /* LLC: control */ uint16_t cbu_protoid; /* protocol id */ uint8_t cbu_protover; /* protocol version */ uint8_t cbu_bpdutype; /* message type */ uint8_t cbu_flags; /* flags (below) */ /* root id */ uint16_t cbu_rootpri; /* root priority */ uint8_t cbu_rootaddr[6]; /* root address */ uint32_t cbu_rootpathcost; /* root path cost */ /* bridge id */ uint16_t cbu_bridgepri; /* bridge priority */ uint8_t cbu_bridgeaddr[6]; /* bridge address */ uint16_t cbu_portid; /* port id */ uint16_t cbu_messageage; /* current message age */ uint16_t cbu_maxage; /* maximum age */ uint16_t cbu_hellotime; /* hello time */ uint16_t cbu_forwarddelay; /* forwarding delay */ } __attribute__((__packed__)); /* topology change notification bridge protocol data unit */ struct bstp_tbpdu { uint8_t tbu_dsap; /* LLC: destination sap */ uint8_t tbu_ssap; /* LLC: source sap */ uint8_t tbu_ctl; /* LLC: control */ uint16_t tbu_protoid; /* protocol id */ uint8_t tbu_protover; /* protocol version */ uint8_t tbu_bpdutype; /* message type */ } __attribute__((__packed__)); const uint8_t bstp_etheraddr[] = { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 }; static void bstp_initialize_port(struct bridge_softc *, struct bridge_iflist *); static void bstp_ifupdstatus(struct bridge_softc *, struct bridge_iflist *); static void bstp_enable_port(struct bridge_softc *, struct bridge_iflist *); static void bstp_disable_port(struct bridge_softc *, struct bridge_iflist *); #ifdef notused static void bstp_enable_change_detection(struct bridge_iflist *); static void bstp_disable_change_detection(struct bridge_iflist *); #endif /* notused */ static int bstp_root_bridge(struct bridge_softc *sc); static void bstp_transmit_config(struct bridge_softc *, struct bridge_iflist *); static void bstp_transmit_tcn(struct bridge_softc *); static void bstp_received_config_bpdu(struct bridge_softc *, struct bridge_iflist *, struct bstp_config_unit *); static void bstp_received_tcn_bpdu(struct bridge_softc *, struct bridge_iflist *, struct bstp_tcn_unit *); static void bstp_record_config_information(struct bridge_softc *, struct bridge_iflist *, struct bstp_config_unit *); static void bstp_record_config_timeout_values(struct bridge_softc *, struct bstp_config_unit *); static void bstp_config_bpdu_generation(struct bridge_softc *); static void bstp_send_config_bpdu(struct bridge_softc *, struct bridge_iflist *, struct bstp_config_unit *); static void bstp_configuration_update(struct bridge_softc *); static void bstp_port_state_selection(struct bridge_softc *); static void bstp_clear_peer_info(struct bridge_softc *, struct bridge_iflist *); static void bstp_make_forwarding(struct bridge_softc *, struct bridge_iflist *); static void bstp_make_blocking(struct bridge_softc *, struct bridge_iflist *); static void bstp_make_l1blocking(struct bridge_softc *sc, struct bridge_iflist *bif); static void bstp_adjust_bonded_states(struct bridge_softc *sc, struct bridge_iflist *obif); static void bstp_set_port_state(struct bridge_iflist *, uint8_t); #ifdef notused static void bstp_set_bridge_priority(struct bridge_softc *, uint64_t); static void bstp_set_port_priority(struct bridge_softc *, struct bridge_iflist *, uint16_t); static void bstp_set_path_cost(struct bridge_softc *, struct bridge_iflist *, uint32_t); #endif /* notused */ static void bstp_topology_change_detection(struct bridge_softc *); static void bstp_topology_change_acknowledged(struct bridge_softc *); static void bstp_acknowledge_topology_change(struct bridge_softc *, struct bridge_iflist *); static void bstp_tick(void *); static void bstp_timer_start(struct bridge_timer *, uint16_t); static void bstp_timer_stop(struct bridge_timer *); static int bstp_timer_expired(struct bridge_timer *, uint16_t); static void bstp_hold_timer_expiry(struct bridge_softc *, struct bridge_iflist *); static void bstp_message_age_timer_expiry(struct bridge_softc *, struct bridge_iflist *); static void bstp_forward_delay_timer_expiry(struct bridge_softc *, struct bridge_iflist *); static void bstp_topology_change_timer_expiry(struct bridge_softc *); static void bstp_tcn_timer_expiry(struct bridge_softc *); static void bstp_hello_timer_expiry(struct bridge_softc *); static int bstp_addr_cmp(const uint8_t *, const uint8_t *); /* * When transmitting a config we tack on our path cost to * our aggregated path-to-root cost. * * Note that sc_designated_cost is the lowest root path cost from all * incoming links. When one is talking about two bridges with multiple * links between them the root path cost the secondary bridge transmits * will be the lowest root path cost it received across those three * links and NOT necessarily the root path cost it received specifically * on that particular link. The receiving end must still add its local * link cost back in for differentiation purposes. */ static void bstp_transmit_config(struct bridge_softc *sc, struct bridge_iflist *bif) { if (bif->bif_hold_timer.active) { bif->bif_config_pending = 1; return; } bif->bif_config_bpdu.cu_message_type = BSTP_MSGTYPE_CFG; bif->bif_config_bpdu.cu_rootid = sc->sc_designated_root; bif->bif_config_bpdu.cu_root_path_cost = sc->sc_designated_cost + bif->bif_path_cost; bif->bif_config_bpdu.cu_bridge_id = sc->sc_bridge_id; bif->bif_config_bpdu.cu_port_id = bif->bif_port_id; if (bstp_root_bridge(sc)) { bif->bif_config_bpdu.cu_message_age = 0; } else if (sc->sc_root_port) { bif->bif_config_bpdu.cu_message_age = sc->sc_root_port->bif_message_age_timer.value + BSTP_MESSAGE_AGE_INCR; } else { bif->bif_config_bpdu.cu_message_age = BSTP_MESSAGE_AGE_INCR; } bif->bif_config_bpdu.cu_max_age = sc->sc_max_age; bif->bif_config_bpdu.cu_hello_time = sc->sc_hello_time; bif->bif_config_bpdu.cu_forward_delay = sc->sc_forward_delay; bif->bif_config_bpdu.cu_topology_change_acknowledgment = bif->bif_topology_change_acknowledge; bif->bif_config_bpdu.cu_topology_change = sc->sc_topology_change; if (bif->bif_config_bpdu.cu_message_age < sc->sc_max_age || (sc->sc_ifp->if_flags & IFF_LINK1)) { bif->bif_topology_change_acknowledge = 0; bif->bif_config_pending = 0; bstp_send_config_bpdu(sc, bif, &bif->bif_config_bpdu); bstp_timer_start(&bif->bif_hold_timer, 0); } } static void bstp_send_config_bpdu(struct bridge_softc *sc, struct bridge_iflist *bif, struct bstp_config_unit *cu) { struct ifnet *ifp; struct mbuf *m; struct ether_header *eh; struct bstp_cbpdu bpdu; ifp = bif->bif_ifp; if ((ifp->if_flags & IFF_RUNNING) == 0) return; MGETHDR(m, M_NOWAIT, MT_DATA); if (m == NULL) return; eh = mtod(m, struct ether_header *); m->m_pkthdr.rcvif = ifp; m->m_pkthdr.len = sizeof(*eh) + sizeof(bpdu); m->m_len = m->m_pkthdr.len; bpdu.cbu_ssap = bpdu.cbu_dsap = LLC_8021D_LSAP; bpdu.cbu_ctl = LLC_UI; bpdu.cbu_protoid = htons(0); bpdu.cbu_protover = 0; bpdu.cbu_bpdutype = cu->cu_message_type; bpdu.cbu_flags = (cu->cu_topology_change ? BSTP_FLAG_TC : 0) | (cu->cu_topology_change_acknowledgment ? BSTP_FLAG_TCA : 0); bpdu.cbu_rootpri = htons(cu->cu_rootid >> 48); bpdu.cbu_rootaddr[0] = cu->cu_rootid >> 40; bpdu.cbu_rootaddr[1] = cu->cu_rootid >> 32; bpdu.cbu_rootaddr[2] = cu->cu_rootid >> 24; bpdu.cbu_rootaddr[3] = cu->cu_rootid >> 16; bpdu.cbu_rootaddr[4] = cu->cu_rootid >> 8; bpdu.cbu_rootaddr[5] = cu->cu_rootid >> 0; bpdu.cbu_rootpathcost = htonl(cu->cu_root_path_cost); bpdu.cbu_bridgepri = htons(cu->cu_bridge_id >> 48); bpdu.cbu_bridgeaddr[0] = cu->cu_bridge_id >> 40; bpdu.cbu_bridgeaddr[1] = cu->cu_bridge_id >> 32; bpdu.cbu_bridgeaddr[2] = cu->cu_bridge_id >> 24; bpdu.cbu_bridgeaddr[3] = cu->cu_bridge_id >> 16; bpdu.cbu_bridgeaddr[4] = cu->cu_bridge_id >> 8; bpdu.cbu_bridgeaddr[5] = cu->cu_bridge_id >> 0; bpdu.cbu_portid = htons(cu->cu_port_id); bpdu.cbu_messageage = htons(cu->cu_message_age); bpdu.cbu_maxage = htons(cu->cu_max_age); bpdu.cbu_hellotime = htons(cu->cu_hello_time); bpdu.cbu_forwarddelay = htons(cu->cu_forward_delay); /* * Packets sent from the bridge always use the bridge MAC * as the source. */ memcpy(eh->ether_shost, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN); memcpy(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN); eh->ether_type = htons(sizeof(bpdu)); memcpy(mtod(m, caddr_t) + sizeof(*eh), &bpdu, sizeof(bpdu)); bridge_enqueue(ifp, m); } static int bstp_root_bridge(struct bridge_softc *sc) { return (sc->sc_designated_root == sc->sc_bridge_id); } /* * Returns TRUE if the recorded information from our peer has a shorter * graph distance than our current best. */ int bstp_supersedes_port_info(struct bridge_softc *sc, struct bridge_iflist *bif) { if (bif->bif_peer_root < sc->sc_designated_root) return (1); if (bif->bif_peer_root > sc->sc_designated_root) return (0); /* * bif_peer_cost and sc_designated_cost do not include the local * bif_path_cost, otherwise we would not be able to make this * comparison. */ if (bif->bif_peer_cost < sc->sc_designated_cost) return (1); if (bif->bif_peer_cost > sc->sc_designated_cost) return (0); /* * When the peer costs match we have to check the local path * cost against the selected root port. sc_designated_cost * in an aggregation and cannot be used. */ if (sc->sc_root_port && bif->bif_path_cost < sc->sc_root_port->bif_path_cost) return (1); if (sc->sc_root_port && bif->bif_path_cost > sc->sc_root_port->bif_path_cost) return (0); /* * If the path costs are identical the bridge with the lowest * bridge_id wins. */ if (bif->bif_peer_bridge < sc->sc_designated_bridge) return (1); if (bif->bif_peer_bridge > sc->sc_designated_bridge) return (0); /* * bridge_id or bridge+port collision w/peer returns TRUE. * * This case can also occur when two bridges are connected * via multiple links whos path costs have not been set. */ if (bif->bif_peer_bridge != sc->sc_bridge_id) return (1); if (bif->bif_peer_port <= sc->sc_designated_port) return (1); return (0); } /* * The shorter graph distance represented by cu (most of which is also * already stored in our bif_peer_* fields) becomes the designated info. * * NOTE: sc_designated_cost does not include bif_path_cost, it is added * in later on a port-by-port basis as needed. */ static void bstp_record_config_information(struct bridge_softc *sc, struct bridge_iflist *bif, struct bstp_config_unit *cu) { sc->sc_designated_root = bif->bif_peer_root; sc->sc_designated_cost = bif->bif_peer_cost; sc->sc_designated_bridge = bif->bif_peer_bridge; sc->sc_designated_port = bif->bif_peer_port; bstp_timer_start(&bif->bif_message_age_timer, cu->cu_message_age); } static void bstp_record_config_timeout_values(struct bridge_softc *sc, struct bstp_config_unit *config) { sc->sc_max_age = config->cu_max_age; sc->sc_hello_time = config->cu_hello_time; sc->sc_forward_delay = config->cu_forward_delay; sc->sc_topology_change = config->cu_topology_change; } static void bstp_config_bpdu_generation(struct bridge_softc *sc) { struct bridge_iflist *bif, *nbif; TAILQ_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid], bif_next, nbif) { if ((bif->bif_flags & IFBIF_STP) == 0) continue; if (bif->bif_state != BSTP_IFSTATE_DISABLED && ((sc->sc_ifp->if_flags & IFF_LINK1) || (bif->bif_flags & IFBIF_DESIGNATED))) { bstp_transmit_config(sc, bif); } if (nbif != NULL && !nbif->bif_onlist) { KKASSERT(bif->bif_onlist); nbif = TAILQ_NEXT(bif, bif_next); } } } static void bstp_transmit_tcn(struct bridge_softc *sc) { struct bstp_tbpdu bpdu; struct ifnet *ifp; struct ether_header *eh; struct mbuf *m; if (sc->sc_root_port == NULL) /* all iterfaces disabled */ return; ifp = sc->sc_root_port->bif_ifp; if ((ifp->if_flags & IFF_RUNNING) == 0) return; MGETHDR(m, M_NOWAIT, MT_DATA); if (m == NULL) return; m->m_pkthdr.rcvif = ifp; m->m_pkthdr.len = sizeof(*eh) + sizeof(bpdu); m->m_len = m->m_pkthdr.len; eh = mtod(m, struct ether_header *); /* * Packets sent from the bridge always use the bridge MAC * as the source. */ memcpy(eh->ether_shost, IF_LLADDR(sc->sc_ifp), ETHER_ADDR_LEN); memcpy(eh->ether_dhost, bstp_etheraddr, ETHER_ADDR_LEN); eh->ether_type = htons(sizeof(bpdu)); bpdu.tbu_ssap = bpdu.tbu_dsap = LLC_8021D_LSAP; bpdu.tbu_ctl = LLC_UI; bpdu.tbu_protoid = 0; bpdu.tbu_protover = 0; bpdu.tbu_bpdutype = BSTP_MSGTYPE_TCN; memcpy(mtod(m, caddr_t) + sizeof(*eh), &bpdu, sizeof(bpdu)); bridge_enqueue(ifp, m); } /* * Recalculate sc->sc_designated* and sc->sc_root_port (if our bridge * is calculated to be the root bridge). We do this by initializing * the designated variables to point at us and then scan our peers. * Any uninitialized peers will have a max-value root. * * Clear IFBIF_DESIGNATED on any ports which no longer match the criteria * required to be a designated port. Only aged out ports and the root * port can be designated. * * If we win we do a second scan to determine which port on our bridge * is the best. */ static void bstp_configuration_update(struct bridge_softc *sc) { uint64_t designated_root = sc->sc_bridge_id; uint64_t designated_bridge = sc->sc_bridge_id; uint32_t designated_cost = 0xFFFFFFFFU; uint32_t designated_root_cost = 0xFFFFFFFFU; uint16_t designated_port = 65535; struct bridge_iflist *root_port = NULL; struct bridge_iflist *bif; /* * Resolve information from our peers. Aged peers will have * a maxed bif_peer_root and not come under consideration. */ TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { if ((bif->bif_flags & IFBIF_STP) == 0) continue; if (bif->bif_state == BSTP_IFSTATE_DISABLED || bif->bif_state == BSTP_IFSTATE_L1BLOCKING) { continue; } if (bif->bif_peer_root > designated_root) continue; if (bif->bif_peer_root < designated_root) goto set_port; /* * NOTE: The designated_cost temporary variable already added * in the path code of the related root port. */ if (bif->bif_peer_cost + bif->bif_path_cost > designated_cost) continue; if (bif->bif_peer_cost + bif->bif_path_cost < designated_cost) goto set_port; if (bif->bif_peer_bridge > designated_bridge) continue; if (bif->bif_peer_bridge < designated_bridge) goto set_port; if (bif->bif_peer_port > designated_port) continue; if (bif->bif_peer_port < designated_port) goto set_port; /* * Same root, path cost, bridge, and port. Set the root * only if we do not already have it. */ if (root_port) continue; /* * New root port (from peers) * * NOTE: Temporarily add bif_path_cost into the designated * cost to reduce complexity in the loop, it will be * subtracted out when we are done. */ set_port: designated_root = bif->bif_peer_root; designated_cost = bif->bif_peer_cost + bif->bif_path_cost; designated_root_cost = bif->bif_peer_cost; designated_bridge = bif->bif_peer_bridge; designated_port = bif->bif_peer_port; root_port = bif; } /* * root_port will be NULL at the start here if all of our * peers are aged or are not as good a root as our bridge would * be. It can also be NULL due to all related links being * disabled. * * If the root winds up being our bridge scan again against local * information. Unconditionally update IFBIF_DESIGNATED. */ TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { bif->bif_flags &= ~(IFBIF_DESIGNATED | IFBIF_ROOT); if ((bif->bif_flags & IFBIF_STP) == 0) continue; if (bif->bif_state == BSTP_IFSTATE_DISABLED || bif->bif_state == BSTP_IFSTATE_L1BLOCKING) { continue; } /* * Set DESIGNATED for an aged or unknown peer. */ if (bif->bif_peer_bridge == 0xFFFFFFFFFFFFFFFFLLU) bif->bif_flags |= IFBIF_DESIGNATED; if (designated_root != sc->sc_bridge_id) continue; /* * This is only reached if our bridge is the root bridge, * select the root port (IFBIF_DESIGNATED is set at the * end). * * The peer cost will incorporate the peer bridge's best * cost to the root bridge (us), which is NOT necessarily * our path cost, plus the peer bridge's own path cost. * Because of this we must still add in our path cost for * further differentiation. Because the peer's 'best' cost * is applied to all of its outgoing messages we should * still come to the same conclusion as the peer. */ if (bif->bif_peer_cost + bif->bif_path_cost > designated_cost) continue; if (bif->bif_peer_cost + bif->bif_path_cost < designated_cost) goto set_port2; if (bif->bif_port_id > designated_port) continue; if (bif->bif_port_id < designated_port) goto set_port2; /* degenerate case (possible peer collision w/our key */ /* * New port. Since we are the root, the root cost is always * 0. The peer's cu_root_path_cost does not necessarily * incorporate our peer path cost, but instead incorporates * the lowest path cost to root for the target bridge * when multiple links are present between the two bridges. * Thus for selection purposes we must still add in our * local path cost. */ set_port2: designated_cost = bif->bif_peer_cost + bif->bif_path_cost; designated_root_cost = 0; designated_bridge = sc->sc_bridge_id; designated_port = bif->bif_port_id; root_port = bif; } /* * Update aggregate information. The selected root port always * becomes a designated port (along with aged ports). This can * either be the port whos peer is closest to the root or it * can be one of our ports if our bridge is the root. * * The root cost we record in sc_designated_root does not include * bif_path_cost of the root port, since we may transmit this * out of any port we will add the cost back in later on on * a per-port basis. * * root_port can be NULL here (if all links are disabled) */ if (root_port) { sc->sc_designated_root = designated_root; sc->sc_designated_cost = designated_root_cost; sc->sc_designated_bridge = designated_bridge; sc->sc_designated_port = designated_port; root_port->bif_flags |= IFBIF_DESIGNATED | IFBIF_ROOT; } else { sc->sc_designated_root = designated_root; sc->sc_designated_cost = designated_cost; sc->sc_designated_bridge = designated_bridge; sc->sc_designated_port = designated_port; } sc->sc_root_port = root_port; } /* * Calculate the desired state for each interface link on our bridge. * * The best port will match against sc->sc_root_port (whether we are root * or whether that port is the closest to the root). We push this port * towards a FORWARDING state. * * Next come designated ports, either aged ports or ports with no peer info * (yet), or the peer who is closest to the root. We push this port towards * a FORWARDING state as well. * * Any remaining ports are pushed towards a BLOCKING state. Both sides of * the port (us and our peer) should wind up placing the two ends in this * state or bad things happen. */ static void bstp_port_state_selection(struct bridge_softc *sc) { struct bridge_iflist *bif, *nbif; TAILQ_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid], bif_next, nbif) { if ((bif->bif_flags & IFBIF_STP) == 0) continue; if (sc->sc_root_port && bif->bif_info == sc->sc_root_port->bif_info) { bif->bif_config_pending = 0; bif->bif_topology_change_acknowledge = 0; bstp_make_forwarding(sc, bif); } else if (bif->bif_flags & IFBIF_DESIGNATED) { bstp_timer_stop(&bif->bif_message_age_timer); bstp_make_forwarding(sc, bif); } else { bif->bif_config_pending = 0; bif->bif_topology_change_acknowledge = 0; bstp_make_blocking(sc, bif); } if (nbif != NULL && !nbif->bif_onlist) { KKASSERT(bif->bif_onlist); nbif = TAILQ_NEXT(bif, bif_next); } } } /* * Clear peer info, effectively makes the port looked aged out. * It becomes a designated go-to port. */ static void bstp_clear_peer_info(struct bridge_softc *sc, struct bridge_iflist *bif) { bif->bif_peer_root = 0xFFFFFFFFFFFFFFFFLLU; bif->bif_peer_cost = 0xFFFFFFFFU; bif->bif_peer_bridge = 0xFFFFFFFFFFFFFFFFLLU; bif->bif_peer_port = 0xFFFFU; if (bif->bif_state != BSTP_IFSTATE_DISABLED && bif->bif_state != BSTP_IFSTATE_L1BLOCKING) { bif->bif_flags |= IFBIF_DESIGNATED; } } static void bstp_make_forwarding(struct bridge_softc *sc, struct bridge_iflist *bif) { if (bif->bif_state == BSTP_IFSTATE_BLOCKING || bif->bif_state == BSTP_IFSTATE_BONDED) { bstp_set_port_state(bif, BSTP_IFSTATE_LISTENING); bstp_timer_start(&bif->bif_forward_delay_timer, 0); } } static void bstp_make_blocking(struct bridge_softc *sc, struct bridge_iflist *bif) { if (bif->bif_state != BSTP_IFSTATE_DISABLED && bif->bif_state != BSTP_IFSTATE_BLOCKING && bif->bif_state != BSTP_IFSTATE_BONDED && bif->bif_state != BSTP_IFSTATE_L1BLOCKING) { if ((bif->bif_state == BSTP_IFSTATE_FORWARDING) || (bif->bif_state == BSTP_IFSTATE_LEARNING)) { if (bif->bif_change_detection_enabled) { bstp_topology_change_detection(sc); } } bstp_set_port_state(bif, BSTP_IFSTATE_BLOCKING); bridge_rtdelete(sc, bif->bif_ifp, IFBF_FLUSHDYN); bstp_timer_stop(&bif->bif_forward_delay_timer); if (sc->sc_ifp->if_flags & IFF_LINK2) bstp_adjust_bonded_states(sc, bif); } } static void bstp_make_l1blocking(struct bridge_softc *sc, struct bridge_iflist *bif) { int was_forwarding = (bif->bif_state == BSTP_IFSTATE_FORWARDING); switch(bif->bif_state) { case BSTP_IFSTATE_LISTENING: case BSTP_IFSTATE_LEARNING: case BSTP_IFSTATE_FORWARDING: case BSTP_IFSTATE_BLOCKING: case BSTP_IFSTATE_BONDED: bstp_set_port_state(bif, BSTP_IFSTATE_L1BLOCKING); bridge_rtdelete(sc, bif->bif_ifp, IFBF_FLUSHDYN); bstp_timer_stop(&bif->bif_forward_delay_timer); bstp_timer_stop(&bif->bif_link1_timer); if (bif->bif_flags & IFBIF_DESIGNATED) { bif->bif_flags &= ~IFBIF_DESIGNATED; bstp_configuration_update(sc); bstp_port_state_selection(sc); } if (was_forwarding && (sc->sc_ifp->if_flags & IFF_LINK2)) bstp_adjust_bonded_states(sc, bif); break; default: break; } } /* * Member (bif) changes to or from a FORWARDING state. All members in the * same bonding group which are in a BLOCKING or BONDED state must be set * to either BLOCKING or BONDED based on whether any members in the bonding * group remain in the FORWARDING state. * * Going between the BLOCKING and BONDED states does not require a * configuration update. */ static void bstp_adjust_bonded_states(struct bridge_softc *sc, struct bridge_iflist *obif) { struct bridge_iflist *bif; int state = BSTP_IFSTATE_BLOCKING; TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { if ((bif->bif_flags & IFBIF_STP) == 0) continue; if (bif->bif_state != BSTP_IFSTATE_FORWARDING) continue; if (memcmp(IF_LLADDR(bif->bif_ifp), IF_LLADDR(obif->bif_ifp), ETHER_ADDR_LEN) != 0) { continue; } state = BSTP_IFSTATE_BONDED; break; } TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { if ((bif->bif_flags & IFBIF_STP) == 0) continue; if (bif->bif_state != BSTP_IFSTATE_BLOCKING && bif->bif_state != BSTP_IFSTATE_BONDED) { continue; } if (memcmp(IF_LLADDR(bif->bif_ifp), IF_LLADDR(obif->bif_ifp), ETHER_ADDR_LEN) != 0) { continue; } if (bif->bif_bond_weight == 0) bif->bif_state = BSTP_IFSTATE_BLOCKING; else bif->bif_state = state; } } static void bstp_set_port_state(struct bridge_iflist *bif, uint8_t state) { bif->bif_state = state; } static void bstp_topology_change_detection(struct bridge_softc *sc) { if (bstp_root_bridge(sc)) { sc->sc_topology_change = 1; bstp_timer_start(&sc->sc_topology_change_timer, 0); } else if (!sc->sc_topology_change_detected) { bstp_transmit_tcn(sc); bstp_timer_start(&sc->sc_tcn_timer, 0); } sc->sc_topology_change_detected = 1; } static void bstp_topology_change_acknowledged(struct bridge_softc *sc) { sc->sc_topology_change_detected = 0; bstp_timer_stop(&sc->sc_tcn_timer); } static void bstp_acknowledge_topology_change(struct bridge_softc *sc, struct bridge_iflist *bif) { bif->bif_topology_change_acknowledge = 1; bstp_transmit_config(sc, bif); } void bstp_input(struct bridge_softc *sc, struct bridge_iflist *bif, struct mbuf *m) { struct ether_header *eh; struct bstp_tbpdu tpdu; struct bstp_cbpdu cpdu; struct bstp_config_unit cu; struct bstp_tcn_unit tu; uint16_t len; if ((bif->bif_flags & IFBIF_STP) == 0) goto out; /* * The L1BLOCKING (ping pong failover) test needs to reset the * timer if LINK1 is active. */ if (bif->bif_state == BSTP_IFSTATE_L1BLOCKING) { bstp_set_port_state(bif, BSTP_IFSTATE_BLOCKING); if (sc->sc_ifp->if_flags & IFF_LINK1) bstp_timer_start(&bif->bif_link1_timer, 0); bstp_make_forwarding(sc, bif); } else if (sc->sc_ifp->if_flags & IFF_LINK1) { bstp_timer_start(&bif->bif_link1_timer, 0); } eh = mtod(m, struct ether_header *); len = ntohs(eh->ether_type); if (len < sizeof(tpdu)) goto out; m_adj(m, ETHER_HDR_LEN); if (m->m_pkthdr.len > len) m_adj(m, len - m->m_pkthdr.len); if (m->m_len < sizeof(tpdu) && (m = m_pullup(m, sizeof(tpdu))) == NULL) goto out; memcpy(&tpdu, mtod(m, caddr_t), sizeof(tpdu)); if (tpdu.tbu_dsap != LLC_8021D_LSAP || tpdu.tbu_ssap != LLC_8021D_LSAP || tpdu.tbu_ctl != LLC_UI) goto out; if (tpdu.tbu_protoid != 0 || tpdu.tbu_protover != 0) goto out; switch (tpdu.tbu_bpdutype) { case BSTP_MSGTYPE_TCN: tu.tu_message_type = tpdu.tbu_bpdutype; bstp_received_tcn_bpdu(sc, bif, &tu); break; case BSTP_MSGTYPE_CFG: if (m->m_len < sizeof(cpdu) && (m = m_pullup(m, sizeof(cpdu))) == NULL) goto out; memcpy(&cpdu, mtod(m, caddr_t), sizeof(cpdu)); cu.cu_rootid = (((uint64_t)ntohs(cpdu.cbu_rootpri)) << 48) | (((uint64_t)cpdu.cbu_rootaddr[0]) << 40) | (((uint64_t)cpdu.cbu_rootaddr[1]) << 32) | (((uint64_t)cpdu.cbu_rootaddr[2]) << 24) | (((uint64_t)cpdu.cbu_rootaddr[3]) << 16) | (((uint64_t)cpdu.cbu_rootaddr[4]) << 8) | (((uint64_t)cpdu.cbu_rootaddr[5]) << 0); cu.cu_bridge_id = (((uint64_t)ntohs(cpdu.cbu_bridgepri)) << 48) | (((uint64_t)cpdu.cbu_bridgeaddr[0]) << 40) | (((uint64_t)cpdu.cbu_bridgeaddr[1]) << 32) | (((uint64_t)cpdu.cbu_bridgeaddr[2]) << 24) | (((uint64_t)cpdu.cbu_bridgeaddr[3]) << 16) | (((uint64_t)cpdu.cbu_bridgeaddr[4]) << 8) | (((uint64_t)cpdu.cbu_bridgeaddr[5]) << 0); cu.cu_root_path_cost = ntohl(cpdu.cbu_rootpathcost); cu.cu_message_age = ntohs(cpdu.cbu_messageage); cu.cu_max_age = ntohs(cpdu.cbu_maxage); cu.cu_hello_time = ntohs(cpdu.cbu_hellotime); cu.cu_forward_delay = ntohs(cpdu.cbu_forwarddelay); cu.cu_port_id = ntohs(cpdu.cbu_portid); cu.cu_message_type = cpdu.cbu_bpdutype; cu.cu_topology_change_acknowledgment = (cpdu.cbu_flags & BSTP_FLAG_TCA) ? 1 : 0; cu.cu_topology_change = (cpdu.cbu_flags & BSTP_FLAG_TC) ? 1 : 0; bstp_received_config_bpdu(sc, bif, &cu); break; default: goto out; } out: if (m) m_freem(m); } static void bstp_received_config_bpdu(struct bridge_softc *sc, struct bridge_iflist *bif, struct bstp_config_unit *cu) { int iamroot; iamroot = bstp_root_bridge(sc); if (bif->bif_state != BSTP_IFSTATE_DISABLED) { /* * Record information from peer. The peer_cost field * does not include the local bif->bif_path_cost, it will * be added in as needed (since it can be modified manually * this way we don't have to worry about fixups). */ bif->bif_peer_root = cu->cu_rootid; bif->bif_peer_cost = cu->cu_root_path_cost; bif->bif_peer_bridge = cu->cu_bridge_id; bif->bif_peer_port = cu->cu_port_id; if (bstp_supersedes_port_info(sc, bif)) { bstp_record_config_information(sc, bif, cu); bstp_configuration_update(sc); bstp_port_state_selection(sc); /* * If our bridge loses its root status (?) * * Hello's (unsolicited CFG packets) are generated * every hello period of LINK1 is set, otherwise * we are no longer the root bridge and must stop * generating unsolicited CFG packets. */ if (iamroot && bstp_root_bridge(sc) == 0) { if ((sc->sc_ifp->if_flags & IFF_LINK1) == 0) bstp_timer_stop(&sc->sc_hello_timer); if (sc->sc_topology_change_detected) { bstp_timer_stop( &sc->sc_topology_change_timer); bstp_transmit_tcn(sc); bstp_timer_start(&sc->sc_tcn_timer, 0); } } if (sc->sc_root_port && bif->bif_info == sc->sc_root_port->bif_info) { bstp_record_config_timeout_values(sc, cu); bstp_config_bpdu_generation(sc); if (cu->cu_topology_change_acknowledgment) bstp_topology_change_acknowledged(sc); } } else if (bif->bif_flags & IFBIF_DESIGNATED) { /* * Update designated ports (aged out peers or * the port closest to the root) at a faster pace. * * Clear our designated flag if we aren't marked * as the root port. */ bstp_transmit_config(sc, bif); if ((bif->bif_flags & IFBIF_ROOT) == 0) { bif->bif_flags &= ~IFBIF_DESIGNATED; bstp_configuration_update(sc); bstp_port_state_selection(sc); } } } } static void bstp_received_tcn_bpdu(struct bridge_softc *sc, struct bridge_iflist *bif, struct bstp_tcn_unit *tcn) { if (bif->bif_state != BSTP_IFSTATE_DISABLED && (bif->bif_flags & IFBIF_DESIGNATED)) { bstp_topology_change_detection(sc); bstp_acknowledge_topology_change(sc, bif); } } /* * link1 forces continuous hello's (the bridge interface must be cycled * to start them up), so keep the timer hot if that is the case, otherwise * only send HELLO's if we are the root. */ static void bstp_hello_timer_expiry(struct bridge_softc *sc) { bstp_config_bpdu_generation(sc); if ((sc->sc_ifp->if_flags & IFF_LINK1) || bstp_root_bridge(sc)) bstp_timer_start(&sc->sc_hello_timer, 0); } static void bstp_message_age_timer_expiry(struct bridge_softc *sc, struct bridge_iflist *bif) { int iamroot; iamroot = bstp_root_bridge(sc); bstp_clear_peer_info(sc, bif); bstp_configuration_update(sc); bstp_port_state_selection(sc); /* * If we've become the root and were not the root before * we have some cleanup to do. This also occurs if we * wind up being completely isolated. */ if (iamroot == 0 && bstp_root_bridge(sc)) { sc->sc_max_age = sc->sc_bridge_max_age; sc->sc_hello_time = sc->sc_bridge_hello_time; sc->sc_forward_delay = sc->sc_bridge_forward_delay; bstp_topology_change_detection(sc); bstp_timer_stop(&sc->sc_tcn_timer); bstp_config_bpdu_generation(sc); bstp_timer_start(&sc->sc_hello_timer, 0); } } static void bstp_forward_delay_timer_expiry(struct bridge_softc *sc, struct bridge_iflist *bif) { if (bif->bif_state == BSTP_IFSTATE_LISTENING) { bstp_set_port_state(bif, BSTP_IFSTATE_LEARNING); bstp_timer_start(&bif->bif_forward_delay_timer, 0); } else if (bif->bif_state == BSTP_IFSTATE_LEARNING) { bstp_set_port_state(bif, BSTP_IFSTATE_FORWARDING); if (sc->sc_designated_bridge == sc->sc_bridge_id && bif->bif_change_detection_enabled) { bstp_topology_change_detection(sc); } bstp_configuration_update(sc); bstp_port_state_selection(sc); if (sc->sc_ifp->if_flags & IFF_LINK2) bstp_adjust_bonded_states(sc, bif); } } static void bstp_tcn_timer_expiry(struct bridge_softc *sc) { bstp_transmit_tcn(sc); bstp_timer_start(&sc->sc_tcn_timer, 0); } static void bstp_topology_change_timer_expiry(struct bridge_softc *sc) { sc->sc_topology_change_detected = 0; sc->sc_topology_change = 0; } static void bstp_hold_timer_expiry(struct bridge_softc *sc, struct bridge_iflist *bif) { if (bif->bif_config_pending) bstp_transmit_config(sc, bif); } /* * If no traffic received directly on this port for the specified * period with link1 set we go into a special blocking mode to * fail-over traffic to another port. */ static void bstp_link1_timer_expiry(struct bridge_softc *sc, struct bridge_iflist *bif) { if (sc->sc_ifp->if_flags & IFF_LINK1) bstp_make_l1blocking(sc, bif); } static int bstp_addr_cmp(const uint8_t *a, const uint8_t *b) { int i, d; for (i = 0, d = 0; i < ETHER_ADDR_LEN && d == 0; i++) { d = ((int)a[i]) - ((int)b[i]); } return (d); } void bstp_initialization(struct bridge_softc *sc) { struct bridge_iflist *bif, *mif, *nbif; u_char *e_addr; KKASSERT(&curthread->td_msgport == BRIDGE_CFGPORT); /* * Figure out our bridge ID, use the lowest-valued MAC. * Include the bridge's own random MAC in the calculation. */ mif = NULL; TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { if ((bif->bif_flags & IFBIF_STP) == 0) continue; if (bif->bif_ifp->if_type != IFT_ETHER) continue; if (mif == NULL) { mif = bif; continue; } bif->bif_port_id = (bif->bif_priority << 8) | (bif->bif_ifp->if_index & 0xff); if (bstp_addr_cmp(IF_LLADDR(bif->bif_ifp), IF_LLADDR(mif->bif_ifp)) < 0) { mif = bif; continue; } } if (mif == NULL) { bstp_stop(sc); return; } if (bstp_addr_cmp(IF_LLADDR(sc->sc_ifp), IF_LLADDR(mif->bif_ifp)) < 0) e_addr = IF_LLADDR(sc->sc_ifp); else e_addr = IF_LLADDR(mif->bif_ifp); sc->sc_bridge_id = (((uint64_t)sc->sc_bridge_priority) << 48) | (((uint64_t)e_addr[0]) << 40) | (((uint64_t)e_addr[1]) << 32) | (((uint64_t)e_addr[2]) << 24) | (((uint64_t)e_addr[3]) << 16) | (((uint64_t)e_addr[4]) << 8) | (((uint64_t)e_addr[5])); /* * Remainder of setup. */ sc->sc_designated_root = sc->sc_bridge_id; sc->sc_designated_cost = 0; sc->sc_root_port = NULL; sc->sc_max_age = sc->sc_bridge_max_age; sc->sc_hello_time = sc->sc_bridge_hello_time; sc->sc_forward_delay = sc->sc_bridge_forward_delay; sc->sc_topology_change_detected = 0; sc->sc_topology_change = 0; bstp_timer_stop(&sc->sc_tcn_timer); bstp_timer_stop(&sc->sc_topology_change_timer); if (callout_pending(&sc->sc_bstpcallout) == 0) callout_reset(&sc->sc_bstpcallout, hz, bstp_tick, sc); TAILQ_FOREACH_MUTABLE(bif, &sc->sc_iflists[mycpuid], bif_next, nbif) { if (sc->sc_ifp->if_flags & IFF_LINK1) bstp_timer_start(&bif->bif_link1_timer, 0); if (bif->bif_flags & IFBIF_STP) bstp_ifupdstatus(sc, bif); else bstp_disable_port(sc, bif); if (nbif != NULL && !nbif->bif_onlist) { KKASSERT(bif->bif_onlist); nbif = TAILQ_NEXT(bif, bif_next); } } bstp_port_state_selection(sc); bstp_config_bpdu_generation(sc); bstp_timer_start(&sc->sc_hello_timer, 0); } void bstp_stop(struct bridge_softc *sc) { struct bridge_iflist *bif; KKASSERT(&curthread->td_msgport == BRIDGE_CFGPORT); TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { bstp_set_port_state(bif, BSTP_IFSTATE_DISABLED); bstp_timer_stop(&bif->bif_hold_timer); bstp_timer_stop(&bif->bif_message_age_timer); bstp_timer_stop(&bif->bif_forward_delay_timer); bstp_timer_stop(&bif->bif_link1_timer); } callout_stop(&sc->sc_bstpcallout); bstp_timer_stop(&sc->sc_topology_change_timer); bstp_timer_stop(&sc->sc_tcn_timer); bstp_timer_stop(&sc->sc_hello_timer); crit_enter(); lwkt_dropmsg(&sc->sc_bstptimemsg.lmsg); crit_exit(); } /* * [re]initialize a port. The port is initialized to a L1BLOCKING state * or a BLOCKING state. When link ping/pong is enabled (LINK1) we start * out in the L1BLOCKING state to prevent flapping from blowing up the * state of the other ports. * * Either way the first config packet received will knock the port into * the LISTENING state. */ static void bstp_initialize_port(struct bridge_softc *sc, struct bridge_iflist *bif) { int needs_adjust = (bif->bif_state == BSTP_IFSTATE_FORWARDING || bif->bif_state == BSTP_IFSTATE_BLOCKING || bif->bif_state == BSTP_IFSTATE_BONDED); if (sc->sc_ifp->if_flags & IFF_LINK1) bstp_set_port_state(bif, BSTP_IFSTATE_L1BLOCKING); else bstp_set_port_state(bif, BSTP_IFSTATE_BLOCKING); bstp_clear_peer_info(sc, bif); bif->bif_topology_change_acknowledge = 0; bif->bif_config_pending = 0; bif->bif_change_detection_enabled = 1; bstp_timer_stop(&bif->bif_message_age_timer); bstp_timer_stop(&bif->bif_forward_delay_timer); bstp_timer_stop(&bif->bif_hold_timer); bstp_timer_stop(&bif->bif_link1_timer); if (needs_adjust && (sc->sc_ifp->if_flags & IFF_LINK2)) bstp_adjust_bonded_states(sc, bif); } /* * When enabling a port that was previously disabled no configuration * update occurs, otherwise down/up or running/not-running flapping * (e.g. by openvpn on a TAP interface) will blow up the other ports. */ static void bstp_enable_port(struct bridge_softc *sc, struct bridge_iflist *bif) { int was_disabled = (bif->bif_state == BSTP_IFSTATE_DISABLED || bif->bif_state == BSTP_IFSTATE_L1BLOCKING); bstp_initialize_port(sc, bif); if (sc->sc_ifp->if_flags & IFF_LINK1) bstp_timer_start(&bif->bif_link1_timer, 0); if (was_disabled == 0) { bstp_configuration_update(sc); bstp_port_state_selection(sc); } bstp_adjust_bonded_states(sc, bif); } /* * When disabling a port that was previously in a non-forwarding or bonded * state no configuration update occurs, otherwise down/up or * running/not-running flapping (e.g. by openvpn on a TAP interface) will * blow up the other ports. */ static void bstp_disable_port(struct bridge_softc *sc, struct bridge_iflist *bif) { int was_forwarding = (bif->bif_state == BSTP_IFSTATE_FORWARDING); int was_bonded = (bif->bif_state == BSTP_IFSTATE_BONDED); int iamroot; iamroot = bstp_root_bridge(sc); bstp_clear_peer_info(sc, bif); bstp_set_port_state(bif, BSTP_IFSTATE_DISABLED); bif->bif_topology_change_acknowledge = 0; bif->bif_config_pending = 0; bstp_timer_stop(&bif->bif_message_age_timer); bstp_timer_stop(&bif->bif_forward_delay_timer); bstp_timer_stop(&bif->bif_link1_timer); if (was_forwarding || was_bonded) { bstp_configuration_update(sc); bstp_port_state_selection(sc); } bridge_rtdelete(sc, bif->bif_ifp, IFBF_FLUSHDYN); if (was_forwarding && (sc->sc_ifp->if_flags & IFF_LINK2)) bstp_adjust_bonded_states(sc, bif); if (iamroot == 0 && bstp_root_bridge(sc)) { sc->sc_max_age = sc->sc_bridge_max_age; sc->sc_hello_time = sc->sc_bridge_hello_time; sc->sc_forward_delay = sc->sc_bridge_forward_delay; bstp_topology_change_detection(sc); bstp_timer_stop(&sc->sc_tcn_timer); bstp_config_bpdu_generation(sc); bstp_timer_start(&sc->sc_hello_timer, 0); } } void bstp_linkstate(struct ifnet *ifp, int state) { struct bridge_softc *sc; struct bridge_iflist *bif; sc = ifp->if_bridge; ifnet_serialize_all(sc->sc_ifp); /* * bstp_ifupdstatus() may block, but it is the last * operation of the member iface iteration, so we * don't need to use LIST_FOREACH_MUTABLE()+bif_onlist * check here. */ TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { if ((bif->bif_flags & IFBIF_STP) == 0) continue; if (bif->bif_ifp == ifp) { bstp_ifupdstatus(sc, bif); break; } } ifnet_deserialize_all(sc->sc_ifp); } static void bstp_ifupdstatus(struct bridge_softc *sc, struct bridge_iflist *bif) { struct ifnet *ifp = bif->bif_ifp; struct ifmediareq ifmr; int error = 0; bzero((char *)&ifmr, sizeof(ifmr)); ifnet_serialize_all(ifp); error = (*ifp->if_ioctl)(ifp, SIOCGIFMEDIA, (caddr_t)&ifmr, NULL); ifnet_deserialize_all(ifp); if ((error == 0) && (ifp->if_flags & IFF_UP)) { if (ifmr.ifm_status & IFM_ACTIVE) { if (bif->bif_state == BSTP_IFSTATE_DISABLED) bstp_enable_port(sc, bif); } else { if (bif->bif_state != BSTP_IFSTATE_DISABLED) bstp_disable_port(sc, bif); } return; } if (bif->bif_state != BSTP_IFSTATE_DISABLED) bstp_disable_port(sc, bif); } static void bstp_tick(void *arg) { struct bridge_softc *sc = arg; struct lwkt_msg *lmsg; KKASSERT(mycpuid == BRIDGE_CFGCPU); crit_enter(); if (callout_pending(&sc->sc_bstpcallout) || !callout_active(&sc->sc_bstpcallout)) { crit_exit(); return; } callout_deactivate(&sc->sc_bstpcallout); lmsg = &sc->sc_bstptimemsg.lmsg; KKASSERT(lmsg->ms_flags & MSGF_DONE); lwkt_sendmsg_oncpu(BRIDGE_CFGPORT, lmsg); crit_exit(); } void bstp_tick_handler(netmsg_t msg) { struct bridge_softc *sc = msg->lmsg.u.ms_resultp; struct bridge_iflist *bif; KKASSERT(&curthread->td_msgport == BRIDGE_CFGPORT); crit_enter(); /* Reply ASAP */ lwkt_replymsg(&msg->lmsg, 0); crit_exit(); ifnet_serialize_all(sc->sc_ifp); /* * NOTE: * We don't need to worry that member iface is ripped * from the per-cpu list during the blocking operation * in the loop body, since deletion is serialized by * BRIDGE_CFGPORT */ TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { if ((bif->bif_flags & IFBIF_STP) == 0) continue; /* * XXX This can cause a lag in "link does away" * XXX and "spanning tree gets updated". We need * XXX come sort of callback from the link state * XXX update code to kick spanning tree. * XXX --thorpej@NetBSD.org */ bstp_ifupdstatus(sc, bif); } if (bstp_timer_expired(&sc->sc_hello_timer, sc->sc_hello_time)) bstp_hello_timer_expiry(sc); if (bstp_timer_expired(&sc->sc_tcn_timer, sc->sc_bridge_hello_time)) bstp_tcn_timer_expiry(sc); if (bstp_timer_expired(&sc->sc_topology_change_timer, sc->sc_topology_change_time)) bstp_topology_change_timer_expiry(sc); TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { if ((bif->bif_flags & IFBIF_STP) == 0) continue; if (bstp_timer_expired(&bif->bif_message_age_timer, sc->sc_max_age)) bstp_message_age_timer_expiry(sc, bif); } TAILQ_FOREACH(bif, &sc->sc_iflists[mycpuid], bif_next) { if ((bif->bif_flags & IFBIF_STP) == 0) continue; if (bstp_timer_expired(&bif->bif_forward_delay_timer, sc->sc_forward_delay)) bstp_forward_delay_timer_expiry(sc, bif); if (bstp_timer_expired(&bif->bif_hold_timer, sc->sc_hold_time)) bstp_hold_timer_expiry(sc, bif); if (bstp_timer_expired(&bif->bif_link1_timer, sc->sc_hello_time * 10)) bstp_link1_timer_expiry(sc, bif); } if (sc->sc_ifp->if_flags & IFF_RUNNING) callout_reset(&sc->sc_bstpcallout, hz, bstp_tick, sc); ifnet_deserialize_all(sc->sc_ifp); } static void bstp_timer_start(struct bridge_timer *t, uint16_t v) { t->value = v; t->active = 1; } static void bstp_timer_stop(struct bridge_timer *t) { t->value = 0; t->active = 0; } static int bstp_timer_expired(struct bridge_timer *t, uint16_t v) { if (t->active == 0) return (0); t->value += BSTP_TICK_VAL; if (t->value >= v) { bstp_timer_stop(t); return (1); } return (0); } |