You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

614 lines
31 KiB

  1. #!/bin/bash
  2. # By Brielle Bruns <bruns@2mbit.com>
  3. # URL: http://www.sosdg.org/freestuff/firewall
  4. # License: GPLv3
  5. #
  6. # Copyright (C) 2009 - 2014 Brielle Bruns
  7. # Copyright (C) 2009 - 2014 The Summit Open Source Development Group
  8. #
  9. # This program is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation, either version 3 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. # iptables_rules_flush (ipv6|ipv4)
  21. # Clear all rules from iptables - be very careful in how this is called as it
  22. # could easily lock out the user from the network. Best way to be safe, is to
  23. # call iptables_policy_reset first then this function.
  24. function iptables_rules_flush {
  25. IP_VERSION=$1
  26. case $IP_VERSION in
  27. ipv6) VER_IPTABLES=${IP6TABLES} ; TABLE_NAMES=/proc/net/ip6_tables_names ;;
  28. ipv4|*) VER_IPTABLES=${IPTABLES} ; TABLE_NAMES=/proc/net/ip_tables_names ;;
  29. esac
  30. ${display} GREEN "Flushing ${IP_VERSION} rules..."
  31. ${VER_IPTABLES} -P INPUT ACCEPT &>/dev/null
  32. ${VER_IPTABLES} -P OUTPUT ACCEPT &>/dev/null
  33. ${VER_IPTABLES} -P FORWARD ACCEPT &>/dev/null
  34. ${VER_IPTABLES} -F &>/dev/null
  35. ${VER_IPTABLES} -X &>/dev/null
  36. ${VER_IPTABLES} -F INPUT &>/dev/null
  37. ${VER_IPTABLES} -F OUTPUT &>/dev/null
  38. ${VER_IPTABLES} -F FORWARD &>/dev/null
  39. #${VER_IPTABLES} -t nat -F &>/dev/null
  40. #${VER_IPTABLES} -t nat -X &>/dev/null
  41. ${VER_IPTABLES} -t mangle -F &>/dev/null
  42. ${VER_IPTABLES} -t mangle -X &>/dev/null
  43. for i in `cat $TABLE_NAMES`; do
  44. ${VER_IPTABLES} -F -t $i &>/dev/null
  45. done
  46. ${VER_IPTABLES} -X &>/dev/null
  47. }
  48. # iptables_policy_set (ipv6|ipv4) (ACCEPT|DROP)
  49. # Sets all policy rules to either ACCEPT or DROP for ipv4 or ipv6
  50. # If no policy given, assume ACCEPT
  51. function default_policy_set {
  52. IP_VERSION=$1
  53. INPOLICY=${2=ACCEPT}
  54. OUTPOLICY=${3=ACCEPT}
  55. FWDPOLICY=${4=ACCEPT}
  56. case $IP_VERSION in
  57. ipv6) VER_IPTABLES=${IP6TABLES} ;;
  58. ipv4|*) VER_IPTABLES=${IPTABLES} ;;
  59. esac
  60. ${display} RED "Setting ${IP_VERSION} policies to INPUT:${INPOLICY} OUTPUT:${OUTPOLICY} FORWARD:${FWDPOLICY}..."
  61. ${VER_IPTABLES} --policy INPUT ${INPOLICY}
  62. ${VER_IPTABLES} --policy OUTPUT ${OUTPOLICY}
  63. ${VER_IPTABLES} --policy FORWARD ${FWDPOLICY}
  64. }
  65. # setup_iptables_chains (ipv4|ipv6)
  66. # Creates the default chains when called
  67. function setup_iptables_chains {
  68. IP_VERSION=$1
  69. case $IP_VERSION in
  70. ipv6) VER_IPTABLES=${IP6TABLES};
  71. IPVER="6" ;;
  72. ipv4|*) VER_IPTABLES=${IPTABLES}
  73. IPVER="4" ;;
  74. esac
  75. # Create the actual chains
  76. ${display} GREEN "Setting up chains for ${IP_VERSION}..."
  77. ${VER_IPTABLES} -N ${InPreRules}
  78. ${VER_IPTABLES} -N ${OutPreRules}
  79. ${VER_IPTABLES} -N ${InEasyBlock}
  80. ${VER_IPTABLES} -N ${OutEasyBlock}
  81. ${VER_IPTABLES} -N ${InFilter}
  82. ${VER_IPTABLES} -N ${OutFilter}
  83. ${VER_IPTABLES} -N ${FwdFilter}
  84. ([[ ${IPVER} == "4" ]] && [[ ${Enablev4NAT} == "yes" ]]) && ${VER_IPTABLES} -N ${NAT} -t nat
  85. ([[ ${IPVER} == "6" ]] && [[ ${Enablev6NAT} == "yes" ]]) && ${VER_IPTABLES} -N ${NAT} -t nat
  86. ([[ ${IPVER} == "4" ]] && [[ ${Enablev4NAT} == "yes" ]]) && ${VER_IPTABLES} -N ${PortForward} -t nat
  87. ([[ ${IPVER} == "6" ]] && [[ ${Enablev6NAT} == "yes" ]]) && ${VER_IPTABLES} -N ${PortForward} -t nat
  88. ${VER_IPTABLES} -N ${InPostRules}
  89. ${VER_IPTABLES} -N ${OutPostRules}
  90. # Set up rules - the order matters - we do it separately here
  91. # for easy viewing of order
  92. if [ -x ${FWCONFIGDIR}/ipv${IPVER}/custom/prerun.sh ]; then . ${FWCONFIGDIR}/ipv${IPVER}/custom/prerun.sh; fi
  93. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} Setting up InPreRules"
  94. ${VER_IPTABLES} -A INPUT -j ${InPreRules}
  95. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} Setting up OutPreRules"
  96. ${VER_IPTABLES} -A OUTPUT -j ${OutPreRules}
  97. if [ -x ${FWCONFIGDIR}/ipv${IPVER}/custom/easyblock.sh ]; then . ${FWCONFIGDIR}/ipv${IPVER}/custom/easyblock.sh; fi
  98. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} Setting up InEasyBlock"
  99. ${VER_IPTABLES} -A INPUT -j ${InEasyBlock}
  100. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} Setting up OutEasyBlock"
  101. ${VER_IPTABLES} -A OUTPUT -j ${OutEasyBlock}
  102. if [ -x ${FWCONFIGDIR}/ipv${IPVER}/custom/filter.sh ]; then . ${FWCONFIGDIR}/ipv${IPVER}/custom/filter.sh; fi
  103. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} Setting up InFilter"
  104. ${VER_IPTABLES} -A INPUT -j ${InFilter}
  105. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} Setting up OutFilter"
  106. ${VER_IPTABLES} -A OUTPUT -j ${OutFilter}
  107. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} Setting up FwdFilter"
  108. ${VER_IPTABLES} -A FORWARD -j ${FwdFilter}
  109. if [ -x ${FWCONFIGDIR}/ipv${IPVER}/custom/nat.sh ]; then . ${FWCONFIGDIR}/ipv${IPVER}/custom/nat.sh; fi
  110. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} Setting up NAT"
  111. ([[ ${IPVER} == "4" ]] && [[ ${Enablev4NAT} == "yes" ]]) && ${VER_IPTABLES} -A POSTROUTING -t nat -j ${NAT}
  112. ([[ ${IPVER} == "6" ]] && [[ ${Enablev6NAT} == "yes" ]]) && ${VER_IPTABLES} -A POSTROUTING -t nat -j ${NAT}
  113. if [ -x ${FWCONFIGDIR}/ipv${IPVER}/custom/portfw.sh ]; then . ${FWCONFIGDIR}/ipv${IPVER}/custom/portfw.sh; fi
  114. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} Setting up PortForward"
  115. ([[ ${IPVER} == "4" ]] && [[ ${Enablev4NAT} == "yes" ]]) && ${VER_IPTABLES} -A PREROUTING -t nat -j ${PortForward}
  116. ([[ ${IPVER} == "6" ]] && [[ ${Enablev6NAT} == "yes" ]]) && ${VER_IPTABLES} -A PREROUTING -t nat -j ${PortForward}
  117. if [ -x ${FWCONFIGDIR}/ipv${IPVER}/custom/postrun.sh ]; then . ${FWCONFIGDIR}/ipv${IPVER}/custom/postrun.sh; fi
  118. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} Setting up InPostRules"
  119. ${VER_IPTABLES} -A INPUT -j ${InPostRules}
  120. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} Setting up OutPostRules"
  121. ${VER_IPTABLES} -A OUTPUT -j ${OutPostRules}
  122. }
  123. function allow_all_loopback {
  124. IP_VERSION=$1
  125. case $IP_VERSION in
  126. ipv6) VER_IPTABLES=${IP6TABLES};
  127. IPVER="6" ;;
  128. ipv4|*) VER_IPTABLES=${IPTABLES}
  129. IPVER="4" ;;
  130. esac
  131. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} loaded"
  132. ${VER_IPTABLES} -A ${InPreRules} -i lo -j ACCEPT
  133. ${VER_IPTABLES} -A ${OutPreRules} -o lo -j ACCEPT
  134. }
  135. function allow_trusted_hosts {
  136. IP_VERSION=$1
  137. case $IP_VERSION in
  138. ipv6) VER_IPTABLES=${IP6TABLES};
  139. IPVER="6" ;;
  140. ipv4|*) VER_IPTABLES=${IPTABLES}
  141. IPVER="4" ;;
  142. esac
  143. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} loading"
  144. if [ -e "${FWCONFIGDIR}/ipv${IPVER}/trusted.conf" ]; then
  145. for i in `grep -v "\#" "${FWCONFIGDIR}/ipv${IPVER}/trusted.conf"`; do
  146. ${VER_IPTABLES} -A ${InPreRules} -s $i -j ACCEPT
  147. ${VER_IPTABLES} -A ${OutPreRules} -d $i -j ACCEPT
  148. done
  149. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} done"
  150. else
  151. ${display} RED "File Missing: ${FWCONFIGDIR}/ipv${IPVER}/trusted.conf"
  152. ${display} RED "Error: can not load trusted hosts file."
  153. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} failed"
  154. fi
  155. }
  156. function enable_mss_clamp {
  157. IP_VERSION=$1
  158. case $IP_VERSION in
  159. ipv6) VER_IPTABLES=${IP6TABLES};
  160. IPVER="6" ;;
  161. ipv4|*) VER_IPTABLES=${IPTABLES}
  162. IPVER="4" ;;
  163. esac
  164. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} loading"
  165. if [ -e "${FWCONFIGDIR}/ipv${IPVER}/mss-clamp.conf" ]; then
  166. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} read ${FWCONFIGDIR}/ipv${IPVER}/mss-clamp.conf successful"
  167. while read -r interface mss type; do
  168. [[ ${interface} = \#* ]] && continue
  169. [[ ${interface} = "" ]] && continue
  170. [[ ${mss} == "-" ]] && mss="1400:1536"
  171. [[ ${type} == "-" ]] && type="${OutFilter}"
  172. [[ ${type} == "out" ]] && type="${OutFilter}"
  173. [[ ${type} == "fwd" ]] && type="${FwdFilter}"
  174. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} Read: ${interface} ${mss} ${type}"
  175. ${VER_IPTABLES} -A ${type} -p tcp --tcp-flags SYN,RST SYN -j TCPMSS \
  176. --clamp-mss-to-pmtu -o ${interface} -m tcpmss --mss ${mss}
  177. done < "${FWCONFIGDIR}/ipv${IPVER}/mss-clamp.conf"
  178. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} done"
  179. else
  180. ${display} RED "File Missing: ${FWCONFIGDIR}/ipv${IPVER}/mss-clamp.conf"
  181. ${display} RED "Error: can not load mss clamp file."
  182. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} failed"
  183. fi
  184. }
  185. function allow_resolvconf_servers {
  186. IP_VERSION=$1
  187. case $IP_VERSION in
  188. ipv6) VER_IPTABLES=${IP6TABLES};
  189. IPVER="6" ;;
  190. ipv4|*) VER_IPTABLES=${IPTABLES}
  191. IPVER="4" ;;
  192. esac
  193. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} loading"
  194. [[ ${IP_VERSION} == "ipv4" ]] && ResolvConfFile="${ResolvConfv4File}"
  195. [[ ${IP_VERSION} == "ipv6" ]] && ResolvConfFile="${ResolvConfv6File}"
  196. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} Using ${ResolvConfFile} as resolv.conf"
  197. while read -r type server; do
  198. [[ ${type} != "nameserver" ]] && continue
  199. [[ ${type} = "" ]] && continue
  200. ([[ ${server} =~ ":" ]] && [[ ${IP_VERSION} = "ipv4" ]]) && continue
  201. ([[ ! ${server} =~ ":" ]] && [[ ${IP_VERSION} = "ipv6" ]]) && continue
  202. use_conntrack="no"
  203. ([[ ${IP_VERSION} == "ipv4" ]] && [[ ${Enablev4ConnectionTracking} == "yes" ]]) && use_conntrack="yes"
  204. ([[ ${IP_VERSION} == "ipv6" ]] && [[ ${Enablev6ConnectionTracking} == "yes" ]]) && use_conntrack="yes"
  205. if [[ ${use_conntrack} == "yes" ]]; then
  206. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} Added ${server} to conntrack list for DNS traffic"
  207. ${VER_IPTABLES} -A ${OutPreRules} -p udp -d ${server} --dport 53 ${M_STATE} ${C_STATE} NEW,ESTABLISHED -j ACCEPT
  208. ${VER_IPTABLES} -A ${InPreRules} -p udp -s ${server} --sport 53 ${M_STATE} ${C_STATE} ESTABLISHED,RELATED -j ACCEPT
  209. else
  210. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} Added ${server} to DNS client trusted list"
  211. ${VER_IPTABLES} -A ${OutPreRules} -p udp -s ${server} --sport 1024:65535 --dport 53 -j ACCEPT
  212. ${VER_IPTABLES} -A ${InPreRules} -p udp -d ${server} --dport 1024:65535 --sport 53 -j ACCEPT
  213. #${VER_IPTABLES} -A ${OutPreRules} -p tcp -s ${server} --sport 1024:65535 --dport 53 -j ACCEPT
  214. #${VER_IPTABLES} -A ${InPreRules} -p tcp -d ${server} --dport 1024:65535 --sport 53 -j ACCEPT
  215. fi
  216. done < "${ResolvConfFile}"
  217. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} done"
  218. }
  219. function allow_dnsclient_manual {
  220. IP_VERSION=$1
  221. case $IP_VERSION in
  222. ipv6) VER_IPTABLES=${IP6TABLES};
  223. IPVER="6" ;;
  224. ipv4|*) VER_IPTABLES=${IPTABLES}
  225. IPVER="4" ;;
  226. esac
  227. DNS_SERVERS="$2"
  228. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} loading"
  229. use_conntrack="no"
  230. ([[ ${IP_VERSION} == "ipv4" ]] && [[ ${Enablev4ConnectionTracking} == "yes" ]]) && use_conntrack="yes"
  231. ([[ ${IP_VERSION} == "ipv6" ]] && [[ ${Enablev6ConnectionTracking} == "yes" ]]) && use_conntrack="yes"
  232. for i in ${DNS_SERVERS}; do
  233. if [[ ${use_conntrack} == "yes" ]]; then
  234. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} Added ${server} to conntrack list for DNS traffic"
  235. ${VER_IPTABLES} -A ${OutPreRules} -p udp -d ${i} --dport 53 ${M_STATE} ${C_STATE} NEW,ESTABLISHED -j ACCEPT
  236. ${VER_IPTABLES} -A ${InPreRules} -p udp -s ${i} --sport 53 ${M_STATE} ${C_STATE} ESTABLISHED,RELATED -j ACCEPT
  237. else
  238. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} Added ${i} to DNS client trusted list"
  239. ${VER_IPTABLES} -A ${OutPreRules} -p udp -s ${i} --sport 1024:65535 --dport 53 -j ACCEPT
  240. ${VER_IPTABLES} -A ${InPreRules} -p udp -d ${i} --dport 1024:65535 --sport 53 -j ACCEPT
  241. #${VER_IPTABLES} -A ${OutPreRules} -p tcp -s ${i} --sport 1024:65535 --dport 53 -j ACCEPT
  242. #${VER_IPTABLES} -A ${InPreRules} -p tcp -d ${i} --dport 1024:65535 --sport 53 -j ACCEPT
  243. fi
  244. done
  245. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} done"
  246. }
  247. function enable_easyblock {
  248. IP_VERSION=$1
  249. case $IP_VERSION in
  250. ipv6) VER_IPTABLES=${IP6TABLES};
  251. IPVER="6" ;;
  252. ipv4|*) VER_IPTABLES=${IPTABLES}
  253. IPVER="4" ;;
  254. esac
  255. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} loading"
  256. if [ -e "${FWCONFIGDIR}/ipv${IPVER}/easyblock.conf" ]; then
  257. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} read ${FWCONFIGDIR}/ipv${IPVER}/easyblock.conf successful"
  258. while read -r direction interface address port protocol; do
  259. [[ ${direction} = \#* ]] && continue
  260. [[ ${direction} = "" ]] && continue
  261. ([[ ${direction} != "IN" ]] && [[ ${direction} != "OUT" ]]) \
  262. && ${display} RED "easyblock.conf: Error - must begin with IN/OUT: ${DEFAULT_COLOR}${direction} ${interface} ${address} ${port} ${protocol}" && continue
  263. # Do some creative work with variables to make building the iptables rules fairly painless
  264. [[ ${port} != "-" ]] && port="--dport ${port}"
  265. ([[ ${address} != "-" ]] && [[ ${direction} == "IN" ]]) && address="-s ${address}"
  266. ([[ ${address} != "-" ]] && [[ ${direction} == "OUT" ]]) && address="-d ${address}"
  267. ([[ ${interface} != "-" ]] && [[ ${direction} == "IN" ]]) && interface="-i ${interface}"
  268. ([[ ${interface} != "-" ]] && [[ ${direction} == "OUT" ]]) && interface="-o ${interface}"
  269. [[ ${direction} == "OUT" ]] && chain="${OutEasyBlock}"
  270. [[ ${direction} == "IN" ]] && chain="${InEasyBlock}"
  271. [[ ${protocol} != "-" ]] && protocol="-p ${protocol}"
  272. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} Read: ${direction} ${interface} ${address} ${port} ${protocol}"
  273. # Blank variables that we're not going to use.
  274. [[ ${interface} == "-" ]] && interface=""
  275. [[ ${port} == "-" ]] && port=""
  276. [[ ${address} == "-" ]] && address=""
  277. [[ ${protocol} == "-" ]] && protocol=""
  278. ${VER_IPTABLES} -A ${chain} ${interface} ${address} ${protocol} ${port} -j DROP
  279. done < "${FWCONFIGDIR}/ipv${IPVER}/easyblock.conf"
  280. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} done"
  281. fi
  282. }
  283. function enable_filtering {
  284. IP_VERSION=$1
  285. case $IP_VERSION in
  286. ipv6) VER_IPTABLES=${IP6TABLES};
  287. IPVER="6" ;;
  288. ipv4|*) VER_IPTABLES=${IPTABLES}
  289. IPVER="4" ;;
  290. esac
  291. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} loading"
  292. if [ -e "${FWCONFIGDIR}/ipv${IPVER}/acl.conf" ]; then
  293. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} read ${FWCONFIGDIR}/ipv${IPVER}/acl.conf successful"
  294. while read -r direction action interface srcaddress srcport dstaddress dstport protocol syn state; do
  295. [[ ${direction} = \#* ]] && continue
  296. [[ ${direction} = "" ]] && continue
  297. ([[ ${direction} != "IN" ]] && [[ ${direction} != "OUT" ]]) \
  298. && ${display} RED "acl.conf: Error - must begin with IN/OUT: ${DEFAULT_COLOR}${direction} ${action} ${interface} ${srcaddress} ${srcport} ${dstaddress} ${dstport} ${protocol} ${syn} ${state}" && continue
  299. ([[ ${action} != "ACCEPT" ]] && [[ ${action} != "DROP" ]] && [[ ${action} != "REJECT" ]]) \
  300. && ${display} RED "acl.conf: Error - action must be either ACCEPT, DROP, or REJECT : ${DEFAULT_COLOR}${direction} ${action} ${interface} ${srcaddress} ${srcport} ${dstaddress} ${dstport} ${protocol} ${syn} ${state}" && continue
  301. # Do some creative work with variables to make building the iptables rules fairly painless
  302. [[ -z ${state} ]] && state="-"
  303. ([[ ${IP_VERSION} == "ipv4" ]] && [[ ${Enablev4ConnectionTracking} == "yes" ]] && [[ ! ${state} == "-" ]]) && conntrack_state="${M_STATE} ${C_STATE} ${state}"
  304. ([[ ${IP_VERSION} == "ipv6" ]] && [[ ${Enablev6ConnectionTracking} == "yes" ]] && [[ ! ${state} == "-" ]]) && conntrack_state="${M_STATE} ${C_STATE} ${state}"
  305. [[ ${dstport} != "-" ]] && dstport="--dport ${dstport}"
  306. [[ ${srcport} != "-" ]] && srcport="--sport ${srcport}"
  307. [[ ${srcaddress} != "-" ]] && srcaddress="-s ${srcaddress}"
  308. [[ ${dstaddress} != "-" ]] && dstaddress="-d ${dstaddress}"
  309. ([[ ${interface} != "-" ]] && [[ ${direction} == "IN" ]]) && interface="-i ${interface}"
  310. ([[ ${interface} != "-" ]] && [[ ${direction} == "OUT" ]]) && interface="-o ${interface}"
  311. [[ ${direction} == "OUT" ]] && chain="${OutFilter}"
  312. [[ ${direction} == "IN" ]] && chain="${InFilter}"
  313. [[ ${protocol} != "-" ]] && protocol="-p ${protocol}"
  314. [[ ${action} == "REJECT" ]] && action="REJECT --reject-with tcp-reset"
  315. [[ ${syn} == "syn" ]] && syn="--syn"
  316. [[ ${syn} == "notsyn" ]] && syn="! --syn"
  317. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR}${direction} ${action} ${interface} ${srcaddress} ${srcport} ${dstaddress} ${dstport} ${protocol} ${syn}"
  318. # Blank variables that we're not going to use.
  319. [[ ${interface} == "-" ]] && interface=""
  320. [[ ${dstport} == "-" ]] && dstport=""
  321. [[ ${srcport} == "-" ]] && srcport=""
  322. [[ ${dstaddress} == "-" ]] && dstaddress=""
  323. [[ ${srcaddress} == "-" ]] && srcaddress=""
  324. [[ ${protocol} == "-" ]] && protocol=""
  325. [[ ${syn} == "-" ]] && syn=""
  326. ${VER_IPTABLES} -A ${chain} ${interface} ${protocol} ${srcaddress} ${srcport} ${syn} ${dstaddress} ${dstport} ${conntrack_state} -j ${action}
  327. done < "${FWCONFIGDIR}/ipv${IPVER}/acl.conf"
  328. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} done"
  329. fi
  330. }
  331. function enable_forwarding {
  332. IP_VERSION=$1
  333. case $IP_VERSION in
  334. ipv6) VER_IPTABLES=${IP6TABLES};
  335. IPVER="6" ;;
  336. ipv4|*) VER_IPTABLES=${IPTABLES}
  337. IPVER="4" ;;
  338. esac
  339. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} loading"
  340. if [ -e "${FWCONFIGDIR}/ipv${IPVER}/forward.conf" ]; then
  341. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} read ${FWCONFIGDIR}/ipv${IPVER}/forward.conf successful"
  342. while read -r action srcinterface srcaddress dstinterface dstaddress bidirectional srcport dstport protocol syn state; do
  343. unset conntrack_state conntrack_udp_new revsrcaddress revdstaddress revdstinterface revsrcinterface revsrcport revdstport
  344. [[ ${action} = \#* ]] && continue
  345. [[ -z ${action} ]] && continue
  346. ([[ ${action} != "ACCEPT" ]] && [[ ${action} != "DROP" ]]) \
  347. && ${display} RED "forward.conf: Error - action must be either ACCEPT or DROP : ${DEFAULT_COLOR}${action} ${srcinterface} ${srcaddress} ${dstinterface} ${dstaddress} ${bidirectional} ${srcport} ${dstport} ${protocol} ${syn} ${state}" && continue
  348. # Do some creative work with variables to make building the iptables rules fairly painless
  349. # Although these next few rules seems like they duplicate some work, they
  350. # actually make handling later rules simpler even if we end up blanking
  351. # them yet again.
  352. [[ -z ${dstport} ]] && dstport="-"
  353. [[ -z ${srcport} ]] && srcport="-"
  354. [[ -z ${protocol} ]] && protocol="-"
  355. [[ -z ${syn} ]] && syn="-"
  356. [[ -z ${state} ]] && state="-"
  357. ([[ ${IP_VERSION} == "ipv4" ]] && [[ ${Enablev4ConnectionTracking} == "yes" ]] && [[ ${state} == "-" ]]) && conntrack_state="${M_STATE} ${C_STATE} ESTABLISHED,RELATED"
  358. ([[ ${IP_VERSION} == "ipv6" ]] && [[ ${Enablev6ConnectionTracking} == "yes" ]] && [[ ${state} == "-" ]]) && conntrack_state="${M_STATE} ${C_STATE} ESTABLISHED,RELATED"
  359. ([[ ${IP_VERSION} == "ipv4" ]] && [[ ${Enablev4ConnectionTracking} == "yes" ]] && [[ ! ${state} == "-" ]]) && conntrack_state="${M_STATE} ${C_STATE} ${state}"
  360. ([[ ${IP_VERSION} == "ipv6" ]] && [[ ${Enablev6ConnectionTracking} == "yes" ]] && [[ ! ${state} == "-" ]]) && conntrack_state="${M_STATE} ${C_STATE} ${state}"
  361. ([[ ${bidirectional} == "yes" ]] && [[ ${srcaddress} != "-" ]]) && revsrcaddress="-d ${srcaddress}"
  362. ([[ ${bidirectional} == "yes" ]] && [[ ${dstaddress} != "-" ]]) && revdstaddress="-s ${dstaddress}"
  363. ([[ ${bidirectional} == "yes" ]] && [[ ${dstinterface} != "-" ]]) && revdstinterface="-i ${dstinterface}"
  364. ([[ ${bidirectional} == "yes" ]] && [[ ${srcinterface} != "-" ]]) && revsrcinterface="-o ${srcinterface}"
  365. [[ ${srcaddress} != "-" ]] && srcaddress="-s ${srcaddress}"
  366. [[ ${dstaddress} != "-" ]] && dstaddress="-d ${dstaddress}"
  367. [[ ${srcinterface} != "-" ]] && srcinterface="-i ${srcinterface}"
  368. [[ ${dstinterface} != "-" ]] && dstinterface="-o ${dstinterface}"
  369. ([[ ${syn} == "syn" ]] && [[ ! -z ${conntrack_state} ]]) && conntrack_udp_new=",NEW"
  370. ([[ ${syn} == "syn" ]] && [[ ${protocol} == "udp" ]]) && syn="-"
  371. [[ ${syn} == "syn" ]] && syn="--syn"
  372. [[ ${syn} == "notsyn" ]] && syn="! --syn"
  373. [[ ${dstport} != "-" ]] && dstport="--dport ${dstport}"
  374. [[ ${srcport} != "-" ]] && srcport="--sport ${srcport}"
  375. ([[ ${bidirectional} == "yes" ]] && [[ ${srcport} != "-" ]]) && revsrcport="--dport ${srcport}"
  376. ([[ ${bidirectional} == "yes" ]] && [[ ${dstport} != "-" ]]) && revdstport="--sport ${dstport}"
  377. [[ ${protocol} != "-" ]] && protocol="-p ${protocol}"
  378. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR}${action} ${srcinterface} ${srcaddress} ${dstinterface} ${dstaddress} ${bidirectional} ${srcport} ${dstport} ${protocol} ${syn} ${state}"
  379. # Blank variables that we're not going to use.
  380. [[ ${srcinterface} == "-" ]] && srcinterface=""
  381. [[ ${dstinterface} == "-" ]] && dstinterface=""
  382. [[ ${dstaddress} == "-" ]] && dstaddress=""
  383. [[ ${srcaddress} == "-" ]] && srcaddress=""
  384. [[ ${dstport} == "-" ]] && dstport=""
  385. [[ ${srcport} == "-" ]] && srcport=""
  386. [[ ${syn} == "-" ]] && syn=""
  387. [[ ${state} == "-" ]] && state=""
  388. [[ ${protocol} == "-" ]] && protocol=""
  389. [[ ${bidirectional} == "-" ]] && bidirectional="no"
  390. [[ ${action} == "DROP" ]] && conntrack_state=""
  391. ${VER_IPTABLES} -A ${FwdFilter} ${protocol} ${srcinterface} ${srcaddress} ${srcport} ${syn} ${dstinterface} ${dstaddress} ${dstport} ${conntrack_state} -j ${action}
  392. [[ ${bidirectional} == "yes" ]] && ${VER_IPTABLES} -A ${FwdFilter} ${protocol} ${revsrcinterface} ${revsrcaddress} ${revsrcport} ${syn} ${revdstinterface} ${revdstaddress} ${revdstport} ${conntrack_state} -j ${action}
  393. done < "${FWCONFIGDIR}/ipv${IPVER}/forward.conf"
  394. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} done"
  395. fi
  396. }
  397. function enable_nat {
  398. IP_VERSION=$1
  399. case $IP_VERSION in
  400. ipv6) VER_IPTABLES=${IP6TABLES};
  401. IPVER="6" ;;
  402. ipv4|*) VER_IPTABLES=${IPTABLES}
  403. IPVER="4" ;;
  404. esac
  405. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} loading"
  406. ([[ ${IPVER} == "4" ]] && [[ ${Enablev4ConnectionTracking} != "yes" ]]) && ${display} RED "${FUNCNAME}: ERROR:${DEFAULT_COLOR} Unable to load NAT rules if Enablev4ConnectionTracking=no" && return 1
  407. ([[ ${IPVER} == "6" ]] && [[ ${Enablev6ConnectionTracking} != "yes" ]]) && ${display} RED "${FUNCNAME}: ERROR:${DEFAULT_COLOR} Unable to load NAT rules if Enablev6ConnectionTracking=no" && return 1
  408. if [ -e "${FWCONFIGDIR}/ipv${IPVER}/nat.conf" ]; then
  409. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} read ${FWCONFIGDIR}/ipv${IPVER}/nat.conf successful"
  410. while read -r type srcinterface srcaddress dstinterface dstaddress; do
  411. [[ ${type} = \#* ]] && continue
  412. [[ ${type} = "" ]] && continue
  413. ([[ ${type} != "SNAT" ]] && [[ ${type} != "MASQ" ]] && [[ ${type} != "NETMAP" ]]) \
  414. && ${display} RED "nat.conf: Error - must begin with SNAT/MASQ/NETMAP: ${DEFAULT_COLOR}${type} ${srcinterface} ${srcaddress} ${dstinterface} ${dstaddress}" && continue
  415. # Do some creative work with variables to make building the iptables rules fairly painless
  416. [[ ${srcaddress} != "-" ]] && revsrcaddress="-d ${srcaddress}"
  417. [[ ${dstinterface} != "-" ]] && revdstinterface="-i ${dstinterface}"
  418. [[ ${srcinterface} != "-" ]] && revsrcinterface="-o ${srcinterface}"
  419. [[ ${srcinterface} != "-" ]] && srcinterface="-i ${srcinterface}"
  420. [[ ${dstinterface} != "-" ]] && dstinterface="-o ${dstinterface}"
  421. ([[ ${srcaddress} != "-" ]] && [[ ${type} != "NETMAP" ]]) && srcaddress="-s ${srcaddress}"
  422. ([[ ${dstinterface} != "-" ]] && [[ ${type} == "MASQ" ]]) && action="-j MASQUERADE"
  423. ([[ ${dstinterface} == "-" ]] && [[ ${type} == "MASQ" ]]) && \
  424. ${display} RED "nat.conf: Error - MASQ rule can not have empty destination interface: ${DEFAULT_COLOR}${type} ${srcinterface} ${srcaddress} ${dstinterface} ${dstaddress}" \
  425. && continue
  426. ([[ ${dstaddress} != "-" ]] && [[ ${type} == "SNAT" ]]) && action="-j SNAT" && dstaddress="--to-source ${dstaddress}"
  427. ([[ ${dstaddress} == "-" ]] && [[ ${type} == "SNAT" ]]) && \
  428. ${display} RED "nat.conf: Error - SNAT rule can not have empty destination address: ${DEFAULT_COLOR}${type} ${srcinterface} ${srcaddress} ${dstinterface} ${dstaddress}" \
  429. && continue
  430. ([[ ${srcaddress} != "-" ]] && [[ ${dstaddress} != "-" ]] && [[ ${type} == "NETMAP" ]]) && action="-j NETMAP" && srcaddress="-d ${srcaddress}" && dstaddress="--to ${dstaddress}"
  431. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR}${direction} ${action} ${interface} ${srcaddress} ${srcport} ${dstaddress} ${dstport} ${protocol}"
  432. # Blank variables that we're not going to use.
  433. [[ ${srcinterface} == "-" ]] && srcinterface=""
  434. [[ ${dstinterface} == "-" ]] && dstinterface=""
  435. [[ ${dstaddress} == "-" ]] && dstaddress=""
  436. [[ ${srcaddress} == "-" ]] && srcaddress=""
  437. ${VER_IPTABLES} -A ${NAT} -t nat ${srcaddress} ${action} ${dstinterface} ${dstaddress}
  438. ${VER_IPTABLES} -A ${FwdFilter} ${M_STATE} ${C_STATE} RELATED,ESTABLISHED,NEW ${srcinterface} ${srcaddress} ${dstinterface} -j ACCEPT
  439. ${VER_IPTABLES} -A ${FwdFilter} ${M_STATE} ${C_STATE} RELATED,ESTABLISHED ${revsrcinterface} ${revsrcaddress} ${revdstinterface} -j ACCEPT
  440. done < "${FWCONFIGDIR}/ipv${IPVER}/nat.conf"
  441. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} done"
  442. fi
  443. }
  444. function enable_services {
  445. IP_VERSION=$1
  446. case $IP_VERSION in
  447. ipv6) VER_IPTABLES=${IP6TABLES};
  448. IPVER="6" ;;
  449. ipv4|*) VER_IPTABLES=${IPTABLES}
  450. IPVER="4" ;;
  451. esac
  452. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} loading"
  453. if [ -e "${FWCONFIGDIR}/ipv${IPVER}/services.conf" ]; then
  454. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} read ${FWCONFIGDIR}/ipv${IPVER}/services.conf successful"
  455. use_conntrack="no"
  456. ([[ ${IP_VERSION} == "ipv4" ]] && [[ ${Enablev4ConnectionTracking} == "yes" ]]) && conntrack_state="${M_STATE} ${C_STATE} NEW"
  457. ([[ ${IP_VERSION} == "ipv6" ]] && [[ ${Enablev6ConnectionTracking} == "yes" ]]) && conntrack_state="${M_STATE} ${C_STATE} NEW"
  458. while read -r service protocol interface address srcaddress; do
  459. multiport="no"
  460. [[ ${service} = \#* ]] && continue
  461. [[ -z ${service} ]] && continue
  462. [[ ${service} == "-" ]] \
  463. && ${display} RED "service.conf: Error - must begin with service name or port number: ${DEFAULT_COLOR}${service} ${protocol} ${interface} ${address} ${srcaddress}" && continue
  464. [[ ${protocol} == "-" ]] \
  465. && ${display} RED "service.conf: Error - protocol can not be empty: ${DEFAULT_COLOR}${service} ${protocol} ${interface} ${address} ${srcaddress}" && continue
  466. [[ ${service} =~ "," ]] && multiport="yes"
  467. # Do some creative work with variables to make building the iptables rules fairly painless
  468. ([[ ${service} != "-" ]] && [[ ${multiport} != "yes" ]]) && service="--dport ${service}"
  469. ([[ ${service} != "-" ]] && [[ ${multiport} == "yes" ]]) && service="-m multiport --dports ${service}"
  470. [[ ${protocol} != "-" ]] && protocol="-p ${protocol}"
  471. [[ ${interface} != "-" ]] && interface="-i ${interface}"
  472. [[ ${address} != "-" ]] && srcaddress="-d ${address}"
  473. [[ ${srcaddress} != "-" ]] && srcaddress="-s ${srcaddress}"
  474. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} Read: ${service} ${protocol} ${interface} ${address} ${srcaddress}"
  475. # Blank variables that we're not going to use.
  476. [[ ${interface} == "-" ]] && interface=""
  477. [[ ${address} == "-" ]] && address=""
  478. [[ ${srcaddress} == "-" ]] && srcaddress=""
  479. ${VER_IPTABLES} -A ${InFilter} ${protocol} ${service} ${interface} ${address} ${srcaddress} ${conntrack_state} -j ACCEPT
  480. done < "${FWCONFIGDIR}/ipv${IPVER}/services.conf"
  481. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} done"
  482. fi
  483. }
  484. function enable_conntrack_int {
  485. IP_VERSION=$1
  486. case $IP_VERSION in
  487. ipv6) VER_IPTABLES=${IP6TABLES};
  488. IPVER="6" ;;
  489. ipv4|*) VER_IPTABLES=${IPTABLES}
  490. IPVER="4" ;;
  491. esac
  492. conntrack_int="$2"
  493. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} loading"
  494. if [[ ${conntrack_int} == "all" ]]; then
  495. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} Enabling conntrack on all interfaces"
  496. ${VER_IPTABLES} -A ${OutPreRules} ${M_STATE} ${C_STATE} ESTABLISHED,RELATED -j ACCEPT
  497. ${VER_IPTABLES} -A ${InPreRules} ${M_STATE} ${C_STATE} ESTABLISHED,RELATED -j ACCEPT
  498. ${VER_IPTABLES} -A ${OutPreRules} ${M_STATE} ${C_STATE} INVALID -j DROP
  499. ${VER_IPTABLES} -A ${InPreRules} ${M_STATE} ${C_STATE} INVALID -j DROP
  500. else
  501. for i in ${conntrack_int}; do
  502. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} Enabling conntrack on ${i}"
  503. ${VER_IPTABLES} -A ${OutPreRules} -o ${i} ${M_STATE} ${C_STATE} ESTABLISHED,RELATED -j ACCEPT
  504. ${VER_IPTABLES} -A ${InPreRules} -i ${i} ${M_STATE} ${C_STATE} ESTABLISHED,RELATED -j ACCEPT
  505. ${VER_IPTABLES} -A ${OutPreRules} -o ${i} ${M_STATE} ${C_STATE} INVALID -j DROP
  506. ${VER_IPTABLES} -A ${InPreRules} -i ${i} ${M_STATE} ${C_STATE} INVALID -j DROP
  507. done
  508. fi
  509. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} done"
  510. }
  511. function enable_portfw {
  512. IP_VERSION=$1
  513. case $IP_VERSION in
  514. ipv6) VER_IPTABLES=${IP6TABLES};
  515. IPVER="6" ;;
  516. ipv4|*) VER_IPTABLES=${IPTABLES}
  517. IPVER="4" ;;
  518. esac
  519. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} loading"
  520. if [ -e "${FWCONFIGDIR}/ipv${IPVER}/portfw.conf" ]; then
  521. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} read ${FWCONFIGDIR}/ipv${IPVER}/portfw.conf successful"
  522. use_conntrack="no"
  523. ([[ ${IP_VERSION} == "ipv4" ]] && [[ ${Enablev4ConnectionTracking} == "yes" ]]) && conntrack_state="${M_STATE} ${C_STATE} NEW"
  524. ([[ ${IP_VERSION} == "ipv6" ]] && [[ ${Enablev6ConnectionTracking} == "yes" ]]) && conntrack_state="${M_STATE} ${C_STATE} NEW"
  525. while read -r service protocol intip intport interface address srcaddress; do
  526. [[ ${service} = \#* ]] && continue
  527. [[ -z ${service} ]] && continue
  528. [[ ${service} == "-" ]] \
  529. && ${display} RED "service.conf: Error - must begin with service name or port number: ${DEFAULT_COLOR}${service} ${intip} ${intport} ${protocol} ${interface} ${address} ${srcaddress}" && continue
  530. [[ ${protocol} == "-" ]] \
  531. && ${display} RED "service.conf: Error - protocol can not be empty: ${DEFAULT_COLOR}${service} ${intip} ${intport} ${protocol} ${interface} ${address} ${srcaddress}" && continue
  532. # Do some creative work with variables to make building the iptables rules fairly painless
  533. # Although these next few rules seems like they duplicate some work, they
  534. # actually make handling later rules simpler even if we end up blanking
  535. # them yet again.
  536. [[ -z ${interface} ]] && interface="-"
  537. [[ -z ${address} ]] && address="-"
  538. [[ -z ${srcaddress} ]] && srcaddress="-"
  539. [[ ${service} != "-" ]] && service="--dport ${service}"
  540. [[ ${protocol} != "-" ]] && protocol="-p ${protocol}"
  541. [[ ${intip} != "-" ]] && intdest="--to-destination ${intip}:${intport}"
  542. [[ ${interface} != "-" ]] && interface="-i ${interface}"
  543. [[ ${address} != "-" ]] && address="-d ${address}"
  544. [[ ${srcaddress} != "-" ]] && srcaddress="-s ${srcaddress}"
  545. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} Read: ${service} ${protocol} ${intip} ${intport} ${interface} ${address} ${srcaddress}"
  546. # Blank variables that we're not going to use.
  547. [[ ${interface} == "-" ]] && interface=""
  548. [[ ${address} == "-" ]] && address=""
  549. [[ ${srcaddress} == "-" ]] && srcaddress=""
  550. ${VER_IPTABLES} -A ${PortForward} -t nat ${protocol} ${service} ${interface} ${address} ${srcaddress} -j DNAT ${intdest}
  551. ${VER_IPTABLES} -A ${InFilter} ${protocol} ${service} ${interface} ${address} ${srcaddress} ${conntrack_state} -j ACCEPT
  552. done < "${FWCONFIGDIR}/ipv${IPVER}/portfw.conf"
  553. ${debug} ${DebugColor} "${FUNCNAME}:${DEFAULT_COLOR} done"
  554. fi
  555. }