1#!/bin/ksh 2# $OpenBSD: l3vpn.sh,v 1.6 2024/10/28 12:11:05 claudio Exp $ 3 4set -e 5 6BGPD=$1 7BGPDCONFIGDIR=$2 8RDOMAIN1=$3 9RDOMAIN2=$4 10PAIR1=$5 11PAIR2=$6 12RDOMAIN3=$7 13RDOMAIN4=$8 14 15RDOMAINS="${RDOMAIN1} ${RDOMAIN2} ${RDOMAIN3} ${RDOMAIN4}" 16IFACES="${PAIR1} ${PAIR2} ${MPE1} ${MPE2}" 17PAIR1IP=10.12.57.1 18PAIR2IP=10.12.57.2 19PAIR1IP6=2001:db8:57::1 20PAIR2IP6=2001:db8:57::2 21 22error_notify() { 23 set -x 24 echo cleanup 25 pkill -T ${RDOMAIN1} bgpd || true 26 pkill -T ${RDOMAIN2} bgpd || true 27 sleep 1 28 ifconfig ${PAIR1} destroy || true 29 ifconfig ${PAIR2} destroy || true 30 ifconfig mpe${RDOMAIN3} destroy || true 31 ifconfig mpe${RDOMAIN4} destroy || true 32 route -qn -T ${RDOMAIN1} flush || true 33 route -qn -T ${RDOMAIN2} flush || true 34 route -qn -T ${RDOMAIN3} flush || true 35 route -qn -T ${RDOMAIN4} flush || true 36 ifconfig lo${RDOMAIN1} destroy || true 37 ifconfig lo${RDOMAIN2} destroy || true 38 ifconfig lo${RDOMAIN3} destroy || true 39 ifconfig lo${RDOMAIN4} destroy || true 40 if [ $1 -ne 0 ]; then 41 echo FAILED 42 exit 1 43 else 44 echo SUCCESS 45 fi 46} 47 48if [ "$(id -u)" -ne 0 ]; then 49 echo need root privileges >&2 50 exit 1 51fi 52 53trap 'error_notify $?' EXIT 54 55echo check if rdomains are busy 56for n in ${RDOMAINS}; do 57 if /sbin/ifconfig | grep -v "^lo${n}:" | grep " rdomain ${n} "; then 58 echo routing domain ${n} is already used >&2 59 exit 1 60 fi 61done 62 63echo check if interfaces are busy 64for n in ${IFACES}; do 65 /sbin/ifconfig "${n}" >/dev/null 2>&1 && \ 66 ( echo interface ${n} is already used >&2; exit 1 ) 67done 68 69set -x 70 71echo setup 72ifconfig ${PAIR1} rdomain ${RDOMAIN1} ${PAIR1IP}/29 mpls up 73ifconfig ${PAIR2} rdomain ${RDOMAIN2} ${PAIR2IP}/29 mpls up 74ifconfig ${PAIR1} inet6 ${PAIR1IP6}/64 75ifconfig ${PAIR2} inet6 ${PAIR2IP6}/64 76ifconfig ${PAIR1} patch ${PAIR2} 77ifconfig lo${RDOMAIN1} inet 127.0.0.1/8 78ifconfig lo${RDOMAIN2} inet 127.0.0.1/8 79ifconfig mpe${RDOMAIN3} rdomain ${RDOMAIN3} mplslabel 42 192.168.237.242/32 80ifconfig mpe${RDOMAIN4} rdomain ${RDOMAIN4} mplslabel 44 192.168.237.244/32 81ifconfig mpe${RDOMAIN3} inet6 2001:db8:242::242/64 82ifconfig mpe${RDOMAIN4} inet6 2001:db8:244::244/64 83ifconfig lo${RDOMAIN3} inet 127.0.0.1/8 84ifconfig lo${RDOMAIN4} inet 127.0.0.1/8 85 86echo run bgpds 87route -T ${RDOMAIN1} exec ${BGPD} \ 88 -v -f ${BGPDCONFIGDIR}/bgpd.l3vpn.rdomain1.conf 89route -T ${RDOMAIN2} exec ${BGPD} \ 90 -v -f ${BGPDCONFIGDIR}/bgpd.l3vpn.rdomain2.conf 91 92sleep 1 93route -T ${RDOMAIN1} exec bgpctl nei RDOMAIN2 up 94route -T ${RDOMAIN1} exec bgpctl nei RDOMAIN2v6 up 95sleep 1 96 97echo Check initial networks 98route -T ${RDOMAIN1} exec bgpctl show 99route -T ${RDOMAIN1} exec bgpctl show rib 100route -T ${RDOMAIN1} exec bgpctl show fib table 13 101route -T ${RDOMAIN3} show 102route -T ${RDOMAIN3} get 192.168.44/24 > /dev/null 103route -T ${RDOMAIN4} get 192.168.42/24 > /dev/null 104route -T ${RDOMAIN3} get -inet6 2001:db8:42:44::/64 > /dev/null 105route -T ${RDOMAIN4} get -inet6 2001:db8:42:42::/64 > /dev/null 106 107echo Add new network 108route -T ${RDOMAIN2} exec bgpctl network add 192.168.45.0/24 rd 4200000002:14 109route -T ${RDOMAIN2} exec bgpctl network add 2001:db8:42:45::/64 rd 4200000002:14 110sleep 1 111route -T ${RDOMAIN3} get 192.168.45/24 > /dev/null 112route -T ${RDOMAIN3} get -inet6 2001:db8:42:45::/64 > /dev/null 113 114echo Remove new network 115route -T ${RDOMAIN2} exec bgpctl network del 192.168.45.0/24 rd 4200000002:14 116route -T ${RDOMAIN2} exec bgpctl network del 2001:db8:42:45::/64 rd 4200000002:14 117sleep 1 118route -T ${RDOMAIN1} exec bgpctl show rib 119! route -T ${RDOMAIN3} get 192.168.45/24 > /dev/null 120! route -T ${RDOMAIN3} get -inet6 2001:db8:42:45::/64 > /dev/null 121 122exit 0 123