diff --git a/defaults/rc.conf b/defaults/rc.conf index d77042f..0eee21a 100644 --- a/defaults/rc.conf +++ b/defaults/rc.conf @@ -25,9 +25,10 @@ generate_internal_dhcpv4_enable='YES' generate_isp_dhcp_enable='YES' generate_rtadvd_conf_enable="YES" -rc_router_gen_dir=${rc_router_dir}/gen +rc_router_gen_dir="${rc_router_dir}/gen" localnet_enable='NO' + rtadvd_pltimedecr='NO' rtadvd_vltimedecr='NO' @@ -38,6 +39,16 @@ dhcpv4_pool_hi="200" rc_router_network_name="Default Network Name" rc_router_ula_prefix_size="48" +fib_setup_enable="YES" +rc_router_fibs="1" + + +route_policy_enable="YES" +route_policy_file="${rc_router_gen_dir}/pf.route-policies.conf" + +allow_hosts_enable="YES" +allow_hosts_rules="${rc_router_gen_dir}/pf.rules.conf" + rtsold_flags="-F" # vim: ft=bash diff --git a/pf-framework.subr b/pf-framework.subr index 559baac..eba5618 100755 --- a/pf-framework.subr +++ b/pf-framework.subr @@ -1,9 +1,3 @@ -_genfile="${route_policy_file}" -if [ -z "${_genfile}" ] -then - _genfile="/dev/fd/1" -fi - __install_route_policy() { iface=$1;shift 1 @@ -42,12 +36,12 @@ _install_route_policy_v4() install_route_policy() { - _install_route_policy internal $* ${_genfile} + _install_route_policy internal $* ${route_policy_file} } install_route_policy_v4() { - _install_route_policy_v4 internal $* ${_genfile} + _install_route_policy_v4 internal $* ${route_policy_file} } @@ -63,7 +57,7 @@ add_route_policy_v4() clear_all_policies() { - echo -n > $_genfile + echo -n > $route_policy_file } # vim: ft=bash diff --git a/rc.d/allow_hosts b/rc.d/allow_hosts new file mode 100755 index 0000000..5812e8a --- /dev/null +++ b/rc.d/allow_hosts @@ -0,0 +1,46 @@ +#!/bin/sh +# +# $FreeBSD$ +# +# PROVIDE: allow_hosts +# REQUIRE: named +# KEYWORD: shutdown +# + +. ${RC_ROUTER_HARNESS}/etc/rc.subr + +name="allow_hosts" +desc="Allow hosts by DNS (from local DNS)" + +start_cmd='allow_hosts_start' +stop_cmd='allow_hosts_stop' +rcvar='allow_hosts_enable' + +load_rc_config 'allow_hosts' + +if [ -z "${allow_hosts_rules}" ] +then + echo "Must have a route policy target file" + exit 1 +fi + + +allow_hosts_start() +{ + echo "Running allow hosts" + echo "include \"${allow_hosts_rules}\"" > /etc/router-conf/gen/pf.rules.conf + pfctl -a allow-hosts -f ${allow_hosts_rules} +} + +allow_hosts_stop() +{ + true +} + + + +################ Epilogue + +run_rc_command "$1" + +# vim: ft=bash diff --git a/rc.d/clear_route_policy b/rc.d/clear_route_policy new file mode 100755 index 0000000..ede629d --- /dev/null +++ b/rc.d/clear_route_policy @@ -0,0 +1,50 @@ +#!/bin/sh +# +# $FreeBSD$ +# +# PROVIDE: clear_route_policy +# BEFORE: pf +# KEYWORD: shutdown +# + +. ${RC_ROUTER_HARNESS}/etc/rc.subr + +name="clear_route_policy" +desc="Policy Based Routing Reset" + +start_cmd='clear_route_policy_start' +stop_cmd='clear_route_policy_stop' +rcvar='route_policy_enable' + +load_rc_config 'route_policy' + +if [ -z "${route_policy_file}" ] +then + echo "Must have a route policy target file" + exit 1 +fi + + +clear_route_policy_start() +{ + echo "" > ${route_policy_file} + pfctl -a allow-hosts -f /dev/null + clear_all_policies + #pfctl -a route-policies -f ${route_policy_file} +} + +clear_route_policy_stop() +{ + echo "" > ${route_policy_file} + pfctl -a allow-hosts -f /dev/null + clear_all_policies + #pfctl -a route-policies -f ${route_policy_file} +} + + + +################ Epilogue + +run_rc_command "$1" + +# vim: ft=bash diff --git a/rc.d/fib_setup b/rc.d/fib_setup new file mode 100755 index 0000000..6a51b0f --- /dev/null +++ b/rc.d/fib_setup @@ -0,0 +1,42 @@ +#!/bin/sh +# +# $FreeBSD$ +# +# PROVIDE: fib_setup +# BEFORE: sysctl +# + +. ${RC_ROUTER_HARNESS}/etc/rc.subr + +name="fib_setup" +desc="Calculate and respect the fib count" + +start_cmd='fib_setup_start' +stop_cmd='fib_setup_stop' +rcvar='fib_setup_enable' + +load_rc_config 'fib_setup' + +fib_setup_start() +{ + echo "Running fib_setup" + fib_count=$(( ${rc_router_fibs} )) + max_fibs=$(( ${fib_count} + 1 )) + + cp ${rc_router_dir}/templates/sysctl.conf ${rc_router_gen_dir}/ + echo "net.fibs=${fib_count}" >> ${rc_router_gen_dir}/sysctl.conf +} + +fib_setup_stop() +{ + true +} + + + +################ Epilogue + +run_rc_command "$1" + +# vim: ft=bash + diff --git a/rc.d/route_policy b/rc.d/route_policy new file mode 100755 index 0000000..8b499ba --- /dev/null +++ b/rc.d/route_policy @@ -0,0 +1,86 @@ +#!/bin/sh +# +# $FreeBSD$ +# +# PROVIDE: route_policy +# REQUIRE: wireguard +# KEYWORD: shutdown +# + +. ${RC_ROUTER_HARNESS}/etc/rc.subr + +name="route_policy" +desc="Policy Based Routing Installer" + +start_cmd='route_policy_start' +stop_cmd='route_policy_stop' +rcvar='route_policy_enable' + +load_rc_config 'route_policy' + +if [ -z "${route_policy_file}" ] +then + echo "Must have a route policy target file" + exit 1 +fi + + +do_all_routes() +{ + while [ -n "$1" ] + do + install_route_policy $1 + shift 1 + done +} + +do_all_routes_v4() +{ + while [ -n "$1" ] + do + install_route_policy_v4 $1 + shift 1 + done +} + +route_policy_start() +{ + clear_all_policies + echo "# This file is generated." >> ${route_policy_file} + echo "# Do not edit." >> ${route_policy_file} + echo "" >> ${route_policy_file} + + # TODO: Record the policies one-per-line via a helper loop subroutine? + echo "# (Generated with \`${route_policies}\`, at `date`.)" >> ${route_policy_file} + echo "# (Generated with \`${route_policies_v4}\`, at `date`.)" >> ${route_policy_file} + echo "" >> ${route_policy_file} + echo "" >> ${route_policy_file} + + do_all_routes ${route_policies} + do_all_routes_v4 ${route_policies_v4} + + echo "" >> ${route_policy_file} + echo -n "# v" >> ${route_policy_file} + echo "im: ft=pf" >> ${route_policy_file} + + #pfctl -a "route-policies" -f ${route_policy_file} + #pfctl -f ${pf_rules} + + # UGLY hack/workaround because of a kernel panic bug from + # rtable rules in files included in `pf` anchors in FreeBSD 15 + /etc/rc.d/pf reload +} + +route_policy_stop() +{ + clear_all_policies +} + + + + +################ Epilogue + +run_rc_command "$1" + +# vim: ft=bash diff --git a/rc.d/tayga b/rc.d/tayga new file mode 100755 index 0000000..08fffbc --- /dev/null +++ b/rc.d/tayga @@ -0,0 +1,80 @@ +#!/bin/sh +# +# $FreeBSD$ +# +# PROVIDE: tayga +# REQUIRE: SERVERS +# KEYWORD: shutdown +# + +. ${RC_ROUTER_HARNESS}/etc/rc.subr + +name='tayga' + +start_cmd='tayga_start' +stop_cmd='tayga_stop' +rcvar='tayga_enable' + +load_rc_config 'tayga' +pidfile="/var/run/${name}.pid" +command="/usr/local/sbin/${name}" + +# Confirm necessary variables are set +check_vars() +{ + [ -z "$tayga_ipv4_addr" ] && err 3 "Must set tayga_ipv4_addr to the address that the tayga server lives on" + [ -z "$tayga_ipv4_endpoint" ] && err 3 "Must set tayga_ipv4_endpoint to the address that the tunnel lives on" + [ -z "$tayga_ipv4_net" ] && err 3 "Must set tayga_ipv4_net to the network that the tunnel lives on" + [ -z "$tayga_ipv6_endpoint" ] && err 3 "Must set tayga_ipv6_endpoint to the address that the tunnel lives on" + [ -z "$tayga_ipv6_net" ] && err 3 "Must set tayga_ipv6_net to the network that the tunnel provides" +} + +[ -z "$tayga_config" ] && tayga_config="/usr/local/etc/tayga.conf" + +command_args="-p ${pidfile} -c ${tayga_config}" + +[ -z "$tayga_enable" ] && tayga_enable='YES' + +tayga_start() +{ + check_vars + "$command" $command_args + while ! ifconfig 'nat64'; do sleep 1; done + ifconfig 'nat64' inet "${tayga_ipv4_endpoint}/32" "${tayga_ipv4_addr}" + if [ ! -z "${tayga_group}" ] ; then ifconfig 'nat64' group ${tayga_group} ; fi + ifconfig 'nat64' inet6 "${tayga_ipv6_endpoint}/128" + #echo Add route 4 + route -4 add "${tayga_ipv4_net}" -interface 'nat64' + #echo Add route 6 for endpoint from net + #route -6 add "${tayga_ipv6_endpoint}" -iface 'nat64' #-fib 0-7 + route -6 add "${tayga_ipv6_net}" "${tayga_ipv6_endpoint}" #-fib 0-7 + #echo Add route 6 for endpoint subnet + route -6 add "${tayga_ipv6_endpoint}/64" "${tayga_ipv6_endpoint}" #-fib 0-7 + + for prefix in ${tayga_v4_prefixes} + do + route add ${prefix} ${tayga_ipv4_addr} -fib 0-${max_fib} + done +} + +tayga_stop() +{ + if [ -n "$rc_pid" ]; then + check_vars + + for prefix in tayga_v4_prefixes + do + route del ${prefix} ${tayga_ipv4_addr} -fib 0-${max_fib} + done + + echo 'stopping tayga' + kill -2 "${rc_pid}" + route -6 del "${tayga_ipv6_net}" -interface 'nat64' + route -4 del "${tayga_ipv4_net}" -interface 'nat64' + ifconfig 'nat64' destroy + else + echo "${name} is not running." + fi +} + +run_rc_command "$1"