/usr/lib/firehol/link-balancer is in firehol-tools 3.1.5+ds-1ubuntu1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 | #!/bin/bash
#
# Link Balancer - Policy Based Routing for humans...
#
# Copyright
#
# Copyright (C) 2015-2017 Costa Tsaousis <costa@tsaousis.gr>
# Copyright (C) 2015-2017 Phil Whineray <phil@sanewall.org>
#
# License
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# See the file COPYING for details.
#
READLINK_CMD=${READLINK_CMD:-readlink}
BASENAME_CMD=${BASENAME_CMD:-basename}
DIRNAME_CMD=${DIRNAME_CMD:-dirname}
function realdir {
local r="$1"; local t=$($READLINK_CMD "$r")
while [ "$t" ]; do
r=$(cd $($DIRNAME_CMD "$r") && cd $($DIRNAME_CMD "$t") && pwd -P)/$($BASENAME_CMD "$t")
t=$($READLINK_CMD "$r")
done
$DIRNAME_CMD "$r"
}
PROGRAM_FILE="$0"
PROGRAM_DIR="${FIREHOL_OVERRIDE_PROGRAM_DIR:-$(realdir "$0")}"
PROGRAM_PWD="${PWD}"
declare -a PROGRAM_ORIGINAL_ARGS=("${@}")
for functions_file in install.config functions.common
do
if [ -r "$PROGRAM_DIR/$functions_file" ]
then
source "$PROGRAM_DIR/$functions_file"
else
1>&2 echo "Cannot access $PROGRAM_DIR/$functions_file"
exit 1
fi
done
common_disable_localization || exit
common_private_umask || exit
common_require_root || exit
# make sure sbin is included in the path
# it seems that pppd ip-up.d script need this
export PATH="$PATH:/sbin:/usr/sbin:/usr/local/sbin"
if [ "$LB_DEBUGGING" ]; then set -v; set -x; fi
# link-balancer temporary directory.
# every instance of link-balancer creates a random directory
# within this one.
LB_RUN_DIR="$LOCALSTATEDIR/run/link-balancer"
# If this is set to 1, no checks will be made if the gateways are available.
# All gateways will be assumed active, if their interfaces are found
# operational.
# WHY: When we have a multi-ppp environment the default gateway of the system
# will be composed my multiple 'nexthop' routes. When a ppp device fails
# the kernel will remove the entire default gateway, not just the 'nexthop'
# that is not available any more.
# By calling link-balancer with option '-f' will enable this option.
# This option will make link-balancer restore the default gateway as soon
# as possible.
# 'link-balancer -f' should thus by called by ppp/ip-down scripts.
FORCE_ALWAYSON=0
# If set to 1, link-balancer will not change anything. It will just show
# what it will execute if run without it.
DRY_RUN=0
# If set to 1, link-balancer will output to standard-error all temporary files
# it generates during its execution.
DEBUG_ALL_DATA=0
# The step which increments the priority of the rules for each rule statement
# in the configuration file.
LB_RULE_STEP=100
# Keeps a list of all tables that have been altered during its run.
declare -A LB_ALTERED=()
# Where is the link-balancer configuration files?
LB_CONFIG_DIR="${FIREHOL_CONFIG_DIR}"
# Link-balancer main configuration file.
LB_CONFIG="${LB_CONFIG_DIR}/link-balancer.conf"
# takes either 4 or 6 to switch the functions in ipv4 or ipv6
LB_DEFAULT_IPV=4
marksreset() { :; }
markdef() { :; }
if [ -r "${FIREHOL_CONFIG_DIR}/firehol-defaults.conf" ]
then
source "${FIREHOL_CONFIG_DIR}/firehol-defaults.conf" || exit 1
fi
# temporary variable (default LB_DEFAULT_IPV=4)
LB_IPV=
RUNNING_ON_TERMINAL=0
if [ "z$1" = "z-nc" ]
then
shift
else
common_setup_terminal && RUNNING_ON_TERMINAL=1
fi
# if called with the parameter 'boot', create a new screen that
# will run forever.
if [ "$1" = "boot" ]
then
shift
common_require_cmd $PROGRAM_FILE SCREEN_CMD
$SCREEN_CMD -S 'link-balancer' -d -m "$0" loop "${@}"
exit 0
fi
# if called with the parameter 'loop', run forever.
if [ "$1" = "loop" ]
then
shift
secs="${1}"
secs=$[ secs + 1 - 1 ]
test "${secs}" = "0" && secs=300
shift
while [ 1 ]
do
$0 "${@}"
echo "Waiting for $secs secs..."
$SLEEP_CMD $secs
done
exit 0
fi
if [ ! -d "${LB_RUN_DIR}" ]
then
$MKDIR_CMD -p "${LB_RUN_DIR}" || exit 1
$CHOWN_CMD root:root "${LB_RUN_DIR}" || exit 1
$CHMOD_CMD 700 "${LB_RUN_DIR}" || exit 1
fi
# get an exclusive lock or wait for it to be available
LOCKFILE="${LB_RUN_DIR}/link-balancer.lock"
[ "${FLOCKER}" != "${LOCKFILE}" ] && exec $ENV_CMD FLOCKER="${LOCKFILE}" $FLOCK_CMD -e "${LOCKFILE}" "$0" "$@" || :
cd "${LB_RUN_DIR}" || exit 1
LB_DIR="`$MKTEMP_CMD -d "${LB_RUN_DIR}/temp-XXXXXXXXXX"`" || exit 1
# -----------------------------------------------------------------------------
# Default FireHOL marks
declare -A MARKS_BITS='([connmark]="6" [usermark]="7" )'
declare -A MARKS_MASKS='([connmark]="0x0000003f" [usermark]="0x00001fc0" )'
declare -A MARKS_MAX='([connmark]="63" [usermark]="127" )'
declare -A MARKS_SHIFT='([connmark]="0" [usermark]="6" )'
FIREHOL_SPOOL_DIR="${FIREHOL_SPOOL_DIR-$LOCALSTATEDIR/spool/firehol}"
if [ -f "${FIREHOL_SPOOL_DIR}/marks.conf" ]
then
source "${FIREHOL_SPOOL_DIR}/marks.conf" || exit 1
fi
mark_value() {
local name="${1}"; shift
local x=
if [ -z "${name}" ]
then
error "Cannot find the value of mark with name '${name}'."
return 1
fi
if [ -z "${1}" ]
then
error "Empty mark value given for mark ${name}."
return 1
fi
if [ -z "${MARKS_MASKS[$name]}" ]
then
error "Mark $name does not exist."
return 1
fi
for x in ${@//,/ }
do
local x=$[ x + 1 - 1 ]
if [ $x -gt ${MARKS_MAX[$name]} -o $x -lt 0 ]
then
error "Cannot get mark $name of value $x. Mark $name is configured to get values from 0 to ${MARKS_MAX[$name]}. Change firehol-defaults.conf to add more."
return 1
fi
#echo "$[ x << ${MARKS_SHIFT[$name]} ]/${MARKS_MASKS[$name]}"
printf "0x%08x/${MARKS_MASKS[$name]}\n" "$[ x << ${MARKS_SHIFT[$name]} ]"
done
return 0
}
# -----------------------------------------------------------------------------
# Find in the BASH execution stack, the line and the source file that has called us.
# Before first use the variable PROGRAM_FILE should be set to the file to be excluded.
# It also sets the variable LAST_CONFIG_LINE on each run.
FORCE_CONFIG_LINEID=
LAST_CONFIG_LINE=
config_line() {
if [ ! -z "${FORCE_CONFIG_LINEID}" ]
then
LAST_CONFIG_LINE="${FORCE_CONFIG_LINEID}"
else
# find the config line in the BASH stack
# start from 2
# 0 is this line
# 1 is the caller - our line for sure
# 2 is the caller's caller - possibly a config file line
local i= all=${#BASH_SOURCE}
for (( i = 2; i < $all; i++ ))
do
[ ! "${BASH_SOURCE[$i]}" = "${PROGRAM_FILE}" ] && break
done
LAST_CONFIG_LINE="${BASH_LINENO[$[i-1]]}@${BASH_SOURCE[$i]}: ${FUNCNAME[$[i-1]]}:"
fi
test ! "z$1" = "z-ne" && echo "${LAST_CONFIG_LINE}"
}
LB_RESULT_CODE=1
lb_exit() {
cd /tmp
if [ ${DEBUG_ALL_DATA} -eq 1 ]
then
echo >&2
echo >&2
echo >&2 "---------------------------------------------------------------------"
echo >&2 "Temporary folder contents:"
$LS_CMD -l "${LB_DIR}/" >&2
for x in "${LB_DIR}"/*
do
echo >&2
echo >&2
echo >&2 "---------------------------------------------------------------------"
echo >&2 "${x}"
echo >&2
$CAT_CMD >&2 "${x}"
done
fi
if [ ! -z "${LB_DIR}" -a -d "${LB_DIR}" ]
then
$RM_CMD -rf "${LB_DIR}"
fi
enable trap
enable exit
trap exit EXIT
exit ${LB_RESULT_CODE}
}
trap lb_exit EXIT
trap lb_exit SIGHUP
trap lb_exit INT
# -----------------------------------------------------------------------------
# COMMON FUNCTIONS
syslog() {
if [ ${DRY_RUN} -eq 0 ]
then
$LOGGER_CMD -p daemon.info -t "LinkBalancer" "${*}"
fi
}
last_debug_depth=0
DEBUGCMDNNL() {
local prefix="${1}"; shift
local name="${1}"; shift
local depth="${1}"; shift
test "$depth" = "" && local depth=$last_debug_depth
last_debug_depth=$depth
# printf >&2 "`date`"
printf >&2 "% $((depth))s ${prefix} %s " " " "${name}"
printf >&2 " ${*} "
}
DEBUGCMD() {
DEBUGCMDNNL "-" "${@}"
printf >&2 "\n"
}
warning() {
local line="$(config_line)"
syslog "WARNING ${line} ${@}"
echo >&2
echo >&2 -e "${COLOR_YELLOW}${COLOR_BOLD}WARNING ${line} $@ ${COLOR_RESET}"
echo >&2
return 0
}
error() {
local line="$(config_line)"
syslog "ERROR ${line} ${@}"
echo >&2
echo >&2 -e "${COLOR_RED}${COLOR_BOLD}ERROR ${line} $@ ${COLOR_RESET}"
echo >&2
exit 1
}
run() {
printf >&2 "${COLOR_BOLD}"
DEBUGCMDNNL "#" "${FUNCNAME}" "${#FUNCNAME[*]}" "${@}" " ... "
local tmp="`$MKTEMP_CMD "${LB_DIR}/run-$$-${RANDOM}-XXXXXXXXXX"`"
"${@}" >"$tmp" 2>"$tmp.err"
local ret=$?
if [ $ret -eq 0 ]
then
printf >&2 "${COLOR_RESET}${COLOR_BGGREEN}${COLOR_BLACK}${COLOR_BOLD} OK ${COLOR_RESET}\n"
else
printf >&2 "${COLOR_RESET}${COLOR_BGRED}${COLOR_WHITE}${COLOR_BOLD}${COLOR_BLINK} FAILED ${COLOR_RESET}\n"
fi
$CAT_CMD >&2 "$tmp.err"
$CAT_CMD "$tmp"
$RM_CMD "$tmp" "$tmp.err"
return $ret
}
action() {
if [ ${DRY_RUN} -eq 0 ]
then
printf >&2 "${COLOR_YELLOW}"
run "${@}"
local ret=$?
if [ $ret -ne 0 ]
then
warning "Command: '${@}' exited with code $ret."
else
syslog "Successfuly run: '${@}'."
fi
return $ret
fi
DEBUGCMDNNL "#" "${FUNCNAME}" "${#FUNCNAME[*]}" "${@}" " ... "
printf >&2 "DEFERRED\n"
return 0
}
trim_spaces() {
$SED_CMD -e "s/[\t\\ ]\+/ /g" -e "s/ \+$//g" -e "s/^ \+//g"
}
loadfile() {
local file="$1"; shift
cd "${LB_CONFIG_DIR}"
test ! -f "${file}" && syslog "WARNING: file '${file}' does not exist."
$CAT_CMD "${file}" | $SED_CMD -e "s/#.*$//g" | trim_spaces | $TR_CMD '\n' ' '
local ret=$?
cd "${LB_RUN_DIR}"
return $ret
}
ipv4() {
DEBUGCMD "${FUNCNAME}" "${#FUNCNAME[*]}" "${@}"
local cmd="${1}"; shift
case "${cmd}" in
gateway|table|rules|mark|fwmark|connmark|src|dst|from|to|tos|not)
"${cmd}" ipv4 "${@}"
return $?
;;
default)
error "Command '${cmd}' inherits IP version from its parent. Do not give ${FUNCNAME} for '${cmd}'."
exit 1
;;
*)
error "Command '${cmd}' cannot change IP version."
exit 1
;;
esac
}
ipv6() {
DEBUGCMD "${FUNCNAME}" "${#FUNCNAME[*]}" "${@}"
local cmd="${1}"; shift
case "${cmd}" in
gateway|table|rules|mark|fwmark|connmark|src|dst|from|to|tos|not)
"${cmd}" ipv6 "${@}"
return $?
;;
default)
error "Command '${cmd}' inherits IP version from its parent. Do not give ${FUNCNAME} for '${cmd}'."
exit 1
;;
*)
error "Command '${cmd}' cannot change IP version."
exit 1
;;
esac
}
# -----------------------------------------------------------------------------
# gateway section
get_interface_ips() {
local ipv="4"
test "${LB_IPV}" = "6" && local ipv="6"
local inet="inet"
test "${LB_IPV}" = "6" && local inet="inet6"
run $IP_CMD -${ipv} addr show dev "$1" |\
trim_spaces |\
$GREP_CMD "${inet} " |\
$CUT_CMD -d ' ' -f 2 |\
$CUT_CMD -d '/' -f 1 |\
$TR_CMD '\n' ' '
}
get_interface_routes() {
local dev="${1}"; shift
local ipv=4
test "${LB_IPV}" = "6" && local ipv=6
local hostnet="/32"
test "${LB_IPV}" = "6" && local hostnet="/128"
local i=
for i in ${@}
do
run $IP_CMD -${ipv} route show dev "${dev}" scope link src "${i}" |\
trim_spaces |\
$CUT_CMD -d ' ' -f 1 |\
$SED_CMD "s|/${hostnet}||g" |\
$GREP_CMD -v "/" |\
$TR_CMD '\n' ' '
done
}
CHECK_PING_COUNT=3
CHECK_PING_WAIT=10
check_ping() {
DEBUGCMD "${FUNCNAME}" "${#FUNCNAME[*]}" "${@}"
local dev="$1"
local src="$2"
local dst="$3"
local cmd="$PING_CMD"
test "${LB_IPV}" = "6" && local cmd="$PING6_CMD"
run $cmd -I ${src} -c ${CHECK_PING_COUNT} -w ${CHECK_PING_WAIT} ${dst} >/dev/null
return $?
}
CHECK_TRACEROUTE_PORT=80
CHECK_TRACEROUTE_WAIT=2
check_traceroute() {
DEBUGCMD "${FUNCNAME}" "${#FUNCNAME[*]}" "${@}"
local dev="$1"
local src="$2"
local dst="$3"
local cmd="$TRACEROUTE_CMD -4"
test "${LB_IPV}" = "6" && local cmd="$TRACEROUTE_CMD -6"
local found="`run $cmd -i "${dev}" -r -s ${src} -w ${CHECK_TRACEROUTE_WAIT} -Tn -p ${CHECK_TRACEROUTE_PORT} ${dst} | $GREP_CMD ${dst}`"
test ! -z "${found}" && return 0
return 1
}
check_alwayson() {
DEBUGCMD "${FUNCNAME}" "${#FUNCNAME[*]}" "${@}"
local dev="$1"
local src="$2"
local dst="$3"
return 0
}
# this array has one entry per gateway.
# - the array index is the gateway name
# - the array value is the number of distinct gateways alive
declare -A LB_GATEWAYS=()
# keep track of the gateways IP version
declare -A LB_GATEWAYS_IPV=()
gateway4() { gateway ipv4 "${@}"; }
gateway6() { gateway ipv6 "${@}"; }
gateway() {
printf >&2 "\n-------------------------------------------------------------------------------\n"
DEBUGCMD "${FUNCNAME}" "${#FUNCNAME[*]}" "${@}"
LB_IPV=${LB_DEFAULT_IPV}
if [ ! -z "${work_route}" ]
then
local name="${work_route}"
else
local name="${1}"
shift
fi
local gw=
local dev=
local src=
local dst="gateway"
local check="ping"
while [ ! -z "${1}" ]
do
case "${1}" in
ipv4)
LB_IPV=4
;;
ipv6)
LB_IPV=6
;;
on|dev)
local dev="${2}"
shift
;;
gateway|gw|via)
local gw="${2//,/ }"
shift
;;
src|from)
local src="${2//,/ }"
shift
;;
check)
local check="${2}"
local dst="${3//,/ }"
shift 2
;;
*)
error "${FUNCNAME} '${name}': cannot understand parameter '${1}'."
return 1
;;
esac
shift
done
LB_GATEWAYS[$name]=0
LB_GATEWAYS_IPV[$name]=${LB_IPV}
# if we don't have source IPs, find them from the interface
if [ -z "${src}" -o "${src}" = "auto" ]
then
local src="`get_interface_ips "${dev}" | $SORT_CMD -u`"
fi
# if we don't have source IPs, cannot proceed
if [ -z "${src}" ]
then
warning "Cannot find gateway ${name} IPs."
return 1
fi
# if we don't have a gateway, cannot proceed
if [ -z "${dev}" ]
then
error "${FUNCNAME} '${name}': cannot add a gateway without a device."
return 2
fi
# if we don't have gateway IPs, find them from the interface
test -z "${gw}" -o "${gw}" = "auto" -o "${gw}" = "detect" && local gw="`get_interface_routes "${dev}" ${src}`"
# if we don't have a gateway, cannot proceed
if [ -z "${gw}" ]
then
warning "Cannot find ${name} gateway IPs."
return 3
fi
# if we don't have a destination, use the gateway
test -z "${dst}" -o "${dst}" = "auto" -o "${dst}" = "detect" -o "${dst}" = "gateway" && local dst="${gw}"
test $FORCE_ALWAYSON -eq 1 && local check="alwayson"
$CAT_CMD <<EOF_GW
--- GATEWAY CONFIGURATION ----
Gateway : ${name}
Via IP : ${gw}
Source IPs: ${src}
Check : ${check} ${dst}
IP VERSION: ${LB_IPV}
EOF_GW
echo "${src}" >"${LB_DIR}/gateway.${name}.source-ips"
# -------------------------------------------------------------------------
# Check if the gateway is alive
echo " CHECKING IF GATEWAY ${name} IS ALIVE"
local alive=0
local s=
for s in ${src}
do
local d=
for d in ${dst}
do
test $alive -ne 0 && break
check_${check} ${dev} ${s} ${d}
test $? -eq 0 && local alive=1
done
done
if [ ${alive} -eq 0 ]
then
echo
echo " Gateway ${name} is not alive."
return 99
fi
local count=0
local g=
for g in ${gw}
do
local count=$[count + 1]
echo "via ${g} dev ${dev}" >>"${LB_DIR}/gateway.${name}.paths"
done
LB_GATEWAYS[$name]="${count}"
echo >&2
echo >&2 -e " ${COLOR_GREEN}${COLOR_BOLD}IPv${LB_IPV} gateway '${name}' is alive, having ${count} active path(s).${COLOR_RESET}"
return 0
}
# -----------------------------------------------------------------------------
# table section
# this array has one entry per table.
# - the array index is the table name
# - the array value is the number of default gateways available
declare -A LB_TABLES=()
declare -A LB_TABLES_FALLBACK=()
# keep track of the IP version per table
# if a table is used by both IPv4 and IPv6, '46' should be stored
declare -A LB_TABLES_IPV=()
# keep track of all origin tables
# if a table is used by both IPv4 and IPv6, '46' should be stored
declare -A LB_ORIGIN_TABLES=()
get_existing_routing_table() {
DEBUGCMD "${FUNCNAME}" "${#FUNCNAME[*]}" "${@}"
local table="${1}"
if [ "${LB_IPV}" = "6" ]
then
run $IP_CMD -6 -o route show table "${table}" |\
trim_spaces |\
$SORT_CMD
else
run $IP_CMD -4 -o route show table "${table}" |\
$GREP_CMD -v " via 127.0.0.1 dev lo" |\
trim_spaces |\
$SORT_CMD
fi
}
work_table=
work_ipv=
table4() { table ipv4 "${@}"; }
table6() { table ipv6 "${@}"; }
table() {
printf >&2 "\n-------------------------------------------------------------------------------\n"
DEBUGCMD "${FUNCNAME}" "${#FUNCNAME[*]}" "${@}"
LB_IPV=${LB_DEFAULT_IPV}
local table="$1"; shift
if [ -z "${table}" ]
then
error "${FUNCNAME} needs a name."
return 1
fi
local origin="main"
local copy_default=1
while [ ! -z "${1}" ]
do
case "${1}" in
ipv4)
LB_IPV=4
;;
ipv6)
LB_IPV=6
;;
from|copy|duplicate)
local origin="${2}"
shift
;;
nodefault|keepdefault|nodef)
local copy_default=0
;;
*)
error "${FUNCNAME} '${table}': Cannot understand option '${1}'."
return 1
;;
esac
shift
done
# set LB_TABLES_IPV
if [ -z "${LB_TABLES_IPV[$table]}" ]
then
LB_TABLES_IPV[$table]="${LB_IPV}"
elif [ "${LB_TABLES_IPV[$table]}" = "${LB_IPV}" ]
then
error "${FUNCNAME}: table ${table} already exists in IPv${LB_IPV}."
exit 1
else
LB_TABLES_IPV[$table]="46"
fi
if [ "${table}" = "${origin}" -o -z "${origin}" -o "${origin}" = "none" ]
then
local origin="none"
fi
# give a summary to the user
$CAT_CMD <<EOF_TABLE
--- TABLE CONFIGURATION ---
Table : ${table}
Copy from : ${origin}
IP VERSION : ${LB_IPV}
EOF_TABLE
if [ ! "${origin}" = "none" ]
then
# just link it
# The source file may not exist yet. This is OK.
# Later, at finizize_tables() we will check the links to find
# the dependencies between the tables so that they will be
# activated in the correct order, so that in one pass all
# tables will get the final routes.
# We will also check if the user requested a circular
# dependency, and stop processing in this case.
$LN_CMD -s "${LB_DIR}/table.${origin}.routes.${LB_IPV}" "${LB_DIR}/table.${table}.origin_routes.${LB_IPV}"
test $copy_default -eq 1 && $LN_CMD -s "${LB_DIR}/table.${origin}.default.${LB_IPV}" "${LB_DIR}/table.${table}.origin_default.${LB_IPV}"
fi
work_table="${table}"
work_ipv="${LB_IPV}"
LB_TABLES[$table]=0
LB_TABLES_FALLBACK[$table]=0
if [ ! "${origin}" = "none" ]
then
# set LB_ORIGIN_TABLES
if [ -z "${LB_ORIGIN_TABLES[$origin]}" ]
then
LB_ORIGIN_TABLES[$origin]="${LB_IPV}"
elif [ ! "${LB_ORIGIN_TABLES[$origin]}" = "${LB_IPV}" ]
then
LB_ORIGIN_TABLES[$origin]="46"
fi
fi
# instruct the finalizer to run IPvX tables
$TOUCH_CMD "${LB_DIR}/tables.${LB_IPV}"
}
fallback() {
DEBUGCMD "${FUNCNAME}" "${#FUNCNAME[*]}" "${@}"
default "${@}" weight 0
}
default() {
DEBUGCMD "${FUNCNAME}" "${#FUNCNAME[*]}" "${@}"
if [ -z "${work_table}" ]
then
error "Statement 'default' should appear after a 'table'."
exit 1
fi
# check IPvX is as it should
LB_IPV=${work_ipv}
if [ ! "${LB_TABLES_IPV[${work_table}]}" = "${LB_IPV}" -a ! "${LB_TABLES_IPV[${work_table}]}" = "46" ]
then
error "${FUNCNAME} INTERNAL ERROR: Inheritted IPv${LB_IPV} but table is IPv${LB_TABLES_IPV[${work_table}]}."
exit 1
fi
local gw=
local weight=100
while [ ! -z "${1}" ]
do
case "${1}" in
via|gateway|gw)
gw="${2}"
shift
;;
weight)
local weight="${2}"
shift
;;
*)
error "${FUNCNAME} on '${work_table}': Cannot understand option '${1}'."
return 1
;;
esac
shift
done
if [ -z "${gw}" ]
then
error "${FUNCNAME} on '${work_table}': Cannot work without a gateway."
return 1
fi
if [ -z "${LB_GATEWAYS[$gw]}" ]
then
warning "'${gw}' on '${work_table}': Unknown gateway '${gw}' requested."
return 1
fi
if [ ! "${LB_GATEWAYS_IPV[$gw]}" = "${LB_IPV}" ]
then
error "${FUNCNAME} '${gw}' on '${work_table}': Cannot assign an IPv${LB_GATEWAYS_IPV[$gw]} gateway to an IPv${LB_IPV} table."
exit 1
fi
if [ "${LB_GATEWAYS[$gw]}" = "0" ]
then
warning "'${gw}' on '${work_table}': Ignoring gateway '${gw}', it is not alive."
return 1
fi
if [ ! -s "${LB_DIR}/gateway.${gw}.paths" ]
then
warning "'${gw}' on '${work_table}': Gateway '${gw}' does not have any routes."
return 1
fi
local context="default"
if [ "${weight}" = "0" ]
then
local context="fallback"
local weight="100"
local count=${LB_TABLES_FALLBACK[${work_table}]}
else
local count=${LB_TABLES[${work_table}]}
fi
while read
do
count=$[count + 1]
echo "nexthop ${REPLY} weight ${weight}" >>"${LB_DIR}/table.${work_table}.${context}.${LB_IPV}"
done <"${LB_DIR}/gateway.${gw}.paths"
if [ "${context}" = "default" ]
then
LB_TABLES[${work_table}]=$count
else
LB_TABLES_FALLBACK[${work_table}]=$count
fi
}
lb_diff_route() {
local cmd="$1"; shift
local table="$1"; shift
case "${cmd}" in
add)
action $IP_CMD -${LB_IPV} route add table ${table} "${@}" || action $IP_CMD -${LB_IPV} route append table ${table} "${@}"
return $?
;;
delete)
action $IP_CMD -${LB_IPV} route del table ${table} "${@}"
return $?
;;
same)
return 0
;;
esac
return 1
}
update_table() {
printf >&2 "\n -----------------------------------------------\n"
DEBUGCMD "${FUNCNAME}" "${#FUNCNAME[*]}" "${@}"
LB_IPV="${1}"; shift
local table="${1}"; shift
# check IPvX is as it should
if [ ! "${LB_TABLES_IPV[${table}]}" = "${LB_IPV}" -a ! "${LB_TABLES_IPV[${table}]}" = "46" ]
then
error "${FUNCNAME} INTERNAL ERROR: Inheritted IPv${LB_IPV} but table is IPv${LB_TABLES_IPV[${work_table}]}."
exit 1
fi
# ---------------------------------------------------------------------
# unit testing - make sure it works right
if [ -h "${LB_DIR}/table.${table}.origin_routes.${LB_IPV}" -a ! -f "${LB_DIR}/table.${table}.origin_routes.${LB_IPV}" ]
then
error "INTERNAL ERROR: table.${table}.origin_routes.${LB_IPV} is an unresolvable link!"
exit 1
fi
if [ -h "${LB_DIR}/table.${table}.origin_default.${LB_IPV}" -a ! -f "${LB_DIR}/table.${table}.origin_default.${LB_IPV}" ]
then
error "INTERNAL ERROR: table.${table}.origin_default.${LB_IPV} is an unresolvable link!"
exit 1
fi
# ---------------------------------------------------------------------
# decide what will be done for this table
# 1. if 'table.${table}.origin_routes' exists, we copy the routing table
# 2. if 'table.${table}.default' or 'table.${table}.origin_default' exist, we set the default gateway
# 3. if none of the above is present, we do nothing
local routing_filter_cmd="$CAT_CMD"
if [ -f "${LB_DIR}/table.${table}.origin_routes.${LB_IPV}" -a \( -f "${LB_DIR}/table.${table}.default.${LB_IPV}" -o -f "${LB_DIR}/table.${table}.fallback.${LB_IPV}" -o -f "${LB_DIR}/table.${table}.origin_default.${LB_IPV}" \) ]
then
# we do both routes copy and set default gateway
routing_filter_cmd="$CAT_CMD"
elif [ -f "${LB_DIR}/table.${table}.default.${LB_IPV}" -o -f "${LB_DIR}/table.${table}.origin_default.${LB_IPV}" ]
then
# we should only process default gateway
routing_filter_cmd="$GREP_CMD ^default"
elif [ -f "${LB_DIR}/table.${table}.origin_routes.${LB_IPV}" ]
then
# we should only copy the routing table without the default gateway
routing_filter_cmd="$GREP_CMD -v ^default"
else
# process both routes and default gateway
routing_filter_cmd="$CAT_CMD"
# commented due to issue 78
# nothing to be done - just return
#echo >&2 -e " ${COLOR_GREEN}${COLOR_BOLD}Nothing to be done for table ${table}${COLOR_RESET}"
#return 0
fi
# ---------------------------------------------------------------------
# get the existing routing table
get_existing_routing_table "${table}" >"${LB_DIR}/table.${table}.existing_routing_table.${LB_IPV}"
# if we don't have origin, assume the existing is the origin
if [ ! -f "${LB_DIR}/table.${table}.origin_routes.${LB_IPV}" ]
then
$CAT_CMD "${LB_DIR}/table.${table}.existing_routing_table.${LB_IPV}" |\
$GREP_CMD -v ^default >"${LB_DIR}/table.${table}.origin_routes.${LB_IPV}"
fi
if [ ! -f "${LB_DIR}/table.${table}.origin_default.${LB_IPV}" ]
then
$CAT_CMD "${LB_DIR}/table.${table}.existing_routing_table.${LB_IPV}" |\
$GREP_CMD -v ^default >"${LB_DIR}/table.${table}.origin_default.${LB_IPV}"
fi
$CAT_CMD "${LB_DIR}/table.${table}.existing_routing_table.${LB_IPV}" |\
${routing_filter_cmd} >"${LB_DIR}/table.${table}.existing_routes.${LB_IPV}"
# ---------------------------------------------------------------------
# generate the new routing table
local context="default"
local gateways=${LB_TABLES[${table}]}
if [ ${gateways} -eq 0 ]
then
# there are no active gateways
# do we have a fallback?
if [ ${LB_TABLES_FALLBACK[${table}]} -gt 0 ]
then
# we have a fallback
local gateways=${LB_TABLES_FALLBACK[${table}]}
local context="fallback"
echo >&2 " USING fallback gateways!"
syslog "Using fallback gateways for table ${table}."
fi
fi
(
$CAT_CMD "${LB_DIR}/table.${table}.origin_routes.${LB_IPV}"
if [ ${gateways} -eq 0 ]
then
# we don't have a gateway alive
# copy the origin default gateway, if we have to
$CAT_CMD "${LB_DIR}/table.${table}.origin_default.${LB_IPV}"
elif [ ${gateways} -eq 1 ]
then
# we only have one gateway alive
printf "default "
$CAT_CMD "${LB_DIR}/table.${table}.${context}.${LB_IPV}" |\
$SED_CMD -e "s/nexthop //g" -e "s/ weight [0-9]\+//g"
else
# we have many (2+) gateways alive
printf "default "
$CAT_CMD "${LB_DIR}/table.${table}.${context}.${LB_IPV}" |\
$TR_CMD '\n' ' '
fi
) | trim_spaces |\
$SORT_CMD -u >"${LB_DIR}/table.${table}.new_routing_table.${LB_IPV}"
# keep the new routes of our dependands
$CAT_CMD "${LB_DIR}/table.${table}.new_routing_table.${LB_IPV}" |\
$GREP_CMD -v ^default >"${LB_DIR}/table.${table}.routes.${LB_IPV}"
$CAT_CMD "${LB_DIR}/table.${table}.new_routing_table.${LB_IPV}" |\
$GREP_CMD ^default >"${LB_DIR}/table.${table}.default.${LB_IPV}"
# generate the new routing table for diff
$CAT_CMD "${LB_DIR}/table.${table}.new_routing_table.${LB_IPV}" |\
${routing_filter_cmd} >"${LB_DIR}/table.${table}.new_routes.${LB_IPV}"
# ---------------------------------------------------------------------
# diff magic...
# generate a script to process the diffs of the two files
#echo "EXISTING"
#cat "${LB_DIR}/table.${table}.existing_routes.${LB_IPV}"
#echo "NEW filter: ${routing_filter_cmd}"
#cat "${LB_DIR}/table.${table}.new_routes.${LB_IPV}"
$DIFF_CMD >"${LB_DIR}/table.${table}.script.${LB_IPV}" \
--old-line-format="lb_diff_route delete ${table} %L" \
--new-line-format="lb_diff_route add ${table} %L" \
--unchanged-line-format="lb_diff_route same ${table} %L" \
"${LB_DIR}/table.${table}.existing_routes.${LB_IPV}" \
"${LB_DIR}/table.${table}.new_routes.${LB_IPV}"
if [ $? -eq 1 ]
then
source "${LB_DIR}/table.${table}.script.${LB_IPV}"
LB_ALTERED[table.${table}]=1
local def=0
test ! -z "`$GREP_CMD " default " "${LB_DIR}/table.${table}.script.${LB_IPV}"`" && def=1
echo >&2
echo >&2 -e " ${COLOR_YELLOW}${COLOR_BOLD}Updated routing table ${table}!${COLOR_RESET}"
updated_routes "${table}" "${def}"
else
echo >&2
echo >&2 -e " ${COLOR_GREEN}${COLOR_BOLD}Table's ${table} routing is already as it should.${COLOR_RESET}"
echo >&2
fi
}
finalize_tables() {
printf >&2 "\n-------------------------------------------------------------------------------\n"
DEBUGCMD "${FUNCNAME}" "${#FUNCNAME[*]}" "${@}"
LB_IPV="${1}"
if [ -z "${LB_IPV}" ]
then
error "INTERNAL ERROR: Detected empty LB_IPV."
exit 1
fi
# put all the tables in this variable
local -A all=()
local x=
for x in "${!LB_ORIGIN_TABLES[@]}"
do
test "${LB_ORIGIN_TABLES[$x]}" = "${LB_IPV}" -o "${LB_ORIGIN_TABLES[$x]}" = "46" && all[$x]=1
done
for x in "${!LB_TABLES[@]}"
do
test "${LB_TABLES_IPV[$x]}" = "${LB_IPV}" -o "${LB_TABLES_IPV[$x]}" = "46" && all[$x]=1
done
# execute the tables with circular dependency check
while [ "${#all[@]}" -ne 0 ]
do
local found=0
for x in "${!all[@]}"
do
printf >&2 "\n > Checking dependency for IPv${LB_IPV} table ${x}... "
if [ ! -h "${LB_DIR}/table.${x}.origin_routes.${LB_IPV}" -o -f "${LB_DIR}/table.${x}.origin_routes.${LB_IPV}" ]
then
if [ ! -z "${LB_TABLES[$x]}" ]
then
printf >&2 "READY!\n"
update_table "${LB_IPV}" "${x}"
else
printf >&2 "ORIGIN ONLY\n"
get_existing_routing_table "${x}" >"${LB_DIR}/table.${x}.existing_routing_table.${LB_IPV}" || exit 1
$CAT_CMD "${LB_DIR}/table.${x}.existing_routing_table" |\
$GREP_CMD -v ^default >"${LB_DIR}/table.${x}.routes.${LB_IPV}"
$CAT_CMD "${LB_DIR}/table.${x}.existing_routing_table" |\
$GREP_CMD ^default >"${LB_DIR}/table.${x}.default.${LB_IPV}"
fi
unset all[$x]
local found=1
break
else
printf >&2 "NOT READY\n"
fi
done
if [ $found -eq 0 ]
then
error "CIRCULAR DEPENDENCY among the tables detected."
exit 1
fi
done
}
# this is to overwritten by the caller
# it is called only when routing rules are updated (added/removed)
updated_routes() {
local table="${1}"
local def="${2}" # if this is 1, the default route has been updated
return 0
}
# -----------------------------------------------------------------------------
# policy based routing - rules section
LB_DO_RULES_IPV4=0
LB_DO_RULES_IPV6=0
LB_RULES_DEFAULT_IPV=${LB_DEFAULT_IPV}
policy4() { policy ipv4 "${@}"; }
policy6() { policy ipv6 "${@}"; }
policy() {
printf >&2 "\n-------------------------------------------------------------------------------\n"
DEBUGCMD "${FUNCNAME}" "${#FUNCNAME[*]}" "${@}"
LB_RULES_DEFAULT_IPV=${LB_DEFAULT_IPV}
while [ ! -z "${1}" ]
do
case "${1}" in
ipv4)
LB_RULES_DEFAULT_IPV=4
;;
ipv6)
LB_RULES_DEFAULT_IPV=6
;;
*)
error "${FUNCNAME}: Cannot understand option '${1}'."
exit 1
;;
esac
shift
done
if [ "${LB_RULES_DEFAULT_IPV}" = "6" ]
then
LB_DO_RULES_IPV6=1
else
LB_DO_RULES_IPV4=1
fi
$TOUCH_CMD "${LB_DIR}/rules.${LB_RULES_DEFAULT_IPV}"
}
LB_RULE_BASE4=1000
LB_RULE_BASE6=1000
rules4() { rules ipv4 "${@}"; }
rules6() { rules ipv6 "${@}"; }
rules() {
DEBUGCMD "${FUNCNAME}" "${#FUNCNAME[*]}" "${@}"
LB_IPV=${LB_RULES_DEFAULT_IPV}
local -a tos=()
local -a mark=()
local -a src=()
local -a dst=()
local -a inface=()
local marktype=
local action=
local table=
local not=
local cmd=
while [ ! -z "${1}" ]
do
case "${1}" in
ipv4)
LB_IPV=4
LB_DO_RULES_IPV4=1
$TOUCH_CMD "${LB_DIR}/rules.${LB_IPV}"
;;
ipv6)
LB_IPV=6
LB_DO_RULES_IPV6=1
$TOUCH_CMD "${LB_DIR}/rules.${LB_IPV}"
;;
at|lookup|table)
local action="lookup ${2}"
local table="${2}"
shift
;;
# nat) # this converts the rule to 'masquerade' and gives a warning it is deprecated
prohibit|unreachable) # 'reject' gives: Error: argument "reject" is wrong: Failed to parse rule type
local action="${1}"
;;
rawmark)
local marktype=
local cmd="mark"
;;
mark)
local marktype="usermark"
local cmd="mark"
;;
custommark)
local marktype="${2}"; shift
local cmd="mark"
if [ -z "${MARKS_MASKS[$marktype]}" ]
then
error "Mark type '${marktype}' is not defined."
exit 1
fi
;;
connmark)
local marktype="connmark"
local cmd="mark"
;;
src|from)
local cmd="src"
;;
inface|iif)
local cmd="inface"
;;
dst|to)
local cmd="dst"
;;
tos)
local cmd="tos"
;;
not)
local not="not"
;;
gw-src-ips)
local x=${2}; shift
if [ -z "${LB_GATEWAYS[$x]}" ]
then
error "Cannot find gateway '${x}'."
exit 1
fi
if [ ! "${LB_GATEWAYS_IPV[$x]}" = "${LB_IPV}" ]
then
error "Cannot add IPv${LB_GATEWAYS_IPV[$x]} gateway source IPs to IPv${LB_IPV} rules."
exit 1
fi
if [ "${LB_GATEWAYS[$x]}" -eq 0 ]
then
warning "Ignoring rules for gateway '${x}'. Gateway '${x}' is not alive."
else
src+=(`loadfile "${LB_DIR}/gateway.${x}.source-ips"`)
fi
;;
loadfile)
case "${cmd}" in
src) src+=(`loadfile "${2}"`);;
dst) dst+=(`loadfile "${2}"`);;
esac
shift
;;
*)
case "${cmd}" in
mark)
if [ -z "${marktype}" ]
then
mark+=( ${1//,/ } )
else
mark+=( $(mark_value $marktype ${1//,/ }) )
fi
;;
tos) tos+=( ${1//,/ } );;
src) src+=( ${1//,/ } );;
dst) dst+=( ${1//,/ } );;
inface) inface+=( ${1//,/ } );;
esac
;;
esac
shift
done
local base=
if [ "${LB_IPV}" = "6" ]
then
local base=${LB_RULE_BASE6}
LB_DO_RULES_IPV6=1
else
local base=${LB_RULE_BASE4}
LB_DO_RULES_IPV4=1
fi
$TOUCH_CMD "${LB_DIR}/rules.${LB_IPV}"
# give some space between rules
base=$[ ( (base + LB_RULE_STEP) / LB_RULE_STEP ) * LB_RULE_STEP ]
# put it back, in case we return before the end
if [ "${LB_IPV}" = "6" ]
then
LB_RULE_BASE6=${base}
else
LB_RULE_BASE4=${base}
fi
if [ "${#mark[*]}${#tos[*]}${#src[*]}${#dst[*]}${#inface[*]}" = "00000" ]
then
warning "No rules generated."
return 0
fi
if [ ! -z "${table}" ]
then
if [ -z "${LB_TABLES[$table]}" ]
then
error "${FUNCNAME}: table $table does not exist."
exit 1
fi
fi
test ${#mark[@]} -eq 0 && mark=('any')
test ${#tos[@]} -eq 0 && tos=('any')
test ${#src[@]} -eq 0 && src=('any')
test ${#dst[@]} -eq 0 && dst=('any')
test ${#inface[@]} -eq 0 && inface=('any')
local m=
local t=
local s=
local d=
local i=
for m in ${mark[@]}
do
test "${m}" = "any" && local m=
test ! -z "${m}" && local m="fwmark ${m}"
for t in ${tos[@]}
do
test "${t}" = "any" && local t=
test ! -z "${t}" && local t="tos ${t}"
for i in ${inface[@]}
do
test "${i}" = "any" && local i=
test ! -z "${i}" && local d="iif ${i}"
for s in ${src[@]}
do
test "${s}" = "any" && local s="all"
test ! -z "${s}" && local s="from ${s}"
for d in ${dst[@]}
do
test "${d}" = "any" && local d=
test ! -z "${d}" && local d="to ${d}"
echo "priority ${base} ${not} ${s} ${d} ${t} ${m} ${i} ${action}" >>"${LB_DIR}/rules.${LB_IPV}"
base=$[base + 1]
done
done
done
done
done
# put the priority back to the global variable
if [ "${LB_IPV}" = "6" ]
then
LB_RULE_BASE6=${base}
else
LB_RULE_BASE4=${base}
fi
}
mark() { rules ${FUNCNAME} "${@}"; }
fwmark() { rules ${FUNCNAME} "${@}"; }
connmark() { rules ${FUNCNAME} "${@}"; }
custommark() { rules ${FUNCNAME} "${@}"; }
rawmark() { rules ${FUNCNAME} "${@}"; }
src() { rules ${FUNCNAME} "${@}"; }
dst() { rules ${FUNCNAME} "${@}"; }
from() { rules ${FUNCNAME} "${@}"; }
to() { rules ${FUNCNAME} "${@}"; }
tos() { rules ${FUNCNAME} "${@}"; }
not() { rules ${FUNCNAME} "${@}"; }
# function to be called by the diff generated script
lb_diff_rule() {
local cmd="$1"; shift
case "${cmd}" in
add)
action $IP_CMD -${LB_IPV} rule add "${@}"
return $?
;;
delete)
action $IP_CMD -${LB_IPV} rule del "${@}"
return $?
;;
same)
return 0
;;
esac
return 1
}
normalize_rules() {
$SED_CMD -e "s| fwmark 0x\([0-9a-fA-F]\)\([ /]\)| fwmark 0x0\1\2|g" \
-e "s| tos 0x\([0-9a-fA-F]\) | tos 0x0\1|g" \
-e "s| tos 0x02 | tos mincost |g" \
-e "s| tos 0x04 | tos reliability |g" \
-e "s| tos 0x08 | tos throughput |g" \
-e "s| tos 0x0a | tos lowdelay |g" \
-e "s| tos 2 | tos mincost |g" \
-e "s| tos 4 | tos reliability |g" \
-e "s| tos 8 | tos throughput |g" \
-e "s| tos 10 | tos lowdelay |g" \
-e "s|0x0\+|0x|g"
}
finalize_rules() {
printf >&2 "\n-------------------------------------------------------------------------------\n"
DEBUGCMD "${FUNCNAME}" "${#FUNCNAME[*]}" "${@}"
LB_IPV="${1}"
if [ -z "${LB_IPV}" ]
then
error "INTERNAL ERROR: Detected empty LB_IPV."
exit 1
fi
# copy the tables and remove 'main' from it
local string=$(declare -p LB_TABLES)
eval "local -A tables=${string#*=}"
unset tables[main]
# get all the rules for all the tables except 'main'
run $IP_CMD -${LB_IPV} rule show |\
trim_spaces |\
$EGREP_CMD -v "^(0|32766|32767)" |\
$SED_CMD "s/\(^[0-9]\+\): /priority \1 /g" |\
normalize_rules |\
$SORT_CMD >"${LB_DIR}/rules.existing.${LB_IPV}"
# fix the generated rules, from the rules()
$CAT_CMD "${LB_DIR}/rules.${LB_IPV}" |\
trim_spaces |\
normalize_rules |\
$SORT_CMD -u >"${LB_DIR}/rules.new.${LB_IPV}"
# diff magic...
# generate a script to process the diffs of the two files
$DIFF_CMD >"${LB_DIR}/rules.script.${LB_IPV}" \
--old-line-format="lb_diff_rule delete ${table} %L" \
--new-line-format="lb_diff_rule add ${table} %L" \
--unchanged-line-format="lb_diff_rule same ${table} %L" \
"${LB_DIR}/rules.existing.${LB_IPV}" \
"${LB_DIR}/rules.new.${LB_IPV}"
if [ $? -eq 1 ]
then
source "${LB_DIR}/rules.script.${LB_IPV}"
LB_ALTERED[routing.rules.ipv${LB_IPV}]=1
echo >&2
echo >&2 -e " ${COLOR_YELLOW}${COLOR_BOLD}Updated IPv${LB_IPV} routing rules!${COLOR_RESET}"
updated_rules
else
echo >&2
echo >&2 -e " ${COLOR_GREEN}${COLOR_BOLD}Nothing to be done for IPv${LB_IPV} routing rules.${COLOR_RESET}"
fi
}
# this is to overwritten by the caller
# it is called only when routing rules are updated (added/removed)
updated_rules() {
return 0
}
# -----------------------------------------------------------------------------
# other functions
# this tries to find all the IPs of an Autonomous System (AS).
# 1. it requires an IP from the caller.
# 2. it queries whois for this IP to find its AS.
# 3. it queries RIPE to get all the IP address space for this AS.
asips() {
common_require_cmd $PROGRAM_FILE WHOIS_CMD
common_require_cmd $PROGRAM_FILE JQ_CMD
common_require_cmd $PROGRAM_FILE HEAD_CMD
common_require_cmd $PROGRAM_FILE WGET_CMD
local ip="${1}"
if [ -z "${ip}" ]
then
echo >&2 "${FUNCNAME} requires an IP address."
echo >&2 "Commands: 'whois' and 'jq' have to be installed for this to work."
return 1
fi
echo >&2
echo >&2 "Querying whois for IP ${ip}..."
local as="`$WHOIS_CMD "${1}" | $GREP_CMD "origin:" | trim_spaces | $CUT_CMD -d ' ' -f 2 | $GREP_CMD ^AS | $HEAD_CMD -n 1`"
if [ -z "${as}" ]
then
echo >&2 "Cannot find the Autonomous System of IP '${ip}'."
return 1
fi
echo >&2 "IP ${ip} is part of Autonomous System: ${as}."
echo >&2
echo >&2 "Querying RIPE for ${as}..."
$WGET_CMD -O - "https://stat.ripe.net/data/as-routing-consistency/data.json?resource=${as}" |\
$JQ_CMD .data.prefixes[].prefix |\
$SED_CMD -e 's| ||g' -e 's|"||g' |\
$EGREP_CMD "^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/[0-9]+" |\
$IPRANGE_CMD
}
# -----------------------------------------------------------------------------
# help
emit_version() {
$CAT_CMD <<EOF
FireHOL LinkBalancer $VERSION
(C) Copyright 2015 Costa Tsaousis <costa@tsaousis.gr>
(C) Copyright 2015 Phil Whineray <phil@firehol.org>
FireHOL is distributed under the GPL v2+.
Home Page: http://firehol.org
-------------------------------------------------------------------------
Get notified of new FireHOL releases by subscribing to the mailing list:
http://lists.firehol.org/mailman/listinfo/firehol-support/
-------------------------------------------------------------------------
EOF
}
help() {
$CAT_CMD <<EOFHELP
Modes:
${PROGRAM_FILE} boot [seconds]
- default 'seconds' is 300 (run every 5 mins)
Runs LinkBalancer in a screen session in the background.
To see it running run: 'screen -r link-balancer'
To exit screen without stopping it, press: Control-a then d.
${PROGRAM_FILE} loop [seconds]
like above, but run in foreground, not in a screen session.
${PROGRAM_FILE} -h|--help
Present this help file.
${PROGRAM_FILE} [-t|--test] [-f|--fast] [-d|--debug]
-t or --test
Do not alter anything on the system.
Just show what would have been done.
-f or --fast
Assume all gateways are up, if their interfaces are up.
This should be run from '/etc/ppp/ip-down' to quickly
restore the default gateways of the system.
-d or --debug
At the end of the execution, output all temporary
files.
EOFHELP
$CAT_CMD >"${LB_CONFIG}.example" <<EOFCONFIG
# Link Balancer Configuration file
# default is IPv4.
# You can change the default to IPv6 by setting this to "6":
LB_DEFAULT_IPV="4"
# You can also specify IPv4 or IPv6 for gateways, tables and policy by
# using gateway4 gateway6 table4 table6 policy4 policy6
# If you use these without the number, the default above will be used.
# You can also run 'ipv4 gateway ...' or 'ipv4 gateway ...'
# for all these statements.
# -----------------------------------------------------------------------------
# define all the gateways
# TEMPLATE:
#
# gateway GATEWAY_NAME dev DEVICE [gw "IPs"] [src "IPs"] [check METHOD "IPs" ]
#
# - DEVICE is the interface via which the gateway is accessible
# - gw IP is autodetected if not given
# - src IP is autodetected if not given
# - METHODs: ping traceroute alwayson (default is ping)
# - when check IPs is empty the gw IPs are used
#
# In general
# - for point to point PPP interfaces everything is autodetected
# - for point to lan PPP interfaces, you have to give gw IP and src IP
# - for eth devices you have to specify the gw IP
#
# Hint: If you use PPP devices, specify in the peers file of pppd the
# unit number you want it to use. This way, you will know that, for example,
# you office VPN is always at ppp14.
# Another way is to rename the ppp device in /etc/ppp/ip-up, so that
# instead of having 'ppp14', you will have the interface as 'work1'.
#
# examples:
# wan providers
gateway dsl1 dev ppp11
gateway dsl2 dev eth3 gw 2.2.2.2
gateway dsl3 dev eth4 gw 3.3.3.3
# dual path office VPN
gateway work1 dev ppp14 check 172.16.1.2
gateway work2 dev tun0 check 172.16.1.4
# -----------------------------------------------------------------------------
# define all the routing tables
# Tables can be defined using numbers from 1 to 252.
# Do not use tables 0, 253, 254 and 255. They are system tables.
# You can give names to tables by editing /etc/iproute2/rt_tables
# If you give names, you can use them here.
# TEMPLATE:
#
# table NAME [from ORIGIN_TABLE_NAME] [nodefault]
#
# - NAME and ORIGIN_TABLE_NAME can either be IDs or names
# - The default ORIGIN_TABLE_NAME is: main
# - nodefault means in case the table is left without a default gateway,
# do not copy the default gateway of the ORIGIN_TABLE_NAME
#
# then for each table, define the default gateways you want:
#
# default via GATEWAY_NAME [weight 0-250]
#
# - GATEWAY_NAME is the name of the gateway as given in the gateways section.
# - default weight is 100. Weights can be given as 0 to 255.
# - weight 0 is special. It means don't use this unless there is no
# other gateway available.
#
# you can add as many default gateways per table, as you need.
#
# You should define one table per gateway and one for load-balancing the
# gateways. You need one table per gateway, so that if you receive requests
# from the gateways, you will respond to these requests via the same path.
#
# examples:
# one table per WAN gateway
table dsl1
default via dsl1
table dsl2
default via dsl2
table dsl3
default via dsl3
# this is the main system routing table
table main
default via dsl1 weigth 250
default via dsl2 weight 150
default via dsl3 weight 50
# one table per WORK gateway
table work1 from WORK # we copy WORK here, not 'main'
default via work1
table work2 from WORK # we copy WORK here, not 'main'
default via work2
# this is based on 'main'
# but we don't need its default gateway
table WORK nodefault
default via work1 weight 100
default via work2 weight 50
# -----------------------------------------------------------------------------
# define rules for selecting tables
policy
# define a mark for each gateway table.
# this makes packets originated from them to be routed back to them
connmark 0x1 table dsl1
connmark 0x2 table dsl2
connmark 0x3 table dsl3
connmark 0x11 table work1
connmark 0x12 table work2
# You will have to add these connmark rules at the top of your firehol.conf:
# connmark 0x1 interface ppp11
# connmark 0x2 interface eth3
# connmark 0x3 interface eth4
# connmark 0x11 interface ppp14
# connmark 0x12 interface tun0
# Now you can add policy routing rules.
# TEMPLATE:
# rules [not] [from|src IPs] [to|dst IPs] [mark X] [tos X] table TABLE_NAME
#
# example:
# clients at 192.168.1.0 use only dsl1
rules src 192.168.1.0/24 table dsl1
# web server at 1.2.3.4 is routed only via dsl2
rules dst 1.2.3.4 table dsl2
# You can use LinkBalancer to find all the IP address space of your upstream
# providers:
# Run:
#
# ${PROGRAM_FILE} asips ONE_IP_OF_YOUR_PROVIDER
#
# LinkBalancer will query RIPE to find all the IPs of your provider.
# So you can add:
rules dst IPS_OF_PROVIDER_1 table dsl1
rules dst IPS_OF_PROVIDER_2 table dsl2
rules dst IPS_OF_PROVIDER_3 table dsl3
# You can also have all the IPs in separate files:
# Run:
#
# ${PROGRAM_FILE} asips ONE_IP_OF_YOUR_PROVIDER_1 >$SYSCONFDIR/firehol/PROVIDER1_IPS
#
# Then:
rules dst loadfile PROVIDER1_IPS table dsl1
# Last, you may want to route local traffic to the proper table.
# This is not required through.
# It will only help if you run daemons that bind to specific IPs
# and you want them to be routed based on these IPs.
rules src gw-src-ips dsl1 table dsl1
rules src gw-src-ips dsl2 table dsl2
rules src gw-src-ips dsl3 table dsl3
rules src gw-src-ips work1 table work1
rules src gw-src-ips work2 table work2
EOFCONFIG
echo >&2 "Saved ${LB_CONFIG}.example for your convinience..."
}
# -----------------------------------------------------------------------------
# options
emit_version
while [ ! -z "$1" ]
do
case "$1" in
-h|--help)
help
exit 0
;;
-t|--test)
DRY_RUN=1
;;
-f|--fast)
FORCE_ALWAYSON=1
;;
-d|--debug)
DEBUG_ALL_DATA=1
;;
asips)
shift
asips "${@}"
LB_RESULT_CODE=$?
exit ${LB_RESULT_CODE}
;;
*)
echo >&2 "Unknown option '$1'"
exit 1
;;
esac
shift
done
syslog "Started"
if [ ! -f "${LB_CONFIG}" ]
then
echo >&2 "Cannot find config file '${LB_CONFIG}'"
exit 1
fi
printf >&2 "\n\n###############################################################################\n"
printf >&2 "PROCESSING CONFIGURATION\n"
# process the script
enable -n trap # Disable the trap buildin shell command.
{ source "${LB_CONFIG}"; }
ret=$?
enable trap
if [ $ret -ne 0 ]
then
error "Preprocessing of '${LB_CONFIG}' failed with code $ret. No changes made to your system."
exit 1
fi
# apply the changes
printf >&2 "\n\n###############################################################################\n"
printf >&2 "APPLYING CHANGES\n"
test -f "${LB_DIR}/tables.4" && finalize_tables 4
test -f "${LB_DIR}/tables.6" && finalize_tables 6
test ${LB_DO_RULES_IPV4} -eq 1 && finalize_rules 4
test ${LB_DO_RULES_IPV6} -eq 1 && finalize_rules 6
LB_RESULT_CODE=0
# all done!
echo >&2
test ! -z "${!LB_ALTERED[*]}" && warning "Altered: ${!LB_ALTERED[@]}"
# No need to cleanup
# The trap will cleanup the temporary directory
|