1#!/bin/sh 2# 3# $FreeBSD$ 4# 5 6# PROVIDE: ipfw 7# REQUIRE: ppp 8# KEYWORD: nojailvnet 9 10. /etc/rc.subr 11. /etc/network.subr 12 13name="ipfw" 14desc="Firewall, traffic shaper, packet scheduler, in-kernel NAT" 15rcvar="firewall_enable" 16start_cmd="ipfw_start" 17start_precmd="ipfw_prestart" 18start_postcmd="ipfw_poststart" 19stop_cmd="ipfw_stop" 20status_cmd="ipfw_status" 21required_modules="ipfw" 22extra_commands="status" 23 24set_rcvar_obsolete ipv6_firewall_enable 25 26ipfw_prestart() 27{ 28 if checkyesno dummynet_enable; then 29 required_modules="$required_modules dummynet" 30 fi 31 if checkyesno natd_enable; then 32 required_modules="$required_modules ipdivert" 33 fi 34 if checkyesno firewall_nat_enable; then 35 required_modules="$required_modules ipfw_nat" 36 fi 37 if checkyesno firewall_nat64_enable; then 38 required_modules="$required_modules ipfw_nat64" 39 fi 40 if checkyesno firewall_nptv6_enable; then 41 required_modules="$required_modules ipfw_nptv6" 42 fi 43 if checkyesno firewall_pmod_enable; then 44 required_modules="$required_modules ipfw_pmod" 45 fi 46} 47 48ipfw_start() 49{ 50 local _firewall_type 51 52 if [ -n "${1}" ]; then 53 _firewall_type=$1 54 else 55 _firewall_type=${firewall_type} 56 fi 57 58 # set the firewall rules script if none was specified 59 [ -z "${firewall_script}" ] && firewall_script=/etc/rc.firewall 60 61 if [ -r "${firewall_script}" ]; then 62 /bin/sh "${firewall_script}" "${_firewall_type}" 63 echo 'Firewall rules loaded.' 64 elif [ "`ipfw list 65535`" = "65535 deny ip from any to any" ]; then 65 echo 'Warning: kernel has firewall functionality, but' \ 66 'firewall rules are not enabled.' 67 echo ' All ip services are disabled.' 68 fi 69 70 # Firewall logging 71 # 72 if checkyesno firewall_logging; then 73 echo 'Firewall logging enabled.' 74 ${SYSCTL} net.inet.ip.fw.verbose=1 >/dev/null 75 fi 76 if checkyesno firewall_logif; then 77 ifconfig ipfw0 create 78 echo 'Firewall logging pseudo-interface (ipfw0) created.' 79 fi 80} 81 82ipfw_poststart() 83{ 84 local _coscript 85 86 # Start firewall coscripts 87 # 88 for _coscript in ${firewall_coscripts} ; do 89 if [ -f "${_coscript}" ]; then 90 ${_coscript} quietstart 91 fi 92 done 93 94 # Enable the firewall 95 # 96 if ! ${SYSCTL} net.inet.ip.fw.enable=1 >/dev/null 2>&1; then 97 warn "failed to enable IPv4 firewall" 98 fi 99 if afexists inet6; then 100 if ! ${SYSCTL} net.inet6.ip6.fw.enable=1 >/dev/null 2>&1 101 then 102 warn "failed to enable IPv6 firewall" 103 fi 104 fi 105} 106 107ipfw_stop() 108{ 109 local _coscript 110 111 # Disable the firewall 112 # 113 ${SYSCTL} net.inet.ip.fw.enable=0 >/dev/null 114 if afexists inet6; then 115 ${SYSCTL} net.inet6.ip6.fw.enable=0 >/dev/null 116 fi 117 118 # Stop firewall coscripts 119 # 120 for _coscript in `reverse_list ${firewall_coscripts}` ; do 121 if [ -f "${_coscript}" ]; then 122 ${_coscript} quietstop 123 fi 124 done 125} 126 127ipfw_status() 128{ 129 status=$(sysctl -i -n net.inet.ip.fw.enable) 130 : ${status:=0} 131 if afexists inet6; then 132 status6=$(sysctl -i -n net.inet6.ip6.fw.enable) 133 : ${status6:=0} 134 status=$((${status} + ${status6})) 135 fi 136 if [ ${status} -eq 0 ]; then 137 echo "ipfw is not enabled" 138 exit 1 139 else 140 echo "ipfw is enabled" 141 exit 0 142 fi 143} 144 145load_rc_config $name 146firewall_coscripts="/etc/rc.d/natd ${firewall_coscripts}" 147 148run_rc_command $* 149