1#!/bin/ksh 2# $OpenBSD: maxprefix.sh,v 1.4 2024/08/28 13:14:39 claudio Exp $ 3 4set -e 5 6BGPD=$1 7BGPDCONFIGDIR=$2 8RDOMAIN1=$3 9RDOMAIN2=$4 10PAIR1=$5 11PAIR2=$6 12 13RDOMAINS="${RDOMAIN1} ${RDOMAIN2}" 14PAIRS="${PAIR1} ${PAIR2}" 15PAIR1IP=10.12.57.1 16PAIR2IP=10.12.57.2 17 18error_notify() { 19 echo cleanup 20 pkill -T ${RDOMAIN1} bgpd || true 21 pkill -T ${RDOMAIN2} bgpd || true 22 sleep 1 23 ifconfig ${PAIR2} destroy || true 24 ifconfig ${PAIR1} destroy || true 25 route -qn -T ${RDOMAIN1} flush || true 26 route -qn -T ${RDOMAIN2} flush || true 27 ifconfig lo${RDOMAIN1} destroy || true 28 ifconfig lo${RDOMAIN2} destroy || true 29 if [ $1 -ne 0 ]; then 30 echo FAILED 31 exit 1 32 else 33 echo SUCCESS 34 fi 35} 36 37if [ "$(id -u)" -ne 0 ]; then 38 echo need root privileges >&2 39 exit 1 40fi 41 42trap 'error_notify $?' EXIT 43 44echo check if rdomains are busy 45for n in ${RDOMAINS}; do 46 if /sbin/ifconfig | grep -v "^lo${n}:" | grep " rdomain ${n} "; then 47 echo routing domain ${n} is already used >&2 48 exit 1 49 fi 50done 51 52echo check if interfaces are busy 53for n in ${PAIRS}; do 54 /sbin/ifconfig "${n}" >/dev/null 2>&1 && \ 55 ( echo interface ${n} is already used >&2; exit 1 ) 56done 57 58set -x 59 60echo setup 61ifconfig ${PAIR1} rdomain ${RDOMAIN1} ${PAIR1IP}/30 up 62ifconfig ${PAIR2} rdomain ${RDOMAIN2} ${PAIR2IP}/30 up 63ifconfig ${PAIR1} patch ${PAIR2} 64ifconfig lo${RDOMAIN1} inet 127.0.0.1/8 65ifconfig lo${RDOMAIN2} inet 127.0.0.1/8 66 67echo test1: run bgpds 68sed -e 's/#MAX-PREFIX#/max-prefix 2/' \ 69 ${BGPDCONFIGDIR}/bgpd.maxprefix.rdomain2.conf > \ 70 ./bgpd.maxprefix.rdomain2.conf 71route -T ${RDOMAIN1} exec ${BGPD} \ 72 -v -f ${BGPDCONFIGDIR}/bgpd.maxprefix.rdomain1.conf 73route -T ${RDOMAIN2} exec ${BGPD} \ 74 -v -f ./bgpd.maxprefix.rdomain2.conf 75sleep 1 76route -T ${RDOMAIN1} exec bgpctl nei RDOMAIN2 up 77sleep 1 78 79echo test1: add two networks 80route -T ${RDOMAIN1} exec bgpctl network add 10.12.58.0/24 81route -T ${RDOMAIN1} exec bgpctl network add 10.12.59.0/24 82sleep 1 83route -T ${RDOMAIN1} exec bgpctl show nei | \ 84 awk '/^ Prefixes/ { if ($2 == "2") { print "ok"; ok=1; exit 0; } } 85 END { if (ok != 1) { print "bad bgpctl output"; exit 2; } }' 86 87echo test1: add another network 88route -T ${RDOMAIN1} exec bgpctl network add 10.12.60.0/24 89sleep 1 90route -T ${RDOMAIN1} exec bgpctl show nei | \ 91 grep '^ Last error received: Cease, received max-prefix exceeded' 92 93echo test1: cleanup 94pkill -T ${RDOMAIN1} bgpd || true 95pkill -T ${RDOMAIN2} bgpd || true 96sleep 1 97 98echo test2: run bgpds 99sed -e 's/#MAX-PREFIX#/max-prefix 10/' \ 100 ${BGPDCONFIGDIR}/bgpd.maxprefix.rdomain2.conf > \ 101 ./bgpd.maxprefix.rdomain2.conf 102route -T ${RDOMAIN1} exec ${BGPD} \ 103 -v -f ${BGPDCONFIGDIR}/bgpd.maxprefix.rdomain1.conf 104route -T ${RDOMAIN2} exec ${BGPD} \ 105 -v -f ./bgpd.maxprefix.rdomain2.conf 106sleep 1 107route -T ${RDOMAIN1} exec bgpctl nei RDOMAIN2 up 108sleep 1 109 110echo test2: add three networks 111route -T ${RDOMAIN1} exec bgpctl network add 10.12.58.0/24 112route -T ${RDOMAIN1} exec bgpctl network add 10.12.59.0/24 113route -T ${RDOMAIN1} exec bgpctl network add 10.12.60.0/24 114sleep 1 115route -T ${RDOMAIN1} exec bgpctl show nei | \ 116 awk '/^ Prefixes/ { if ($2 == "3") { print "ok"; ok=1; exit 0; } } 117 END { if (ok != 1) { print "bad bgpctl output"; exit 2; } }' 118 119echo test2: reload config 120sed -e 's/#MAX-PREFIX#/max-prefix 2/' \ 121 ${BGPDCONFIGDIR}/bgpd.maxprefix.rdomain2.conf > \ 122 ./bgpd.maxprefix.rdomain2.conf 123route -T ${RDOMAIN2} exec bgpctl reload 124sleep 1 125route -T ${RDOMAIN1} exec bgpctl show nei | \ 126 grep '^ Last error received: Cease, received max-prefix exceeded' 127