Guest User

arno-iptables-firewall 2.0

a guest
Dec 13th, 2011
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 197.83 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. MY_VERSION="2.0.0c"
  4.  
  5. # Location of the main configuration file for the firewall
  6. ##########################################################
  7. CONFIG_FILE=/etc/arno-iptables-firewall/firewall.conf
  8.  
  9. # ------------------------------------------------------------------------------
  10. # -= Arno's iptables firewall =-
  11. # Single- & multi-homed firewall script with DSL/ADSL support
  12. #
  13. # ~ In memory of my dear father ~
  14. #
  15. # (C) Copyright 2001-2011 by Arno van Amersfoort
  16. # Co-authors : Lonnie Abelbeck & Philip Prindeville
  17. # Homepage : http://rocky.eld.leidenuniv.nl/
  18. # Freshmeat homepage : http://freshmeat.net/projects/iptables-firewall/?topic_id=151
  19. # Email : a r n o v a AT r o c k y DOT e l d DOT l e i d e n u n i v DOT n l
  20. # (note: you must remove all spaces and substitute the @ and the .
  21. # at the proper locations!)
  22. # ------------------------------------------------------------------------------
  23. # This program is free software; you can redistribute it and/or
  24. # modify it under the terms of the GNU General Public License
  25. # version 2 as published by the Free Software Foundation.
  26.  
  27. # This program is distributed in the hope that it will be useful,
  28. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. # GNU General Public License for more details.
  31.  
  32. # You should have received a copy of the GNU General Public License
  33. # along with this program; if not, write to the Free Software
  34. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  35. # ------------------------------------------------------------------------------
  36.  
  37. printf "\033[40m\033[1;32mArno's Iptables Firewall Script v$MY_VERSION\033[0m\n"
  38. echo "-------------------------------------------------------------------------------"
  39.  
  40. # Check if the main config file exists and if so load it
  41. ########################################################
  42. if [ -e "$CONFIG_FILE" ]; then
  43. . "$CONFIG_FILE"
  44. else
  45. printf "\033[40m\033[1;31mERROR: Could not read configuration file $CONFIG_FILE!\033[0m\n" >&2
  46. printf "\033[40m\033[1;31m Please, check the file's location and (root) rights.\033[0m\n" >&2
  47. exit 2
  48. fi
  49.  
  50. # Check if the environment file exists and if so, load it
  51. #########################################################
  52. if [ -z "$ENV_FILE" ]; then
  53. if [ -f /usr/local/share/arno-iptables-firewall/environment ]; then
  54. ENV_FILE="/usr/local/share/arno-iptables-firewall/environment"
  55. else
  56. if [ -f /usr/share/arno-iptables-firewall/environment ]; then
  57. ENV_FILE="/usr/share/arno-iptables-firewall/environment"
  58. else
  59. printf "\033[40m\033[1;31mERROR: The environment file (ENV_FILE) has not been specified\033[0m\n" >&2
  60. printf "\033[40m\033[1;31m in the configuration file. Try upgrading your config-file!\033[0m\n" >&2
  61. exit 2
  62. fi
  63. fi
  64. fi
  65.  
  66. if [ -e "$ENV_FILE" ]; then
  67. . "$ENV_FILE"
  68. else
  69. printf "\033[40m\033[1;31mERROR: Could not read environment file \"$ENV_FILE\"!\033[0m\n" >&2
  70. printf "\033[40m\033[1;31m Please, check the file's location and (root) rights.\033[0m\n" >&2
  71. exit 2
  72. fi
  73.  
  74.  
  75. sanity_check()
  76. {
  77. # root check
  78. if [ "$(id -u)" != "0" ]; then
  79. printf "\033[40m\033[1;31mERROR: Root check FAILED (you MUST be root to use this script)! Quitting...\033[0m\n" >&2
  80. exit 1
  81. fi
  82.  
  83. # Check whether the required binaries exist and are executable
  84. ##############################################################
  85. check_command_error iptables
  86. if [ "$IPV6_DETECTED" = "1" ]; then
  87. check_command_error ip6tables
  88. fi
  89. check_command_error awk
  90. check_command_error tr
  91. check_command_error ip
  92. check_command_error cut
  93. check_command_error uname
  94. check_command_error sed
  95. check_command_error cat
  96. check_command_error date
  97. check_command_error modprobe
  98. check_command_error sysctl
  99. check_command_error head
  100. check_command_error tail
  101. check_command_error wc
  102. check_command_error logger
  103.  
  104. if [ "$RESOLV_IPS" = "1" ]; then
  105. check_command_error dig nslookup
  106. fi
  107.  
  108. if [ "$IPV6_SUPPORT" = "1" ]; then
  109. if ! kernel_ver_chk 2 6 24; then
  110. printf "\033[40m\033[1;31mWARNING: IPv6 support is enabled but your kernel is rather old (<2.6.24)! This *could* cause problems...\033[0m\n" >&2
  111. fi
  112. fi
  113. }
  114.  
  115.  
  116. config_check()
  117. {
  118. # Make sure EXT_IF != ""
  119. ########################
  120. if [ -z "$EXT_IF" ]; then
  121. printf "\033[40m\033[1;31mERROR: The required variable EXT_IF is empty!\033[0m\n" >&2
  122. printf "\033[40m\033[1;31m Please, check the configuration file.\033[0m\n" >&2
  123. exit 2
  124. fi
  125.  
  126. # Check whether EXT_IF exists
  127. #############################
  128. IFS=' ,'
  129. for interface in $EXT_IF; do
  130. if ! check_interface $interface; then
  131. printf "\033[40m\033[1;31mWARNING: External interface $interface does NOT exist (yet?)\033[0m\n" >&2
  132. fi
  133. done
  134.  
  135. # Check whether INT_IF exists
  136. #############################
  137. IFS=' ,'
  138. for interface in $INT_IF; do
  139. if ! check_interface $interface; then
  140. printf "\033[40m\033[1;31mWARNING: Internal interface $interface does NOT exist (yet?)\033[0m\n" >&2
  141. fi
  142. done
  143.  
  144. # Check whether DMZ_IF exists
  145. #############################
  146. IFS=' ,'
  147. for interface in $DMZ_IF; do
  148. if ! check_interface $interface; then
  149. printf "\033[40m\033[1;31mWARNING: DMZ interface $interface does NOT exist (yet?)\033[0m\n" >&2
  150. fi
  151. done
  152.  
  153. # Check whether TRUSTED_IF exists
  154. #################################
  155. IFS=' ,'
  156. for interface in $TRUSTED_IF; do
  157. if ! check_interface $interface; then
  158. printf "\033[40m\033[1;31mWARNING: Trusted interface $interface does NOT exist (yet?)\033[0m\n" >&2
  159. fi
  160. done
  161.  
  162. # Make sure INT_IF != EXT_IF
  163. ############################
  164. IFS=' ,'
  165. for eif in $EXT_IF; do
  166. for iif in $INT_IF; do
  167. if [ "$iif" = "$eif" ]; then
  168. printf "\033[40m\033[1;31mERROR: One or more interfaces specified in EXT_IF is the same as one in\033[0m\n" >&2
  169. printf "\033[40m\033[1;31m INT_IF! Please, check the configuration file.\033[0m\n" >&2
  170. exit 3
  171. fi
  172. done
  173. done
  174.  
  175. # Make sure EXT_IF != lo / 127.0.0.1
  176. ####################################
  177. IFS=' ,'
  178. for eif in $EXT_IF; do
  179. if [ "$eif" = "lo" -o "$eif" = "127.0.0.1" ]; then
  180. printf "\033[40m\033[1;31mERROR: One or more interfaces specified in EXT_IF has the address or name of the\033[0m\n" >&2
  181. printf "\033[40m\033[1;31m local loopback device! Please, check the configuration file.\033[0m\n" >&2
  182. exit 6
  183. fi
  184. done
  185.  
  186. # Make sure INT_IF != lo / 127.0.0.1
  187. ####################################
  188. IFS=' ,'
  189. for iif in $INT_IF; do
  190. if [ "$iif" = "lo" -o "$iif" = "127.0.0.1" ]; then
  191. printf "\033[40m\033[1;31mERROR: At least one of the interfaces specified in INT_IF has the address or\033[0m\n" >&2
  192. printf "\033[40m\033[1;31m name of the local loopback device! Please, check the configuration file.\033[0m\n" >&2
  193. exit 7
  194. fi
  195. done
  196.  
  197. # If support for an DHCP server serving an external net is enabled, we
  198. # also need to know what the external net is.
  199. ##########################################################################
  200. if [ "$EXTERNAL_DHCP_SERVER" = "1" -a -z "$EXTERNAL_NET" ]; then
  201. printf "\033[40m\033[1;31mERROR: You have enabled external DHCP server support but required variable\033[0m\n" >&2
  202. printf "\033[40m\033[1;31m EXTERNAL_NET has NOT been defined!\033[0m\n" >&2
  203. exit 10
  204. fi
  205.  
  206. # We can only perform NAT if NAT_INTERNAL_NET is defined
  207. if [ "$NAT" = "1" -a -z "$NAT_INTERNAL_NET" ]; then
  208. printf "\033[40m\033[1;31mERROR: Unable to enable NAT because there's no (NAT_)INTERNAL_NET specified!\033[0m\n" >&2
  209. exit 11
  210. fi
  211.  
  212. # If support the nmb_broadcast_fix is enabled we need the EXTERNAL_NET set
  213. ##########################################################################
  214. if [ "$NMB_BROADCAST_FIX" = "1" -a -z "$EXTERNAL_NET" ]; then
  215. printf "\033[40m\033[1;31mERROR: You have enabled the NMB_BROADCAST_FIX but required variable\033[0m\n" >&2
  216. printf "\033[40m\033[1;31m EXTERNAL_NET has NOT been defined!\033[0m\n" >&2
  217. exit 12
  218. fi
  219.  
  220. # Warn if no_broadcast variables are used and external net is NOT defined
  221. ##########################################################################
  222. if [ -n "$BROADCAST_TCP_NOLOG" -o -n "$BROADCAST_UDP_NOLOG" ]; then
  223. if [ -z "$EXTERNAL_NET" -a -z "$EXT_NET_BCAST_ADDRESS" ]; then
  224. printf "\033[40m\033[1;31mWARNING: You are using the BROADCAST_xxx_NOLOG variables but EXTERNAL_NET and EXT_NET_BCAST_ADDRESS\033[0m\n" >&2
  225. printf "\033[40m\033[1;31m have NOT been defined!\033[0m\n" >&2
  226. fi
  227. fi
  228.  
  229. # Check whether we know the plugin binary path
  230. ##############################################
  231. if [ ! -d "$PLUGIN_BIN_PATH" ]; then
  232. printf "\033[40m\033[1;31mERROR: The PLUGIN_BIN_PATH ($PLUGIN_BIN_PATH) does not exist!\033[0m\n" >&2
  233. printf "\033[40m\033[1;31m Please check your installation and/or configuration file.\033[0m\n" >&2
  234. exit 2
  235. fi
  236.  
  237. # Check whether we know the plugin config path
  238. ##############################################
  239. if [ ! -d "$PLUGIN_CONF_PATH" ]; then
  240. printf "\033[40m\033[1;31mERROR: The PLUGIN_CONF_PATH ($PLUGIN_CONF_PATH) does not exist!\033[0m\n" >&2
  241. printf "\033[40m\033[1;31m Please check your installation and/or configuration file.\033[0m\n" >&2
  242. exit 2
  243. fi
  244. }
  245.  
  246.  
  247. load_modules()
  248. {
  249. unset IFS
  250. # Set indent for functions
  251. INDENT=' '
  252.  
  253. echo "Checking/probing Iptables modules:"
  254.  
  255. # Required; all IPv4 modules depend on this one
  256. modprobe ip_tables
  257. if [ "$IPV6_SUPPORT" = "1" ]; then
  258. modprobe ip6_tables
  259. fi
  260.  
  261. # Allows connection tracking state match, which allows you to
  262. # write rules matching the state of a connection
  263. modprobe_multi nf_conntrack ip_conntrack
  264. if [ "$IPV6_SUPPORT" = "1" ]; then
  265. modprobe nf_conntrack_ipv6
  266. fi
  267.  
  268. # Permits active FTP; requires ip_conntrack
  269. modprobe_multi nf_conntrack_ftp ip_conntrack_ftp
  270.  
  271. # Allows tracking for various protocols, placing entries in the conntrack table etc.
  272. if [ "$IPV6_SUPPORT" = "1" ]; then
  273. modprobe_multi xt_conntrack "ipt_conntrack,ip6t_conntrack"
  274. else
  275. modprobe_multi xt_conntrack ipt_conntrack
  276. fi
  277.  
  278. # Allows log limits
  279. if [ "$IPV6_SUPPORT" = "1" ]; then
  280. modprobe_multi xt_limit "ipt_limit,ip6t_limit"
  281. else
  282. modprobe_multi xt_limit ipt_limit
  283. fi
  284.  
  285. # Permits packet state checking (SYN, SYN-ACK, ACK, and so on).
  286. if [ "$IPV6_SUPPORT" = "1" ]; then
  287. modprobe_multi xt_state "ipt_state,ip6t_state"
  288. else
  289. modprobe_multi xt_state ipt_state
  290. fi
  291.  
  292. # Allows packet specifications on multiple ports
  293. if [ "$IPV6_SUPPORT" = "1" ]; then
  294. modprobe_multi xt_multiport "ipt_multiport,ip6t_multiport"
  295. else
  296. modprobe_multi xt_multiport ipt_multiport
  297. fi
  298.  
  299. # Implement the filter table:
  300. modprobe iptable_filter
  301. if [ "$IPV6_SUPPORT" = "1" ]; then
  302. modprobe ip6table_filter
  303. fi
  304.  
  305. # Implement the mangle table
  306. modprobe iptable_mangle
  307. if [ "$IPV6_SUPPORT" = "1" ]; then
  308. modprobe ip6table_mangle
  309. fi
  310.  
  311. # Implement the REJECT target
  312. modprobe ipt_REJECT
  313. if [ "$IPV6_SUPPORT" = "1" ]; then
  314. modprobe ip6t_REJECT
  315. fi
  316.  
  317. # Implement the LOG target
  318. modprobe ipt_LOG
  319. if [ "$IPV6_SUPPORT" = "1" ]; then
  320. modprobe ip6t_LOG
  321. fi
  322.  
  323. if [ "$SET_MSS" != "0" ]; then
  324. # Implement the TCPMSS target
  325. if [ "$IPV6_SUPPORT" = "1" ]; then
  326. modprobe_multi xt_TCPMSS "ipt_TCPMSS,ip6t_TCPMSS"
  327. else
  328. modprobe_multi xt_TCPMSS ipt_TCPMSS
  329. fi
  330. fi
  331.  
  332. if [ "$MANGLE_TOS" != "0" ]; then
  333. # Implement the TOS target
  334. if [ "$IPV6_SUPPORT" = "1" ]; then
  335. modprobe_multi xt_DSCP "ipt_DSCP,ip6t_DSCP" "ipt_TOS,ip6t_TOS"
  336. else
  337. modprobe_multi xt_DSCP ipt_DSCP ipt_TOS
  338. fi
  339. fi
  340.  
  341. if [ "$PACKET_TTL" = "1" -o "$TTL_INC" = "1" ]; then
  342. # Load the TTL target:
  343. modprobe ipt_TTL
  344. fi
  345.  
  346. # (Currently) unused modules:
  347. # modprobe ipt_addrtype # Allows matching src/dst address type (BROKEN!)
  348. # modprobe ipt_pkttype # Permits checking for packet type (BROADCAST, MULTICAST etc.) (BROKEN!)
  349. # modprobe ip_queue # Allows queuing packets to user space
  350. # modprobe ipt_owner # Permits user/group checking on OUTPUT packets
  351. # modprobe ipt_mark # Allows use of mark match
  352. # modprobe ip_conntrack_egg
  353.  
  354. # Implement the NAT table
  355. modprobe iptable_nat
  356.  
  357. if [ "$NAT" = "1" -o -n "$NAT_FORWARD_TCP" -o -n "$NAT_FORWARD_UDP" -o -n "$NAT_FORWARD_IP" ]; then
  358. # Load the module implementing DNAT/SNAT/NAT support
  359. modprobe_multi nf_nat ip_nat
  360. fi
  361.  
  362. if [ "$NAT" = "1" ]; then
  363. modprobe_multi nf_nat_ftp ip_nat_ftp # Permits active FTP via nat; requires ip_conntrack & iptable_nat
  364.  
  365. if [ -z "$NAT_STATIC_IP" ]; then
  366. # Load the MASQUERADE target:
  367. modprobe ipt_MASQUERADE
  368. fi
  369. fi
  370.  
  371. if [ "$USE_IRC" = "1" ]; then
  372. # echo "Enabling IRC DCC module support..."
  373. modprobe_multi nf_conntrack_irc ip_conntrack_irc #ports=6661,6662,6663,6664,6665,6666,6667,6668,6669,7000,7001
  374.  
  375. if [ "$NAT" = "1" ]; then
  376. modprobe_multi nf_nat_irc ip_nat_irc #ports=6661,6662,6663,6664,6665,6666,6667,6668,6669,7000,7001
  377. fi
  378. fi
  379.  
  380. echo " Module check done..."
  381. }
  382.  
  383.  
  384. setup_misc()
  385. {
  386. # Remove any stale plugin restart file
  387. rm -f "$PLUGIN_LOAD_FILE_RESTART"
  388.  
  389. # Most people don't want to get any firewall logs being spit to the console
  390. # This option makes the kernel ring buffer to only log messages with level "panic"
  391. if [ "$DMESG_PANIC_ONLY" = "1" ]; then
  392. echo "Setting the kernel ring buffer to only log panic messages to the console"
  393. # dmesg -c # Clear ring buffer
  394. dmesg -n 1 # Only show panic messages on the console
  395. fi
  396. }
  397.  
  398.  
  399. setup_kernel_settings()
  400. {
  401. # Set INDENT value for functions
  402. INDENT=' '
  403.  
  404. echo "Configuring general kernel parameters:"
  405.  
  406. # Set the maximum number of connections to track.
  407. # The kernel "default" depends on the available amount of RAM, 128 MB of RAM -> 8192
  408. # possible entries, 256 MB of RAM --> 16376 possible entries, etc...
  409. #######################################################################################
  410. if [ -n "$CONNTRACK" -a "$CONNTRACK" != "0" ]; then
  411. echo " Setting the max. amount of simultaneous connections to $CONNTRACK"
  412. sysctl_multi -w net.nf_conntrack_max=$CONNTRACK \
  413. net.ipv4.netfilter.ip_conntrack_max=$CONNTRACK \
  414. net.ipv4.ip_conntrack_max=$CONNTRACK
  415. fi
  416.  
  417. # Change some default timings to fix false logs generated by "lost connections"
  418. # Defaults:
  419. # echo 60 > /proc/sys/net/ipv4/netfilter/ip_conntrack_udp_timeout
  420. # echo 180 > /proc/sys/net/ipv4/netfilter/ip_conntrack_udp_timeout_stream
  421. # echo 10 >/proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_close
  422. # echo 300 > /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_max_retrans
  423. # echo 120 > /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_time_wait
  424. # echo 30 > /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_last_ack
  425. # echo 60 > /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_close_wait
  426. # echo 120 > /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_fin_wait
  427. # echo 60 > /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_syn_recv
  428. # echo 120 > /proc/sys/net/ipv4/netfilter/ip_conntrack_tcp_timeout_syn_sent
  429. # echo 30 > /proc/sys/net/ipv4/netfilter/ip_conntrack_icmp_timeout
  430. # echo 1200 > /proc/sys/net/ipv4/netfilter/ip_conntrack_generic_timeout
  431. ###############################################################################
  432. # echo " Setting default conntrack timeouts"
  433.  
  434. # This is to fix issues with DNS:
  435. # sysctl_multi -w net.netfilter.nf_conntrack_udp_timeout=60 \
  436. # net.ipv4.netfilter.ip_conntrack_udp_timeout=60
  437.  
  438. # sysctl_multi -w net.netfilter.nf_conntrack_udp_timeout_stream=180 \
  439. # net.ipv4.netfilter.ip_conntrack_udp_timeout_stream=180
  440.  
  441. # Enable Conntrack Accounting (kernel config CONFIG_NF_CT_ACCT)
  442. # CONFIG_NF_CT_ACCT is deprecated and will be removed sometime after kernel 2.6.27
  443. sysctl -w net.netfilter.nf_conntrack_acct=1 2>/dev/null
  444.  
  445. # Always set IPv4 options for IPv4 or IPv4/IPv6
  446. ######################################################
  447. echo "Configuring kernel parameters:"
  448.  
  449. # Disable ICMP send_redirect
  450. ############################
  451. echo " Disabling send redirects"
  452. sysctl_set_all "net.ipv4.conf" "send_redirects" 0
  453. if [ "$IPV6_SUPPORT" = "1" ]; then
  454. sysctl_set_all "net.ipv6.conf" "send_redirects" 0
  455. fi
  456.  
  457. # Don't accept source routed packets.
  458. # Attackers can use source routing to generate
  459. # traffic pretending to be from inside your network, but which is routed back along
  460. # the path from which it came, namely outside, so attackers can compromise your
  461. # network. Source routing is rarely used for legitimate purposes.
  462. ###################################################################################
  463. if [ "$SOURCE_ROUTE_PROTECTION" = "0" ]; then
  464. echo " DISABLING protection against source routed packets"
  465. sysctl_set_all "net.ipv4.conf" "accept_source_route" 1
  466. if [ "$IPV6_SUPPORT" = "1" ]; then
  467. sysctl_set_all "net.ipv6.conf" "accept_source_route" 1
  468. fi
  469. else
  470. echo " Enabling protection against source routed packets"
  471. sysctl_set_all "net.ipv4.conf" "accept_source_route" 0
  472. if [ "$IPV6_SUPPORT" = "1" ]; then
  473. sysctl_set_all "net.ipv6.conf" "accept_source_route" 0
  474. fi
  475. fi
  476.  
  477. # ICMP Broadcasting protection (smurf amplifier protection)
  478. ###########################################################
  479. sysctl -w net.ipv4.icmp_echo_ignore_broadcasts=1
  480.  
  481. # ICMP Dead Error Messages protection
  482. #####################################
  483. sysctl -w net.ipv4.icmp_ignore_bogus_error_responses=1
  484.  
  485. # IP forwarding (need it to perform for example NAT)
  486. ####################################################
  487. if [ "$IP_FORWARDING" != "0" ]; then
  488. echo " Enabling packet forwarding"
  489. sysctl_set_all "net.ipv4.conf" "forwarding" 1 || sysctl -w net.ipv4.ip_forward=1 ||
  490. {
  491. printf "\033[40m\033[1;31m WARNING: net.ipv4.conf.*.forwarding (or net.ipv4.ip_forward) could not be set! If you're using\033[0m\n" >&2
  492. printf "\033[40m\033[1;31m NAT or any other type of forwarding this may be a problem.\033[0m\n" >&2
  493. }
  494. if [ "$IPV6_SUPPORT" = "1" ]; then
  495. sysctl_set_all "net.ipv6.conf" "forwarding" 1
  496. echo " Disabling Local IPv6 Auto-Configuration"
  497. sysctl_set_all "net.ipv6.conf" "autoconf" 0
  498. sysctl_set_all "net.ipv6.conf" "accept_ra" 0
  499. fi
  500. else
  501. echo " DISABLING packet forwarding"
  502. sysctl_set_all "net.ipv4.conf" "forwarding" 0 2>/dev/null || sysctl -w -e net.ipv4.ip_forward=0 2>/dev/null
  503. if [ "$IPV6_SUPPORT" = "1" ]; then
  504. sysctl_set_all "net.ipv6.conf" "forwarding" 0 2>/dev/null
  505. if [ "$IPV6_AUTO_CONFIGURATION" != "0" ]; then
  506. echo " Enabling IPv6 Auto-Configuration"
  507. sysctl_set_all "net.ipv6.conf" "autoconf" 1
  508. sysctl_set_all "net.ipv6.conf" "accept_ra" 1
  509. else
  510. echo " DISABLING IPv6 Auto-Configuration"
  511. sysctl_set_all "net.ipv6.conf" "autoconf" 0
  512. sysctl_set_all "net.ipv6.conf" "accept_ra" 0
  513. fi
  514. fi
  515. fi
  516.  
  517. # Enable some general settings
  518. ##############################
  519. echo " Setting some kernel performance options"
  520. sysctl -w net.ipv4.tcp_window_scaling=1
  521. sysctl -w net.ipv4.tcp_timestamps=1
  522. sysctl -w net.ipv4.tcp_sack=1
  523. sysctl -w net.ipv4.tcp_dsack=1
  524. sysctl -w net.ipv4.tcp_fack=1
  525. sysctl -w net.ipv4.tcp_low_latency=0
  526.  
  527. # Reduce DoS'ing ability by reducing timeouts
  528. #############################################################
  529. if [ "$REDUCE_DOS_ABILITY" = "1" ]; then
  530. echo " Enabling reduction of the DoS'ing ability"
  531.  
  532. sysctl -w net.ipv4.tcp_fin_timeout=30
  533. sysctl -w net.ipv4.tcp_keepalive_time=1800
  534.  
  535. # Set number of times to retry SYN in a new connection
  536. sysctl -w net.ipv4.tcp_syn_retries=3
  537.  
  538. # Set number of times to retry a SYN-ACK in a half-open new connections
  539. sysctl -w net.ipv4.tcp_synack_retries=2
  540.  
  541. # Enable a fix for RFC1337 - time-wait assassination hazards in TCP
  542. sysctl -w net.ipv4.tcp_rfc1337=1
  543. else
  544. echo " Disabling reduction of the DoS'ing ability"
  545.  
  546. # Defaults:
  547. sysctl -w net.ipv4.tcp_fin_timeout=60
  548. sysctl -w net.ipv4.tcp_keepalive_time=7200
  549. sysctl -w net.ipv4.tcp_syn_retries=5
  550. sysctl -w net.ipv4.tcp_synack_retries=5
  551. sysctl -w net.ipv4.tcp_rfc1337=0
  552. fi
  553.  
  554. # Set out local port range. Kernel default = "1024 4999"
  555. ########################################################
  556. if [ -z "$LOCAL_PORT_RANGE" ]; then
  557. LOCAL_PORT_RANGE="32768 61000"
  558. fi
  559. sysctl -w net.ipv4.ip_local_port_range="$LOCAL_PORT_RANGE"
  560.  
  561. # Now we change the LOCAL_PORT_RANGE for further use by iptables (replace space with :)
  562. LOCAL_PORT_RANGE="$(echo "$LOCAL_PORT_RANGE" |tr ' ' ':')"
  563.  
  564. # Use rp_filter to drop connections from non-routable IPs
  565. ######################################################################
  566. if [ "$RP_FILTER" = "1" ]; then
  567. echo " Enabling anti-spoof with rp_filter"
  568. sysctl_set_all "net.ipv4.conf" "rp_filter" 1
  569. else
  570. echo " Disabling anti-spoof with rp_filter"
  571. sysctl_set_all "net.ipv4.conf" "rp_filter" 0
  572. fi
  573.  
  574. # Block ALL ICMP echo requests?
  575. ###############################
  576. if [ "$ECHO_IGNORE" = "1" ]; then
  577. echo " Blocking all ICMP echo-requests"
  578. sysctl -w net.ipv4.icmp_echo_ignore_all=1
  579. else
  580. sysctl -w net.ipv4.icmp_echo_ignore_all=0
  581. fi
  582.  
  583. # Add synflood protection?
  584. ##########################
  585. if [ "$SYN_PROT" != 0 ]; then
  586. echo " Enabling SYN-flood protection via SYN-cookies"
  587. sysctl -w net.ipv4.tcp_syncookies=1
  588. else
  589. echo " Disabling SYN-flood protection via SYN-cookies"
  590. sysctl -w net.ipv4.tcp_syncookies=0
  591. fi
  592.  
  593. # Log martians?
  594. ###############
  595. if [ "$LOG_MARTIANS" = "1" ]; then
  596. echo " Enabling the logging of martians"
  597. sysctl_set_all "net.ipv4.conf" "log_martians" 1
  598. else
  599. echo " Disabling the logging of martians"
  600. sysctl_set_all "net.ipv4.conf" "log_martians" 0
  601. fi
  602.  
  603. # Accept ICMP redirect messages?
  604. ################################
  605. if [ "$ICMP_REDIRECT" = "1" ]; then
  606. echo " Enabling the acception of ICMP-redirect messages"
  607. sysctl_set_all "net.ipv4.conf" "accept_redirects" 1
  608. if [ "$IPV6_SUPPORT" = "1" ]; then
  609. sysctl_set_all "net.ipv6.conf" "accept_redirects" 1
  610. fi
  611. else
  612. echo " Disabling the acception of ICMP-redirect messages"
  613. sysctl_set_all "net.ipv4.conf" "accept_redirects" 0
  614. if [ "$IPV6_SUPPORT" = "1" ]; then
  615. sysctl_set_all "net.ipv6.conf" "accept_redirects" 0
  616. fi
  617. fi
  618.  
  619. # Time To Live (TTL) is the term for a data field in the internet protocol.
  620. # TTL is today interpreted to indicate the maximum number of routers a packet may transit.
  621. # Each router that handles a packet will decrement the TTL field by 1.
  622. # Raise if you have a huge network.
  623. # Set the default ttl. (Kernel Default: 64)
  624. ###########################################################################################
  625. if [ -z "$DEFAULT_TTL" ]; then
  626. DEFAULT_TTL="64"
  627. fi
  628.  
  629. if [ $DEFAULT_TTL -gt 9 -a $DEFAULT_TTL -lt 256 ]; then
  630. echo " Setting default TTL=$DEFAULT_TTL"
  631. sysctl -w net.ipv4.ip_default_ttl=$DEFAULT_TTL
  632. else
  633. printf "\033[40m\033[1;31m WARNING: Ingoring invalid value for DEFAULT_TTL ($DEFAULT_TTL), it should be between 10 and 255!\033[0m\n" >&2
  634. fi
  635.  
  636. # Increase the default queuelength. (Kernel Default: 1024)
  637. ##########################################################
  638. # sysctl -w -e net.ipv4.ip_queue_maxlen=2048
  639.  
  640. # Enable ECN? (Explicit Congestion Notification)
  641. ################################################
  642. if [ "$ECN" = "1" ]; then
  643. echo " Enabling ECN (Explicit Congestion Notification)"
  644. sysctl -w net.ipv4.tcp_ecn=1
  645. else
  646. echo " Disabling ECN (Explicit Congestion Notification)"
  647. sysctl -w net.ipv4.tcp_ecn=0
  648. fi
  649.  
  650. # This enables dynamic-address hacking which makes the
  651. # life with Diald and similar programs much easier.
  652. ######################################################
  653. if [ "$EXT_IF_DHCP_IP" = "1" ]; then
  654. echo " Enabling kernel support for dynamic IPs"
  655. sysctl -w net.ipv4.ip_dynaddr=1
  656. else
  657. echo " Disabling kernel support for dynamic IPs"
  658. sysctl -w net.ipv4.ip_dynaddr=0
  659. fi
  660.  
  661. # In most cases pmtu discovery is ok, but in some rare cases (when having problems)
  662. # you might want to disable it.
  663. if [ "$NO_PMTU_DISCOVERY" = "1" ]; then
  664. echo " Disabling PMTU discovery"
  665. sysctl -w net.ipv4.ip_no_pmtu_disc=1
  666. else
  667. echo " Enabling PMTU discovery"
  668. sysctl -w net.ipv4.ip_no_pmtu_disc=0
  669. fi
  670.  
  671. echo " Flushing route table"
  672. sysctl -w net.ipv4.route.flush=1
  673. if [ "$IPV6_SUPPORT" = "1" ]; then
  674. sysctl -w net.ipv6.route.flush=1
  675. fi
  676.  
  677. echo " Kernel setup done..."
  678.  
  679. # Return "no error"
  680. return 0
  681. }
  682.  
  683.  
  684. init_firewall_chains()
  685. {
  686. echo "Initializing firewall chains"
  687.  
  688. # Set INDENT for functions
  689. INDENT=' '
  690.  
  691. # Attempt to flush all IPv4 chains
  692. ##################################
  693. ip4tables -F
  694. ip4tables -X
  695.  
  696. # Flush builtin IPv4 chains
  697. ###########################
  698. ip4tables -F INPUT
  699. ip4tables -F OUTPUT
  700. ip4tables -F FORWARD
  701.  
  702. # Flush rules in nat/mangle tables
  703. ##################################
  704. ip4tables -t nat -F
  705. ip4tables -t nat -X
  706. ip4tables -t mangle -F
  707. ip4tables -t mangle -X
  708.  
  709. if [ "$IPV6_DETECTED" = "1" ]; then
  710. # Attempt to flush all IPv6 chains
  711. ##################################
  712. ip6tables -F
  713. ip6tables -X
  714.  
  715. # Flush builtin IPv6 chains
  716. ###########################
  717. ip6tables -F INPUT
  718. ip6tables -F OUTPUT
  719. ip6tables -F FORWARD
  720.  
  721. # Flush rules in mangle table
  722. #############################
  723. ip6tables -t mangle -F
  724. ip6tables -t mangle -X
  725. fi
  726.  
  727. # Create our "base" chains
  728. ##########################
  729. iptables -N BASE_INPUT_CHAIN
  730. iptables -N BASE_FORWARD_CHAIN
  731. iptables -N BASE_OUTPUT_CHAIN
  732.  
  733. # Accept packets of ESTABLISHED connections
  734. ###########################################
  735. iptables -A BASE_INPUT_CHAIN -m state --state ESTABLISHED -j ACCEPT
  736. iptables -A BASE_FORWARD_CHAIN -m state --state ESTABLISHED -j ACCEPT
  737. iptables -A BASE_OUTPUT_CHAIN -m state --state ESTABLISHED -j ACCEPT
  738.  
  739. # Accept packets of RELATED connections
  740. #######################################
  741. iptables -A BASE_INPUT_CHAIN -m state --state RELATED -p tcp --dport 1024: -j ACCEPT
  742. iptables -A BASE_INPUT_CHAIN -m state --state RELATED -p udp --dport 1024: -j ACCEPT
  743. iptables -A BASE_INPUT_CHAIN -m state --state RELATED -p icmp -j ACCEPT
  744. iptables -A BASE_FORWARD_CHAIN -m state --state RELATED -p tcp --dport 1024: -j ACCEPT
  745. iptables -A BASE_FORWARD_CHAIN -m state --state RELATED -p udp --dport 1024: -j ACCEPT
  746. iptables -A BASE_FORWARD_CHAIN -m state --state RELATED -p icmp -j ACCEPT
  747.  
  748. # Drop all IPv6 packets with Routing Header Type 0
  749. ##################################################
  750. if [ "$IPV6_SUPPORT" = "1" -a "$IPV6_DROP_RH_ZERO" != "0" ]; then
  751. if try_ip6tables -A BASE_INPUT_CHAIN -m rt --rt-type 0 -j DROP; then
  752. ip6tables -A BASE_FORWARD_CHAIN -m rt --rt-type 0 -j DROP
  753. ip6tables -A BASE_OUTPUT_CHAIN -m rt --rt-type 0 -j DROP
  754. else
  755. echo " WARNING: IPv6 Routing Header Type 0 matching not supported"
  756. fi
  757. fi
  758.  
  759. # Accept all packets for the loopback device
  760. ############################################
  761. iptables -A BASE_INPUT_CHAIN -i lo -j ACCEPT
  762. iptables -A BASE_FORWARD_CHAIN -i lo -j ACCEPT
  763. iptables -A BASE_OUTPUT_CHAIN -o lo -j ACCEPT
  764.  
  765. # Insert our base chains
  766. ########################
  767. iptables -A INPUT -j BASE_INPUT_CHAIN
  768. iptables -A FORWARD -j BASE_FORWARD_CHAIN
  769. iptables -A OUTPUT -j BASE_OUTPUT_CHAIN
  770.  
  771. # Create several chains that we will use later on
  772. #################################################
  773. create_user_chains;
  774.  
  775. # Reset the iptables counters
  776. iptables -Z
  777. iptables -t mangle -Z
  778. ip4tables -t nat -Z
  779. }
  780.  
  781.  
  782. # Check if the base chains are appropriate for the IPV6_SUPPORT setting
  783. #######################################################################
  784. check_for_base_chains()
  785. {
  786. local ipv4_rtn ipv6_rtn
  787.  
  788. ip4tables -nL BASE_INPUT_CHAIN >/dev/null 2>&1
  789. ipv4_rtn=$?
  790.  
  791. if [ "$IPV6_DETECTED" = "1" ]; then
  792. ip6tables -nL BASE_INPUT_CHAIN >/dev/null 2>&1
  793. ipv6_rtn=$?
  794. else
  795. # No IPv6 available, therefore no ip6tables call
  796. # Set a return error result of 1 to specify no IPv6 BASE_INPUT_CHAIN
  797. ipv6_rtn=1
  798. fi
  799.  
  800. if [ "$IPV6_SUPPORT" = "1" ]; then
  801. if [ $ipv4_rtn -eq 0 -a $ipv6_rtn -eq 0 ]; then
  802. echo "yes"
  803. elif [ $ipv4_rtn -eq 0 ]; then
  804. echo "other"
  805. else
  806. echo "no"
  807. fi
  808. else
  809. if [ $ipv4_rtn -eq 0 -a $ipv6_rtn -eq 0 ]; then
  810. echo "other"
  811. elif [ $ipv4_rtn -eq 0 ]; then
  812. echo "yes"
  813. else
  814. echo "no"
  815. fi
  816. fi
  817. }
  818.  
  819.  
  820. setup_default_policies()
  821. {
  822. # Set the default policies for the builtin INPUT & FORWARD tables. The
  823. # default for other chains (eg. OUTPUT) is always set to ACCEPT.
  824. #######################################################################
  825. if [ "$DEFAULT_POLICY_DROP" != "0" ]; then
  826. echo " Setting all default policies to DROP while \"setting up firewall rules\""
  827. iptables -P INPUT DROP
  828. iptables -P FORWARD DROP
  829. iptables -P OUTPUT DROP
  830. else
  831. echo " WARNING: Setting all default policies to ACCEPT while \"setting up firewall rules\""
  832. iptables -P INPUT ACCEPT
  833. iptables -P FORWARD ACCEPT
  834. iptables -P OUTPUT ACCEPT
  835. fi
  836.  
  837. if [ "$IPV6_SUPPORT" = "1" ]; then
  838. echo "IPv4/IPv6 mixed mode selected"
  839. elif [ "$IPV6_DETECTED" = "1" ]; then
  840. echo "IPv4 mode selected but IPv6 available, DROP all IPv6 packets"
  841. ip6tables -P INPUT DROP
  842. ip6tables -P FORWARD DROP
  843. ip6tables -P OUTPUT DROP
  844.  
  845. # Allow IPv6 traffic from the loopback (localhost)
  846. ip6tables -A INPUT -i lo -j ACCEPT
  847. ip6tables -A FORWARD -i lo -j ACCEPT
  848. ip6tables -A OUTPUT -o lo -j ACCEPT
  849.  
  850. # DROP all IPv6 traffic
  851. ip6tables -A INPUT -j DROP
  852. ip6tables -A FORWARD -j DROP
  853. ip6tables -A OUTPUT -j DROP
  854. else
  855. echo "IPv4 mode selected, no IPv6 available"
  856. fi
  857.  
  858. # The POST_INPUT_DROP_CHAIN should temporarily DROP for now
  859. # We'll change this once the rules are in place
  860. ###########################################################
  861. iptables -A POST_INPUT_DROP_CHAIN -j DROP
  862.  
  863. # The HOST_BLOCK_DROP chain should always DROP
  864. ###########################################################
  865. if [ "$BLOCKED_HOST_LOG" != "0" ]; then
  866. iptables -A HOST_BLOCK_DROP -m limit --limit 1/m --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Blocked host(s): "
  867. fi
  868. iptables -A HOST_BLOCK_DROP -j DROP
  869.  
  870. # The LINK_LOCAL_DROP chain should always DROP
  871. ###########################################################
  872. if [ "$IPV6_SUPPORT" = "1" ]; then
  873. if [ "$LINK_LOCAL_DROP_LOG" != "0" ]; then
  874. ip6tables -A LINK_LOCAL_DROP -m limit --limit 1/m --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Dropped Link-Local: "
  875. fi
  876. ip6tables -A LINK_LOCAL_DROP -j DROP
  877. fi
  878. }
  879.  
  880.  
  881. ##################################################################################################################
  882. ## Chain VALID_CHK - Check packets for invalid flags etc. ##
  883. ##################################################################################################################
  884. setup_valid_chk_chain()
  885. {
  886. ## Log scanning of nmap etc.
  887. ############################
  888. if [ "$SCAN_LOG" != "0" ]; then
  889. echo "Logging of stealth scans (nmap probes etc.) enabled"
  890.  
  891. # (NMAP) FIN/URG/PSH
  892. ####################
  893. iptables -A VALID_CHK -p tcp --tcp-flags ALL FIN,URG,PSH \
  894. -m limit --limit 3/m --limit-burst 5 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Stealth XMAS scan: "
  895.  
  896. # SYN/RST/ACK/FIN/URG
  897. #####################
  898. iptables -A VALID_CHK -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG \
  899. -m limit --limit 3/m --limit-burst 5 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Stealth XMAS-PSH scan: "
  900.  
  901. # ALL/ALL
  902. #########
  903. iptables -A VALID_CHK -p tcp --tcp-flags ALL ALL \
  904. -m limit --limit 3/m --limit-burst 5 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Stealth XMAS-ALL scan: "
  905.  
  906. # NMAP FIN Stealth
  907. ##################
  908. iptables -A VALID_CHK -p tcp --tcp-flags ALL FIN \
  909. -m limit --limit 3/m --limit-burst 5 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Stealth FIN scan: "
  910.  
  911. # SYN/RST
  912. #########
  913. iptables -A VALID_CHK -p tcp --tcp-flags SYN,RST SYN,RST \
  914. -m limit --limit 3/m --limit-burst 5 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Stealth SYN/RST scan: "
  915.  
  916. # SYN/FIN (probably)
  917. ####################
  918. iptables -A VALID_CHK -p tcp --tcp-flags SYN,FIN SYN,FIN \
  919. -m limit --limit 3/m --limit-burst 5 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Stealth SYN/FIN scan?: "
  920.  
  921. # Null scan
  922. ###########
  923. iptables -A VALID_CHK -p tcp --tcp-flags ALL NONE \
  924. -m limit --limit 3/m --limit-burst 5 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Stealth Null scan: "
  925.  
  926. else
  927. echo "Logging of stealth scans (nmap probes etc.) disabled"
  928. fi
  929.  
  930. # Drop (NMAP) scan packets:
  931. ###########################
  932.  
  933. # NMAP FIN/URG/PSH
  934. ##################
  935. iptables -A VALID_CHK -p tcp --tcp-flags ALL FIN,URG,PSH -j POST_INPUT_DROP_CHAIN
  936.  
  937. # SYN/RST/ACK/FIN/URG
  938. #####################
  939. iptables -A VALID_CHK -p tcp --tcp-flags ALL SYN,RST,ACK,FIN,URG -j POST_INPUT_DROP_CHAIN
  940.  
  941. # ALL/ALL Scan
  942. ##############
  943. iptables -A VALID_CHK -p tcp --tcp-flags ALL ALL -j POST_INPUT_DROP_CHAIN
  944.  
  945. # NMAP FIN Stealth
  946. ##################
  947. iptables -A VALID_CHK -p tcp --tcp-flags ALL FIN -j POST_INPUT_DROP_CHAIN
  948.  
  949. # SYN/RST
  950. #########
  951. iptables -A VALID_CHK -p tcp --tcp-flags SYN,RST SYN,RST -j POST_INPUT_DROP_CHAIN
  952.  
  953. # SYN/FIN -- Scan(probably)
  954. ###########################
  955. iptables -A VALID_CHK -p tcp --tcp-flags SYN,FIN SYN,FIN -j POST_INPUT_DROP_CHAIN
  956.  
  957. # NMAP Null Scan
  958. ################
  959. iptables -A VALID_CHK -p tcp --tcp-flags ALL NONE -j POST_INPUT_DROP_CHAIN
  960.  
  961. # Log packets with bad flags?
  962. #############################
  963. if [ "$BAD_FLAGS_LOG" != "0" ]; then
  964. echo "Logging of packets with bad TCP-flags enabled"
  965. iptables -A VALID_CHK -p tcp --tcp-option 64 \
  966. -m limit --limit 3/m --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Bad TCP flag(64): "
  967.  
  968. iptables -A VALID_CHK -p tcp --tcp-option 128 \
  969. -m limit --limit 3/m --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Bad TCP flag(128): "
  970. else
  971. echo "Logging of packets with bad TCP-flags disabled"
  972. fi
  973.  
  974. # Drop packets with bad tcp flags
  975. #################################
  976. iptables -A VALID_CHK -p tcp --tcp-option 64 -j POST_INPUT_DROP_CHAIN
  977. iptables -A VALID_CHK -p tcp --tcp-option 128 -j POST_INPUT_DROP_CHAIN
  978.  
  979. # These packets are normally from "lost connection" and thus can generate false alarms
  980. # So we might want to ignore such packets
  981. ######################################################################################
  982. # if [ "$LOST_CONNECTION_LOG" != "1" ]; then
  983. # iptables -A VALID_CHK -p tcp --tcp-flags SYN,ACK,FIN,RST ACK -j POST_INPUT_DROP_CHAIN
  984. # iptables -A VALID_CHK -p tcp --tcp-flags SYN,ACK,FIN,RST FIN -j POST_INPUT_DROP_CHAIN
  985. # iptables -A VALID_CHK -p tcp --tcp-flags SYN,ACK,FIN,RST RST -j POST_INPUT_DROP_CHAIN
  986. # iptables -A VALID_CHK -p tcp --tcp-flags SYN,ACK,FIN,RST ACK,FIN -j POST_INPUT_DROP_CHAIN
  987. # iptables -A VALID_CHK -p tcp --tcp-flags SYN,ACK,FIN,RST ACK,RST -j POST_INPUT_DROP_CHAIN
  988. # iptables -A VALID_CHK -p tcp --tcp-flags SYN,ACK,FIN,RST SYN,ACK -j POST_INPUT_DROP_CHAIN
  989. # fi
  990.  
  991. # Here we add some protection from random packets we receive, such as random sweeps from other
  992. # (possible) hacked computers, or just packets who are invalid, not belonging to ANY connection
  993. ###############################################################################################
  994. if [ "$INVALID_TCP_LOG" = "1" ]; then
  995. echo "Logging of INVALID TCP packets enabled"
  996.  
  997. iptables -A VALID_CHK -p tcp -m state --state INVALID \
  998. -m limit --limit 1/m --limit-burst 2 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:INVALID TCP: "
  999. else
  1000. echo "Logging of INVALID TCP packets disabled"
  1001. fi
  1002.  
  1003. if [ "$INVALID_UDP_LOG" = "1" ]; then
  1004. echo "Logging of INVALID UDP packets enabled"
  1005.  
  1006. iptables -A VALID_CHK -p udp -m state --state INVALID \
  1007. -m limit --limit 1/m --limit-burst 2 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:INVALID UDP: "
  1008. else
  1009. echo "Logging of INVALID UDP packets disabled"
  1010. fi
  1011.  
  1012. if [ "$INVALID_ICMP_LOG" = "1" ]; then
  1013. echo "Logging of INVALID ICMP packets enabled"
  1014.  
  1015. # Only log INVALID ICMP-request packets when we also want to log "normal" ICMP-request packets
  1016. if [ "$ICMP_REQUEST_LOG" != "0" ]; then
  1017. iptables -A VALID_CHK -p icmp --icmp-type echo-request -m state --state INVALID \
  1018. -m limit --limit 1/m --limit-burst 2 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:INVALID ICMP-request: "
  1019. fi
  1020.  
  1021. # Only log INVALID ICMP-other when enabled in the config
  1022. if [ "$ICMP_OTHER_LOG" != "0" ]; then
  1023. iptables -A VALID_CHK -p icmp ! --icmp-type echo-request -m state --state INVALID \
  1024. -m limit --limit 1/m --limit-burst 2 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:INVALID ICMP-other: "
  1025. fi
  1026. else
  1027. echo "Logging of INVALID ICMP packets disabled"
  1028. fi
  1029.  
  1030. # Drop invalid packets
  1031. ######################
  1032. iptables -A VALID_CHK -m state --state INVALID -j POST_INPUT_DROP_CHAIN
  1033.  
  1034. # Fragmentation cannot happen with IPV6 (and maybe even not with iptables/ipv4?)
  1035. ## Log fragmented packets
  1036. #########################
  1037. if [ "$FRAG_LOG" = "1" ]; then
  1038. echo "Logging of fragmented packets enabled"
  1039. ip4tables -A VALID_CHK -f -m limit --limit 3/m --limit-burst 1 -j LOG --log-prefix "AIF:Fragment packet: "
  1040. else
  1041. echo "Logging of IPv4 fragmented packets disabled"
  1042. fi
  1043.  
  1044. # Drop fragmented packets
  1045. #########################
  1046. ip4tables -A VALID_CHK -f -j DROP
  1047. }
  1048.  
  1049.  
  1050. ################################################################################################################
  1051. ## Chain RESERVED_NET_CHK - Check if the source addresses of the packets are (in)valid ##
  1052. ################################################################################################################
  1053. setup_reserved_net_chk_chain()
  1054. {
  1055. # Log access from reserved addresses
  1056. ####################################
  1057. if [ "$RESERVED_NET_LOG" = "1" ]; then
  1058. echo "Logging of access from reserved addresses enabled"
  1059. ip4tables -A RESERVED_NET_CHK -s 10.0.0.0/8 \
  1060. -m limit --limit 1/m --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:IPv4 Private address: "
  1061.  
  1062. ip4tables -A RESERVED_NET_CHK -s 172.16.0.0/12 \
  1063. -m limit --limit 1/m --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:IPv4 Private address: "
  1064.  
  1065. ip4tables -A RESERVED_NET_CHK -s 192.168.0.0/16 \
  1066. -m limit --limit 1/m --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:IPv4 Private address: "
  1067.  
  1068. ip4tables -A RESERVED_NET_CHK -s 169.254.0.0/16 \
  1069. -m limit --limit 1/m --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:IPv4 Link-local address: "
  1070.  
  1071. ip4tables -A RESERVED_NET_CHK -s 224.0.0.0/24 \
  1072. -m limit --limit 1/m --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:IPv4 Multicast address: "
  1073.  
  1074. ip4tables -A RESERVED_NET_CHK -s 239.0.0.0/24 \
  1075. -m limit --limit 1/m --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:IPv4 Multicast address: "
  1076.  
  1077. if [ "$IPV6_SUPPORT" = "1" ]; then
  1078. # IPv6 not 2000::/3 is non-Global Unicast
  1079. ip6tables -A RESERVED_NET_CHK ! -s 2000::/3 \
  1080. -m limit --limit 1/m --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:IPv6 non-Global address: "
  1081. fi
  1082. else
  1083. echo "Logging of access from reserved addresses disabled"
  1084. fi
  1085.  
  1086. if [ "$DROP_PRIVATE_ADDRESSES" = "1" ]; then
  1087. # rp_filter drops some of these addresses, but just to be sure :)
  1088. ################################################################
  1089. #echo "Denying access from reserved addresses..."
  1090. ip4tables -A RESERVED_NET_CHK -s 10.0.0.0/8 -j POST_INPUT_DROP_CHAIN
  1091. ip4tables -A RESERVED_NET_CHK -s 172.16.0.0/12 -j POST_INPUT_DROP_CHAIN
  1092. ip4tables -A RESERVED_NET_CHK -s 192.168.0.0/16 -j POST_INPUT_DROP_CHAIN
  1093. ip4tables -A RESERVED_NET_CHK -s 169.254.0.0/16 -j POST_INPUT_DROP_CHAIN
  1094. ip4tables -A RESERVED_NET_CHK -s 224.0.0.0/24 -j POST_INPUT_DROP_CHAIN
  1095. ip4tables -A RESERVED_NET_CHK -s 239.0.0.0/24 -j POST_INPUT_DROP_CHAIN
  1096.  
  1097. if [ "$IPV6_SUPPORT" = "1" ]; then
  1098. # IPv6 not 2000::/3 is non-Global Unicast
  1099. ip6tables -A RESERVED_NET_CHK ! -s 2000::/3 -j POST_INPUT_DROP_CHAIN
  1100. fi
  1101. fi
  1102. }
  1103.  
  1104.  
  1105. ################################################################################################################
  1106. ## Chain SPOOF_CHK - Check if the source address is not spoofed ##
  1107. ################################################################################################################
  1108. setup_spoof_chk_chain()
  1109. {
  1110. # Anti-spoof protection for the internal net
  1111. if [ -n "$INT_IF" -a -n "$INTERNAL_NET" ]; then
  1112. if [ "$INTERNAL_NET_ANTISPOOF" != "0" ]; then
  1113. printf "Setting up antispoof for INTERNAL net(s): "
  1114. IFS=' ,'
  1115. for net in $INTERNAL_NET; do
  1116. printf "$net "
  1117. for interface in $INT_IF; do
  1118. # Any internal net is valid
  1119. iptables -A SPOOF_CHK -i $interface -s $net -j RETURN
  1120. done
  1121. iptables -A SPOOF_CHK -s $net -m limit --limit 3/m -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Spoofed packet: "
  1122. iptables -A SPOOF_CHK -s $net -j POST_INPUT_DROP_CHAIN
  1123. done
  1124. echo ""
  1125. else
  1126. echo "Antispoof for INTERNAL net(s) DISABLED!"
  1127. fi
  1128. fi
  1129.  
  1130. # Anti-spoof protection for the DMZ net
  1131. if [ -n "$DMZ_IF" -a -n "$DMZ_NET" ]; then
  1132. if [ "$DMZ_NET_ANTISPOOF" != "0" ]; then
  1133. printf "Setting up antispoof for DMZ net(s): "
  1134. IFS=' ,'
  1135. for net in $DMZ_NET; do
  1136. printf "$net "
  1137. for interface in $DMZ_IF; do
  1138. # Any dmz net is valid
  1139. iptables -A SPOOF_CHK -i $interface -s $net -j RETURN
  1140. done
  1141. iptables -A SPOOF_CHK -s $net -m limit --limit 3/m -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Spoofed packet: "
  1142. iptables -A SPOOF_CHK -s $net -j POST_INPUT_DROP_CHAIN
  1143. done
  1144. echo ""
  1145. else
  1146. echo "Antispoof for DMZ net(s) DISABLED!"
  1147. fi
  1148. fi
  1149.  
  1150. # Everything else is valid
  1151. iptables -A SPOOF_CHK -j RETURN
  1152. }
  1153.  
  1154.  
  1155. ################################################################
  1156. # Setup rules to forward INET IPv6 and non-NAT'ed IPv4 traffic #
  1157. ################################################################
  1158. setup_inet_forward_rules()
  1159. {
  1160. # TCP ports to ALLOW for certain INET hosts
  1161. #########################################
  1162. unset IFS
  1163. for rule in $INET_FORWARD_TCP; do
  1164. if_shosts=`echo "$rule" |cut -s -d'>' -f1`
  1165. interfaces=`get_ifs "$if_shosts" "$EXT_IF"`
  1166. shosts=`get_hosts_ih "$if_shosts"`
  1167. dhosts_ports=`echo "$rule" |cut -s -d'>' -f2`
  1168. dhosts=`get_hosts_hp "$dhosts_ports"`
  1169. ports=`get_ports_hp "$dhosts_ports" "$ANYPORT"`
  1170.  
  1171. echo "$(show_if_ip "$interfaces")Forwarding(non-NAT) TCP port(s): $ports from $shosts(INET) to $dhosts"
  1172.  
  1173. IFS=' ,'
  1174. for shost in `ip_range "$shosts"`; do
  1175. for dhost in `ip_range "$dhosts"`; do
  1176. for port in $ports; do
  1177. for interface in $interfaces; do
  1178. iptables -A EXT_FORWARD_IN_CHAIN -i $interface ! -o $interface -s $shost -d $dhost -p tcp --dport $port -j ACCEPT
  1179. done
  1180. done
  1181. done
  1182. done
  1183. done
  1184.  
  1185. # UDP ports to ALLOW for certain INET hosts
  1186. #########################################
  1187. unset IFS
  1188. for rule in $INET_FORWARD_UDP; do
  1189. if_shosts=`echo "$rule" |cut -s -d'>' -f1`
  1190. interfaces=`get_ifs "$if_shosts" "$EXT_IF"`
  1191. shosts=`get_hosts_ih "$if_shosts"`
  1192. dhosts_ports=`echo "$rule" |cut -s -d'>' -f2`
  1193. dhosts=`get_hosts_hp "$dhosts_ports"`
  1194. ports=`get_ports_hp "$dhosts_ports" "$ANYPORT"`
  1195.  
  1196. echo "$(show_if_ip "$interfaces")Forwarding(non-NAT) UDP port(s): $ports from $shosts(INET) to $dhosts"
  1197.  
  1198. IFS=' ,'
  1199. for shost in `ip_range "$shosts"`; do
  1200. for dhost in `ip_range "$dhosts"`; do
  1201. for port in $ports; do
  1202. for interface in $interfaces; do
  1203. iptables -A EXT_FORWARD_IN_CHAIN -i $interface ! -o $interface -s $shost -d $dhost -p udp --dport $port -j ACCEPT
  1204. done
  1205. done
  1206. done
  1207. done
  1208. done
  1209.  
  1210. # (Other) IP protocols to ALLOW for certain INET hosts
  1211. ######################################################
  1212. unset IFS
  1213. for rule in $INET_FORWARD_IP; do
  1214. if_shosts=`echo "$rule" |cut -s -d'>' -f1`
  1215. interfaces=`get_ifs "$if_shosts" "$EXT_IF"`
  1216. shosts=`get_hosts_ih "$if_shosts"`
  1217. dhosts_ports=`echo "$rule" |cut -s -d'>' -f2`
  1218. dhosts=`get_hosts_hp "$dhosts_ports"`
  1219. protos=`get_ports_hp "$dhosts_ports"`
  1220.  
  1221. echo "$(show_if_ip "$interfaces")Forwarding(non-NAT) IP protocol(s): $protos from $shosts(INET) to $dhosts"
  1222.  
  1223. IFS=' ,'
  1224. for shost in `ip_range "$shosts"`; do
  1225. for dhost in `ip_range "$dhosts"`; do
  1226. for proto in $protos; do
  1227. for interface in $interfaces; do
  1228. iptables -A EXT_FORWARD_IN_CHAIN -i $interface ! -o $interface -s $shost -d $dhost -p $proto -j ACCEPT
  1229. done
  1230. done
  1231. done
  1232. done
  1233. done
  1234. }
  1235.  
  1236.  
  1237. ##################################################
  1238. # Setup chain for the DMZ input traffic #
  1239. ##################################################
  1240. setup_dmz_input_chain()
  1241. {
  1242. # Add TCP ports to allow for certain hosts
  1243. ##########################################
  1244. unset IFS
  1245. for rule in $DMZ_HOST_OPEN_TCP; do
  1246. hosts=`get_hosts_ihp "$rule"`
  1247. ports=`get_ports_ihp "$rule"`
  1248.  
  1249. echo " Allowing $hosts(DMZ) for TCP port(s): $ports"
  1250.  
  1251. IFS=','
  1252. for host in `ip_range "$hosts"`; do
  1253. for port in $ports; do
  1254. iptables -A DMZ_INPUT_CHAIN -s $host -p tcp --dport $port -j ACCEPT
  1255. done
  1256. done
  1257. done
  1258.  
  1259. # Add UDP ports to allow for certain hosts
  1260. ##########################################
  1261. unset IFS
  1262. for rule in $DMZ_HOST_OPEN_UDP; do
  1263. hosts=`get_hosts_ihp "$rule"`
  1264. ports=`get_ports_ihp "$rule"`
  1265.  
  1266. echo " Allowing $hosts(DMZ) for UDP port(s): $ports"
  1267.  
  1268. IFS=','
  1269. for host in `ip_range "$hosts"`; do
  1270. for port in $ports; do
  1271. iptables -A DMZ_INPUT_CHAIN -s $host -p udp --dport $port -j ACCEPT
  1272. done
  1273. done
  1274. done
  1275.  
  1276. # Add IP protocols to allow for certain hosts
  1277. #############################################
  1278. unset IFS
  1279. for rule in $DMZ_HOST_OPEN_IP; do
  1280. hosts=`get_hosts_ihp "$rule"`
  1281. protos=`get_ports_ihp "$rule"`
  1282.  
  1283. echo " Allowing $hosts(DMZ) for IP protocol(s): $protos"
  1284.  
  1285. IFS=','
  1286. for host in `ip_range "$hosts"`; do
  1287. for proto in $protos; do
  1288. iptables -A DMZ_INPUT_CHAIN -s $host -p $proto -j ACCEPT
  1289. done
  1290. done
  1291. done
  1292.  
  1293. # Adding TCP ports NOT to be firewalled
  1294. #######################################
  1295. if [ -n "$DMZ_OPEN_TCP" ]; then
  1296. echo " Allowing TCP port(s): $DMZ_OPEN_TCP"
  1297. IFS=' ,'
  1298. for port in $DMZ_OPEN_TCP; do
  1299. iptables -A DMZ_INPUT_CHAIN -p tcp --dport $port -j ACCEPT
  1300. done
  1301. fi
  1302.  
  1303. # Adding UDP ports NOT to be firewalled
  1304. #######################################
  1305. if [ -n "$DMZ_OPEN_UDP" ]; then
  1306. echo " Allowing UDP port(s): $DMZ_OPEN_UDP"
  1307. IFS=' ,'
  1308. for port in $DMZ_OPEN_UDP; do
  1309. iptables -A DMZ_INPUT_CHAIN -p udp --dport $port -j ACCEPT
  1310. done
  1311. fi
  1312.  
  1313. # Adding IP protocols NOT to be firewalled
  1314. ##########################################
  1315. if [ -n "$DMZ_OPEN_IP" ]; then
  1316. echo " Allowing IP protocol(s): $DMZ_OPEN_IP"
  1317. IFS=' ,'
  1318. for proto in $DMZ_OPEN_IP; do
  1319. iptables -A DMZ_INPUT_CHAIN -p $proto -j ACCEPT
  1320. done
  1321. fi
  1322.  
  1323. # Allow to send ICMP packets?
  1324. #############################
  1325. if [ "$DMZ_OPEN_ICMP" != "0" ]; then
  1326. echo " Allowing ICMP-requests(ping)"
  1327. iptables -A DMZ_INPUT_CHAIN -p icmp --icmp-type echo-request -m limit --limit 20/second --limit-burst 100 -j ACCEPT
  1328. if [ "$IPV6_SUPPORT" = "1" ]; then
  1329. for icmpv6_type in $ICMPV6_SPECIAL_TYPES; do
  1330. ip6tables -A DMZ_INPUT_CHAIN -p icmpv6 --icmpv6-type $icmpv6_type -m hl --hl-eq 255 -j ACCEPT
  1331. done
  1332. fi
  1333. fi
  1334.  
  1335. # Log incoming ICMP-request packets?
  1336. ####################################
  1337. if [ "$ICMP_REQUEST_LOG" != "0" ]; then
  1338. iptables -A DMZ_INPUT_CHAIN -p icmp --icmp-type echo-request \
  1339. -m limit --limit 3/m --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:ICMP-request: "
  1340. fi
  1341.  
  1342. # Drop ICMP packets
  1343. iptables -A DMZ_INPUT_CHAIN -p icmp --icmp-type echo-request -j DROP
  1344.  
  1345. # Log everything else
  1346. iptables -A DMZ_INPUT_CHAIN -m limit --limit 3/m -j LOG --log-level $LOGLEVEL --log-prefix "AIF:DMZ-INPUT denied: "
  1347.  
  1348. # Everything else is denied
  1349. iptables -A DMZ_INPUT_CHAIN -j DROP
  1350. }
  1351.  
  1352.  
  1353. ##################################################
  1354. # Setup chain for the DMZ-to-LAN forward traffic #
  1355. ##################################################
  1356. setup_dmz_lan_forward_chain()
  1357. {
  1358. echo " Setting up DMZ->LAN policy"
  1359.  
  1360. # TCP ports to ALLOW for certain DMZ hosts
  1361. ##########################################
  1362. unset IFS
  1363. for rule in $DMZ_LAN_HOST_OPEN_TCP; do
  1364. shosts=`get_hosts_ih $(echo "$rule" |cut -s -d'>' -f1) "$ANYHOST"`
  1365. dhosts=`get_hosts_hp $(echo "$rule" |cut -s -d'>' -f2)`
  1366. ports=`get_ports_hp $(echo "$rule" |cut -s -d'>' -f2) "$ANYPORT"`
  1367.  
  1368. echo " Allowing $shosts(DMZ) to $dhosts(LAN) for TCP port(s): $ports"
  1369.  
  1370. IFS=','
  1371. for shost in `ip_range "$shosts"`; do
  1372. for dhost in `ip_range "$dhosts"`; do
  1373. for port in $ports; do
  1374. iptables -A DMZ_LAN_FORWARD_CHAIN -s $shost -d $dhost -p tcp --dport $port -j ACCEPT
  1375. done
  1376. done
  1377. done
  1378. done
  1379.  
  1380. # UDP ports to ALLOW for certain DMZ hosts
  1381. ##########################################
  1382. unset IFS
  1383. for rule in $DMZ_LAN_HOST_OPEN_UDP; do
  1384. shosts=`get_hosts_ih $(echo "$rule" |cut -s -d'>' -f1) "$ANYHOST"`
  1385. dhosts=`get_hosts_hp $(echo "$rule" |cut -s -d'>' -f2)`
  1386. ports=`get_ports_hp $(echo "$rule" |cut -s -d'>' -f2) "$ANYPORT"`
  1387.  
  1388. echo " Allowing $shosts(DMZ) to $dhosts(LAN) for UDP port(s): $ports"
  1389.  
  1390. IFS=','
  1391. for shost in `ip_range "$shosts"`; do
  1392. for dhost in `ip_range "$dhosts"`; do
  1393. for port in $ports; do
  1394. iptables -A DMZ_LAN_FORWARD_CHAIN -s $shost -d $dhost -p udp --dport $port -j ACCEPT
  1395. done
  1396. done
  1397. done
  1398. done
  1399.  
  1400. # IP protocol(s) to ALLOW for certain DMZ hosts
  1401. ###############################################
  1402. unset IFS
  1403. for rule in $DMZ_LAN_HOST_OPEN_IP; do
  1404. shosts=`get_hosts_ih $(echo "$rule" |cut -s -d'>' -f1) "$ANYHOST"`
  1405. dhosts=`get_hosts_hp $(echo "$rule" |cut -s -d'>' -f2)`
  1406. protos=`get_ports_hp $(echo "$rule" |cut -s -d'>' -f2)`
  1407.  
  1408. echo " Allowing $shosts(DMZ) to $dhosts(LAN) for IP protocol(s): $protos"
  1409.  
  1410. IFS=','
  1411. for shost in `ip_range "$shosts"`; do
  1412. for dhost in `ip_range "$dhosts"`; do
  1413. for proto in $protos; do
  1414. iptables -A DMZ_LAN_FORWARD_CHAIN -s $shost -d $dhost -p $proto -j ACCEPT
  1415. done
  1416. done
  1417. done
  1418. done
  1419.  
  1420. # Allow ICMP-requests(ping) for DMZ->LAN?
  1421. ##########################################
  1422. if [ "$DMZ_LAN_OPEN_ICMP" = "1" ]; then
  1423. echo " Allowing ICMP-requests(ping)"
  1424. iptables -A DMZ_LAN_FORWARD_CHAIN -p icmp --icmp-type echo-request \
  1425. -m limit --limit 20/second --limit-burst 100 -j ACCEPT
  1426. fi
  1427.  
  1428. # Log incoming ICMP-request packets?
  1429. ####################################
  1430. if [ "$ICMP_REQUEST_LOG" != "0" ]; then
  1431. iptables -A DMZ_LAN_FORWARD_CHAIN -p icmp --icmp-type echo-request \
  1432. -m limit --limit 3/m --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:ICMP-request: "
  1433. fi
  1434.  
  1435. # Drop ICMP packets
  1436. iptables -A DMZ_LAN_FORWARD_CHAIN -p icmp --icmp-type echo-request -j DROP
  1437.  
  1438. # Log everything else
  1439. iptables -A DMZ_LAN_FORWARD_CHAIN -m limit --limit 3/m -j LOG --log-level $LOGLEVEL --log-prefix "AIF:DMZ->LAN denied: "
  1440.  
  1441. # Everything else is denied
  1442. iptables -A DMZ_LAN_FORWARD_CHAIN -j DROP
  1443. }
  1444.  
  1445.  
  1446. ###################################################
  1447. # Setup chain for the INET-to-DMZ forward traffic #
  1448. ###################################################
  1449. setup_inet_dmz_forward_chain()
  1450. {
  1451. echo " Setting up INET->DMZ policy"
  1452.  
  1453. # TCP ports to ALLOW for certain INET hosts
  1454. #########################################
  1455. unset IFS
  1456. for rule in $INET_DMZ_HOST_OPEN_TCP; do
  1457. if_shosts=`echo "$rule" |cut -s -d'>' -f1`
  1458. interfaces=`get_ifs "$if_shosts"`
  1459. shosts=`get_hosts_ih "$if_shosts"`
  1460. dhosts_ports=`echo "$rule" |cut -s -d'>' -f2`
  1461. dhosts=`get_hosts_hp "$dhosts_ports"`
  1462. ports=`get_ports_hp "$dhosts_ports" "$ANYPORT"`
  1463.  
  1464. echo " $(show_if_ip "$interfaces")Allowing $shosts(INET) to $dhosts(DMZ) for TCP port(s): $ports"
  1465.  
  1466. IFS=','
  1467. for shost in `ip_range "$shosts"`; do
  1468. for dhost in `ip_range "$dhosts"`; do
  1469. for port in $ports; do
  1470. for interface in $interfaces; do
  1471. iptables -A INET_DMZ_FORWARD_CHAIN -i $interface -s $shost -d $dhost -p tcp --dport $port -j ACCEPT
  1472. done
  1473. done
  1474. done
  1475. done
  1476. done
  1477.  
  1478. # UDP ports to ALLOW for certain INET hosts
  1479. #########################################
  1480. unset IFS
  1481. for rule in $INET_DMZ_HOST_OPEN_UDP; do
  1482. if_shosts=`echo "$rule" |cut -s -d'>' -f1`
  1483. interfaces=`get_ifs "$if_shosts"`
  1484. shosts=`get_hosts_ih "$if_shosts"`
  1485. dhosts_ports=`echo "$rule" |cut -s -d'>' -f2`
  1486. dhosts=`get_hosts_hp "$dhosts_ports"`
  1487. ports=`get_ports_hp "$dhosts_ports" "$ANYPORT"`
  1488.  
  1489. echo " $(show_if_ip "$interfaces")Allowing $shosts(INET) to $dhosts(DMZ) for UDP port(s): $ports"
  1490.  
  1491. IFS=','
  1492. for shost in `ip_range "$shosts"`; do
  1493. for dhost in `ip_range "$dhosts"`; do
  1494. for port in $ports; do
  1495. for interface in $interfaces; do
  1496. iptables -A INET_DMZ_FORWARD_CHAIN -i $interface -s $shost -d $dhost -p udp --dport $port -j ACCEPT
  1497. done
  1498. done
  1499. done
  1500. done
  1501. done
  1502.  
  1503. # (Other) IP protocols to ALLOW for certain INET hosts
  1504. ######################################################
  1505. unset IFS
  1506. for rule in $INET_DMZ_HOST_OPEN_IP; do
  1507. if_shosts=`echo "$rule" |cut -s -d'>' -f1`
  1508. interfaces=`get_ifs "$if_shosts"`
  1509. shosts=`get_hosts_ih "$if_shosts"`
  1510. dhosts_ports=`echo "$rule" |cut -s -d'>' -f2`
  1511. dhosts=`get_hosts_hp "$dhosts_ports"`
  1512. protos=`get_ports_hp "$dhosts_ports"`
  1513.  
  1514. echo " $(show_if_ip "$interfaces")Allowing $shosts(INET) to $dhosts(DMZ) for IP protocol(s): $protos"
  1515.  
  1516. IFS=','
  1517. for shost in `ip_range "$shosts"`; do
  1518. for dhost in `ip_range "$dhosts"`; do
  1519. for proto in $protos; do
  1520. for interface in $interfaces; do
  1521. iptables -A INET_DMZ_FORWARD_CHAIN -i $interface -s $shost -d $dhost -p $proto -j ACCEPT
  1522. done
  1523. done
  1524. done
  1525. done
  1526. done
  1527.  
  1528.  
  1529. # TCP ports to DENY for certain INET hosts
  1530. #########################################
  1531. unset IFS
  1532. for rule in $INET_DMZ_HOST_DENY_TCP; do
  1533. if_shosts=`echo "$rule" |cut -s -d'>' -f1`
  1534. interfaces=`get_ifs "$if_shosts"`
  1535. shosts=`get_hosts_ih "$if_shosts" "$ANYHOST"`
  1536. dhosts_ports=`echo "$rule" |cut -s -d'>' -f2`
  1537. dhosts=`get_hosts_hp "$dhosts_ports"`
  1538. ports=`get_ports_hp "$dhosts_ports" "$ANYPORT"`
  1539.  
  1540. echo " $(show_if_ip "$interfaces")Denying $shosts(INET) to $dhosts(DMZ) for TCP port(s): $ports"
  1541.  
  1542. IFS=','
  1543. for shost in `ip_range "$shosts"`; do
  1544. for dhost in `ip_range "$dhosts"`; do
  1545. for port in $ports; do
  1546. for interface in $interfaces; do
  1547. if [ "$DMZ_INPUT_DENY_LOG" != "0" ]; then
  1548. iptables -A INET_DMZ_FORWARD_CHAIN -i $interface -s $shost -d $dhost -p tcp --dport $port \
  1549. -m limit --limit 1/h --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Host INET->DMZ denied: "
  1550. fi
  1551. iptables -A INET_DMZ_FORWARD_CHAIN -i $interface -s $shost -d $dhost -p tcp --dport $port -j DROP
  1552. done
  1553. done
  1554. done
  1555. done
  1556. done
  1557.  
  1558. # UDP ports to DENY for certain INET hosts
  1559. #########################################
  1560. unset IFS
  1561. for rule in $INET_DMZ_HOST_DENY_UDP; do
  1562. if_shosts=`echo "$rule" |cut -s -d'>' -f1`
  1563. interfaces=`get_ifs "$if_shosts"`
  1564. shosts=`get_hosts_ih "$if_shosts" "$ANYHOST"`
  1565. dhosts_ports=`echo "$rule" |cut -s -d'>' -f2`
  1566. dhosts=`get_hosts_hp "$dhosts_ports"`
  1567. ports=`get_ports_hp "$dhosts_ports" "$ANYPORT"`
  1568.  
  1569. echo " $(show_if_ip "$interfaces")Denying $shosts(INET) to $dhosts(DMZ) for UDP port(s): $ports"
  1570.  
  1571. IFS=','
  1572. for shost in `ip_range "$shosts"`; do
  1573. for dhost in `ip_range "$dhosts"`; do
  1574. for port in $ports; do
  1575. for interface in $interfaces; do
  1576. if [ "$DMZ_INPUT_DENY_LOG" != "0" ]; then
  1577. iptables -A INET_DMZ_FORWARD_CHAIN -i $interface -s $shost -d $dhost -p udp --dport $port \
  1578. -m limit --limit 1/h --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Host INET->DMZ denied: "
  1579. fi
  1580. iptables -A INET_DMZ_FORWARD_CHAIN -i $interface -s $shost -d $dhost -p udp --dport $port -j DROP
  1581. done
  1582. done
  1583. done
  1584. done
  1585. done
  1586.  
  1587. # (Other) IP protocols to DENY for certain INET hosts
  1588. #####################################################
  1589. unset IFS
  1590. for rule in $INET_DMZ_HOST_DENY_IP; do
  1591. if_shosts=`echo "$rule" |cut -s -d'>' -f1`
  1592. interfaces=`get_ifs "$if_shosts"`
  1593. shosts=`get_hosts_ih "$if_shosts" "$ANYHOST"`
  1594. dhosts_ports=`echo "$rule" |cut -s -d'>' -f2`
  1595. dhosts=`get_hosts_hp "$dhosts_ports"`
  1596. protos=`get_ports_hp "$dhosts_ports"`
  1597.  
  1598. echo " $(show_if_ip "$interfaces")Denying $shosts(INET) to $dhosts(DMZ) for IP protocol(s): $protos"
  1599.  
  1600. IFS=','
  1601. for shost in `ip_range "$shosts"`; do
  1602. for dhost in `ip_range "$dhosts"`; do
  1603. for proto in $protos; do
  1604. for interface in $interfaces; do
  1605. if [ "$DMZ_INPUT_DENY_LOG" != "0" ]; then
  1606. iptables -A INET_DMZ_FORWARD_CHAIN -i $interface -s $shost -d $dhost -p $proto \
  1607. -m limit --limit 1/h --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Host INET->DMZ denied: "
  1608. fi
  1609. iptables -A INET_DMZ_FORWARD_CHAIN -i $interface -s $shost -d $dhost -p $proto -j DROP
  1610. done
  1611. done
  1612. done
  1613. done
  1614. done
  1615.  
  1616. # Allow only certain TCP ports to be used from the INET->DMZ?
  1617. #############################################################
  1618. unset IFS
  1619. for rule in $INET_DMZ_OPEN_TCP; do
  1620. interfaces=`get_ifs "$rule"`
  1621. destips=`get_ips "$rule"`
  1622. ports=`get_ports_ip "$rule"`
  1623.  
  1624. echo " $(show_if_ip "$interfaces" "$destips")Allowing TCP port(s): $ports"
  1625.  
  1626. IFS=','
  1627. for port in $ports; do
  1628. for destip in $destips; do
  1629. for interface in $interfaces; do
  1630. iptables -A INET_DMZ_FORWARD_CHAIN -i $interface -d $destip -p tcp --dport $port -j ACCEPT
  1631. done
  1632. done
  1633. done
  1634. done
  1635.  
  1636. # Allow only certain UDP ports to be used from the INET->DMZ?
  1637. #############################################################
  1638. unset IFS
  1639. for rule in $INET_DMZ_OPEN_UDP; do
  1640. interfaces=`get_ifs "$rule"`
  1641. destips=`get_ips "$rule"`
  1642. ports=`get_ports_ip "$rule"`
  1643.  
  1644. echo " $(show_if_ip "$interfaces" "$destips")Allowing UDP port(s): $ports"
  1645.  
  1646. IFS=','
  1647. for port in $ports; do
  1648. for destip in $destips; do
  1649. for interface in $interfaces; do
  1650. iptables -A INET_DMZ_FORWARD_CHAIN -i $interface -d $destip -p udp --dport $port -j ACCEPT
  1651. done
  1652. done
  1653. done
  1654. done
  1655.  
  1656. # Allow only certain IP protocols to be used from the INET->DMZ?
  1657. ################################################################
  1658. unset IFS
  1659. for rule in $INET_DMZ_OPEN_IP; do
  1660. interfaces=`get_ifs "$rule"`
  1661. destips=`get_ips "$rule"`
  1662. protos=`get_ports_ip "$rule"`
  1663.  
  1664. echo " $(show_if_ip "$interfaces" "$destips")Allowing IP protocol(s): $protos"
  1665.  
  1666. IFS=','
  1667. for proto in $protos; do
  1668. for destip in $destips; do
  1669. for interface in $interfaces; do
  1670. iptables -A INET_DMZ_FORWARD_CHAIN -i $interface -d $destip -p $proto -j ACCEPT
  1671. done
  1672. done
  1673. done
  1674. done
  1675.  
  1676. # Allow ICMP-requests(ping) for INET->DMZ?
  1677. ##########################################
  1678. if [ "$INET_DMZ_OPEN_ICMP" = "1" ]; then
  1679. echo " Allowing ICMP-requests(ping)"
  1680. iptables -A INET_DMZ_FORWARD_CHAIN -p icmp --icmp-type echo-request \
  1681. -m limit --limit 20/second --limit-burst 100 -j ACCEPT
  1682. fi
  1683.  
  1684. # TCP ports to DENY for INET->DMZ
  1685. #################################
  1686. unset IFS
  1687. for rule in $INET_DMZ_DENY_TCP; do
  1688. interfaces=`get_ifs "$rule"`
  1689. destips=`get_ips "$rule"`
  1690. ports=`get_ports_ip "$rule"`
  1691.  
  1692. echo " $(show_if_ip "$interfaces" "$destips")Denying TCP port(s): $ports"
  1693.  
  1694. IFS=','
  1695. for port in $ports; do
  1696. for destip in $destips; do
  1697. for interface in $interfaces; do
  1698. if [ "$DMZ_INPUT_DENY_LOG" != "0" ]; then
  1699. iptables -A INET_DMZ_FORWARD_CHAIN -i $interface -d $destip -p tcp --dport $port -m limit \
  1700. --limit 1/s --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:INET->DMZ denied: "
  1701. fi
  1702. iptables -A INET_DMZ_FORWARD_CHAIN -i $interface -d $destip -p tcp --dport $port -j DROP
  1703. done
  1704. done
  1705. done
  1706. done
  1707.  
  1708. # UDP ports to DENY for INET->DMZ
  1709. #################################
  1710. unset IFS
  1711. for rule in $INET_DMZ_DENY_UDP; do
  1712. interfaces=`get_ifs "$rule"`
  1713. destips=`get_ips "$rule"`
  1714. ports=`get_ports_ip "$rule"`
  1715.  
  1716. echo " $(show_if_ip "$interfaces" "$destips")Denying UDP port(s): $ports"
  1717.  
  1718. IFS=','
  1719. for port in $ports; do
  1720. for destip in $destips; do
  1721. for interface in $interfaces; do
  1722. if [ "$DMZ_INPUT_DENY_LOG" != "0" ]; then
  1723. iptables -A INET_DMZ_FORWARD_CHAIN -i $interface -d $destip -p udp --dport $port -m limit \
  1724. --limit 1/s --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:INET->DMZ denied: "
  1725. fi
  1726. iptables -A INET_DMZ_FORWARD_CHAIN -i $interface -d $destip -p udp --dport $port -j DROP
  1727. done
  1728. done
  1729. done
  1730. done
  1731.  
  1732. # IP protocols to DENY for INET->DMZ
  1733. ####################################
  1734. unset IFS
  1735. for rule in $INET_DMZ_DENY_IP; do
  1736. interfaces=`get_ifs "$rule"`
  1737. destips=`get_ips "$rule"`
  1738. protos=`get_ports_ip "$rule"`
  1739.  
  1740. echo " $(show_if_ip "$interfaces" "$destips")Denying IP protocol(s): $protos"
  1741.  
  1742. IFS=','
  1743. for proto in $protos; do
  1744. for destip in $destips; do
  1745. for interface in $interfaces; do
  1746. if [ "$DMZ_INPUT_DENY_LOG" != "0" ]; then
  1747. iptables -A INET_DMZ_FORWARD_CHAIN -i $interface -d $destip -p $proto -m limit \
  1748. --limit 1/s --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:INET->DMZ denied: "
  1749. fi
  1750. iptables -A INET_DMZ_FORWARD_CHAIN -i $interface -d $destip -p $proto -j DROP
  1751. done
  1752. done
  1753. done
  1754. done
  1755.  
  1756. # Log incoming ICMP-request packets?
  1757. ####################################
  1758. if [ "$ICMP_REQUEST_LOG" != "0" ]; then
  1759. iptables -A INET_DMZ_FORWARD_CHAIN -p icmp --icmp-type echo-request \
  1760. -m limit --limit 3/m --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:ICMP-request: "
  1761. fi
  1762.  
  1763. echo " Denying all other INET->DMZ packets"
  1764.  
  1765. # Drop ICMP packets
  1766. iptables -A INET_DMZ_FORWARD_CHAIN -p icmp --icmp-type echo-request -j DROP
  1767.  
  1768. if [ "$DMZ_INPUT_DENY_LOG" != "0" ]; then
  1769. iptables -A INET_DMZ_FORWARD_CHAIN \
  1770. -m limit --limit 1/s --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:INET->DMZ denied: "
  1771. fi
  1772. iptables -A INET_DMZ_FORWARD_CHAIN -j DROP
  1773. }
  1774.  
  1775.  
  1776. ###################################################
  1777. # Setup chain for the DMZ-to-INET forward traffic #
  1778. ###################################################
  1779. setup_dmz_inet_forward_chain()
  1780. {
  1781. echo " Setting up DMZ->INET policy"
  1782.  
  1783. # TCP ports to ALLOW for certain DMZ hosts
  1784. #########################################
  1785. unset IFS
  1786. for rule in $DMZ_INET_HOST_OPEN_TCP; do
  1787. if_shosts=`echo "$rule" |cut -s -d'>' -f1`
  1788. interfaces=`get_ifs "$if_shosts"`
  1789. shosts=`get_hosts_ih "$if_shosts" "$ANYHOST"`
  1790. dhosts_ports=`echo "$rule" |cut -s -d'>' -f2`
  1791. dhosts=`get_hosts_hp "$dhosts_ports"`
  1792. ports=`get_ports_hp "$dhosts_ports" "$ANYPORT"`
  1793.  
  1794. echo " $(show_if_ip "$interfaces" "$destips")Allowing $shosts(DMZ) to $dhosts(INET) for TCP port(s): $ports"
  1795.  
  1796. IFS=','
  1797. for shost in `ip_range "$shosts"`; do
  1798. for dhost in `ip_range "$dhosts"`; do
  1799. for port in $ports; do
  1800. for interface in $interfaces; do
  1801. iptables -A DMZ_INET_FORWARD_CHAIN -o $interface -s $shost -d $dhost -p tcp --dport $port -j ACCEPT
  1802. done
  1803. done
  1804. done
  1805. done
  1806. done
  1807.  
  1808. # UDP ports to ALLOW for certain DMZ hosts
  1809. #########################################
  1810. unset IFS
  1811. for rule in $DMZ_INET_HOST_OPEN_UDP; do
  1812. if_shosts=`echo "$rule" |cut -s -d'>' -f1`
  1813. interfaces=`get_ifs "$if_shosts"`
  1814. shosts=`get_hosts_ih "$if_shosts" "$ANYHOST"`
  1815. dhosts_ports=`echo "$rule" |cut -s -d'>' -f2`
  1816. dhosts=`get_hosts_hp "$dhosts_ports"`
  1817. ports=`get_ports_hp "$dhosts_ports" "$ANYPORT"`
  1818.  
  1819. echo " $(show_if_ip "$interfaces")Allowing $shosts(DMZ) to $dhosts(INET) for UDP port(s): $ports"
  1820.  
  1821. IFS=','
  1822. for shost in `ip_range "$shosts"`; do
  1823. for dhost in `ip_range "$dhosts"`; do
  1824. for port in $ports; do
  1825. for interface in $interfaces; do
  1826. iptables -A DMZ_INET_FORWARD_CHAIN -o $interface -s $shost -d $dhost -p udp --dport $port -j ACCEPT
  1827. done
  1828. done
  1829. done
  1830. done
  1831. done
  1832.  
  1833. # (Other) IP protocols to ALLOW for certain DMZ hosts
  1834. #####################################################
  1835. unset IFS
  1836. for rule in $DMZ_INET_HOST_OPEN_IP; do
  1837. if_shosts=`echo "$rule" |cut -s -d'>' -f1`
  1838. interfaces=`get_ifs "$if_shosts"`
  1839. shosts=`get_hosts_ih "$if_shosts" "$ANYHOST"`
  1840. dhosts_protos=`echo "$rule" |cut -s -d'>' -f2`
  1841. dhosts=`get_hosts_hp "$dhosts_protos"`
  1842. protos=`get_ports_hp "$dhosts_protos"`
  1843.  
  1844. echo " $(show_if_ip "$interfaces")Allowing $shosts(DMZ) to $dhosts(INET) for IP protocol(s): $protos"
  1845.  
  1846. IFS=','
  1847. for shost in `ip_range "$shosts"`; do
  1848. for dhost in `ip_range "$dhosts"`; do
  1849. for proto in $protos; do
  1850. for interface in $interfaces; do
  1851. iptables -A DMZ_INET_FORWARD_CHAIN -o $interface -s $shost -d $dhost -p $proto -j ACCEPT
  1852. done
  1853. done
  1854. done
  1855. done
  1856. done
  1857.  
  1858.  
  1859. # TCP ports to DENY for certain DMZ hosts
  1860. #########################################
  1861. unset IFS
  1862. for rule in $DMZ_INET_HOST_DENY_TCP; do
  1863. if_shosts=`echo "$rule" |cut -s -d'>' -f1`
  1864. interfaces=`get_ifs "$if_shosts"`
  1865. shosts=`get_hosts_ih "$if_shosts" "$ANYHOST"`
  1866. dhosts_ports=`echo "$rule" |cut -s -d'>' -f2`
  1867. dhosts=`get_hosts_hp "$dhosts_ports"`
  1868. ports=`get_ports_hp "$dhosts_ports" "$ANYPORT"`
  1869.  
  1870. echo " $(show_if_ip "$interfaces")Denying $shosts(DMZ) to $dhosts(INET) for TCP port(s): $ports"
  1871.  
  1872. IFS=','
  1873. for shost in `ip_range "$shosts"`; do
  1874. for dhost in `ip_range "$dhosts"`; do
  1875. for port in $ports; do
  1876. for interface in $interfaces; do
  1877. if [ "$DMZ_OUTPUT_DENY_LOG" != "0" ]; then
  1878. iptables -A DMZ_INET_FORWARD_CHAIN -o $interface -s $shost -d $dhost -p tcp --dport $port \
  1879. -m limit --limit 1/h --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Host DMZ->INET denied: "
  1880. fi
  1881. iptables -A DMZ_INET_FORWARD_CHAIN -o $interface -s $shost -d $dhost -p tcp --dport $port -j DROP
  1882. done
  1883. done
  1884. done
  1885. done
  1886. done
  1887.  
  1888. # UDP ports to DENY for certain DMZ hosts
  1889. #########################################
  1890. unset IFS
  1891. for rule in $DMZ_INET_HOST_DENY_UDP; do
  1892. if_shosts=`echo "$rule" |cut -s -d'>' -f1`
  1893. interfaces=`get_ifs "$if_shosts"`
  1894. shosts=`get_hosts_ih "$if_shosts" "$ANYHOST"`
  1895. dhosts_ports=`echo "$rule" |cut -s -d'>' -f2`
  1896. dhosts=`get_hosts_hp "$dhosts_ports"`
  1897. ports=`get_ports_hp "$dhosts_ports" "$ANYPORT"`
  1898.  
  1899. echo " $(show_if_ip "$interfaces")Denying $shosts(DMZ) to $dhosts(INET) for UDP port(s): $ports"
  1900.  
  1901. IFS=','
  1902. for shost in `ip_range "$shosts"`; do
  1903. for dhost in `ip_range "$dhosts"`; do
  1904. for port in $ports; do
  1905. for interface in $interfaces; do
  1906. if [ "$DMZ_OUTPUT_DENY_LOG" != "0" ]; then
  1907. iptables -A DMZ_INET_FORWARD_CHAIN -o $interface -s $shost -d $dhost -p udp --dport $port \
  1908. -m limit --limit 1/h --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Host DMZ->INET denied: "
  1909. fi
  1910. iptables -A DMZ_INET_FORWARD_CHAIN -o $interface -s $shost -d $dhost -p udp --dport $port -j DROP
  1911. done
  1912. done
  1913. done
  1914. done
  1915. done
  1916.  
  1917. # (Other) IP protocols to DENY for certain DMZ hosts
  1918. #####################################################
  1919. unset IFS
  1920. for rule in $DMZ_INET_HOST_DENY_IP; do
  1921. if_shosts=`echo "$rule" |cut -s -d'>' -f1`
  1922. interfaces=`get_ifs "$if_shosts"`
  1923. shosts=`get_hosts_ih "$if_shosts" "$ANYHOST"`
  1924. dhosts_protos=`echo "$rule" |cut -s -d'>' -f2`
  1925. dhosts=`get_hosts_hp "$dhosts_protos"`
  1926. protos=`get_ports_hp "$dhosts_protos"`
  1927.  
  1928. echo " $(show_if_ip "$interfaces")Denying $shosts(DMZ) to $dhosts(INET) for IP protocol(s): $protos"
  1929.  
  1930. IFS=','
  1931. for shost in `ip_range "$shosts"`; do
  1932. for dhost in `ip_range "$dhosts"`; do
  1933. for proto in $protos; do
  1934. for interface in $interfaces; do
  1935. if [ "$DMZ_OUTPUT_DENY_LOG" != "0" ]; then
  1936. iptables -A DMZ_INET_FORWARD_CHAIN -o $interface -s $shost -d $dhost -p $proto \
  1937. -m limit --limit 1/h --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Host DMZ->INET denied: "
  1938. fi
  1939. iptables -A DMZ_INET_FORWARD_CHAIN -o $interface -s $shost -d $dhost -p $proto -j DROP
  1940. done
  1941. done
  1942. done
  1943. done
  1944. done
  1945.  
  1946. # Allow only certain TCP ports to be used from the DMZ->INET?
  1947. #############################################################
  1948. unset IFS
  1949. for rule in $DMZ_INET_OPEN_TCP; do
  1950. interfaces=`get_ifs "$rule"`
  1951. ports=`get_ports_ip "$rule"`
  1952.  
  1953. echo " $(show_if_ip "$interfaces")Allowing TCP port(s): $ports"
  1954.  
  1955. IFS=','
  1956. for port in $ports; do
  1957. for interface in $interfaces; do
  1958. iptables -A DMZ_INET_FORWARD_CHAIN -o $interface -p tcp --dport $port -j ACCEPT
  1959. done
  1960. done
  1961. done
  1962.  
  1963. # Allow only certain UDP ports to be used from the DMZ->INET?
  1964. #############################################################
  1965. unset IFS
  1966. for rule in $DMZ_INET_OPEN_UDP; do
  1967. interfaces=`get_ifs "$rule"`
  1968. ports=`get_ports_ip "$rule"`
  1969.  
  1970. echo " $(show_if_ip "$interfaces")Allowing UDP port(s): $ports"
  1971.  
  1972. IFS=','
  1973. for port in $ports; do
  1974. for interface in $interfaces; do
  1975. iptables -A DMZ_INET_FORWARD_CHAIN -o $interface -p udp --dport $port -j ACCEPT
  1976. done
  1977. done
  1978. done
  1979.  
  1980. # Allow only certain IP protocols to be used from the DMZ->INET?
  1981. ################################################################
  1982. unset IFS
  1983. for rule in $DMZ_INET_OPEN_IP; do
  1984. interfaces=`get_ifs "$rule"`
  1985. protos=`get_ports_ip "$rule"`
  1986.  
  1987. echo " $(show_if_ip "$interfaces")Allowing IP protocol(s): $protos"
  1988.  
  1989. IFS=','
  1990. for proto in $protos; do
  1991. for interface in $interfaces; do
  1992. iptables -A DMZ_INET_FORWARD_CHAIN -o $interface -p $proto -j ACCEPT
  1993. done
  1994. done
  1995. done
  1996.  
  1997. # Allow ICMP-requests(ping) for DMZ->INET?
  1998. ##########################################
  1999. if [ "$DMZ_INET_OPEN_ICMP" != "0" ]; then
  2000. echo " Allowing ICMP-requests(ping)"
  2001. iptables -A DMZ_INET_FORWARD_CHAIN -p icmp --icmp-type echo-request \
  2002. -m limit --limit 20/second --limit-burst 100 -j ACCEPT
  2003. fi
  2004.  
  2005. # TCP ports to DENY for DMZ->INET
  2006. #################################
  2007. unset IFS
  2008. for rule in $DMZ_INET_DENY_TCP; do
  2009. interfaces=`get_ifs "$rule"`
  2010. ports=`get_ports_ip "$rule"`
  2011.  
  2012. echo " $(show_if_ip "$interfaces")Denying TCP port(s): $ports"
  2013.  
  2014. IFS=','
  2015. for port in $ports; do
  2016. for interface in $interfaces; do
  2017. if [ "$DMZ_OUTPUT_DENY_LOG" != "0" ]; then
  2018. iptables -A DMZ_INET_FORWARD_CHAIN -o $interface -p tcp --dport $port -m limit \
  2019. --limit 1/s --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:DMZ->INET denied: "
  2020. fi
  2021. iptables -A DMZ_INET_FORWARD_CHAIN -o $interface -p tcp --dport $port -j DROP
  2022. done
  2023. done
  2024. done
  2025.  
  2026. # UDP ports to DENY for DMZ->INET
  2027. #################################
  2028. unset IFS
  2029. for rule in $DMZ_INET_DENY_UDP; do
  2030. interfaces=`get_ifs "$rule"`
  2031. ports=`get_ports_ip "$rule"`
  2032.  
  2033. echo " $(show_if_ip "$interfaces")Denying UDP port(s): $ports"
  2034.  
  2035. IFS=','
  2036. for port in $ports; do
  2037. for interface in $interfaces; do
  2038. if [ "$DMZ_OUTPUT_DENY_LOG" != "0" ]; then
  2039. iptables -A DMZ_INET_FORWARD_CHAIN -o $interface -p udp --dport $port -m limit \
  2040. --limit 1/s --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:DMZ->INET denied: "
  2041. fi
  2042. iptables -A DMZ_INET_FORWARD_CHAIN -o $interface -p udp --dport $port -j DROP
  2043. done
  2044. done
  2045. done
  2046.  
  2047. # IP protocols to DENY for DMZ->INET
  2048. ####################################
  2049. unset IFS
  2050. for rule in $DMZ_INET_DENY_IP; do
  2051. interfaces=`get_ifs "$rule"`
  2052. protos=`get_ports_ip "$rule"`
  2053.  
  2054. echo " $(show_if_ip "$interfaces")Denying IP protocol(s): $protos"
  2055.  
  2056. IFS=','
  2057. for proto in $protos; do
  2058. for interface in $interfaces; do
  2059. if [ "$DMZ_OUTPUT_DENY_LOG" != "0" ]; then
  2060. iptables -A DMZ_INET_FORWARD_CHAIN -o $interface -p $proto -m limit \
  2061. --limit 1/s --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:DMZ->INET denied: "
  2062. fi
  2063. iptables -A DMZ_INET_FORWARD_CHAIN -o $interface -p $proto -j DROP
  2064. done
  2065. done
  2066. done
  2067.  
  2068. # Log incoming ICMP-request packets?
  2069. ####################################
  2070. if [ "$ICMP_REQUEST_LOG" != "0" ]; then
  2071. iptables -A DMZ_INET_FORWARD_CHAIN -p icmp --icmp-type echo-request \
  2072. -m limit --limit 3/m --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:ICMP-request: "
  2073. fi
  2074.  
  2075. # Drop ICMP packets
  2076. iptables -A DMZ_INET_FORWARD_CHAIN -p icmp --icmp-type echo-request -j DROP
  2077.  
  2078. # Set the default policy (switch to DROP for a protocol when xxx_OPEN_xxx variable is used)
  2079. ###########################################################################################
  2080. if [ -z "$DMZ_INET_OPEN_TCP" ]; then
  2081. echo " Allowing all (other) TCP ports"
  2082. iptables -A DMZ_INET_FORWARD_CHAIN -p tcp -j ACCEPT
  2083. else
  2084. if [ "$DMZ_OUTPUT_DENY_LOG" != "0" ]; then
  2085. iptables -A DMZ_INET_FORWARD_CHAIN -p tcp -m limit \
  2086. --limit 1/s --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:DMZ->INET denied: "
  2087. fi
  2088. echo " Denying all (other) TCP ports"
  2089. iptables -A DMZ_INET_FORWARD_CHAIN -p tcp -j DROP
  2090. fi
  2091.  
  2092. if [ -z "$DMZ_INET_OPEN_UDP" ]; then
  2093. echo " Allowing all (other) UDP ports"
  2094. iptables -A DMZ_INET_FORWARD_CHAIN -p udp -j ACCEPT
  2095. else
  2096. if [ "$DMZ_OUTPUT_DENY_LOG" != "0" ]; then
  2097. iptables -A DMZ_INET_FORWARD_CHAIN -p udp -m limit \
  2098. --limit 1/s --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:DMZ->INET denied: "
  2099. fi
  2100. echo " Denying all (other) UDP ports"
  2101. iptables -A DMZ_INET_FORWARD_CHAIN -p udp -j DROP
  2102. fi
  2103.  
  2104. if [ -z "$DMZ_INET_OPEN_IP" ]; then
  2105. echo " Allowing all (other) protocols"
  2106. iptables -A DMZ_INET_FORWARD_CHAIN -j ACCEPT
  2107. else
  2108. if [ "$DMZ_OUTPUT_DENY_LOG" != "0" ]; then
  2109. iptables -A DMZ_INET_FORWARD_CHAIN -m limit \
  2110. --limit 1/s --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:DMZ->INET denied: "
  2111. fi
  2112. echo " Denying all (other) protocols"
  2113. iptables -A DMZ_INET_FORWARD_CHAIN -j DROP
  2114. fi
  2115. }
  2116.  
  2117.  
  2118. #########################################
  2119. # Setup chain for the LAN input traffic #
  2120. #########################################
  2121. setup_int_input_chain()
  2122. {
  2123. # TCP ports to OPEN for certain LAN hosts
  2124. #########################################
  2125. unset IFS
  2126. for rule in $LAN_HOST_OPEN_TCP; do
  2127. hosts=`get_hosts_ihp "$rule"`
  2128. ports=`get_ports_ihp "$rule"`
  2129.  
  2130. echo " Allowing $hosts(LAN) for TCP port(s): $ports"
  2131.  
  2132. IFS=','
  2133. for host in `ip_range "$hosts"`; do
  2134. for port in $ports; do
  2135. iptables -A INT_INPUT_CHAIN -s $host -p tcp --dport $port -j ACCEPT
  2136. done
  2137. done
  2138. done
  2139.  
  2140. # UDP ports to OPEN for certain LAN hosts
  2141. #########################################
  2142. unset IFS
  2143. for rule in $LAN_HOST_OPEN_UDP; do
  2144. hosts=`get_hosts_ihp "$rule"`
  2145. ports=`get_ports_ihp "$rule"`
  2146.  
  2147. echo " Allowing $hosts(LAN) for UDP port(s): $ports"
  2148.  
  2149. IFS=','
  2150. for host in `ip_range "$hosts"`; do
  2151. for port in $ports; do
  2152. iptables -A INT_INPUT_CHAIN -s $host -p udp --dport $port -j ACCEPT
  2153. done
  2154. done
  2155. done
  2156.  
  2157. # IP protocols to OPEN for certain LAN hosts
  2158. ############################################
  2159. unset IFS
  2160. for rule in $LAN_HOST_OPEN_IP; do
  2161. hosts=`get_hosts_ihp "$rule"`
  2162. protos=`get_ports_ihp "$rule"`
  2163.  
  2164. echo " Allowing $hosts(LAN) for IP protocol(s): $protos"
  2165.  
  2166. IFS=','
  2167. for host in `ip_range "$hosts"`; do
  2168. for proto in $protos; do
  2169. iptables -A INT_INPUT_CHAIN -s $host -p $proto -j ACCEPT
  2170. done
  2171. done
  2172. done
  2173.  
  2174. # TCP ports to DENY for certain LAN hosts
  2175. #########################################
  2176. unset IFS
  2177. for rule in $LAN_HOST_DENY_TCP; do
  2178. hosts=`get_hosts_ihp "$rule" "$ANYHOST"`
  2179. ports=`get_ports_ihp "$rule" "$ANYPORT"`
  2180.  
  2181. echo " Denying $hosts(LAN) for TCP port(s): $ports"
  2182.  
  2183. IFS=','
  2184. for host in `ip_range "$hosts"`; do
  2185. for port in $ports; do
  2186. if [ "$LAN_INPUT_DENY_LOG" != "0" ]; then
  2187. iptables -A INT_INPUT_CHAIN -s $host -p tcp --dport $port \
  2188. -m limit --limit 1/h --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Host LAN denied: "
  2189. fi
  2190. iptables -A INT_INPUT_CHAIN -s $host -p tcp --dport $port -j DROP
  2191. done
  2192. done
  2193. done
  2194.  
  2195. # UDP ports to DENY for certain LAN hosts
  2196. #########################################
  2197. unset IFS
  2198. for rule in $LAN_HOST_DENY_UDP; do
  2199. hosts=`get_hosts_ihp "$rule" "$ANYHOST"`
  2200. ports=`get_ports_ihp "$rule" "$ANYPORT"`
  2201.  
  2202. echo " Denying $hosts(LAN) for UDP port(s): $ports"
  2203.  
  2204. IFS=','
  2205. for host in `ip_range "$hosts"`; do
  2206. for port in $ports; do
  2207. if [ "$LAN_INPUT_DENY_LOG" != "0" ]; then
  2208. iptables -A INT_INPUT_CHAIN -s $host -p udp --dport $port \
  2209. -m limit --limit 1/h --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Host LAN denied: "
  2210. fi
  2211. iptables -A INT_INPUT_CHAIN -s $host -p udp --dport $port -j DROP
  2212. done
  2213. done
  2214. done
  2215.  
  2216. # IP protocols to DENY for certain LAN hosts
  2217. ############################################
  2218. unset IFS
  2219. for rule in $LAN_HOST_DENY_IP; do
  2220. hosts=`get_hosts_ihp "$rule" "$ANYHOST"`
  2221. protos=`get_ports_ihp "$rule"`
  2222.  
  2223. echo " Denying $hosts(LAN) for IP protocol(s): $protos"
  2224.  
  2225. IFS=','
  2226. for host in `ip_range "$hosts"`; do
  2227. for proto in $protos; do
  2228. if [ "$LAN_INPUT_DENY_LOG" != "0" ]; then
  2229. iptables -A INT_INPUT_CHAIN -s $host -p $proto \
  2230. -m limit --limit 1/h --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Host LAN denied: "
  2231. fi
  2232. iptables -A INT_INPUT_CHAIN -s $host -p $proto -j DROP
  2233. done
  2234. done
  2235. done
  2236.  
  2237. # Allow only certain TCP ports to be used from the LAN?
  2238. #######################################################
  2239. if [ -n "$LAN_OPEN_TCP" ]; then
  2240. echo " Allowing TCP port(s): $LAN_OPEN_TCP"
  2241. IFS=' ,'
  2242. for port in $LAN_OPEN_TCP; do
  2243. iptables -A INT_INPUT_CHAIN -p tcp --dport $port -j ACCEPT
  2244. done
  2245. fi
  2246.  
  2247. # Allow only certain UDP ports to be used from the LAN?
  2248. #######################################################
  2249. if [ -n "$LAN_OPEN_UDP" ]; then
  2250. echo " Allowing UDP port(s): $LAN_OPEN_UDP"
  2251. IFS=' ,'
  2252. for port in $LAN_OPEN_UDP; do
  2253. iptables -A INT_INPUT_CHAIN -p udp --dport $port -j ACCEPT
  2254. done
  2255. fi
  2256.  
  2257. # Allow only certain IP protocols to be used from the LAN?
  2258. ##########################################################
  2259. if [ -n "$LAN_OPEN_IP" ]; then
  2260. echo " Allowing IP protocol(s): $LAN_OPEN_IP"
  2261. IFS=' ,'
  2262. for proto in $LAN_OPEN_IP; do
  2263. iptables -A INT_INPUT_CHAIN -p $proto -j ACCEPT
  2264. done
  2265. fi
  2266.  
  2267. # Allow world to send ICMP packets?
  2268. ###################################
  2269. if [ "$LAN_OPEN_ICMP" != "0" ]; then
  2270. echo " Allowing ICMP-requests(ping)"
  2271. iptables -A INT_INPUT_CHAIN -p icmp --icmp-type echo-request -m limit --limit 20/second --limit-burst 100 -j ACCEPT
  2272. fi
  2273.  
  2274. # TCP ports to DENY for LAN hosts
  2275. #################################
  2276. if [ -n "$LAN_DENY_TCP" ]; then
  2277. echo " Denying TCP port(s): $LAN_DENY_TCP"
  2278. IFS=' ,'
  2279. for port in $LAN_DENY_TCP; do
  2280. if [ "$LAN_INPUT_DENY_LOG" != "0" ]; then
  2281. iptables -A INT_INPUT_CHAIN -p tcp --dport $port -m limit \
  2282. --limit 1/s --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:LAN-INPUT denied: "
  2283. fi
  2284. iptables -A INT_INPUT_CHAIN -p tcp --dport $port -j DROP
  2285. done
  2286. fi
  2287.  
  2288. # UDP ports to DENY for LAN hosts
  2289. #################################
  2290. if [ -n "$LAN_DENY_UDP" ]; then
  2291. echo " Denying UDP port(s): $LAN_DENY_UDP"
  2292. IFS=' ,'
  2293. for port in $LAN_DENY_UDP; do
  2294. if [ "$LAN_INPUT_DENY_LOG" != "0" ]; then
  2295. iptables -A INT_INPUT_CHAIN -p udp --dport $port -m limit \
  2296. --limit 1/s --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:LAN-INPUT denied: "
  2297. fi
  2298. iptables -A INT_INPUT_CHAIN -p udp --dport $port -j DROP
  2299. done
  2300. fi
  2301.  
  2302. # IP protocols to DENY for LAN hosts
  2303. ####################################
  2304. if [ -n "$LAN_DENY_IP" ]; then
  2305. echo " Denying IP protocol(s): $LAN_DENY_IP"
  2306. IFS=' ,'
  2307. for proto in $LAN_DENY_IP; do
  2308. if [ "$LAN_INPUT_DENY_LOG" != "0" ]; then
  2309. iptables -A INT_INPUT_CHAIN -p $proto -m limit \
  2310. --limit 1/s --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:LAN-INPUT denied: "
  2311. fi
  2312. iptables -A INT_INPUT_CHAIN -p $proto -j DROP
  2313. done
  2314. fi
  2315.  
  2316. # Log incoming ICMP-request packets?
  2317. ####################################
  2318. if [ "$ICMP_REQUEST_LOG" != "0" ]; then
  2319. iptables -A INT_INPUT_CHAIN -p icmp --icmp-type echo-request \
  2320. -m limit --limit 3/m --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:ICMP-request: "
  2321. fi
  2322.  
  2323. # Drop ICMP packets
  2324. iptables -A INT_INPUT_CHAIN -p icmp --icmp-type echo-request -j DROP
  2325.  
  2326. # Set the default policy
  2327. ########################
  2328. if [ -z "$LAN_OPEN_TCP" -a -z "$LAN_HOST_OPEN_TCP" -a \
  2329. -z "$LAN_OPEN_UDP" -a -z "$LAN_HOST_OPEN_UDP" -a \
  2330. -z "$LAN_OPEN_IP" -a -z "$LAN_HOST_OPEN_IP" ]; then
  2331. echo " Allowing all (other) ports/protocols"
  2332. iptables -A INT_INPUT_CHAIN -j ACCEPT
  2333. else
  2334. echo " Denying all (other) ports/protocols"
  2335. if [ "$LAN_INPUT_DENY_LOG" != "0" ]; then
  2336. iptables -A INT_INPUT_CHAIN -m limit \
  2337. --limit 1/s --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:LAN-INPUT denied: "
  2338. fi
  2339. iptables -A INT_INPUT_CHAIN -j DROP
  2340. fi
  2341. }
  2342.  
  2343.  
  2344. ###################################################
  2345. # Setup chain for the LAN-to-INET forward traffic #
  2346. ###################################################
  2347. setup_lan_inet_forward_chain()
  2348. {
  2349. echo " Setting up LAN->INET policy"
  2350.  
  2351. # udp ports to ALLOW for certain LAN hosts
  2352. #########################################
  2353. unset IFS
  2354. for rule in $LAN_INET_HOST_OPEN_TCP; do
  2355. if_shosts=`echo "$rule" |cut -s -d'>' -f1`
  2356. interfaces=`get_ifs "$if_shosts"`
  2357. shosts=`get_hosts_ih "$if_shosts" "$ANYHOST"`
  2358. dhosts_ports=`echo "$rule" |cut -s -d'>' -f2`
  2359. dhosts=`get_hosts_hp "$dhosts_ports"`
  2360. ports=`get_ports_hp "$dhosts_ports" "$ANYPORT"`
  2361.  
  2362. echo " $(show_if_ip "$interfaces")Allowing $shosts(LAN) to $dhosts(INET) for TCP port(s): $ports"
  2363.  
  2364. IFS=','
  2365. for shost in `ip_range "$shosts"`; do
  2366. for dhost in `ip_range "$dhosts"`; do
  2367. for port in $ports; do
  2368. for interface in $interfaces; do
  2369. iptables -A LAN_INET_FORWARD_CHAIN -o $interface -s $shost -d $dhost -p tcp --dport $port -j ACCEPT
  2370. done
  2371. done
  2372. done
  2373. done
  2374. done
  2375.  
  2376. # UDP ports to ALLOW for certain LAN hosts
  2377. #########################################
  2378. unset IFS
  2379. for rule in $LAN_INET_HOST_OPEN_UDP; do
  2380. if_shosts=`echo "$rule" |cut -s -d'>' -f1`
  2381. interfaces=`get_ifs "$if_shosts"`
  2382. shosts=`get_hosts_ih "$if_shosts" "$ANYHOST"`
  2383. dhosts_ports=`echo "$rule" |cut -s -d'>' -f2`
  2384. dhosts=`get_hosts_hp "$dhosts_ports"`
  2385. ports=`get_ports_hp "$dhosts_ports" "$ANYPORT"`
  2386.  
  2387. echo " $(show_if_ip "$interfaces")Allowing $shosts(LAN) to $dhosts(INET) for UDP port(s): $ports"
  2388.  
  2389. IFS=','
  2390. for shost in `ip_range "$shosts"`; do
  2391. for dhost in `ip_range "$dhosts"`; do
  2392. for port in $ports; do
  2393. for interface in $interfaces; do
  2394. iptables -A LAN_INET_FORWARD_CHAIN -o $interface -s $shost -d $dhost -p udp --dport $port -j ACCEPT
  2395. done
  2396. done
  2397. done
  2398. done
  2399. done
  2400.  
  2401. # (Other) IP protocols to ALLOW for certain LAN hosts
  2402. #####################################################
  2403. unset IFS
  2404. for rule in $LAN_INET_HOST_OPEN_IP; do
  2405. if_shosts=`echo "$rule" |cut -s -d'>' -f1`
  2406. interfaces=`get_ifs "$if_shosts"`
  2407. shosts=`get_hosts_ih "$if_shosts" "$ANYHOST"`
  2408. dhosts_protos=`echo "$rule" |cut -s -d'>' -f2`
  2409. dhosts=`get_hosts_hp "$dhosts_protos"`
  2410. protos=`get_ports_hp "$dhosts_protos"`
  2411.  
  2412. echo " $(show_if_ip "$interfaces")Allowing $shosts(LAN) to $dhosts(INET) for IP protocol(s): $protos"
  2413.  
  2414. IFS=','
  2415. for shost in `ip_range "$shosts"`; do
  2416. for dhost in `ip_range "$dhosts"`; do
  2417. for proto in $protos; do
  2418. for interface in $interfaces; do
  2419. iptables -A LAN_INET_FORWARD_CHAIN -o $interface -s $shost -d $dhost -p $proto -j ACCEPT
  2420. done
  2421. done
  2422. done
  2423. done
  2424. done
  2425.  
  2426. # udp ports to DENY for certain LAN hosts
  2427. #########################################
  2428. unset IFS
  2429. for rule in $LAN_INET_HOST_DENY_TCP; do
  2430. if_shosts=`echo "$rule" |cut -s -d'>' -f1`
  2431. interfaces=`get_ifs "$if_shosts"`
  2432. shosts=`get_hosts_ih "$if_shosts" "$ANYHOST"`
  2433. dhosts_ports=`echo "$rule" |cut -s -d'>' -f2`
  2434. dhosts=`get_hosts_hp "$dhosts_ports"`
  2435. ports=`get_ports_hp "$dhosts_ports" "$ANYPORT"`
  2436.  
  2437. echo " $(show_if_ip "$interfaces")Denying $shosts(LAN) to $dhosts(INET) for TCP port(s): $ports"
  2438.  
  2439. IFS=','
  2440. for shost in `ip_range "$shosts"`; do
  2441. for dhost in `ip_range "$dhosts"`; do
  2442. for port in $ports; do
  2443. for interface in $interfaces; do
  2444. if [ "$LAN_OUTPUT_DENY_LOG" != "0" ]; then
  2445. iptables -A LAN_INET_FORWARD_CHAIN -o $interface -s $shost -d $dhost -p tcp --dport $port \
  2446. -m limit --limit 1/h --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Host LAN->INET denied: "
  2447. fi
  2448. iptables -A LAN_INET_FORWARD_CHAIN -o $interface -s $shost -d $dhost -p tcp --dport $port -j DROP
  2449. done
  2450. done
  2451. done
  2452. done
  2453. done
  2454.  
  2455. # UDP ports to DENY for certain LAN hosts
  2456. #########################################
  2457. unset IFS
  2458. for rule in $LAN_INET_HOST_DENY_UDP; do
  2459. if_shosts=`echo "$rule" |cut -s -d'>' -f1`
  2460. interfaces=`get_ifs "$if_shosts"`
  2461. shosts=`get_hosts_ih "$if_shosts" "$ANYHOST"`
  2462. dhosts_ports=`echo "$rule" |cut -s -d'>' -f2`
  2463. dhosts=`get_hosts_hp "$dhosts_ports"`
  2464. ports=`get_ports_hp "$dhosts_ports" "$ANYPORT"`
  2465.  
  2466. echo " $(show_if_ip "$interfaces")Denying $shosts(LAN) to $dhosts(INET) for UDP port(s): $ports"
  2467.  
  2468. IFS=','
  2469. for shost in `ip_range "$shosts"`; do
  2470. for dhost in `ip_range "$dhosts"`; do
  2471. for port in $ports; do
  2472. for interface in $interfaces; do
  2473. if [ "$LAN_OUTPUT_DENY_LOG" != "0" ]; then
  2474. iptables -A LAN_INET_FORWARD_CHAIN -o $interface -s $shost -d $dhost -p udp --dport $port \
  2475. -m limit --limit 1/h --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Host LAN->INET denied: "
  2476. fi
  2477. iptables -A LAN_INET_FORWARD_CHAIN -o $interface -s $shost -d $dhost -p udp --dport $port -j DROP
  2478. done
  2479. done
  2480. done
  2481. done
  2482. done
  2483.  
  2484. # (Other) IP protocols to DENY for certain LAN hosts
  2485. #####################################################
  2486. unset IFS
  2487. for rule in $LAN_INET_HOST_DENY_IP; do
  2488. if_shosts=`echo "$rule" |cut -s -d'>' -f1`
  2489. interfaces=`get_ifs "$if_shosts"`
  2490. shosts=`get_hosts_ih "$if_shosts" "$ANYHOST"`
  2491. dhosts_protos=`echo "$rule" |cut -s -d'>' -f2`
  2492. dhosts=`get_hosts_hp "$dhosts_protos"`
  2493. protos=`get_ports_hp "$dhosts_protos"`
  2494.  
  2495. echo " $(show_if_ip "$interfaces")Denying $shosts(LAN) to $dhosts(INET) for IP protocol(s): $protos"
  2496.  
  2497. IFS=','
  2498. for shost in `ip_range "$shosts"`; do
  2499. for dhost in `ip_range "$dhosts"`; do
  2500. for proto in $protos; do
  2501. for interface in $interfaces; do
  2502. if [ "$LAN_OUTPUT_DENY_LOG" != "0" ]; then
  2503. iptables -A LAN_INET_FORWARD_CHAIN -o $interface -s $shost -d $dhost -p $proto \
  2504. -m limit --limit 1/h --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Host LAN->INET denied: "
  2505. fi
  2506. iptables -A LAN_INET_FORWARD_CHAIN -o $interface -s $shost -d $dhost -p $proto -j DROP
  2507. done
  2508. done
  2509. done
  2510. done
  2511. done
  2512.  
  2513. # Allow only certain udp ports to be used from the LAN->INET?
  2514. #############################################################
  2515. unset IFS
  2516. for rule in $LAN_INET_OPEN_TCP; do
  2517. interfaces=`get_ifs "$rule"`
  2518. ports=`get_ports_ip "$rule"`
  2519.  
  2520. echo " $(show_if_ip "$interfaces")Allowing TCP port(s): $ports"
  2521.  
  2522. IFS=','
  2523. for port in $ports; do
  2524. for interface in $interfaces; do
  2525. iptables -A LAN_INET_FORWARD_CHAIN -o $interface -p tcp --dport $port -j ACCEPT
  2526. done
  2527. done
  2528. done
  2529.  
  2530. # Allow only certain UDP ports to be used from the LAN->INET?
  2531. #############################################################
  2532. unset IFS
  2533. for rule in $LAN_INET_OPEN_UDP; do
  2534. interfaces=`get_ifs "$rule"`
  2535. ports=`get_ports_ip "$rule"`
  2536.  
  2537. echo " $(show_if_ip "$interfaces")Allowing UDP port(s): $ports"
  2538.  
  2539. IFS=','
  2540. for port in $ports; do
  2541. for interface in $interfaces; do
  2542. iptables -A LAN_INET_FORWARD_CHAIN -o $interface -p udp --dport $port -j ACCEPT
  2543. done
  2544. done
  2545. done
  2546.  
  2547. # Allow only certain IP protocols to be used from the LAN->INET?
  2548. ################################################################
  2549. unset IFS
  2550. for rule in $LAN_INET_OPEN_IP; do
  2551. interfaces=`get_ifs "$rule"`
  2552. protos=`get_ports_ip "$rule"`
  2553.  
  2554. echo " $(show_if_ip "$interfaces")Allowing IP protocol(s): $protos"
  2555.  
  2556. IFS=','
  2557. for proto in $protos; do
  2558. for interface in $interfaces; do
  2559. iptables -A LAN_INET_FORWARD_CHAIN -o $interface -p $proto -j ACCEPT
  2560. done
  2561. done
  2562. done
  2563.  
  2564. # Allow ICMP-requests(ping) for LAN->INET?
  2565. ##########################################
  2566. if [ "$LAN_INET_OPEN_ICMP" != "0" ]; then
  2567. echo " Allowing ICMP-requests(ping)"
  2568. iptables -A LAN_INET_FORWARD_CHAIN -p icmp --icmp-type echo-request \
  2569. -m limit --limit 20/second --limit-burst 100 -j ACCEPT
  2570. fi
  2571.  
  2572. # TCP ports to DENY for LAN->INET
  2573. #################################
  2574. unset IFS
  2575. for rule in $LAN_INET_DENY_TCP; do
  2576. interfaces=`get_ifs "$rule"`
  2577. ports=`get_ports_ip "$rule"`
  2578.  
  2579. echo " $(show_if_ip "$interfaces")Denying TCP port(s): $ports"
  2580.  
  2581. IFS=','
  2582. for port in $ports; do
  2583. for interface in $interfaces; do
  2584. if [ "$LAN_OUTPUT_DENY_LOG" != "0" ]; then
  2585. iptables -A LAN_INET_FORWARD_CHAIN -o $interface -p tcp --dport $port -m limit \
  2586. --limit 1/s --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:LAN->INET denied: "
  2587. fi
  2588. iptables -A LAN_INET_FORWARD_CHAIN -o $interface -p tcp --dport $port -j DROP
  2589. done
  2590. done
  2591. done
  2592.  
  2593. # UDP ports to DENY for LAN->INET
  2594. #################################
  2595. unset IFS
  2596. for rule in $LAN_INET_DENY_UDP; do
  2597. interfaces=`get_ifs "$rule"`
  2598. ports=`get_ports_ip "$rule"`
  2599.  
  2600. echo " $(show_if_ip "$interfaces")Denying UDP port(s): $ports"
  2601.  
  2602. IFS=','
  2603. for port in $ports; do
  2604. for interface in $interfaces; do
  2605. if [ "$LAN_OUTPUT_DENY_LOG" != "0" ]; then
  2606. iptables -A LAN_INET_FORWARD_CHAIN -o $interface -p udp --dport $port -m limit \
  2607. --limit 1/s --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:LAN->INET denied: "
  2608. fi
  2609. iptables -A LAN_INET_FORWARD_CHAIN -o $interface -p udp --dport $port -j DROP
  2610. done
  2611. done
  2612. done
  2613.  
  2614. # IP protocols to DENY for LAN->INET
  2615. ####################################
  2616. unset IFS
  2617. for rule in $LAN_INET_DENY_IP; do
  2618. interfaces=`get_ifs "$rule"`
  2619. protos=`get_ports_ip "$rule"`
  2620.  
  2621. echo " $(show_if_ip "$interfaces")Denying IP protocol(s): $protos"
  2622.  
  2623. IFS=','
  2624. for proto in $protos; do
  2625. for interface in $interfaces; do
  2626. if [ "$LAN_OUTPUT_DENY_LOG" != "0" ]; then
  2627. iptables -A LAN_INET_FORWARD_CHAIN -o $interface -p $proto -m limit \
  2628. --limit 1/s --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:LAN->INET denied: "
  2629. fi
  2630. iptables -A LAN_INET_FORWARD_CHAIN -o $interface -p $proto -j DROP
  2631. done
  2632. done
  2633. done
  2634.  
  2635. # Log incoming ICMP-request packets?
  2636. ####################################
  2637. if [ "$ICMP_REQUEST_LOG" != "0" ]; then
  2638. iptables -A LAN_INET_FORWARD_CHAIN -p icmp --icmp-type echo-request \
  2639. -m limit --limit 3/m --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:ICMP-request: "
  2640. fi
  2641.  
  2642. # Drop ICMP packets
  2643. iptables -A LAN_INET_FORWARD_CHAIN -p icmp --icmp-type echo-request -j DROP
  2644.  
  2645. # Set the default policy (switch to DROP for a protocol when xxx_OPEN_xxx variable is used)
  2646. ###########################################################################################
  2647. if [ -z "$LAN_INET_OPEN_TCP" ]; then
  2648. echo " Allowing all (other) TCP ports"
  2649. iptables -A LAN_INET_FORWARD_CHAIN -p tcp -j ACCEPT
  2650. else
  2651. if [ "$LAN_OUTPUT_DENY_LOG" != "0" ]; then
  2652. iptables -A LAN_INET_FORWARD_CHAIN -p tcp -m limit \
  2653. --limit 1/s --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:LAN->INET denied: "
  2654. fi
  2655. echo " Denying all (other) TCP ports"
  2656. iptables -A LAN_INET_FORWARD_CHAIN -p tcp -j DROP
  2657. fi
  2658.  
  2659. if [ -z "$LAN_INET_OPEN_UDP" ]; then
  2660. echo " Allowing all (other) UDP ports"
  2661. iptables -A LAN_INET_FORWARD_CHAIN -p udp -j ACCEPT
  2662. else
  2663. if [ "$LAN_OUTPUT_DENY_LOG" != "0" ]; then
  2664. iptables -A LAN_INET_FORWARD_CHAIN -p udp -m limit \
  2665. --limit 1/s --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:LAN->INET denied: "
  2666. fi
  2667. echo " Denying all (other) UDP ports"
  2668. iptables -A LAN_INET_FORWARD_CHAIN -p udp -j DROP
  2669. fi
  2670.  
  2671. if [ -z "$LAN_INET_OPEN_IP" ]; then
  2672. echo " Allowing all (other) protocols"
  2673. iptables -A LAN_INET_FORWARD_CHAIN -j ACCEPT
  2674. else
  2675. if [ "$LAN_OUTPUT_DENY_LOG" != "0" ]; then
  2676. iptables -A LAN_INET_FORWARD_CHAIN -m limit \
  2677. --limit 1/s --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:LAN->INET denied: "
  2678. fi
  2679. echo " Denying all (other) protocols"
  2680. iptables -A LAN_INET_FORWARD_CHAIN -j DROP
  2681. fi
  2682. }
  2683.  
  2684.  
  2685. ######################################################################################################################
  2686. ## Chain EXT_INPUT_CHAIN - Checks all incoming packets for the EXTERNAL interface(s) ##
  2687. ######################################################################################################################
  2688. setup_ext_input_chain()
  2689. {
  2690. ## Log scanning of port 0 fingerprinting
  2691. ########################################
  2692. if [ "$SCAN_LOG" != "0" ]; then
  2693. iptables -A EXT_INPUT_CHAIN -p tcp --dport 0 \
  2694. -m limit --limit 6/h --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Port 0 OS fingerprint: "
  2695. iptables -A EXT_INPUT_CHAIN -p udp --dport 0 \
  2696. -m limit --limit 6/h --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Port 0 OS fingerprint: "
  2697. fi
  2698.  
  2699. # Drop port 0 scan packets
  2700. ##########################
  2701. iptables -A EXT_INPUT_CHAIN -p tcp --dport 0 -j POST_INPUT_DROP_CHAIN
  2702. iptables -A EXT_INPUT_CHAIN -p udp --dport 0 -j POST_INPUT_DROP_CHAIN
  2703.  
  2704. ## Log scanning of source port 0
  2705. ################################
  2706. if [ "$SCAN_LOG" != "0" ]; then
  2707. iptables -A EXT_INPUT_CHAIN -p tcp --sport 0 \
  2708. -m limit --limit 6/h --limit-burst 5 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:TCP source port 0: "
  2709. iptables -A EXT_INPUT_CHAIN -p udp --sport 0 \
  2710. -m limit --limit 6/h --limit-burst 5 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:UDP source port 0: "
  2711. fi
  2712.  
  2713. # Drop source port 0 packets
  2714. ############################
  2715. iptables -A EXT_INPUT_CHAIN -p tcp --sport 0 -j POST_INPUT_DROP_CHAIN
  2716. iptables -A EXT_INPUT_CHAIN -p udp --sport 0 -j POST_INPUT_DROP_CHAIN
  2717.  
  2718. # Here we add support for DHCP assigned IP
  2719. ##########################################
  2720. if [ "$EXT_IF_DHCP_IP" = "1" ]; then
  2721. echo " Enabling support for DHCP-assigned-IP (DHCP client)"
  2722. # Allow this host to be an DHCP client:
  2723. ip4tables -A EXT_INPUT_CHAIN -p udp --sport 67 --dport 68 -j ACCEPT
  2724. if [ "$IPV6_SUPPORT" = "1" ]; then
  2725. # Allow this host to be an DHCPv6 client:
  2726. ip6tables -A EXT_INPUT_CHAIN -p udp --sport 547 --dport 546 -j ACCEPT
  2727. fi
  2728. fi
  2729.  
  2730. # Support for a DHCP/BootP service on the EXTERNAL interface
  2731. ############################################################
  2732. if [ "$EXTERNAL_DHCP_SERVER" = "1" ]; then
  2733. echo " Enabling support for DHCP/BOOTP (DHCP server) for subnet(s): $EXTERNAL_NET"
  2734. IFS=' ,'
  2735. for net in $EXTERNAL_NET; do
  2736. # Allow this host to be a DHCP/BOOTP-server:
  2737. ip4tables -A EXT_INPUT_CHAIN -d 255.255.255.255 -p udp --dport 67 -j ACCEPT
  2738. ip4tables -A EXT_INPUT_CHAIN -s $net -p udp --dport 67 -j ACCEPT
  2739. # ip4tables -A EXT_INPUT_CHAIN -d 255.255.255.255 -p udp --sport 68 --dport 67 -j ACCEPT
  2740. # ip4tables -A EXT_INPUT_CHAIN -s $net -p udp --sport 68 --dport 67 -j ACCEPT
  2741.  
  2742. # Extra rules to allow packets from other dhcp servers in the same segment
  2743. ip4tables -A EXT_INPUT_CHAIN -s $net -d 255.255.255.255 -p udp --sport 67 --dport 68 -j ACCEPT
  2744. done
  2745. fi
  2746.  
  2747. # Support for a DHCPv6 service on the EXTERNAL interface
  2748. ########################################################
  2749. if [ "$IPV6_SUPPORT" = "1" -a "$EXTERNAL_DHCPV6_SERVER" = "1" ]; then
  2750. echo " Enabling support for DHCPv6 server on external interface(s)"
  2751. # Allow only Link-Local clients
  2752. ip6tables -A EXT_INPUT_CHAIN -s fe80::/10 -p udp --dport 547 -j ACCEPT
  2753. fi
  2754.  
  2755. # This is the fix(hack) for nmb broadcast packets (nmblookup/Samba)
  2756. ###################################################################
  2757. if [ "$NMB_BROADCAST_FIX" = "1" ]; then
  2758. echo " Enabling support for NMB-broadcasts(Samba) for subnet(s): $EXTERNAL_NET"
  2759. IFS=' ,'
  2760. for net in $EXTERNAL_NET; do
  2761. ip4tables -A EXT_INPUT_CHAIN -s $net -p udp --sport 137 --dport "$LOCAL_PORT_RANGE" -j ACCEPT
  2762. done
  2763. fi
  2764.  
  2765. # Enable logging of blocked hosts?
  2766. ##################################
  2767. if [ "$BLOCKED_HOST_LOG" != "0" ]; then
  2768. echo " Logging of explicitly blocked hosts enabled"
  2769. else
  2770. echo " Logging of explicitly blocked hosts disabled"
  2771. fi
  2772.  
  2773. # Enable logging of denied output connections?
  2774. ##############################################
  2775. if [ "$INET_OUTPUT_DENY_LOG" != "0" ]; then
  2776. echo " Logging of denied local output connections enabled"
  2777. else
  2778. echo " Logging of denied local output connections disabled"
  2779. fi
  2780.  
  2781. # Add TCP ports to allow for certain hosts
  2782. ##########################################
  2783. unset IFS
  2784. for rule in $HOST_OPEN_TCP; do
  2785. interfaces=`get_ifs "$rule"`
  2786. destips=`get_ips "$rule"`
  2787. hosts=`get_hosts_ihp "$rule"`
  2788. ports=`get_ports_ihp "$rule"`
  2789.  
  2790. echo " $(show_if_ip "$interfaces" "$destips")Allowing $hosts for TCP port(s): $ports"
  2791.  
  2792. IFS=','
  2793. for host in `ip_range "$hosts"`; do
  2794. for port in $ports; do
  2795. for destip in $destips; do
  2796. for interface in $interfaces; do
  2797. iptables -A EXT_INPUT_CHAIN -i $interface -s $host -d $destip -p tcp --dport $port -j ACCEPT
  2798. done
  2799. done
  2800. done
  2801. done
  2802. done
  2803.  
  2804.  
  2805. # Add UDP ports to allow for certain hosts
  2806. ##########################################
  2807. unset IFS
  2808. for rule in $HOST_OPEN_UDP; do
  2809. interfaces=`get_ifs "$rule"`
  2810. destips=`get_ips "$rule"`
  2811. hosts=`get_hosts_ihp "$rule"`
  2812. ports=`get_ports_ihp "$rule"`
  2813.  
  2814. echo " $(show_if_ip "$interfaces" "$destips")Allowing $hosts for UDP port(s): $ports"
  2815.  
  2816. IFS=','
  2817. for host in `ip_range "$hosts"`; do
  2818. for port in $ports; do
  2819. for destip in $destips; do
  2820. for interface in $interfaces; do
  2821. iptables -A EXT_INPUT_CHAIN -i $interface -s $host -d $destip -p udp --dport $port -j ACCEPT
  2822. done
  2823. done
  2824. done
  2825. done
  2826. done
  2827.  
  2828.  
  2829. # Add IP protocols to allow for certain hosts
  2830. #############################################
  2831. unset IFS
  2832. for rule in $HOST_OPEN_IP; do
  2833. interfaces=`get_ifs "$rule"`
  2834. destips=`get_ips "$rule"`
  2835. hosts=`get_hosts_ihp "$rule"`
  2836. protos=`get_ports_ihp "$rule"`
  2837.  
  2838. echo " $(show_if_ip "$interfaces" "$destips")Allowing $hosts for IP protocol(s): $protos"
  2839.  
  2840. IFS=','
  2841. for host in `ip_range "$hosts"`; do
  2842. IFS=','
  2843. for proto in $protos; do
  2844. for destip in $destips; do
  2845. for interface in $interfaces; do
  2846. iptables -A EXT_INPUT_CHAIN -i $interface -s $host -d $destip -p $proto -j ACCEPT
  2847. done
  2848. done
  2849. done
  2850. done
  2851. done
  2852.  
  2853.  
  2854. # Add ICMP to allow for certain hosts
  2855. #####################################
  2856. unset IFS
  2857. for rule in $HOST_OPEN_ICMP; do
  2858. interfaces=`get_ifs "$rule"`
  2859. destips=`get_ips "$rule"`
  2860. hosts=`get_hosts_ih "$rule"`
  2861.  
  2862. echo " $(show_if_ip "$interfaces" "$destips")Allowing $hosts for ICMP-requests(ping)"
  2863.  
  2864. IFS=','
  2865. for host in `ip_range "$hosts"`; do
  2866. for destip in $destips; do
  2867. for interface in $interfaces; do
  2868. iptables -A EXT_INPUT_CHAIN -i $interface -s $host -d $destip -p icmp --icmp-type echo-request -j ACCEPT
  2869. done
  2870. done
  2871. done
  2872. done
  2873.  
  2874.  
  2875. # Add TCP ports to REJECT for certain hosts but NOT logged
  2876. ##########################################################
  2877. unset IFS
  2878. for rule in $HOST_REJECT_TCP_NOLOG; do
  2879. interfaces=`get_ifs "$rule"`
  2880. destips=`get_ips "$rule"`
  2881. hosts=`get_hosts_ihp "$rule"`
  2882. ports=`get_ports_ihp "$rule"`
  2883.  
  2884. echo " $(show_if_ip "$interfaces" "$destips")Rejecting $hosts for TCP port(s) (NO LOG): $ports"
  2885.  
  2886. IFS=','
  2887. for host in `ip_range "$hosts"`; do
  2888. for port in $ports; do
  2889. for destip in $destips; do
  2890. for interface in $interfaces; do
  2891. iptables -A EXT_INPUT_CHAIN -i $interface -s $host -d $destip -p tcp --dport $port -j REJECT --reject-with tcp-reset
  2892. done
  2893. done
  2894. done
  2895. done
  2896. done
  2897.  
  2898.  
  2899. # Add UDP ports to REJECT for certain hosts NOT logged
  2900. ######################################################
  2901. unset IFS
  2902. for rule in $HOST_REJECT_UDP_NOLOG; do
  2903. interfaces=`get_ifs "$rule"`
  2904. destips=`get_ips "$rule"`
  2905. hosts=`get_hosts_ihp "$rule"`
  2906. ports=`get_ports_ihp "$rule"`
  2907.  
  2908. echo " $(show_if_ip "$interfaces" "$destips")Rejecting $hosts for UDP port(s) (NO LOG): $ports"
  2909.  
  2910. IFS=','
  2911. for host in `ip_range "$hosts"`; do
  2912. for port in $ports; do
  2913. for destip in $destips; do
  2914. for interface in $interfaces; do
  2915. iptables -A EXT_INPUT_CHAIN -i $interface -s $host -d $destip -p udp --dport $port -j REJECT --reject-with icmp-host-unreachable
  2916. done
  2917. done
  2918. done
  2919. done
  2920. done
  2921.  
  2922.  
  2923. # Add TCP ports to REJECT for certain hosts
  2924. ###########################################
  2925. unset IFS
  2926. for rule in $HOST_REJECT_TCP; do
  2927. interfaces=`get_ifs "$rule"`
  2928. destips=`get_ips "$rule"`
  2929. hosts=`get_hosts_ihp "$rule"`
  2930. ports=`get_ports_ihp "$rule"`
  2931.  
  2932. echo " $(show_if_ip "$interfaces" "$destips")Rejecting $hosts for TCP port(s): $ports"
  2933.  
  2934. IFS=','
  2935. for host in `ip_range "$hosts"`; do
  2936. for port in $ports; do
  2937. for destip in $destips; do
  2938. for interface in $interfaces; do
  2939. iptables -A EXT_INPUT_CHAIN -i $interface -s $host -d $destip -p tcp --dport $port \
  2940. -m limit --limit 1/h --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Hostwise TCP rejected: "
  2941.  
  2942. iptables -A EXT_INPUT_CHAIN -i $interface -s $host -d $destip -p tcp --dport $port -j REJECT --reject-with tcp-reset
  2943. done
  2944. done
  2945. done
  2946. done
  2947. done
  2948.  
  2949.  
  2950. # Add UDP ports to REJECT for certain hosts
  2951. ###########################################
  2952. unset IFS
  2953. for rule in $HOST_REJECT_UDP; do
  2954. interfaces=`get_ifs "$rule"`
  2955. destips=`get_ips "$rule"`
  2956. hosts=`get_hosts_ihp "$rule"`
  2957. ports=`get_ports_ihp "$rule"`
  2958.  
  2959. echo " $(show_if_ip "$interfaces" "$destips")Rejecting $hosts for UDP port(s): $ports"
  2960.  
  2961. IFS=','
  2962. for host in `ip_range "$hosts"`; do
  2963. for port in $ports; do
  2964. for destip in $destips; do
  2965. for interface in $interfaces; do
  2966. iptables -A EXT_INPUT_CHAIN -i $interface -s $host -d $destip -p udp --dport $port \
  2967. -m limit --limit 1/h --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Hostwise UDP rejected: "
  2968.  
  2969. iptables -A EXT_INPUT_CHAIN -i $interface -s $host -d $destip -p udp --dport $port -j REJECT --reject-with icmp-host-unreachable
  2970. done
  2971. done
  2972. done
  2973. done
  2974. done
  2975.  
  2976.  
  2977. # Add TCP ports to DENY for certain hosts but NOT logged
  2978. ########################################################
  2979. unset IFS
  2980. for rule in $HOST_DENY_TCP_NOLOG; do
  2981. interfaces=`get_ifs "$rule"`
  2982. destips=`get_ips "$rule"`
  2983. hosts=`get_hosts_ihp "$rule"`
  2984. ports=`get_ports_ihp "$rule"`
  2985.  
  2986. echo " $(show_if_ip "$interfaces" "$destips")Denying $hosts for TCP port(s) (NO LOG): $ports"
  2987.  
  2988. IFS=','
  2989. for host in `ip_range "$hosts"`; do
  2990. for port in $ports; do
  2991. for destip in $destips; do
  2992. for interface in $interfaces; do
  2993. iptables -A EXT_INPUT_CHAIN -i $interface -s $host -d $destip -p tcp --dport $port -j POST_INPUT_DROP_CHAIN
  2994. done
  2995. done
  2996. done
  2997. done
  2998. done
  2999.  
  3000.  
  3001. # Add UDP ports to DENY for certain hosts but NOT logged
  3002. ########################################################
  3003. unset IFS
  3004. for rule in $HOST_DENY_UDP_NOLOG; do
  3005. interfaces=`get_ifs "$rule"`
  3006. destips=`get_ips "$rule"`
  3007. hosts=`get_hosts_ihp "$rule"`
  3008. ports=`get_ports_ihp "$rule"`
  3009.  
  3010. echo " $(show_if_ip "$interfaces" "$destips")Denying $hosts for UDP port(s) (NO LOG): $ports"
  3011.  
  3012. IFS=','
  3013. for host in `ip_range "$hosts"`; do
  3014. for port in $ports; do
  3015. for destip in $destips; do
  3016. for interface in $interfaces; do
  3017. iptables -A EXT_INPUT_CHAIN -i $interface -s $host -d $destip -p udp --dport $port -j POST_INPUT_DROP_CHAIN
  3018. done
  3019. done
  3020. done
  3021. done
  3022. done
  3023.  
  3024. # Add IP protocols to DENY for certain hosts but NOT logged
  3025. ###########################################################
  3026. unset IFS
  3027. for rule in $HOST_DENY_IP_NOLOG; do
  3028. interfaces=`get_ifs "$rule"`
  3029. destips=`get_ips "$rule"`
  3030. hosts=`get_hosts_ihp "$rule"`
  3031. protos=`get_ports_ihp "$rule"`
  3032.  
  3033. echo " $(show_if_ip "$interfaces" "$destips")Denying $hosts for IP protocol(s) (NO LOG): $protos"
  3034.  
  3035. IFS=','
  3036. for host in `ip_range "$hosts"`; do
  3037. for proto in $protos; do
  3038. for destip in $destips; do
  3039. for interface in $interfaces; do
  3040. iptables -A EXT_INPUT_CHAIN -i $interface -s $host -d $destip -p $proto -j POST_INPUT_DROP_CHAIN
  3041. done
  3042. done
  3043. done
  3044. done
  3045. done
  3046.  
  3047. # Add ICMP-request to DENY for certain hosts but NOT logged
  3048. ############################################################
  3049. unset IFS
  3050. for rule in $HOST_DENY_ICMP_NOLOG; do
  3051. interfaces=`get_ifs "$rule"`
  3052. destips=`get_ips "$rule"`
  3053. hosts=`get_hosts_ih "$rule"`
  3054.  
  3055. echo " $(show_if_ip "$interfaces" "$destips")Denying $hosts for ICMP-requests(ping)"
  3056.  
  3057. IFS=','
  3058. for host in `ip_range "$hosts"`; do
  3059. for destip in $destips; do
  3060. for interface in $interfaces; do
  3061. iptables -A EXT_INPUT_CHAIN -i $interface -s $host -d $destip -p icmp --icmp-type echo-request -j POST_INPUT_DROP_CHAIN
  3062. done
  3063. done
  3064. done
  3065. done
  3066.  
  3067.  
  3068. # Add TCP ports to DENY for certain hosts
  3069. #########################################
  3070. unset IFS
  3071. for rule in $HOST_DENY_TCP; do
  3072. interfaces=`get_ifs "$rule"`
  3073. destips=`get_ips "$rule"`
  3074. hosts=`get_hosts_ihp "$rule"`
  3075. ports=`get_ports_ihp "$rule"`
  3076.  
  3077. echo " $(show_if_ip "$interfaces" "$destips")Denying $hosts for TCP port(s): $ports"
  3078.  
  3079. IFS=','
  3080. for host in `ip_range "$hosts"`; do
  3081. for port in $ports; do
  3082. for destip in $destips; do
  3083. for interface in $interfaces; do
  3084. iptables -A EXT_INPUT_CHAIN -i $interface -s $host -d $destip -p tcp --dport $port \
  3085. -m limit --limit 1/h --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Host INET denied: "
  3086.  
  3087. iptables -A EXT_INPUT_CHAIN -i $interface -s $host -d $destip -p tcp --dport $port -j POST_INPUT_DROP_CHAIN
  3088. done
  3089. done
  3090. done
  3091. done
  3092. done
  3093.  
  3094.  
  3095. # Add UDP ports to DENY for certain hosts
  3096. #########################################
  3097. unset IFS
  3098. for rule in $HOST_DENY_UDP; do
  3099. interfaces=`get_ifs "$rule"`
  3100. destips=`get_ips "$rule"`
  3101. hosts=`get_hosts_ihp "$rule"`
  3102. ports=`get_ports_ihp "$rule"`
  3103.  
  3104. echo " $(show_if_ip "$interfaces" "$destips")Denying $hosts for UDP port(s): $ports"
  3105.  
  3106. IFS=','
  3107. for host in `ip_range "$hosts"`; do
  3108. for port in $ports; do
  3109. for destip in $destips; do
  3110. for interface in $interfaces; do
  3111. iptables -A EXT_INPUT_CHAIN -i $interface -s $host -d $destip -p udp --dport $port \
  3112. -m limit --limit 1/h --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Host INET denied: "
  3113.  
  3114. iptables -A EXT_INPUT_CHAIN -i $interface -s $host -d $destip -p udp --dport $port -j POST_INPUT_DROP_CHAIN
  3115. done
  3116. done
  3117. done
  3118. done
  3119. done
  3120.  
  3121.  
  3122. # Add IP protocols to DENY for certain hosts
  3123. ############################################
  3124. unset IFS
  3125. for rule in $HOST_DENY_IP; do
  3126. interfaces=`get_ifs "$rule"`
  3127. destips=`get_ips "$rule"`
  3128. hosts=`get_hosts_ihp "$rule"`
  3129. protos=`get_ports_ihp "$rule"`
  3130.  
  3131. echo " $(show_if_ip "$interfaces" "$destips")Denying $hosts for IP protocol(s): $protos"
  3132.  
  3133. IFS=','
  3134. for host in `ip_range "$hosts"`; do
  3135. for proto in $protos; do
  3136. for destip in $destips; do
  3137. for interface in $interfaces; do
  3138. iptables -A EXT_INPUT_CHAIN -s $host -p $proto \
  3139. -m limit --limit 1/h --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Host INET denied: "
  3140.  
  3141. iptables -A EXT_INPUT_CHAIN -s $host -p $proto -j POST_INPUT_DROP_CHAIN
  3142. done
  3143. done
  3144. done
  3145. done
  3146. done
  3147.  
  3148.  
  3149. # Add ICMP-request to DENY for certain hosts
  3150. ############################################
  3151. unset IFS
  3152. for rule in $HOST_DENY_ICMP; do
  3153. interfaces=`get_ifs "$rule"`
  3154. destips=`get_ips "$rule"`
  3155. hosts=`get_hosts_ih "$rule"`
  3156.  
  3157. echo " $(show_if_ip "$interfaces" "$destips")Denying $hosts for ICMP-requests(ping)"
  3158.  
  3159. IFS=','
  3160. for host in `ip_range "$hosts"`; do
  3161. for destip in $destips; do
  3162. for interface in $interfaces; do
  3163. if [ "$ICMP_DROP_LOG" != "0" ]; then
  3164. iptables -A EXT_INPUT_CHAIN -s $host -p icmp --icmp-type echo-request -m limit --limit 1/h --limit-burst 1 \
  3165. -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Host INET denied: "
  3166. fi
  3167. iptables -A EXT_INPUT_CHAIN -s $host -p icmp --icmp-type echo-request -j POST_INPUT_DROP_CHAIN
  3168. done
  3169. done
  3170. done
  3171. done
  3172.  
  3173. # Adding TCP ports to be denied for everyone
  3174. ############################################
  3175. unset IFS
  3176. for rule in $DENY_TCP; do
  3177. interfaces=`get_ifs "$rule"`
  3178. destips=`get_ips "$rule"`
  3179. ports=`get_ports_ip "$rule"`
  3180.  
  3181. echo " $(show_if_ip "$interfaces" "$destips")Denying ANYHOST for TCP port(s): $ports"
  3182.  
  3183. IFS=','
  3184. for port in $ports; do
  3185. for destip in $destips; do
  3186. for interface in $interfaces; do
  3187. iptables -A EXT_INPUT_CHAIN -i $interface -d $destip -p tcp --dport $port \
  3188. -m limit --limit 3/m --limit-burst 5 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:INET-INPUT denied: "
  3189.  
  3190. iptables -A EXT_INPUT_CHAIN -i $interface -d $destip -p tcp --dport $port -j POST_INPUT_DROP_CHAIN
  3191. done
  3192. done
  3193. done
  3194. done
  3195.  
  3196. # Adding UDP ports to be denied for everyone
  3197. ############################################
  3198. unset IFS
  3199. for rule in $DENY_UDP; do
  3200. interfaces=`get_ifs "$rule"`
  3201. destips=`get_ips "$rule"`
  3202. ports=`get_ports_ip "$rule"`
  3203.  
  3204. echo " $(show_if_ip "$interfaces" "$destips")Denying ANYHOST for UDP port(s): $ports"
  3205.  
  3206. IFS=','
  3207. for port in $ports; do
  3208. for destip in $destips; do
  3209. for interface in $interfaces; do
  3210. iptables -A EXT_INPUT_CHAIN -i $interface -d $destip -p udp --dport $port \
  3211. -m limit --limit 3/m --limit-burst 5 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:INET-INPUT denied: "
  3212.  
  3213. iptables -A EXT_INPUT_CHAIN -i $interface -d $destip -p udp --dport $port -j POST_INPUT_DROP_CHAIN
  3214. done
  3215. done
  3216. done
  3217. done
  3218.  
  3219. # Adding TCP ports to be rejected for everyone
  3220. ##############################################
  3221. unset IFS
  3222. for rule in $REJECT_TCP; do
  3223. interfaces=`get_ifs "$rule"`
  3224. destips=`get_ips "$rule"`
  3225. ports=`get_ports_ip "$rule"`
  3226.  
  3227. echo " $(show_if_ip "$interfaces" "$destips")Rejecting ANYHOST for TCP port(s): $ports"
  3228.  
  3229. IFS=','
  3230. for port in $ports; do
  3231. for destip in $destips; do
  3232. for interface in $interfaces; do
  3233. iptables -A EXT_INPUT_CHAIN -i $interface -d $destip -p tcp --dport $port \
  3234. -m limit --limit 3/m --limit-burst 5 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Rejected TCP port: "
  3235.  
  3236. iptables -A EXT_INPUT_CHAIN -i $interface -d $destip -p tcp --dport $port -j REJECT --reject-with tcp-reset
  3237. done
  3238. done
  3239. done
  3240. done
  3241.  
  3242. # Adding UDP ports to be rejected for everyone
  3243. ##############################################
  3244. unset IFS
  3245. for rule in $REJECT_UDP; do
  3246. interfaces=`get_ifs "$rule"`
  3247. destips=`get_ips "$rule"`
  3248. ports=`get_ports_ip "$rule"`
  3249.  
  3250. echo " $(show_if_ip "$interfaces" "$destips")Rejecting ANYHOST for UDP port(s): $ports"
  3251.  
  3252. IFS=','
  3253. for port in $ports; do
  3254. for destip in $destips; do
  3255. for interface in $interfaces; do
  3256. iptables -A EXT_INPUT_CHAIN -p udp --dport $port \
  3257. -m limit --limit 3/m --limit-burst 5 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Rejected UDP port: "
  3258.  
  3259. iptables -A EXT_INPUT_CHAIN -p udp --dport $port -j REJECT --reject-with icmp-host-unreachable
  3260. done
  3261. done
  3262. done
  3263. done
  3264.  
  3265. # Adding the "full access hosts"
  3266. ################################
  3267. unset IFS
  3268. for rule in $FULL_ACCESS_HOSTS; do
  3269. interfaces=`get_ifs "$rule"`
  3270. destips=`get_ips "$rule"`
  3271. hosts=`get_hosts_ih "$rule"`
  3272.  
  3273. echo " $(show_if_ip "$interfaces" "$destips")Allowing $hosts full (inbound) access"
  3274.  
  3275. IFS=','
  3276. for host in `ip_range "$hosts"`; do
  3277. for destip in $destips; do
  3278. for interface in $interfaces; do
  3279. iptables -A EXT_INPUT_CHAIN -i $interface -s $host -d $destip -j ACCEPT
  3280. done
  3281. done
  3282. done
  3283. done
  3284.  
  3285. # TCP ports to DENY but NOT to LOG
  3286. ##################################
  3287. unset IFS
  3288. for rule in $DENY_TCP_NOLOG; do
  3289. interfaces=`get_ifs "$rule"`
  3290. destips=`get_ips "$rule"`
  3291. ports=`get_ports_ip "$rule"`
  3292.  
  3293. echo " $(show_if_ip "$interfaces" "$destips")Denying ANYHOST for TCP port(s) (NO LOG): $ports"
  3294.  
  3295. IFS=','
  3296. for port in $ports; do
  3297. for destip in $destips; do
  3298. for interface in $interfaces; do
  3299. iptables -A EXT_INPUT_CHAIN -i $interface -d $destip -p tcp --dport $port -j POST_INPUT_DROP_CHAIN
  3300. done
  3301. done
  3302. done
  3303. done
  3304.  
  3305. # UDP ports to DENY but NOT to LOG
  3306. ##################################
  3307. unset IFS
  3308. for rule in $DENY_UDP_NOLOG; do
  3309. interfaces=`get_ifs "$rule"`
  3310. destips=`get_ips "$rule"`
  3311. ports=`get_ports_ip "$rule"`
  3312.  
  3313. echo " $(show_if_ip "$interfaces" "$destips")Denying ANYHOST for UDP port(s) (NO LOG): $ports"
  3314.  
  3315. IFS=','
  3316. for port in $ports; do
  3317. for destip in $destips; do
  3318. for interface in $interfaces; do
  3319. iptables -A EXT_INPUT_CHAIN -i $interface -d $destip -p udp --dport $port -j POST_INPUT_DROP_CHAIN
  3320. done
  3321. done
  3322. done
  3323. done
  3324.  
  3325. # TCP ports to REJECT but NOT to LOG
  3326. ####################################
  3327. unset IFS
  3328. for rule in $REJECT_TCP_NOLOG; do
  3329. interfaces=`get_ifs "$rule"`
  3330. destips=`get_ips "$rule"`
  3331. ports=`get_ports_ip "$rule"`
  3332.  
  3333. echo " $(show_if_ip "$interfaces" "$destips")Rejecting ANYHOST for TCP port(s) (NO LOG): $ports"
  3334.  
  3335. IFS=','
  3336. for port in $ports; do
  3337. for destip in $destips; do
  3338. for interface in $interfaces; do
  3339. iptables -A EXT_INPUT_CHAIN -i $interface -d $destip -p tcp --dport $port -j REJECT --reject-with tcp-reset
  3340. done
  3341. done
  3342. done
  3343. done
  3344.  
  3345. # UDP ports to REJECT but NOT to LOG
  3346. ####################################
  3347. unset IFS
  3348. for rule in $REJECT_UDP_NOLOG; do
  3349. interfaces=`get_ifs "$rule"`
  3350. destips=`get_ips "$rule"`
  3351. ports=`get_ports_ip "$rule"`
  3352.  
  3353. echo " $(show_if_ip "$interfaces" "$destips")Rejecting ANYHOST for UDP port(s) (NO LOG): $ports"
  3354.  
  3355. IFS=','
  3356. for port in $ports; do
  3357. for destip in $destips; do
  3358. for interface in $interfaces; do
  3359. iptables -A EXT_INPUT_CHAIN -i $interface -d $destip -p udp --dport $port -j REJECT --reject-with icmp-host-unreachable
  3360. done
  3361. done
  3362. done
  3363. done
  3364.  
  3365. # Check the packet source address
  3366. #################################
  3367. if [ "$DROP_PRIVATE_ADDRESSES" = "1" ]; then
  3368. echo " Packets will be checked for private source addresses"
  3369. else
  3370. echo " Packets will NOT be checked for private source addresses"
  3371. fi
  3372. if [ "$DROP_PRIVATE_ADDRESSES" = "1" -o "$RESERVED_NET_LOG" = "1" ]; then
  3373. iptables -A EXT_INPUT_CHAIN -j RESERVED_NET_CHK
  3374. fi
  3375.  
  3376. # Do NOT allow DRDOS abuse (Distributed Reflection Denial Of Service attack)
  3377. ############################################################################
  3378. if [ "$DRDOS_PROTECT" = "1" ]; then
  3379. echo " Enabling protection against DRDOS-abuse"
  3380.  
  3381. iptables -A EXT_INPUT_CHAIN -p tcp ! --dport 2049 -m multiport --sports 20,21,22,23,80,110,143,443,993,995 \
  3382. -m limit --limit 6/h --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Possible DRDOS abuse: "
  3383. iptables -A EXT_INPUT_CHAIN -p udp ! --dport 2049 -m multiport --sports 20,21,22,23,80,110,143,443,993,995 \
  3384. -m limit --limit 6/h --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Possible DRDOS abuse: "
  3385.  
  3386. iptables -A EXT_INPUT_CHAIN -p tcp ! --dport 2049 -m multiport --sports 20,21,22,23,80,110,143,443,993,995 -j POST_INPUT_DROP_CHAIN
  3387. iptables -A EXT_INPUT_CHAIN -p udp ! --dport 2049 -m multiport --sports 20,21,22,23,80,110,143,443,993,995 -j POST_INPUT_DROP_CHAIN
  3388. fi
  3389.  
  3390. # Adding TCP ports NOT to be firewalled
  3391. #######################################
  3392. unset IFS
  3393. for rule in $OPEN_TCP; do
  3394. interfaces=`get_ifs "$rule"`
  3395. destips=`get_ips "$rule"`
  3396. ports=`get_ports_ip "$rule"`
  3397.  
  3398. echo " $(show_if_ip "$interfaces" "$destips")Allowing ANYHOST for TCP port(s): $ports"
  3399.  
  3400. IFS=','
  3401. for port in $ports; do
  3402. for destip in $destips; do
  3403. for interface in $interfaces; do
  3404. iptables -A EXT_INPUT_CHAIN -i $interface -d $destip -p tcp --dport $port -j ACCEPT
  3405. done
  3406. done
  3407. done
  3408. done
  3409.  
  3410.  
  3411. # Adding UDP ports NOT to be firewalled
  3412. #######################################
  3413. unset IFS
  3414. for rule in $OPEN_UDP; do
  3415. interfaces=`get_ifs "$rule"`
  3416. destips=`get_ips "$rule"`
  3417. ports=`get_ports_ip "$rule"`
  3418.  
  3419. echo " $(show_if_ip "$interfaces" "$destips")Allowing ANYHOST for UDP port(s): $ports"
  3420.  
  3421. IFS=','
  3422. for port in $ports; do
  3423. for destip in $destips; do
  3424. for interface in $interfaces; do
  3425. iptables -A EXT_INPUT_CHAIN -i $interface -d $destip -p udp --dport $port -j ACCEPT
  3426. done
  3427. done
  3428. done
  3429. done
  3430.  
  3431.  
  3432. # Adding IP protocols NOT to be firewalled
  3433. ##########################################
  3434. unset IFS
  3435. for rule in $OPEN_IP; do
  3436. interfaces=`get_ifs "$rule"`
  3437. destips=`get_ips "$rule"`
  3438. protos=`get_ports_ip "$rule"`
  3439.  
  3440. echo " $(show_if_ip "$interfaces" "$destips")Allowing ANYHOST for IP protocol(s): $protos"
  3441.  
  3442. IFS=','
  3443. for proto in $protos; do
  3444. for destip in $destips; do
  3445. for interface in $interfaces; do
  3446. iptables -A EXT_INPUT_CHAIN -i $interface -d $destip -p $proto -j ACCEPT
  3447. done
  3448. done
  3449. done
  3450. done
  3451.  
  3452. # Allow world to send IPv4 ICMP packets?
  3453. ########################################
  3454. if [ "$OPEN_ICMP" = "1" ]; then
  3455. echo " Allowing ANYHOST to send IPv4 ICMP-requests (ping)"
  3456. ip4tables -A EXT_INPUT_CHAIN -p icmp --icmp-type echo-request -m limit --limit 20/second --limit-burst 100 -j ACCEPT
  3457. else
  3458. echo " Denying ANYHOST to send IPv4 ICMP-requests (ping)"
  3459. fi
  3460.  
  3461. # Allow world to send IPv6 ICMPv6 packets?
  3462. ##########################################
  3463. if [ "$IPV6_SUPPORT" = "1" ]; then
  3464. if [ "$OPEN_ICMPV6" != "0" ]; then
  3465. echo " Allowing ANYHOST to send IPv6 ICMPv6-requests"
  3466. ip6tables -A EXT_INPUT_CHAIN -p icmpv6 --icmpv6-type echo-request -m limit --limit 20/second --limit-burst 100 -j ACCEPT
  3467. else
  3468. echo " Denying ANYHOST to send IPv6 ICMPv6-requests"
  3469. fi
  3470. fi
  3471.  
  3472. # Logging of possible stealth scans
  3473. ###################################
  3474. if [ "$POSSIBLE_SCAN_LOG" = "1" ]; then
  3475. echo " Logging of possible stealth scans enabled"
  3476. if [ "$UNPRIV_TCP_LOG" != "0" ]; then
  3477. iptables -A EXT_INPUT_CHAIN -p tcp ! --syn --dport 1024: \
  3478. -m limit --limit 3/m --limit-burst 5 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Stealth scan? (UNPRIV): "
  3479. fi
  3480.  
  3481. if [ "$PRIV_TCP_LOG" != "0" ]; then
  3482. iptables -A EXT_INPUT_CHAIN -p tcp ! --syn --dport :1023 \
  3483. -m limit --limit 3/m --limit-burst 5 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Stealth scan? (PRIV): "
  3484. fi
  3485. else
  3486. echo " Logging of possible stealth scans disabled"
  3487. fi
  3488.  
  3489. # General stealth scan drop
  3490. ###########################
  3491. iptables -A EXT_INPUT_CHAIN -p tcp ! --syn -j POST_INPUT_DROP_CHAIN
  3492.  
  3493. # Setup IPv4 chain to handle broadcast traffic
  3494. ##############################################
  3495. ip4tables -A EXT_INPUT_CHAIN -d 255.255.255.255 -j EXT_BROADCAST_CHAIN
  3496.  
  3497. # ip4tables -A EXT_INPUT_CHAIN -m pkttype --pkt-type broadcast -j EXT_BROADCAST_CHAIN
  3498. # ip4tables -A EXT_INPUT_CHAIN -m addrtype --dst-type BROADCAST -j EXT_BROADCAST_CHAIN
  3499. if [ -n "$EXT_NET_BCAST_ADDRESS" ]; then
  3500. IFS=' ,'
  3501. for address in $EXT_NET_BCAST_ADDRESS; do
  3502. ip4tables -A EXT_INPUT_CHAIN -d $address -j EXT_BROADCAST_CHAIN
  3503. done
  3504. else
  3505. IFS=' ,'
  3506. for net in $EXTERNAL_NET; do
  3507. # Class C net
  3508. ip4tables -A EXT_INPUT_CHAIN -d $(echo $net |awk -F. '{ print $1"."$2"."$3".255" }') \
  3509. -j EXT_BROADCAST_CHAIN
  3510.  
  3511. # Class B net
  3512. ip4tables -A EXT_INPUT_CHAIN -d $(echo $net |awk -F. '{ print $1"."$2".255.255" }') \
  3513. -j EXT_BROADCAST_CHAIN
  3514.  
  3515. # Class A net
  3516. ip4tables -A EXT_INPUT_CHAIN -d $(echo $net |awk -F. '{ print $1".255.255.255" }') \
  3517. -j EXT_BROADCAST_CHAIN
  3518. done
  3519. fi
  3520.  
  3521. # Handle multicast traffic
  3522. ##########################
  3523. ip4tables -A EXT_INPUT_CHAIN -d 224.0.0.0/4 -j EXT_MULTICAST_CHAIN
  3524. if [ "$IPV6_SUPPORT" = "1" ]; then
  3525. ip6tables -A EXT_INPUT_CHAIN -d ff00::/8 -j EXT_MULTICAST_CHAIN
  3526. fi
  3527.  
  3528. # Log packets to privileged TCP ports?
  3529. ##################################################
  3530. if [ "$PRIV_TCP_LOG" != "0" ]; then
  3531. echo " Logging of (other) packets to PRIVILEGED TCP ports enabled"
  3532. iptables -A EXT_INPUT_CHAIN -p tcp --dport :1023 \
  3533. -m limit --limit 6/m --limit-burst 2 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:PRIV TCP packet: "
  3534. iptables -A EXT_MULTICAST_CHAIN -p tcp --dport :1023 \
  3535. -m limit --limit 6/m --limit-burst 2 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:PRIV TCP multicast: "
  3536. iptables -A EXT_BROADCAST_CHAIN -p tcp --dport :1023 \
  3537. -m limit --limit 6/m --limit-burst 2 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:PRIV TCP broadcast: "
  3538. else
  3539. echo " Logging of (other) packets to PRIVILEGED TCP ports disabled"
  3540. fi
  3541.  
  3542. # Log packets to privileged UDP ports?
  3543. ##################################################
  3544. if [ "$PRIV_UDP_LOG" != "0" ]; then
  3545. echo " Logging of (other) packets to PRIVILEGED UDP ports enabled"
  3546. iptables -A EXT_INPUT_CHAIN -p udp --dport :1023 \
  3547. -m limit --limit 6/m --limit-burst 2 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:PRIV UDP packet: "
  3548. iptables -A EXT_MULTICAST_CHAIN -p udp --dport :1023 \
  3549. -m limit --limit 6/m --limit-burst 2 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:PRIV UDP multicast: "
  3550. iptables -A EXT_BROADCAST_CHAIN -p udp --dport :1023 \
  3551. -m limit --limit 6/m --limit-burst 2 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:PRIV UDP broadcast: "
  3552. else
  3553. echo " Logging of (other) packets to PRIVILEGED UDP ports disabled"
  3554. fi
  3555.  
  3556. # Log packets to unprivileged TCP ports?
  3557. ####################################################
  3558. if [ "$UNPRIV_TCP_LOG" != "0" ]; then
  3559. echo " Logging of (other) packets to UNPRIVILEGED TCP ports enabled"
  3560. iptables -A EXT_INPUT_CHAIN -p tcp --dport 1024: \
  3561. -m limit --limit 6/m --limit-burst 2 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:UNPRIV TCP packet: "
  3562. iptables -A EXT_MULTICAST_CHAIN -p tcp --dport 1024: \
  3563. -m limit --limit 6/m --limit-burst 2 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:UNPRIV TCP multicast: "
  3564. iptables -A EXT_BROADCAST_CHAIN -p tcp --dport 1024: \
  3565. -m limit --limit 6/m --limit-burst 2 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:UNPRIV TCP broadcast: "
  3566. else
  3567. echo " Logging of (other) packets to UNPRIVILEGED TCP ports disabled"
  3568. fi
  3569.  
  3570. # Log packets to unprivileged UDP ports?
  3571. ####################################################
  3572. if [ "$UNPRIV_UDP_LOG" != "0" ]; then
  3573. echo " Logging of (other) packets to UNPRIVILEGED UDP ports enabled"
  3574. iptables -A EXT_INPUT_CHAIN -p udp --dport 1024: \
  3575. -m limit --limit 6/m --limit-burst 2 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:UNPRIV UDP packet: "
  3576. iptables -A EXT_MULTICAST_CHAIN -p udp --dport 1024 \
  3577. -m limit --limit 6/m --limit-burst 2 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:UNPRIV UDP multicast: "
  3578. iptables -A EXT_BROADCAST_CHAIN -p udp --dport 1024 \
  3579. -m limit --limit 6/m --limit-burst 2 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:UNPRIV UDP broadcast: "
  3580. else
  3581. echo "Logging of (other) packets to UNPRIVILEGED UDP ports disabled"
  3582. fi
  3583.  
  3584. # Do we want to log igmp packets?
  3585. #############################################
  3586. if [ "$IGMP_LOG" != "0" ]; then
  3587. echo " Logging of IGMP packets enabled"
  3588. ip4tables -A EXT_INPUT_CHAIN -p 2 \
  3589. -m limit --limit 1/m --limit-burst 5 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:IGMP packet: "
  3590. else
  3591. echo " Logging of IPv4 IGMP packets disabled"
  3592. fi
  3593.  
  3594. # Allow all packets that have been locally redirected
  3595. #####################################################
  3596. if [ "$NAT_LOCAL_REDIRECT" = "1" ]; then
  3597. echo " Enabling support for NAT local redirect"
  3598. ip4tables -A EXT_INPUT_CHAIN -m conntrack --ctstate DNAT -j ACCEPT
  3599. fi
  3600.  
  3601. # Finally drop all in the broadcast chain
  3602. iptables -A EXT_BROADCAST_CHAIN -j DROP
  3603.  
  3604. # Jump into the POST_INPUT_CHAIN before we start to DROP
  3605. iptables -A EXT_INPUT_CHAIN -j POST_INPUT_CHAIN
  3606.  
  3607. if [ "$ICMP_REQUEST_LOG" != "0" ]; then
  3608. echo " Logging of dropped ICMP-request(ping) packets enabled"
  3609. iptables -A EXT_INPUT_CHAIN -p icmp --icmp-type echo-request \
  3610. -m limit --limit 3/m --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:ICMP-request: "
  3611. iptables -A EXT_MULTICAST_CHAIN -p icmp --icmp-type echo-request \
  3612. -m limit --limit 3/m --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:ICMP-multicast-request: "
  3613. else
  3614. echo " Logging of dropped ICMP-request(ping) packets disabled"
  3615. fi
  3616.  
  3617. if [ "$ICMP_OTHER_LOG" != "0" ]; then
  3618. echo " Logging of dropped other ICMP packets enabled"
  3619. iptables -A EXT_INPUT_CHAIN -p icmp ! --icmp-type echo-request \
  3620. -m limit --limit 12/hour --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:ICMP-other: "
  3621. iptables -A EXT_MULTICAST_CHAIN -p icmp ! --icmp-type echo-request \
  3622. -m limit --limit 12/hour --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:ICMP-multicast-other: "
  3623. else
  3624. echo " Logging of dropped other ICMP packets disabled"
  3625. fi
  3626.  
  3627. # Drop all in the multicast chain
  3628. iptables -A EXT_MULTICAST_CHAIN -j DROP
  3629.  
  3630. # Drop all "standard" IP protocols
  3631. ##################################
  3632. iptables -A EXT_INPUT_CHAIN -p tcp -j POST_INPUT_DROP_CHAIN
  3633. iptables -A EXT_INPUT_CHAIN -p udp -j POST_INPUT_DROP_CHAIN
  3634. ip4tables -A EXT_INPUT_CHAIN -p 2 -j POST_INPUT_DROP_CHAIN
  3635. iptables -A EXT_INPUT_CHAIN -p icmp -j POST_INPUT_DROP_CHAIN
  3636.  
  3637. # Do we want to log non udp/tcp/icmp packets?
  3638. #############################################
  3639. if [ "$OTHER_IP_LOG" != "0" ]; then
  3640. echo " Logging of other IP protocols (non TCP/UDP/ICMP/IGMP) packets enabled"
  3641. iptables -A EXT_INPUT_CHAIN \
  3642. -m limit --limit 1/m --limit-burst 5 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Other connect: "
  3643. else
  3644. echo " Logging of other IP protocols (non TCP/UDP/ICMP/IGMP) packets disabled"
  3645. fi
  3646.  
  3647. # Drop all remaining packets
  3648. ############################
  3649. iptables -A EXT_INPUT_CHAIN -j POST_INPUT_DROP_CHAIN
  3650. }
  3651.  
  3652.  
  3653. ######################################################################################################################
  3654. ## Chain EXT_ICMP_FLOOD_CHAIN - Checks all ICMP (flooded) packets for the EXTERNAL interface(s) ##
  3655. ######################################################################################################################
  3656. setup_ext_icmp_flood_chain()
  3657. {
  3658. # Log of ICMP flooding
  3659. ######################
  3660. if [ "$ICMP_FLOOD_LOG" != "0" ]; then
  3661. echo " Logging of ICMP flooding enabled"
  3662.  
  3663. iptables -A EXT_ICMP_FLOOD_CHAIN -p icmp --icmp-type destination-unreachable \
  3664. -m limit --limit 12/hour --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:ICMP-unreachable flood: "
  3665. iptables -A EXT_ICMP_FLOOD_CHAIN -p icmp --icmp-type destination-unreachable -j POST_INPUT_DROP_CHAIN
  3666.  
  3667. iptables -A EXT_ICMP_FLOOD_CHAIN -p icmp --icmp-type time-exceeded \
  3668. -m limit --limit 12/hour --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:ICMP-time-exceeded fld: "
  3669. iptables -A EXT_ICMP_FLOOD_CHAIN -p icmp --icmp-type time-exceeded -j POST_INPUT_DROP_CHAIN
  3670.  
  3671. iptables -A EXT_ICMP_FLOOD_CHAIN -p icmp --icmp-type parameter-problem \
  3672. -m limit --limit 12/hour --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:ICMP-param-problem fld: "
  3673. iptables -A EXT_ICMP_FLOOD_CHAIN -p icmp --icmp-type parameter-problem -j POST_INPUT_DROP_CHAIN
  3674.  
  3675. iptables -A EXT_ICMP_FLOOD_CHAIN -p icmp --icmp-type echo-request \
  3676. -m limit --limit 12/hour --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:ICMP-request(ping) fld: "
  3677. iptables -A EXT_ICMP_FLOOD_CHAIN -p icmp --icmp-type echo-request -j POST_INPUT_DROP_CHAIN
  3678.  
  3679. iptables -A EXT_ICMP_FLOOD_CHAIN -p icmp --icmp-type echo-reply \
  3680. -m limit --limit 12/hour --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:ICMP-reply(pong) flood: "
  3681. iptables -A EXT_ICMP_FLOOD_CHAIN -p icmp --icmp-type echo-reply -j POST_INPUT_DROP_CHAIN
  3682.  
  3683. ip4tables -A EXT_ICMP_FLOOD_CHAIN -p icmp --icmp-type source-quench \
  3684. -m limit --limit 12/hour --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:ICMP-source-quench fld: "
  3685. ip4tables -A EXT_ICMP_FLOOD_CHAIN -p icmp --icmp-type source-quench -j POST_INPUT_DROP_CHAIN
  3686.  
  3687. if [ "$IPV6_SUPPORT" = "1" ]; then
  3688. ip6tables -A EXT_ICMP_FLOOD_CHAIN -p icmpv6 --icmpv6-type packet-too-big \
  3689. -m limit --limit 12/hour --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:ICMP-packet-too-big fld: "
  3690. ip6tables -A EXT_ICMP_FLOOD_CHAIN -p icmpv6 --icmpv6-type packet-too-big -j POST_INPUT_DROP_CHAIN
  3691. fi
  3692.  
  3693. # All other ICMP into the general log rule
  3694. iptables -A EXT_ICMP_FLOOD_CHAIN -p icmp \
  3695. -m limit --limit 12/hour --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:ICMP(other) flood: "
  3696. else
  3697. echo " Logging of ICMP flooding disabled"
  3698. fi
  3699.  
  3700. # Drop any ICMP packets left
  3701. iptables -A EXT_ICMP_FLOOD_CHAIN -p icmp -j POST_INPUT_DROP_CHAIN
  3702. }
  3703.  
  3704.  
  3705. ######################################################################################################################
  3706. ## Chain EXT_OUTPUT_CHAIN - Checks all outgoing packets for the EXTERNAL interface(s) ##
  3707. ######################################################################################################################
  3708. setup_ext_output_chain()
  3709. {
  3710. # This rule is for hostwise OUTPUT TCP blocking
  3711. ###############################################
  3712. unset IFS
  3713. for rule in $HOST_DENY_TCP_OUTPUT; do
  3714. interfaces=`get_ifs "$rule"`
  3715. srcips=`get_ips "$rule"`
  3716. hosts=`get_hosts_ihp "$rule"`
  3717. ports=`get_ports_ihp "$rule"`
  3718.  
  3719. echo " $(show_if_ip "$interfaces" "$srcips")Denying $hosts for TCP port(s): $ports"
  3720.  
  3721. IFS=','
  3722. for host in `ip_range "$hosts"`; do
  3723. for port in $ports; do
  3724. for interface in $interfaces; do
  3725. for srcip in $srcips; do
  3726. if [ "$INET_OUTPUT_DENY_LOG" != "0" ]; then
  3727. iptables -A EXT_OUTPUT_CHAIN -o $interface -s $srcip -d $host -p tcp --dport $port \
  3728. -m limit --limit 1/s --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:INET-OUTPUT denied: "
  3729. fi
  3730. iptables -A EXT_OUTPUT_CHAIN -o $interface -s $srcip -d $host -p tcp --dport $port -j DROP
  3731. done
  3732. done
  3733. done
  3734. done
  3735. done
  3736.  
  3737. # This rule is for hostwise OUTPUT UDP blocking
  3738. ###############################################
  3739. unset IFS
  3740. for rule in $HOST_DENY_UDP_OUTPUT; do
  3741. interfaces=`get_ifs "$rule"`
  3742. srcips=`get_ips "$rule"`
  3743. hosts=`get_hosts_ihp "$rule"`
  3744. ports=`get_ports_ihp "$rule"`
  3745.  
  3746. echo " $(show_if_ip "$interfaces" "$srcips")Denying $hosts for UDP port(s): $ports"
  3747.  
  3748. IFS=','
  3749. for host in `ip_range "$hosts"`; do
  3750. for port in $ports; do
  3751. for interface in $interfaces; do
  3752. for srcip in $srcips; do
  3753. if [ "$INET_OUTPUT_DENY_LOG" != "0" ]; then
  3754. iptables -A EXT_OUTPUT_CHAIN -o $interface -s $srcip -d $host -p udp --dport $port \
  3755. -m limit --limit 1/s --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:INET-OUTPUT denied: "
  3756. fi
  3757. iptables -A EXT_OUTPUT_CHAIN -o $interface -s $srcip -d $host -p udp --dport $port -j DROP
  3758. done
  3759. done
  3760. done
  3761. done
  3762. done
  3763.  
  3764. # This rule is for hostwise OUTPUT IP blocking
  3765. ##############################################
  3766. unset IFS
  3767. for rule in $HOST_DENY_IP_OUTPUT; do
  3768. interfaces=`get_ifs "$rule"`
  3769. srcips=`get_ips "$rule"`
  3770. hosts=`get_hosts_ihp "$rule"`
  3771. protos=`get_ports_ihp "$rule"`
  3772.  
  3773. echo " $(show_if_ip "$interfaces" "$srcips")Denying $hosts for IP protocol(s): $protos"
  3774.  
  3775. IFS=','
  3776. for host in `ip_range "$hosts"`; do
  3777. for proto in $protos; do
  3778. for interface in $interfaces; do
  3779. for srcip in $srcips; do
  3780. if [ "$INET_OUTPUT_DENY_LOG" != "0" ]; then
  3781. iptables -A EXT_OUTPUT_CHAIN -o $interface -s $srcip -d $host -p $proto \
  3782. -m limit --limit 1/s --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:INET-OUTPUT denied: "
  3783. fi
  3784. iptables -A EXT_OUTPUT_CHAIN -o $interface -s $srcip -d $host -p $proto -j DROP
  3785. done
  3786. done
  3787. done
  3788. done
  3789. done
  3790.  
  3791. # Adding the "full access hosts"
  3792. ################################
  3793. unset IFS
  3794. for rule in $FULL_ACCESS_HOSTS; do
  3795. interfaces=`get_ifs "$rule"`
  3796. srcips=`get_ips "$rule"`
  3797. hosts=`get_hosts_ih "$rule"`
  3798.  
  3799. echo " $(show_if_ip "$interfaces")Allowing $hosts full (outbound) access"
  3800.  
  3801. IFS=','
  3802. for host in `ip_range "$hosts"`; do
  3803. for interface in $interfaces; do
  3804. for srcip in $srcips; do
  3805. iptables -A EXT_OUTPUT_CHAIN -o $interface -s $srcip -d $host -j ACCEPT
  3806. done
  3807. done
  3808. done
  3809. done
  3810.  
  3811. # This rule is for local OUTPUT TCP blocking
  3812. ############################################
  3813. unset IFS
  3814. for rule in $DENY_TCP_OUTPUT; do
  3815. interfaces=`get_ifs "$rule"`
  3816. srcips=`get_ips "$rule"`
  3817. ports=`get_ports_ip "$rule"`
  3818.  
  3819. echo " $(show_if_ip "$interfaces" "$srcips")Denying TCP port(s): $ports"
  3820.  
  3821. IFS=','
  3822. for port in $ports; do
  3823. for interface in $interfaces; do
  3824. for srcip in $srcips; do
  3825. if [ "$INET_OUTPUT_DENY_LOG" != "0" ]; then
  3826. iptables -A EXT_OUTPUT_CHAIN -o $interface -s $srcip -p tcp --dport $port \
  3827. -m limit --limit 3/m --limit-burst 5 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:INET-OUTPUT denied: "
  3828. fi
  3829. iptables -A EXT_OUTPUT_CHAIN -o $interface -s $srcip -p tcp --dport $port -j DROP
  3830. done
  3831. done
  3832. done
  3833. done
  3834.  
  3835. # This rule is for local OUTPUT UDP blocking
  3836. ############################################
  3837. unset IFS
  3838. for rule in $DENY_UDP_OUTPUT; do
  3839. interfaces=`get_ifs "$rule"`
  3840. srcips=`get_ips "$rule"`
  3841. ports=`get_ports_ip "$rule"`
  3842.  
  3843. echo " $(show_if_ip "$interfaces" "$srcips")Denying UDP port(s): $ports"
  3844.  
  3845. IFS=','
  3846. for port in $ports; do
  3847. for interface in $interfaces; do
  3848. for srcip in $srcips; do
  3849. if [ "$INET_OUTPUT_DENY_LOG" != "0" ]; then
  3850. iptables -A EXT_OUTPUT_CHAIN -o $interface -s $srcip -p udp --dport $port \
  3851. -m limit --limit 3/m --limit-burst 5 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:INET-OUTPUT denied: "
  3852. fi
  3853. iptables -A EXT_OUTPUT_CHAIN -o $interface -s $srcip -p udp --dport $port -j DROP
  3854. done
  3855. done
  3856. done
  3857. done
  3858.  
  3859. # This rule is for local OUTPUT IP blocking
  3860. ############################################
  3861. unset IFS
  3862. for rule in $DENY_IP_OUTPUT; do
  3863. interfaces=`get_ifs "$rule"`
  3864. srcips=`get_ips "$rule"`
  3865. protos=`get_ports_ip "$rule"`
  3866.  
  3867. echo " $(show_if_ip "$interfaces" "$srcips")Denying IP protocol(s): $protos"
  3868.  
  3869. IFS=','
  3870. for proto in $DENY_IP_OUTPUT; do
  3871. for interface in $interfaces; do
  3872. for srcip in $srcips; do
  3873. if [ "$INET_OUTPUT_DENY_LOG" != "0" ]; then
  3874. iptables -A EXT_OUTPUT_CHAIN -o $interface -s $srcip -p $proto \
  3875. -m limit --limit 1/s --limit-burst 1 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:INET-OUTPUT denied: "
  3876. fi
  3877. iptables -A EXT_OUTPUT_CHAIN -o $interface -s $srcip -p $proto -j DROP
  3878. done
  3879. done
  3880. done
  3881. done
  3882. }
  3883.  
  3884.  
  3885. # Helper chain to catch broadcast traffic
  3886. setup_ext_broadcast_chain()
  3887. {
  3888. # Disable logging of certain TCP broadcasts on the external interface
  3889. #####################################################################
  3890. unset IFS
  3891. for rule in $BROADCAST_TCP_NOLOG; do
  3892. interfaces=`get_ifs "$rule"`
  3893. destips=`get_ips "$rule"`
  3894. ports=`get_ports_ip "$rule"`
  3895.  
  3896. echo " $(show_if_ip "$interfaces" "$destips")Logging of external subnet broadcasts disabled for TCP port(s): $ports"
  3897.  
  3898. IFS=' ,'
  3899. for port in $ports; do
  3900. for interface in $interfaces; do
  3901. iptables -A EXT_BROADCAST_CHAIN -i $interface -p tcp --dport $port -j DROP
  3902. done
  3903. done
  3904. done
  3905.  
  3906. # Disable logging of certain UDP broadcasts on the external interface
  3907. ##########################################################################################
  3908. unset IFS
  3909. for rule in $BROADCAST_UDP_NOLOG; do
  3910. interfaces=`get_ifs "$rule"`
  3911. destips=`get_ips "$rule"`
  3912. ports=`get_ports_ip "$rule"`
  3913.  
  3914. echo " $(show_if_ip "$interfaces" "$destips")Logging of external subnet broadcasts disabled for UDP port(s): $ports"
  3915.  
  3916. IFS=' ,'
  3917. for port in $ports; do
  3918. for interface in $interfaces; do
  3919. iptables -A EXT_BROADCAST_CHAIN -i $interface -p udp --dport $port -j DROP
  3920. done
  3921. done
  3922. done
  3923. }
  3924.  
  3925.  
  3926. # This creates the input logging rules
  3927. ##########################################################
  3928. setup_input_log()
  3929. {
  3930. # This rule is for local INPUT TCP watching
  3931. ############################################
  3932. unset IFS
  3933. for rule in $LOG_INPUT_TCP; do
  3934. interfaces=`get_ifs "$rule"`
  3935. destips=`get_ips "$rule"`
  3936. ports=`get_ports_ip "$rule"`
  3937.  
  3938. echo "$(show_if_ip "$interfaces" "$destips")Logging incoming TCP port(s): $ports"
  3939.  
  3940. IFS=','
  3941. for port in $ports; do
  3942. for destip in $destips; do
  3943. for interface in $interfaces; do
  3944. iptables -A INPUT -i $interface -d $destip -p tcp --dport $port -m state --state NEW -m limit \
  3945. --limit 3/m --limit-burst 15 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:TCP INPUT log: "
  3946. done
  3947. done
  3948. done
  3949. done
  3950.  
  3951.  
  3952. # This rule is for local INPUT UDP watching
  3953. ###########################################
  3954. unset IFS
  3955. for rule in $LOG_INPUT_UDP; do
  3956. interfaces=`get_ifs "$rule"`
  3957. destips=`get_ips "$rule"`
  3958. ports=`get_ports_ip "$rule"`
  3959.  
  3960. echo "$(show_if_ip "$interfaces" "$destips")Logging incoming UDP port(s): $ports"
  3961.  
  3962. IFS=','
  3963. for port in $ports; do
  3964. for destip in $destips; do
  3965. for interface in $interfaces; do
  3966. iptables -A INPUT -i $interface -d $destip -p udp --dport $port -m state --state NEW -m limit \
  3967. --limit 3/m --limit-burst 15 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:UDP INPUT log: "
  3968. done
  3969. done
  3970. done
  3971. done
  3972.  
  3973. # This rule is for local INPUT IP watching
  3974. ##########################################
  3975. unset IFS
  3976. for rule in $LOG_INPUT_IP; do
  3977. interfaces=`get_ifs "$rule"`
  3978. destips=`get_ips "$rule"`
  3979. protos=`get_ports_ip "$rule"`
  3980.  
  3981. echo "$(show_if_ip "$interfaces" "$destips")Logging incoming IP protocol(s): $protos"
  3982.  
  3983. IFS=','
  3984. for proto in $protos; do
  3985. for destip in $destips; do
  3986. for interface in $interfaces; do
  3987. iptables -A INPUT -i $interface -d $destip -p $proto -m state --state NEW -m limit \
  3988. --limit 3/m --limit-burst 15 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:IP INPUT log: "
  3989. done
  3990. done
  3991. done
  3992. done
  3993.  
  3994.  
  3995. # Hostwise logging of input connection attempts
  3996. ###############################################
  3997. unset IFS
  3998. for rule in $LOG_HOST_INPUT; do
  3999. interfaces=`get_ifs "$rule"`
  4000. destips=`get_ips "$rule"`
  4001. hosts=`get_hosts_ih "$rule"`
  4002.  
  4003. echo "$(show_if_ip "$interfaces" "$destips")Logging incoming connections of: $hosts"
  4004.  
  4005. IFS=','
  4006. for host in `ip_range "$hosts"`; do
  4007. for destip in $destips; do
  4008. for interface in $interfaces; do
  4009. iptables -A INPUT -i $interface -s $host -d $destip -m state --state NEW \
  4010. -m limit --limit 12/m --limit-burst 50 \
  4011. -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Hostwise INPUT log: "
  4012. done
  4013. done
  4014. done
  4015. done
  4016.  
  4017. # Hostwise logging of certain TCP port connection attempts
  4018. ##########################################################
  4019. unset IFS
  4020. for rule in $LOG_HOST_INPUT_TCP; do
  4021. interfaces=`get_ifs "$rule"`
  4022. destips=`get_ips "$rule"`
  4023. hosts=`get_hosts_ihp "$rule"`
  4024. ports=`get_ports_ihp "$rule"`
  4025.  
  4026. echo "$(show_if_ip "$interfaces" "$destips")Logging incoming connections of $hosts to TCP port(s): $ports"
  4027.  
  4028. IFS=','
  4029. for host in `ip_range "$hosts"`; do
  4030. for port in $ports; do
  4031. for destip in $destips; do
  4032. for interface in $interfaces; do
  4033. iptables -A INPUT -i $interface -s $host -d $destip -p tcp --dport $port -m state --state NEW \
  4034. -m limit --limit 12/m --limit-burst 5 \
  4035. -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Hostwise TCP log (IN): "
  4036. done
  4037. done
  4038. done
  4039. done
  4040. done
  4041.  
  4042.  
  4043. # Hostwise logging of certain UDP port connection attempts
  4044. ##########################################################
  4045. unset IFS
  4046. for rule in $LOG_HOST_INPUT_UDP; do
  4047. interfaces=`get_ifs "$rule"`
  4048. destips=`get_ips "$rule"`
  4049. hosts=`get_hosts_ihp "$rule"`
  4050. ports=`get_ports_ihp "$rule"`
  4051.  
  4052. echo "$(show_if_ip "$interfaces" "$destips")Logging incoming connections of $hosts to UDP port(s): $ports"
  4053.  
  4054. IFS=','
  4055. for host in `ip_range "$hosts"`; do
  4056. for port in $ports; do
  4057. for destip in $destips; do
  4058. for interface in $interfaces; do
  4059. iptables -A INPUT -i $interface -s $host -d $destip -p udp --dport $port -m state --state NEW \
  4060. -m limit --limit 12/m --limit-burst 5 \
  4061. -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Hostwise UDP INPUT log: "
  4062. done
  4063. done
  4064. done
  4065. done
  4066. done
  4067.  
  4068. # Hostwise logging of certain IP protocols connection attempts
  4069. ##############################################################
  4070. unset IFS
  4071. for rule in $LOG_HOST_INPUT_IP; do
  4072. interfaces=`get_ifs "$rule"`
  4073. destips=`get_ips "$rule"`
  4074. hosts=`get_hosts_ihp "$rule"`
  4075. protos=`get_ports_ihp "$rule"`
  4076.  
  4077. echo "$(show_if_ip "$interfaces" "$destips")Logging incoming connections of $hosts to IP protocol(s): $protos"
  4078.  
  4079. IFS=','
  4080. for host in `ip_range "$hosts"`; do
  4081. for proto in $protos; do
  4082. for destip in $destips; do
  4083. for interface in $interfaces; do
  4084. iptables -A INPUT -i $interface -s $host -d $destip -p $proto -m state --state NEW \
  4085. -m limit --limit 12/m --limit-burst 5 \
  4086. -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Hostwise IP INPUT log: "
  4087. done
  4088. done
  4089. done
  4090. done
  4091. done
  4092. }
  4093.  
  4094.  
  4095. # This creates the output logging rules
  4096. ##########################################################
  4097. setup_output_log()
  4098. {
  4099. # This rule is for local OUTPUT TCP watching
  4100. ############################################
  4101. unset IFS
  4102. for rule in $LOG_OUTPUT_TCP; do
  4103. interfaces=`get_ifs "$rule"`
  4104. srcips=`get_ips "$rule"`
  4105. ports=`get_ports_ip "$rule"`
  4106.  
  4107. echo "$(show_if_ip "$interfaces" "$srcips")Logging outgoing TCP port(s): $ports"
  4108.  
  4109. IFS=','
  4110. for port in $ports; do
  4111. for interface in $interfaces; do
  4112. for srcip in $srcips; do
  4113. iptables -A OUTPUT -o $interface -s $srcip -p tcp --dport $port -m state --state NEW -m limit \
  4114. --limit 3/m --limit-burst 15 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:TCP OUTPUT log: "
  4115. done
  4116. done
  4117. done
  4118. done
  4119.  
  4120. # This rule is for local OUTPUT UDP watching
  4121. ############################################
  4122. unset IFS
  4123. for rule in $LOG_OUTPUT_UDP; do
  4124. interfaces=`get_ifs "$rule"`
  4125. srcips=`get_ips "$rule"`
  4126. ports=`get_ports_ip "$rule"`
  4127.  
  4128. echo "$(show_if_ip "$interfaces")Logging outgoing UDP port(s): $ports"
  4129.  
  4130. IFS=','
  4131. for port in $ports; do
  4132. for interface in $interfaces; do
  4133. for srcip in $srcips; do
  4134. iptables -A OUTPUT -o $interface -s $srcip -p udp --dport $port -m state --state NEW -m limit \
  4135. --limit 3/m --limit-burst 15 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:UDP OUTPUT log: "
  4136. done
  4137. done
  4138. done
  4139. done
  4140.  
  4141. # This rule is for local OUTPUT IP watching
  4142. ###########################################
  4143. unset IFS
  4144. for rule in $LOG_OUTPUT_IP; do
  4145. interfaces=`get_ifs "$rule"`
  4146. srcips=`get_ips "$rule"`
  4147. protos=`get_ports_ip "$rule"`
  4148.  
  4149. echo "$(show_if_ip "$interfaces" "$srcips")Logging outgoing IP protocol(s): $protos"
  4150.  
  4151. IFS=','
  4152. for proto in $protos; do
  4153. for interface in $interfaces; do
  4154. for srcip in $srcips; do
  4155. iptables -A OUTPUT -o $interface -s $srcip -p $proto -m state --state NEW -m limit \
  4156. --limit 3/m --limit-burst 15 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:IP OUTPUT log: "
  4157. done
  4158. done
  4159. done
  4160. done
  4161.  
  4162. # Hostwise logging of output connection attempts
  4163. ################################################
  4164. unset IFS
  4165. for rule in $LOG_HOST_OUTPUT; do
  4166. interfaces=`get_ifs "$rule"`
  4167. srcips=`get_ips "$rule"`
  4168. hosts=`get_hosts_ih "$rule"`
  4169.  
  4170. echo "$(show_if_ip "$interfaces" "$srcips")Logging outgoing connections to: $hosts"
  4171.  
  4172. IFS=','
  4173. for host in `ip_range "$hosts"`; do
  4174. for interface in $interfaces; do
  4175. for srcip in $srcips; do
  4176. iptables -A OUTPUT -o $interface -s $srcip -d $host -m state --state NEW \
  4177. -m limit --limit 12/m --limit-burst 50 \
  4178. -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Hostwise OUTPUT log: "
  4179. done
  4180. done
  4181. done
  4182. done
  4183.  
  4184.  
  4185. # Hostwise logging of certain TCP port connection attempts
  4186. ##########################################################
  4187. unset IFS
  4188. for rule in $LOG_HOST_OUTPUT_TCP; do
  4189. interfaces=`get_ifs "$rule"`
  4190. srcips=`get_ips "$rule"`
  4191. hosts=`get_hosts_ihp "$rule"`
  4192. ports=`get_ports_ihp "$rule"`
  4193.  
  4194. echo "$(show_if_ip "$interfaces" "$srcips")Logging outgoing connections of $hosts to TCP port(s): $ports"
  4195.  
  4196. IFS=','
  4197. for host in `ip_range "$hosts"`; do
  4198. for port in $ports; do
  4199. for interface in $interfaces; do
  4200. for srcip in $srcips; do
  4201. iptables -A OUTPUT -o $interface -s $srcip -d $host -p tcp --dport $port -m state --state NEW \
  4202. -m limit --limit 12/m --limit-burst 5 \
  4203. -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Host TCP log (OUT): "
  4204. done
  4205. done
  4206. done
  4207. done
  4208. done
  4209.  
  4210.  
  4211. # Hostwise logging of certain UDP port connection attempts
  4212. ##########################################################
  4213. unset IFS
  4214. for rule in $LOG_HOST_OUTPUT_UDP; do
  4215. interfaces=`get_ifs "$rule"`
  4216. srcips=`get_ips "$rule"`
  4217. hosts=`get_hosts_ihp "$rule"`
  4218. ports=`get_ports_ihp "$rule"`
  4219.  
  4220. echo "$(show_if_ip "$interfaces" "$srcips")Logging outgoing connections of $hosts to UDP port(s): $ports"
  4221.  
  4222. IFS=','
  4223. for host in `ip_range "$hosts"`; do
  4224. for port in $ports; do
  4225. for interface in $interfaces; do
  4226. for srcip in $srcips; do
  4227. iptables -A OUTPUT -o $interface -s $srcip -d $host -p udp --dport $port -m state --state NEW \
  4228. -m limit --limit 12/m --limit-burst 5 \
  4229. -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Host UDP log (OUT): "
  4230. done
  4231. done
  4232. done
  4233. done
  4234. done
  4235.  
  4236. # Hostwise logging of certain IP protocols connection attempts
  4237. ##############################################################
  4238. unset IFS
  4239. for rule in $LOG_HOST_OUTPUT_IP; do
  4240. interfaces=`get_ifs "$rule"`
  4241. srcips=`get_ips "$rule"`
  4242. hosts=`get_hosts_ihp "$rule"`
  4243. protos=`get_ports_ihp "$rule"`
  4244.  
  4245. echo "$(show_if_ip "$interfaces" "$srcips")Logging outgoing connections of $hosts to IP protocol(s): $protos"
  4246.  
  4247. IFS=','
  4248. for host in `ip_range "$hosts"`; do
  4249. for proto in $protos; do
  4250. for interface in $interfaces; do
  4251. for srcip in $srcips; do
  4252. iptables -A OUTPUT -o $interface -s $srcip -d $host -p $proto -m state --state NEW \
  4253. -m limit --limit 12/m --limit-burst 5 \
  4254. -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Host IP log (OUT): "
  4255. done
  4256. done
  4257. done
  4258. done
  4259. done
  4260. }
  4261.  
  4262.  
  4263. # This creates the separate host block
  4264. ######################################
  4265. setup_hostblock_chain()
  4266. {
  4267. if iptables -F HOST_BLOCK_SRC 2>&1 |grep -q "No chain" || \
  4268. iptables -F HOST_BLOCK_DST 2>&1 |grep -q "No chain"; then
  4269. printf "\033[40m\033[1;31mERROR: The firewall isn't running!\033[0m\n" >&2
  4270. printf "\033[40m\033[1;31m You should first run this script with the \"start\" command.\033[0m\n" >&2
  4271. return 5
  4272. fi
  4273.  
  4274. # Return if no Blocked Hosts are defined
  4275. if [ -z "$BLOCK_HOSTS" -a -z "$BLOCK_HOSTS_FILE" ]; then
  4276. return
  4277. fi
  4278.  
  4279. if [ "$BLOCK_HOSTS_BIDIRECTIONAL" != "0" ]; then
  4280. echo "Blocking (blackhole) direction: Inbound and Outbound"
  4281. else
  4282. echo "Blocking (blackhole) direction: Inbound Only"
  4283. fi
  4284.  
  4285. # Set default to DROP all while rules are added
  4286. iptables -A HOST_BLOCK_SRC -j DROP
  4287. iptables -A HOST_BLOCK_DST -j DROP
  4288.  
  4289. iptables_batch start
  4290. iptables_batch init HOST_BLOCK_SRC
  4291. iptables_batch init HOST_BLOCK_DST
  4292.  
  4293. if [ -n "$BLOCK_HOSTS" ]; then
  4294. printf "Blocking (blackhole) host(s): "
  4295.  
  4296. IFS=' ,'
  4297. for hosts in $BLOCK_HOSTS; do
  4298. printf "$hosts "
  4299.  
  4300. for host in `ip_range "$hosts"`; do
  4301. get_numeric_ip_version "$host"
  4302. case $? in
  4303. 4)
  4304. ip4tables_batch -A HOST_BLOCK_SRC -s $host -j HOST_BLOCK_DROP
  4305. if [ "$BLOCK_HOSTS_BIDIRECTIONAL" != "0" ]; then
  4306. ip4tables_batch -A HOST_BLOCK_DST -d $host -j HOST_BLOCK_DROP
  4307. fi
  4308. ;;
  4309. 6)
  4310. if [ "$IPV6_SUPPORT" = "1" ]; then
  4311. ip6tables_batch -A HOST_BLOCK_SRC -s $host -j HOST_BLOCK_DROP
  4312. if [ "$BLOCK_HOSTS_BIDIRECTIONAL" != "0" ]; then
  4313. ip6tables_batch -A HOST_BLOCK_DST -d $host -j HOST_BLOCK_DROP
  4314. fi
  4315. fi
  4316. ;;
  4317. esac
  4318. done
  4319. done
  4320. echo ""
  4321. fi
  4322.  
  4323.  
  4324. # Setup the blocked hosts from our file
  4325. if [ -n "$BLOCK_HOSTS_FILE" ]; then
  4326. if [ -e "$BLOCK_HOSTS_FILE" ]; then
  4327. local cur_cnt=0 total_cnt
  4328. total_cnt=$(( $(cat "$BLOCK_HOSTS_FILE" |sed -e 's|[#;].*||' -e 's| *$||' -e '/^$/d' |wc -l) ))
  4329.  
  4330. echo "(Re)loading list of BLOCKED hosts from $BLOCK_HOSTS_FILE..."
  4331. progress_bar $cur_cnt $total_cnt
  4332.  
  4333. # Support both a '#' and a ';' as a comment delimiter in BLOCK_HOSTS_FILE file
  4334. unset IFS
  4335. cat "$BLOCK_HOSTS_FILE" |sed -e 's|[#;].*||' -e 's| *$||' -e '/^$/d' |while read hosts; do
  4336. cur_cnt=$(($cur_cnt + 100))
  4337. progress_bar $cur_cnt $total_cnt
  4338.  
  4339. if [ -n "$hosts" ]; then
  4340. IFS=','
  4341. for host in `ip_range "$hosts"`; do
  4342. get_numeric_ip_version "$host"
  4343. case $? in
  4344. 4)
  4345. ip4tables_batch -A HOST_BLOCK_SRC -s $host -j HOST_BLOCK_DROP
  4346. if [ "$BLOCK_HOSTS_BIDIRECTIONAL" != "0" ]; then
  4347. ip4tables_batch -A HOST_BLOCK_DST -d $host -j HOST_BLOCK_DROP
  4348. fi
  4349. ;;
  4350. 6)
  4351. if [ "$IPV6_SUPPORT" = "1" ]; then
  4352. ip6tables_batch -A HOST_BLOCK_SRC -s $host -j HOST_BLOCK_DROP
  4353. if [ "$BLOCK_HOSTS_BIDIRECTIONAL" != "0" ]; then
  4354. ip6tables_batch -A HOST_BLOCK_DST -d $host -j HOST_BLOCK_DROP
  4355. fi
  4356. fi
  4357. ;;
  4358. esac
  4359. done
  4360. unset IFS
  4361. fi
  4362. done
  4363. echo ".........$total_cnt line(s) read."
  4364. else
  4365. printf "\033[40m\033[1;31mNOTE: Cannot read the blocked hosts file \"$BLOCK_HOSTS_FILE\".\033[0m\n"
  4366. fi
  4367. fi
  4368.  
  4369. iptables_batch apply HOST_BLOCK_SRC
  4370. iptables_batch apply HOST_BLOCK_DST
  4371. iptables_batch stop
  4372.  
  4373. # All Blocked Host rules are applied
  4374. # Remove default rule at the beginning of the HOST_BLOCK_SRC and HOST_BLOCK_DST chains
  4375. iptables -D HOST_BLOCK_SRC 1
  4376. iptables -D HOST_BLOCK_DST 1
  4377. }
  4378.  
  4379.  
  4380. setup_mangle_tables()
  4381. {
  4382. # The following line is intended to hide the firewall during a traceroute.
  4383. ##########################################################################
  4384. if [ "$TTL_INC" = "1" ]; then
  4385. echo "Enabling TTL-increase for the PREROUTING chain"
  4386. IFS=' ,'
  4387. for interface in $EXT_IF; do
  4388. ip4tables -t mangle -A PREROUTING -i $interface -j TTL --ttl-inc 1
  4389. done
  4390. fi
  4391.  
  4392. # Set TTL on outgoing & forwarded packets:
  4393. ##########################################
  4394. if [ -n "$PACKET_TTL" ]; then
  4395. if [ $PACKET_TTL -gt 9 -a $PACKET_TTL -lt 256 ]; then
  4396. echo "Setting TTL=$PACKET_TTL for the FORWARD & OUTPUT chains"
  4397. IFS=' ,'
  4398. for interface in $EXT_IF; do
  4399. ip4tables -t mangle -A FORWARD -o $interface -j TTL --ttl-set $PACKET_TTL
  4400. ip4tables -t mangle -A OUTPUT -o $interface -j TTL --ttl-set $PACKET_TTL
  4401. done
  4402. else
  4403. printf "\033[40m\033[1;31m WARNING: Ingoring invalid value for PACKET_TTL ($PACKET_TTL), it should be between 10 and 255!\033[0m\n" >&2
  4404. fi
  4405. fi
  4406.  
  4407. # Mangles the TOS on standard ports so they get priority in routers
  4408. ###################################################################
  4409. # TOS table
  4410. # Options:
  4411. # Normal-Service = 0 (0x00)
  4412. # Minimize-Cost = 2 (0x02)
  4413. # Maximize-Reliability = 4 (0x04)
  4414. # Maximize-Throughput = 8 (0x08)
  4415. # Minimize-Delay = 16 (0x10)
  4416. if [ "$MANGLE_TOS" = "1" ]; then
  4417. echo "Enabling mangling TOS"
  4418. # ToS: Client Applications; data => tos_client
  4419. # Most of these are the RFC 1060/1349 suggested TOS values, yours might vary.
  4420. # To view mangle table, type: iptables -L -t mangle
  4421. IFS=' ,'
  4422. for interface in $EXT_IF; do
  4423. # Mangle values of packets created locally.
  4424. iptables -t mangle -A OUTPUT -o $interface -p tcp --dport 20 -j TOS --set-tos Maximize-Throughput
  4425. iptables -t mangle -A OUTPUT -o $interface -p tcp --dport 21 -j TOS --set-tos Minimize-Delay
  4426. iptables -t mangle -A OUTPUT -o $interface -p tcp --dport 22 -j TOS --set-tos Minimize-Delay
  4427. iptables -t mangle -A OUTPUT -o $interface -p tcp --dport 23 -j TOS --set-tos Minimize-Delay
  4428. iptables -t mangle -A OUTPUT -o $interface -p tcp --dport 25 -j TOS --set-tos Minimize-Delay
  4429. iptables -t mangle -A OUTPUT -o $interface -p udp --dport 53 -j TOS --set-tos Maximize-Throughput
  4430. iptables -t mangle -A OUTPUT -o $interface -p tcp --dport 67 -j TOS --set-tos Minimize-Delay
  4431. iptables -t mangle -A OUTPUT -o $interface -p tcp --dport 80 -j TOS --set-tos Maximize-Throughput
  4432. iptables -t mangle -A OUTPUT -o $interface -p tcp --dport 110 -j TOS --set-tos Maximize-Throughput
  4433. iptables -t mangle -A OUTPUT -o $interface -p tcp --dport 113 -j TOS --set-tos Minimize-Delay
  4434. iptables -t mangle -A OUTPUT -o $interface -p tcp --dport 123 -j TOS --set-tos Minimize-Delay
  4435. iptables -t mangle -A OUTPUT -o $interface -p tcp --dport 143 -j TOS --set-tos Maximize-Throughput
  4436. iptables -t mangle -A OUTPUT -o $interface -p tcp --dport 443 -j TOS --set-tos Maximize-Throughput
  4437. iptables -t mangle -A OUTPUT -o $interface -p tcp --dport 993 -j TOS --set-tos Maximize-Throughput
  4438. iptables -t mangle -A OUTPUT -o $interface -p tcp --dport 995 -j TOS --set-tos Maximize-Throughput
  4439. iptables -t mangle -A OUTPUT -o $interface -p tcp --dport 1080 -j TOS --set-tos Minimize-Delay
  4440. iptables -t mangle -A OUTPUT -o $interface -p tcp --dport 6000:6063 -j TOS --set-tos Maximize-Throughput
  4441. done
  4442.  
  4443. # Rules to mangle TOS values of packets routed through the firewall
  4444. iptables -t mangle -A PREROUTING -p tcp --dport 20 -j TOS --set-tos Maximize-Throughput
  4445. iptables -t mangle -A PREROUTING -p tcp --dport 21 -j TOS --set-tos Minimize-Delay
  4446. iptables -t mangle -A PREROUTING -p tcp --dport 22 -j TOS --set-tos Minimize-Delay
  4447. iptables -t mangle -A PREROUTING -p tcp --dport 23 -j TOS --set-tos Minimize-Delay
  4448. iptables -t mangle -A PREROUTING -p tcp --dport 25 -j TOS --set-tos Minimize-Delay
  4449. iptables -t mangle -A PREROUTING -p udp --dport 53 -j TOS --set-tos Minimize-Delay
  4450. iptables -t mangle -A PREROUTING -p tcp --dport 67 -j TOS --set-tos Minimize-Delay
  4451. iptables -t mangle -A PREROUTING -p tcp --dport 80 -j TOS --set-tos Maximize-Throughput
  4452. iptables -t mangle -A PREROUTING -p tcp --dport 110 -j TOS --set-tos Maximize-Throughput
  4453. iptables -t mangle -A PREROUTING -p tcp --dport 113 -j TOS --set-tos Minimize-Delay
  4454. iptables -t mangle -A PREROUTING -p tcp --dport 123 -j TOS --set-tos Minimize-Delay
  4455. iptables -t mangle -A PREROUTING -p tcp --dport 143 -j TOS --set-tos Maximize-Throughput
  4456. iptables -t mangle -A PREROUTING -p tcp --dport 443 -j TOS --set-tos Maximize-Throughput
  4457. iptables -t mangle -A PREROUTING -p tcp --dport 993 -j TOS --set-tos Maximize-Throughput
  4458. iptables -t mangle -A PREROUTING -p tcp --dport 995 -j TOS --set-tos Maximize-Throughput
  4459. iptables -t mangle -A PREROUTING -p tcp --dport 1080 -j TOS --set-tos Minimize-Delay
  4460. iptables -t mangle -A PREROUTING -p tcp --dport 6000:6063 -j TOS --set-tos Maximize-Throughput
  4461. fi
  4462. }
  4463.  
  4464.  
  4465. setup_tcp_mss()
  4466. {
  4467. # Besides MTU, there is yet another way to set the maximum packet size, the so called Maximum Segment Size.
  4468. # This is a field in the TCP Options part of a SYN packet.
  4469. # The good thing about this is that by setting the MSS value, you are telling the remote side unequivocally
  4470. # 'do not ever try to send me packets bigger than this value'. No ICMP traffic is needed to get this to work.
  4471. # In order for this to work you need at least iptables-1.2.1a and Linux 2.4.3 or higher.
  4472. ##################################################################################################################
  4473. if [ "$SET_MSS" = "1" ]; then
  4474. echo "Enabling setting the maximum packet size via MSS"
  4475. IFS=' ,'
  4476. for interface in $EXT_IF; do
  4477. iptables -A FORWARD -o $interface -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
  4478. iptables -A OUTPUT -o $interface -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
  4479.  
  4480. if [ "$NAT" = "1" ]; then
  4481. ip4tables -t nat -A POSTROUTING -o $interface -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu
  4482. fi
  4483. done
  4484. fi
  4485. }
  4486.  
  4487.  
  4488. plugins_start()
  4489. {
  4490. # Truncate/create file
  4491. cp -f /dev/null "$PLUGIN_LOAD_FILE"
  4492.  
  4493. printf "Checking for (user) plugins in $PLUGIN_BIN_PATH..."
  4494.  
  4495. # Check for plugins in our plugins binary path:
  4496. if [ -n "$(find "$PLUGIN_BIN_PATH/" -maxdepth 1 -name "*.plugin" 2>/dev/null)" ]; then
  4497. echo ""
  4498. plugin_count=0
  4499.  
  4500. unset IFS
  4501. for plugin in "$PLUGIN_BIN_PATH"/*.plugin; do
  4502. PLUGIN_CMD=start
  4503. if [ -e "$PLUGIN_LOAD_FILE_RESTART" ]; then
  4504. IFS=$EOL
  4505. for plugin_restart in `cat "$PLUGIN_LOAD_FILE_RESTART"`; do
  4506. if [ "$plugin_restart" = "$plugin" ]; then
  4507. if grep -q "^plugin_restart\(\)" "$plugin"; then
  4508. PLUGIN_CMD=restart
  4509. fi
  4510. break
  4511. fi
  4512. done
  4513. fi
  4514.  
  4515. # Preset ENABLED=0 to make sure the plugin only
  4516. # gets loaded if the config has an explicit ENABLED=1:
  4517. ENABLED=0
  4518.  
  4519. # Set indent
  4520. INDENT=' '
  4521.  
  4522. # Explicit unset IFS, just in case
  4523. unset IFS
  4524.  
  4525. # Source the plugin:
  4526. . "$plugin"
  4527.  
  4528. # By checking the ENABLED variable, we know whether the plugin
  4529. # was actually loaded. If so increase the plugin count:
  4530. if [ "$ENABLED" = "1" ]; then
  4531. echo "$plugin" >> "$PLUGIN_LOAD_FILE"
  4532. plugin_count=$(($plugin_count + 1))
  4533. fi
  4534. done
  4535. rm -f "$PLUGIN_LOAD_FILE_RESTART"
  4536. echo " Loaded $plugin_count plugin(s)..."
  4537. else
  4538. echo "None found"
  4539. fi
  4540. }
  4541.  
  4542.  
  4543. plugins_stop()
  4544. {
  4545. local restart="$1"
  4546.  
  4547. # Remove any stale plugin restart file
  4548. rm -f "$PLUGIN_LOAD_FILE_RESTART"
  4549.  
  4550. if [ -e "$PLUGIN_LOAD_FILE" ]; then
  4551. echo "Stopping (user) plugins..."
  4552.  
  4553. IFS=$EOL
  4554. for plugin in `cat "$PLUGIN_LOAD_FILE"`; do
  4555. plugin_name="$(basename "$plugin" |sed 's/^[0-9]*//')"
  4556.  
  4557. if [ -e "$plugin" ]; then
  4558. plugin_file="$plugin"
  4559. else
  4560. # If we can't find it, ignore the priority number in front of the plugin-filename
  4561. plugin_file="$(find "$PLUGIN_BIN_PATH/" -maxdepth 1 -name "*.plugin" |grep "[0-9]*$plugin_name$" |head -n1)"
  4562. fi
  4563.  
  4564. if [ -e "$plugin_file" ]; then
  4565. # Only issue the stop command for plugins that support it:
  4566. if grep -q "^plugin_stop\(\)" "$plugin_file"; then
  4567. # Preset ENABLED=0 to make sure the plugin only
  4568. # gets loaded if the config has an explicit ENABLED=1:
  4569. ENABLED=0
  4570.  
  4571. # Set indent
  4572. INDENT=' '
  4573.  
  4574. PLUGIN_CMD=stop
  4575. if [ "$restart" = "restart" ] && grep -q "^plugin_restart\(\)" "$plugin_file"; then
  4576. echo "$plugin" >> "$PLUGIN_LOAD_FILE_RESTART"
  4577. PLUGIN_CMD=stop-restart
  4578. fi
  4579.  
  4580. # Explicit unset IFS, just in case
  4581. unset IFS
  4582.  
  4583. # Source the plugin:
  4584. . "$plugin_file"
  4585. fi
  4586. else
  4587. printf "\033[40m\033[1;31mERROR: Could not stop plugin \"$plugin_name\" as it does no exist!\033[0m\n" >&2
  4588. fi
  4589. done
  4590. rm -f "$PLUGIN_LOAD_FILE"
  4591. fi
  4592. }
  4593.  
  4594.  
  4595. plugins_status()
  4596. {
  4597. # Load/insert user plugins
  4598. if [ -e "$PLUGIN_LOAD_FILE" ]; then
  4599. printf "\nShowing status of (user) plugins:\n"
  4600. echo "---------------------------------"
  4601.  
  4602. IFS=$EOL
  4603. for plugin in `cat "$PLUGIN_LOAD_FILE"`; do
  4604. # Only issue the status command for plugins that support the PLUGIN_CMD-variable:
  4605. if grep -q "^plugin_status\(\)" "$plugin"; then
  4606. # Preset ENABLED=0 to make sure the plugin only
  4607. # gets loaded if the config has an explicit ENABLED=1:
  4608. ENABLED=0
  4609.  
  4610. # Set indent
  4611. INDENT=' '
  4612.  
  4613. PLUGIN_CMD=status
  4614.  
  4615. # Source the plugin:
  4616. printf "=>"
  4617. . "$plugin"
  4618. echo ""
  4619. fi
  4620. done
  4621. fi
  4622. }
  4623.  
  4624.  
  4625. # Here the actual iptables rules are loaded
  4626. ###########################################
  4627. setup_firewall_rules()
  4628. {
  4629. echo "Using loglevel \"$LOGLEVEL\" for syslogd"
  4630. echo ""
  4631.  
  4632. echo "Setting up firewall rules:"
  4633. echo "-------------------------------------------------------------------------------"
  4634.  
  4635. # Setup all TCP MSS stuff
  4636. #########################
  4637. setup_tcp_mss;
  4638.  
  4639. # Setup all mangle stuff
  4640. ########################
  4641. setup_mangle_tables;
  4642.  
  4643. # Setup basic input/forward/output/... chains
  4644. #############################################
  4645. iptables -A INPUT -j INPUT_CHAIN
  4646. iptables -A FORWARD -j FORWARD_CHAIN
  4647. iptables -A OUTPUT -j OUTPUT_CHAIN
  4648. ip4tables -t nat -A PREROUTING -j NAT_PREROUTING_CHAIN
  4649. ip4tables -t nat -A POSTROUTING -j NAT_POSTROUTING_CHAIN
  4650.  
  4651. # Block all hosts in the custom blocked hosts file
  4652. ##################################################
  4653. iptables -A INPUT -j HOST_BLOCK_SRC
  4654. iptables -A FORWARD -j HOST_BLOCK_SRC
  4655. iptables -A FORWARD -j HOST_BLOCK_DST
  4656. iptables -A OUTPUT -j HOST_BLOCK_DST
  4657.  
  4658. # Allow DNS out for plugins and iptables while setting up rules
  4659. ###############################################################
  4660. iptables -A OUTPUT_CHAIN -p udp --dport 53 -j ACCEPT
  4661. iptables -A OUTPUT_CHAIN -p tcp --dport 53 -j ACCEPT
  4662.  
  4663. # Setup global helper chains
  4664. ############################
  4665. setup_valid_chk_chain;
  4666. setup_reserved_net_chk_chain;
  4667. setup_spoof_chk_chain;
  4668.  
  4669. # Check if source address is spoofed
  4670. ####################################
  4671. iptables -A INPUT -j SPOOF_CHK
  4672.  
  4673. # Setup rules for input/output logging
  4674. ######################################
  4675. setup_input_log;
  4676. setup_output_log;
  4677.  
  4678. # Explicit unset IFS, just in case
  4679. unset IFS
  4680.  
  4681. # Insert the custom rules
  4682. #########################
  4683. if [ -e "$CUSTOM_RULES" ]; then
  4684. echo "Reading custom rules from $CUSTOM_RULES"
  4685. . $CUSTOM_RULES
  4686. fi
  4687.  
  4688. # Start (user) plugins
  4689. ######################
  4690. plugins_start;
  4691.  
  4692. # Drop outgoing fragmented packets (this should in principle never happen because of netfilter's
  4693. # packet defragmentation
  4694. ################################################################################################
  4695. ip4tables -A OUTPUT -f \
  4696. -m limit --limit 3/m -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Fragment packet: "
  4697. ip4tables -A OUTPUT -f -j DROP
  4698.  
  4699. # Setup helper chains for EXTERNAL input traffic:
  4700. echo "Setting up external(INET) INPUT policy"
  4701. setup_ext_broadcast_chain;
  4702. setup_ext_icmp_flood_chain;
  4703. setup_ext_input_chain;
  4704.  
  4705. # Setup helper chains for EXTERNAL output traffic:
  4706. echo "Setting up external(INET) OUTPUT policy"
  4707. setup_ext_output_chain;
  4708.  
  4709. # This is used for your external (untrusted) interfaces
  4710. #######################################################
  4711. COUNT=0
  4712. IFS=' ,'
  4713. for interface in $EXT_IF; do
  4714. COUNT=$(($COUNT + 1))
  4715. EXTIF_NET=$(echo "$EXTERNAL_NET" |awk "{ print \$$COUNT }")
  4716.  
  4717. printf "Applying external(INET) policy to interface: $interface"
  4718. if [ -z "$EXTIF_NET" ]; then
  4719. echo " (without an external subnet specified)"
  4720. else
  4721. echo " (EXTERNAL_NET=$EXTIF_NET)"
  4722. fi
  4723.  
  4724. # Apply external (internet) interface policy for the output chain:
  4725. ##################################################################
  4726. iptables -A OUTPUT -o $interface -j EXT_OUTPUT_CHAIN
  4727.  
  4728. # We must allow special icmpv6 packets since CONNTRACK doesn't handle all icmpv6 types:
  4729. #######################################################################################
  4730. if [ "$IPV6_SUPPORT" = "1" -a "$OPEN_ICMPV6" != "0" ]; then
  4731. for icmpv6_type in $ICMPV6_SPECIAL_TYPES; do
  4732. ip6tables -A INPUT -i $interface -p icmpv6 --icmpv6-type $icmpv6_type -m hl --hl-eq 255 -j ACCEPT
  4733. done
  4734. fi
  4735.  
  4736. # Apply external (internet) interface policy for the input chain:
  4737. #################################################################
  4738. # Check packets for invalid flags:
  4739. iptables -A INPUT -i $interface -j VALID_CHK
  4740.  
  4741. # Perform check:
  4742. ################
  4743. # Non-ICMP traffic:
  4744. iptables -A INPUT -i $interface ! -p icmp -m state --state NEW -j EXT_INPUT_CHAIN
  4745.  
  4746. # ICMP traffic (rate limited):
  4747. iptables -A INPUT -i $interface -p icmp -m state --state NEW \
  4748. -m limit --limit 60/second --limit-burst 100 -j EXT_INPUT_CHAIN
  4749.  
  4750. # ICMP traffic (flood)
  4751. iptables -A INPUT -i $interface -p icmp -m state --state NEW -j EXT_ICMP_FLOOD_CHAIN
  4752. done
  4753.  
  4754.  
  4755. # Setup input rules for your internal net
  4756. #########################################
  4757. if [ -n "$INT_IF" ]; then
  4758. # Setup helper chain for the LAN:
  4759. echo "Setting up internal(LAN) INPUT policy"
  4760. setup_int_input_chain;
  4761.  
  4762. IFS=' ,'
  4763. for interface in $INT_IF; do
  4764. echo "Applying internal(LAN) policy to interface: $interface"
  4765. iptables -A INPUT -i $interface -j INT_INPUT_CHAIN
  4766. iptables -A OUTPUT -o $interface -j INT_OUTPUT_CHAIN
  4767. done
  4768. fi
  4769.  
  4770. # Setup input rules for your DMZ net
  4771. ####################################
  4772. if [ -n "$DMZ_IF" ]; then
  4773. # Setup helper chain for the DMZ:
  4774. echo "Setting up DMZ INPUT policy"
  4775. setup_dmz_input_chain;
  4776.  
  4777. IFS=' ,'
  4778. for interface in $DMZ_IF; do
  4779. echo "Applying DMZ policy to interface: $interface"
  4780. iptables -A INPUT -i $interface -j DMZ_INPUT_CHAIN
  4781. iptables -A OUTPUT -o $interface -j DMZ_OUTPUT_CHAIN
  4782. done
  4783. fi
  4784.  
  4785. # Accept all packets on "other" interfaces
  4786. ##########################################
  4787. if [ -n "$TRUSTED_IF" ]; then
  4788. echo "Accepting ALL INPUT packets from trusted interface(s): $TRUSTED_IF"
  4789. IFS=' ,'
  4790. for interface in $TRUSTED_IF; do
  4791. iptables -A INPUT -i $interface -j ACCEPT
  4792. done
  4793. fi
  4794.  
  4795. # Jump to the POST INPUT chain
  4796. ##############################
  4797. iptables -A INPUT -j POST_INPUT_CHAIN
  4798.  
  4799. # Everything else is logged & dropped in INPUT (just in case)...
  4800. ################################################################
  4801. iptables -A INPUT -m limit --limit 1/s -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Dropped INPUT packet: "
  4802. iptables -A INPUT -j DROP
  4803.  
  4804. # Drop all packets at the end of the POST_INPUT_DROP_CHAIN
  4805. iptables -A POST_INPUT_DROP_CHAIN -j DROP
  4806.  
  4807. # Remove the temp rule at the beginning of the POST_INPUT_DROP_CHAIN
  4808. iptables -D POST_INPUT_DROP_CHAIN 1
  4809.  
  4810. ###############################################################################################
  4811. # FORWARD rules #
  4812. ###############################################################################################
  4813.  
  4814. # Drop forward traffic to/from IPv6 Link Local Addresses
  4815. ########################################################
  4816. if [ "$IPV6_SUPPORT" = "1" ]; then
  4817. if [ "$FORWARD_LINK_LOCAL" != "1" ]; then
  4818. ip6tables -A FORWARD -s fe80::/10 -j LINK_LOCAL_DROP
  4819. ip6tables -A FORWARD -d fe80::/10 -j LINK_LOCAL_DROP
  4820. else
  4821. echo "IPv6 Link-Local Addresses are forwarded!"
  4822. fi
  4823. fi
  4824.  
  4825. # Allow forward traffic from "trusted-if"
  4826. #########################################
  4827. if [ -n "$TRUSTED_IF" ]; then
  4828. echo "Accepting ALL FORWARD packets for trusted interface(s): $TRUSTED_IF"
  4829. IFS=' ,'
  4830. for interface in $TRUSTED_IF; do
  4831. # Allow forward traffic in:
  4832. iptables -A FORWARD -i $interface -j ACCEPT
  4833. # Allow forward traffic out:
  4834. iptables -A FORWARD -o $interface -j ACCEPT
  4835. done
  4836. fi
  4837.  
  4838. # Source the IN/OUT chains for the external interface(s)
  4839. ########################################################
  4840. IFS=' ,'
  4841. for eif in $EXT_IF; do
  4842. iptables -A FORWARD -i $eif -j EXT_FORWARD_IN_CHAIN
  4843. iptables -A FORWARD -o $eif -j EXT_FORWARD_OUT_CHAIN
  4844. done
  4845.  
  4846. # Source the IN/OUT chains for the internal interface(s)
  4847. ########################################################
  4848. IFS=' ,'
  4849. for iif in $INT_IF; do
  4850. iptables -A FORWARD -i $iif -j INT_FORWARD_IN_CHAIN
  4851. iptables -A FORWARD -o $iif -j INT_FORWARD_OUT_CHAIN
  4852. done
  4853.  
  4854. # Source the IN/OUT chains for the dmz interface(s)
  4855. ########################################################
  4856. IFS=' ,'
  4857. for dif in $DMZ_IF; do
  4858. iptables -A FORWARD -i $dif -j DMZ_FORWARD_IN_CHAIN
  4859. iptables -A FORWARD -o $dif -j DMZ_FORWARD_OUT_CHAIN
  4860. done
  4861.  
  4862. # Check source address for spoofing
  4863. ###################################
  4864. iptables -A FORWARD -j SPOOF_CHK
  4865.  
  4866. # Check if the incoming packet is (in)valid
  4867. ###########################################
  4868. iptables -A EXT_FORWARD_IN_CHAIN -j VALID_CHK
  4869.  
  4870. # Check if incoming packet sources are (in)valid
  4871. ################################################
  4872. if [ "$DROP_PRIVATE_ADDRESSES" = "1" -o "$RESERVED_NET_LOG" = "1" ]; then
  4873. iptables -A EXT_FORWARD_IN_CHAIN -j RESERVED_NET_CHK
  4874. fi
  4875.  
  4876. # Setup forward policy for the DMZ
  4877. ##################################
  4878. if [ -n "$DMZ_IF" ]; then
  4879.  
  4880. echo "Setting up DMZ FORWARD policy"
  4881.  
  4882. if [ "$DMZ_OUTPUT_DENY_LOG" != "0" ]; then
  4883. echo " Logging of denied DMZ (forward) output connections enabled"
  4884. else
  4885. echo " Logging of denied DMZ (forward) output connections disabled"
  4886. fi
  4887.  
  4888. if [ "$DMZ_INPUT_DENY_LOG" != "0" ]; then
  4889. echo " Logging of denied DMZ (forward) input connections enabled"
  4890. else
  4891. echo " Logging of denied DMZ (forward) input connections disabled"
  4892. fi
  4893.  
  4894. # Setup helper chains for the DMZ:
  4895. setup_inet_dmz_forward_chain;
  4896. setup_dmz_inet_forward_chain;
  4897. setup_dmz_lan_forward_chain;
  4898.  
  4899. IFS=' ,'
  4900. for dif in $DMZ_IF; do
  4901. echo "Applying DMZ FORWARD policy to interface: $dif"
  4902.  
  4903. # Always make subnets on the SAME interface trust each other
  4904. iptables -A FORWARD -i $dif -o $dif -j ACCEPT
  4905.  
  4906. for eif in $EXT_IF; do
  4907. # Apply policy for inet->DMZ traffic
  4908. iptables -A FORWARD -i $eif -o $dif -j INET_DMZ_FORWARD_CHAIN
  4909.  
  4910. # Apply policy for DMZ->inet traffic
  4911. iptables -A FORWARD -i $dif -o $eif -j DMZ_INET_FORWARD_CHAIN
  4912. done
  4913.  
  4914. # Apply policy for DMZ->LAN traffic & LAN->DMZ traffic
  4915. for iif in $INT_IF; do
  4916. # Apply policy for DMZ->LAN
  4917. iptables -A FORWARD -i $dif -o $iif -j DMZ_LAN_FORWARD_CHAIN
  4918.  
  4919. # Apply policy for LAN->DMZ (allow all)
  4920. iptables -A FORWARD -i $iif -o $dif -j ACCEPT
  4921. done
  4922. done
  4923. fi
  4924.  
  4925. # Forward traffic for LAN interface(s) that trust each other
  4926. #######################################################################
  4927. IFS=$SEP3
  4928. for if_group in $IF_TRUSTS; do
  4929. echo "Setting up trust FORWARD policy for interface(s): $if_group"
  4930. IFS=' ,'
  4931. for input_if in $if_group; do
  4932. for output_if in $if_group; do
  4933. if [ "$input_if" != "$output_if" ]; then
  4934. iptables -A FORWARD -i $input_if -o $output_if -j ACCEPT
  4935. fi
  4936. done
  4937. done
  4938. done
  4939.  
  4940. # Additional rules for the internal subnet(s)
  4941. #############################################
  4942. if [ -n "$INT_IF" ]; then
  4943. echo "Setting up internal(LAN) FORWARD policy"
  4944.  
  4945. # Enable logging of denied LAN output connections?
  4946. ##################################################
  4947. if [ "$LAN_OUTPUT_DENY_LOG" != "0" ]; then
  4948. echo " Logging of denied LAN->INET FORWARD connections enabled"
  4949. else
  4950. echo " Logging of denied LAN->INET FORWARD connections disabled"
  4951. fi
  4952.  
  4953. # Setup helper chain for the LAN:
  4954. setup_lan_inet_forward_chain;
  4955.  
  4956. IFS=' ,'
  4957. for iif in $INT_IF; do
  4958. echo "Applying internal(LAN) FORWARD policy to interface: $iif"
  4959.  
  4960. # Always make subnets on the SAME interface trust each other
  4961. iptables -A FORWARD -i $iif -o $iif -j ACCEPT
  4962.  
  4963. for eif in $EXT_IF; do
  4964. iptables -A FORWARD -i $iif -o $eif -j LAN_INET_FORWARD_CHAIN
  4965. done
  4966. done
  4967. fi
  4968.  
  4969.  
  4970. ##############################################################################
  4971. # Masquerade (NAT) or SNAT. Share the gateway's internet connection with #
  4972. # the internal network #
  4973. ##############################################################################
  4974. if [ "$NAT" = "1" ]; then
  4975. if [ -n "$NAT_STATIC_IP" ]; then
  4976. echo "Enabling SNAT via external interface(s): $EXT_IF"
  4977.  
  4978. printf " Adding (internal) host(s): "
  4979.  
  4980. IFS=' ,'
  4981. for net in $NAT_INTERNAL_NET; do
  4982. printf "$net "
  4983.  
  4984. COUNT=0
  4985. for interface in $EXT_IF; do
  4986. COUNT=$(($COUNT + 1)) # x++;
  4987. STATIC_IP=$(echo "$NAT_STATIC_IP" |awk "{ print \$$COUNT }")
  4988. ip4tables -t nat -A POSTROUTING -o $interface -s $net ! -d $net -j SNAT --to-source $STATIC_IP
  4989. done
  4990. done
  4991. echo ""
  4992. else
  4993. # All internal traffic is masqueraded externally
  4994. ################################################
  4995. echo "Enabling masquerading(NAT) via external interface(s): $EXT_IF"
  4996.  
  4997. printf " Adding (internal) host(s): "
  4998.  
  4999. IFS=' ,'
  5000. for net in $NAT_INTERNAL_NET; do
  5001. printf "$net "
  5002.  
  5003. for interface in $EXT_IF; do
  5004. # The "! -d $net" is to avoid problems with Freeswan etc.
  5005. ip4tables -t nat -A POSTROUTING -o $interface -s $net ! -d $net -j MASQUERADE
  5006. done
  5007. done
  5008. echo ""
  5009. fi
  5010. fi
  5011.  
  5012. # Port / protocol forwarding. Forward ports or protocols on the internet-gateway to machines in our LAN
  5013. #######################################################################################################
  5014.  
  5015. # NAT TCP port forwards
  5016. #######################
  5017. unset IFS
  5018. for rule in $NAT_FORWARD_TCP; do
  5019. if_shosts_sports=`echo "$rule" |cut -s -d'>' -f1`
  5020. interfaces=`get_ifs "$if_shosts_sports" "$EXT_IF"`
  5021. destips=`get_ips "$if_shosts_sports"`
  5022. shosts=`get_hosts_ihp "$if_shosts_sports" "$ANYHOST"`
  5023. sports=`get_ports_ihp "$if_shosts_sports"`
  5024. dhost_dport=`echo "$rule" |cut -s -d'>' -f2`
  5025. dhost=`get_hosts_hp "$dhost_dport"`
  5026.  
  5027. echo "$(show_if_ip "$interfaces" "$destips")Forwarding(NAT) TCP port(s) $(show_hosts_ports "$shosts" "$sports") to $(echo "$dhost_dport" |tr "$SEP-" '::')"
  5028.  
  5029. IFS=' ,'
  5030. for shost in `ip_range "$shosts"`; do
  5031. for sport in $sports; do
  5032. for destip in $destips; do
  5033. # Portforward for all specified interfaces
  5034. for eif in $interfaces; do
  5035. # This code makes it possible to forward to a different port on the internal host
  5036. dport=`get_ports_hp "$dhost_dport" "$sport"`
  5037.  
  5038. ip4tables -t nat -A PREROUTING -i $eif -s $shost -d $destip -p tcp --dport $sport -j DNAT --to-destination `echo "$dhost_dport" |tr "$SEP-" '::'`
  5039. if [ -n "$dhost" ]; then
  5040. ip4tables -A EXT_FORWARD_IN_CHAIN -i $eif ! -o $eif -s $shost -d $dhost -p tcp --dport $dport -j ACCEPT
  5041. fi
  5042. done
  5043. done
  5044. done
  5045. done
  5046. done
  5047.  
  5048.  
  5049. # NAT UDP port forwards
  5050. #######################
  5051. unset IFS
  5052. for rule in $NAT_FORWARD_UDP; do
  5053. if_shosts_sports=`echo "$rule" |cut -s -d'>' -f1`
  5054. interfaces=`get_ifs "$if_shosts_sports" "$EXT_IF"`
  5055. destips=`get_ips "$if_shosts_sports"`
  5056. shosts=`get_hosts_ihp "$if_shosts_sports" "$ANYHOST"`
  5057. sports=`get_ports_ihp "$if_shosts_sports"`
  5058. dhost_dport=`echo "$rule" |cut -s -d'>' -f2`
  5059. dhost=`get_hosts_hp "$dhost_dport"`
  5060.  
  5061. echo "$(show_if_ip "$interfaces" "$destips")Forwarding(NAT) UDP port(s) $(show_hosts_ports "$shosts" "$sports") to $(echo "$dhost_dport" |tr "$SEP-" '::')"
  5062.  
  5063. IFS=' ,'
  5064. for shost in `ip_range "$shosts"`; do
  5065. for sport in $sports; do
  5066. for destip in $destips; do
  5067. # Portforward for all specified interfaces
  5068. for eif in $interfaces; do
  5069. # This code makes it possible to forward to a different port on the internal host
  5070. dport=`get_ports_hp "$dhost_dport" "$sport"`
  5071.  
  5072. ip4tables -t nat -A PREROUTING -i $eif -s $shost -d $destip -p udp --dport $sport -j DNAT --to-destination `echo "$dhost_dport" |tr "$SEP-" '::'`
  5073. if [ -n "$dhost" ]; then
  5074. ip4tables -A EXT_FORWARD_IN_CHAIN -i $eif ! -o $eif -s $shost -d $dhost -p udp --dport $dport -j ACCEPT
  5075. fi
  5076. done
  5077. done
  5078. done
  5079. done
  5080. done
  5081.  
  5082.  
  5083. # Non-TCP/UDP port forwards (protocol based)
  5084. ############################################
  5085. unset IFS
  5086. for rule in $NAT_FORWARD_IP; do
  5087. if_shosts_protos=`echo "$rule" |cut -s -d'>' -f1`
  5088. interfaces=`get_ifs "$if_shosts_protos" "$EXT_IF"`
  5089. destips=`get_ips "$if_shosts_protos"`
  5090. shosts=`get_hosts_ihp "$if_shosts_protos" "$ANYHOST"`
  5091. protos=`get_ports_ihp "$if_shosts_protos"`
  5092. dhost=`echo "$rule" |cut -s -d'>' -f2`
  5093.  
  5094. echo "$(show_if_ip "$interfaces" "$destips")Forwarding(NAT) IP protocol(s) $(show_hosts_ports "$shosts" "$protos") to $dhost"
  5095.  
  5096. IFS=' ,'
  5097. for shost in `ip_range "$shosts"`; do
  5098. for proto in $protos; do
  5099. for destip in $destips; do
  5100. for eif in $interfaces; do
  5101. ip4tables -t nat -A PREROUTING -i $eif -s $shost -d $destip -p $proto -j DNAT --to-destination $dhost
  5102. if [ -n "$dhost" ]; then
  5103. ip4tables -A EXT_FORWARD_IN_CHAIN -i $eif ! -o $eif -s $shost -d $dhost -p $proto -j ACCEPT
  5104. fi
  5105. done
  5106. done
  5107. done
  5108. done
  5109. done
  5110.  
  5111. # Setup rules to allow INET IPv6 and non-NAT'ed IPv4 traffic in FORWARD chain
  5112. setup_inet_forward_rules;
  5113.  
  5114. # Do we want to loose the forward chain to make use of protocols like UPnP possible?
  5115. ####################################################################################
  5116. if [ "$LOOSE_FORWARD" = "1" ]; then
  5117. echo "Security is LOOSENED for external interface(s) in the FORWARD chain!"
  5118. # Loose the forward chain
  5119. IFS=' ,'
  5120. for eif in $EXT_IF; do
  5121. iptables -A FORWARD -i $eif ! -o $eif -j ACCEPT
  5122. done
  5123. else
  5124. echo "Security is ENFORCED for external interface(s) in the FORWARD chain"
  5125. fi
  5126.  
  5127. # Jump to the post forward chain
  5128. iptables -A FORWARD -j POST_FORWARD_CHAIN
  5129.  
  5130. # Everything else is logged & dropped in FORWARD
  5131. if [ "$FORWARD_DROP_LOG" != "0" ]; then
  5132. echo " Logging of dropped FORWARD packets enabled"
  5133. iptables -A FORWARD -m limit --limit 1/m --limit-burst 3 -j LOG --log-level $LOGLEVEL --log-prefix "AIF:Dropped FORWARD packet: "
  5134. else
  5135. echo " Logging of dropped FORWARD packets disabled"
  5136. fi
  5137. iptables -A FORWARD -j DROP
  5138.  
  5139. # Post/pre routing post chains
  5140. ip4tables -t nat -A PREROUTING -j POST_NAT_PREROUTING_CHAIN
  5141. ip4tables -t nat -A POSTROUTING -j POST_NAT_POSTROUTING_CHAIN
  5142.  
  5143. # Mark outgoing packets for traffic shaping?
  5144. ############################################
  5145. #if [ "$TRAFFIC_SHAPING" = "1" ]; then
  5146. # iptables -t mangle -I OUTPUT -m length --length 0:500 -j MARK --set-mark 1
  5147. # iptables -t mangle -I OUTPUT -m length --length 500:1500 -j MARK --set-mark 2
  5148. #fi
  5149.  
  5150. # Jump to the post output chain
  5151. iptables -A OUTPUT -j POST_OUTPUT_CHAIN
  5152.  
  5153. # Everything else is accepted in OUTPUT
  5154. iptables -A OUTPUT -j ACCEPT
  5155.  
  5156. # Clean-up DNS output rules
  5157. iptables -D OUTPUT_CHAIN 2
  5158. iptables -D OUTPUT_CHAIN 1
  5159. }
  5160.  
  5161.  
  5162. # Create our custom chains
  5163. create_user_chains()
  5164. {
  5165. # General chains
  5166. iptables -N HOST_BLOCK_SRC
  5167. iptables -N HOST_BLOCK_DST
  5168. iptables -N HOST_BLOCK_DROP
  5169. iptables -N VALID_CHK
  5170. iptables -N RESERVED_NET_CHK
  5171. iptables -N SPOOF_CHK
  5172. iptables -N INPUT_CHAIN
  5173. iptables -N FORWARD_CHAIN
  5174. iptables -N OUTPUT_CHAIN
  5175. ip4tables -t nat -N NAT_PREROUTING_CHAIN
  5176. ip4tables -t nat -N NAT_POSTROUTING_CHAIN
  5177. iptables -N POST_INPUT_DROP_CHAIN
  5178. iptables -N POST_INPUT_CHAIN
  5179. iptables -N POST_FORWARD_CHAIN
  5180. iptables -N POST_OUTPUT_CHAIN
  5181. ip4tables -t nat -N POST_NAT_PREROUTING_CHAIN
  5182. ip4tables -t nat -N POST_NAT_POSTROUTING_CHAIN
  5183.  
  5184. if [ "$IPV6_SUPPORT" = "1" ]; then
  5185. ip6tables -N LINK_LOCAL_DROP
  5186. fi
  5187.  
  5188. # Interface-2-interface chains
  5189. iptables -N DMZ_LAN_FORWARD_CHAIN
  5190. iptables -N INET_DMZ_FORWARD_CHAIN
  5191. iptables -N DMZ_INET_FORWARD_CHAIN
  5192. iptables -N LAN_INET_FORWARD_CHAIN
  5193.  
  5194. # Chains for the external interface
  5195. iptables -N EXT_MULTICAST_CHAIN
  5196. iptables -N EXT_BROADCAST_CHAIN
  5197. iptables -N EXT_ICMP_FLOOD_CHAIN
  5198. iptables -N EXT_INPUT_CHAIN
  5199. iptables -N EXT_FORWARD_IN_CHAIN
  5200. iptables -N EXT_FORWARD_OUT_CHAIN
  5201. iptables -N EXT_OUTPUT_CHAIN
  5202.  
  5203. # INT(LAN) chains
  5204. iptables -N INT_INPUT_CHAIN
  5205. iptables -N INT_FORWARD_IN_CHAIN
  5206. iptables -N INT_FORWARD_OUT_CHAIN
  5207. iptables -N INT_OUTPUT_CHAIN
  5208.  
  5209. # DMZ chains
  5210. iptables -N DMZ_INPUT_CHAIN
  5211. iptables -N DMZ_FORWARD_IN_CHAIN
  5212. iptables -N DMZ_FORWARD_OUT_CHAIN
  5213. iptables -N DMZ_OUTPUT_CHAIN
  5214. }
  5215.  
  5216.  
  5217. # Flush our custom chains
  5218. flush_user_chains()
  5219. {
  5220. # General chains
  5221. iptables -F HOST_BLOCK_SRC
  5222. iptables -F HOST_BLOCK_DST
  5223. iptables -F HOST_BLOCK_DROP
  5224. iptables -F VALID_CHK
  5225. iptables -F RESERVED_NET_CHK
  5226. iptables -F SPOOF_CHK
  5227. iptables -F INPUT_CHAIN
  5228. iptables -F FORWARD_CHAIN
  5229. iptables -F OUTPUT_CHAIN
  5230. iptables -F POST_INPUT_DROP_CHAIN
  5231. iptables -F POST_INPUT_CHAIN
  5232. iptables -F POST_FORWARD_CHAIN
  5233. iptables -F POST_OUTPUT_CHAIN
  5234.  
  5235. ip4tables -t nat -F NAT_PREROUTING_CHAIN
  5236. ip4tables -t nat -F NAT_POSTROUTING_CHAIN
  5237. ip4tables -t nat -F POST_NAT_PREROUTING_CHAIN
  5238. ip4tables -t nat -F POST_NAT_POSTROUTING_CHAIN
  5239.  
  5240. if [ "$IPV6_SUPPORT" = "1" ]; then
  5241. ip6tables -F LINK_LOCAL_DROP
  5242. fi
  5243.  
  5244. # Interface-2-interface chains
  5245. iptables -F DMZ_LAN_FORWARD_CHAIN
  5246. iptables -F INET_DMZ_FORWARD_CHAIN
  5247. iptables -F DMZ_INET_FORWARD_CHAIN
  5248. iptables -F LAN_INET_FORWARD_CHAIN
  5249.  
  5250. # Chains for the external interface
  5251. iptables -F EXT_MULTICAST_CHAIN
  5252. iptables -F EXT_BROADCAST_CHAIN
  5253. iptables -F EXT_ICMP_FLOOD_CHAIN
  5254. iptables -F EXT_INPUT_CHAIN
  5255. iptables -F EXT_FORWARD_IN_CHAIN
  5256. iptables -F EXT_FORWARD_OUT_CHAIN
  5257. iptables -F EXT_OUTPUT_CHAIN
  5258.  
  5259. # INT(LAN) chains
  5260. iptables -F INT_INPUT_CHAIN
  5261. iptables -F INT_FORWARD_IN_CHAIN
  5262. iptables -F INT_FORWARD_OUT_CHAIN
  5263. iptables -F INT_OUTPUT_CHAIN
  5264.  
  5265. # DMZ chains
  5266. iptables -F DMZ_INPUT_CHAIN
  5267. iptables -F DMZ_FORWARD_IN_CHAIN
  5268. iptables -F DMZ_FORWARD_OUT_CHAIN
  5269. iptables -F DMZ_OUTPUT_CHAIN
  5270. }
  5271.  
  5272.  
  5273. stop_firewall()
  5274. {
  5275. # Set default IPv4 policies
  5276. ip4tables -P INPUT ACCEPT
  5277. ip4tables -P FORWARD DROP
  5278. ip4tables -P OUTPUT ACCEPT
  5279.  
  5280. # Flush builtin IPv4 chains
  5281. ip4tables -F INPUT
  5282. ip4tables -F OUTPUT
  5283. ip4tables -F FORWARD
  5284.  
  5285. # Flush rules in the mangle/nat tables
  5286. ip4tables -t mangle -F
  5287. ip4tables -t mangle -X
  5288. ip4tables -t nat -F
  5289. ip4tables -t nat -X
  5290.  
  5291. # Attempt to flush all IPv4 chains
  5292. ip4tables -F
  5293. ip4tables -X
  5294.  
  5295. if [ "$IPV6_DETECTED" = "1" ]; then
  5296. # Set default IPv6 policies
  5297. ip6tables -P INPUT ACCEPT
  5298. ip6tables -P FORWARD DROP
  5299. ip6tables -P OUTPUT ACCEPT
  5300.  
  5301. # Flush builtin IPv6 chains
  5302. ip6tables -F INPUT
  5303. ip6tables -F OUTPUT
  5304. ip6tables -F FORWARD
  5305.  
  5306. # Flush rules in the mangle table
  5307. ip6tables -t mangle -F
  5308. ip6tables -t mangle -X
  5309.  
  5310. # Attempt to flush all IPv6 chains
  5311. ip6tables -F
  5312. ip6tables -X
  5313. fi
  5314. }
  5315.  
  5316.  
  5317. reinit_firewall_chains()
  5318. {
  5319. echo "Reinitializing firewall chains"
  5320.  
  5321. # Set INDENT for functions
  5322. INDENT=' '
  5323.  
  5324. # Create chains, just in case
  5325. create_user_chains 2>/dev/null
  5326.  
  5327. # Flush our user chains
  5328. flush_user_chains;
  5329.  
  5330. # Temporarily set OUTPUT default policy to ACCEPT
  5331. # Without it, active states will be flushed.
  5332. iptables -P OUTPUT ACCEPT
  5333.  
  5334. # Flush builtin IPv4 chains
  5335. ip4tables -F INPUT
  5336. ip4tables -F OUTPUT
  5337. ip4tables -F FORWARD
  5338.  
  5339. # Flush rules in the nat/mangle tables
  5340. ip4tables -t mangle -F
  5341. ip4tables -t nat -F
  5342.  
  5343. if [ "$IPV6_DETECTED" = "1" ]; then
  5344. # Flush builtin IPv6 chains
  5345. ip6tables -F INPUT
  5346. ip6tables -F OUTPUT
  5347. ip6tables -F FORWARD
  5348.  
  5349. # Flush rules in the mangle table
  5350. ip6tables -t mangle -F
  5351. fi
  5352.  
  5353. # Restore our base chains (which weren't flushed so any
  5354. # running connections should be maintained
  5355. #######################################################
  5356. iptables -A INPUT -j BASE_INPUT_CHAIN
  5357. iptables -A FORWARD -j BASE_FORWARD_CHAIN
  5358. iptables -A OUTPUT -j BASE_OUTPUT_CHAIN
  5359. }
  5360.  
  5361.  
  5362. stop_block_firewall()
  5363. {
  5364. # Temporarely set default IPv4 policies to DROP,
  5365. # to not even have a small window of opportunity
  5366. ################################################
  5367. ip4tables -P INPUT DROP
  5368. ip4tables -P FORWARD DROP
  5369. ip4tables -P OUTPUT DROP
  5370.  
  5371. # Flush builtin IPv4 chains
  5372. ip4tables -F INPUT
  5373. ip4tables -F OUTPUT
  5374. ip4tables -F FORWARD
  5375.  
  5376. # Flush nat/mangle table rules
  5377. ip4tables -t mangle -F
  5378. ip4tables -t mangle -X
  5379. ip4tables -t nat -F
  5380. ip4tables -t nat -X
  5381.  
  5382. # Attempt to flush all IPv4 chains
  5383. ip4tables -F
  5384. ip4tables -X
  5385.  
  5386. if [ "$IPV6_DETECTED" = "1" ]; then
  5387. # Temporarely set default IPv6 policies to DROP,
  5388. # to not even have a small window of opportunity
  5389. ################################################
  5390. ip6tables -P INPUT DROP
  5391. ip6tables -P FORWARD DROP
  5392. ip6tables -P OUTPUT DROP
  5393.  
  5394. # Flush builtin IPv6 chains
  5395. ip6tables -F INPUT
  5396. ip6tables -F OUTPUT
  5397. ip6tables -F FORWARD
  5398.  
  5399. # Flush mangle table rules
  5400. ip6tables -t mangle -F
  5401. ip6tables -t mangle -X
  5402.  
  5403. # Attempt to flush all IPv6 chains
  5404. ip6tables -F
  5405. ip6tables -X
  5406. fi
  5407.  
  5408. # Deny traffic from our internet interfaces
  5409. IFS=' ,'
  5410. for interface in $EXT_IF; do
  5411. ip4tables -A INPUT -i $interface -j DROP
  5412.  
  5413. if [ "$IPV6_DETECTED" = "1" ]; then
  5414. ip6tables -A INPUT -i $interface -j DROP
  5415. fi
  5416. done
  5417.  
  5418. # Allow IPv4 traffic from the loopback (localhost)
  5419. ip4tables -A INPUT -i lo -j ACCEPT
  5420. ip4tables -A FORWARD -i lo -j ACCEPT
  5421. ip4tables -A OUTPUT -o lo -j ACCEPT
  5422.  
  5423. # Set default IPv4 policies
  5424. ip4tables -P INPUT ACCEPT
  5425. ip4tables -P FORWARD DROP
  5426. ip4tables -P OUTPUT ACCEPT
  5427.  
  5428. if [ "$IPV6_DETECTED" = "1" ]; then
  5429. # Allow IPv6 traffic from the loopback (localhost)
  5430. ip6tables -A INPUT -i lo -j ACCEPT
  5431. ip6tables -A FORWARD -i lo -j ACCEPT
  5432. ip6tables -A OUTPUT -o lo -j ACCEPT
  5433.  
  5434. # Set default IPv6 policies
  5435. ip6tables -P INPUT ACCEPT
  5436. ip6tables -P FORWARD DROP
  5437. ip6tables -P OUTPUT ACCEPT
  5438. fi
  5439. }
  5440.  
  5441.  
  5442. show_status()
  5443. {
  5444. # Check if the user gave any parameters
  5445. if [ -z "$1" ]; then
  5446. iptables -xnvL
  5447.  
  5448. if [ "$NAT" = "1" ]; then
  5449. ip4tables -t nat -xnvL
  5450. fi
  5451.  
  5452. if [ "$MANGLE_TOS" = "1" ]; then
  5453. iptables -t mangle -nvL
  5454. fi
  5455.  
  5456. # Show plugin status
  5457. plugins_status;
  5458. else
  5459. #iptables -nvL $2 $3 $4 $5
  5460. iptables -xnvL $@
  5461. fi
  5462. }
  5463.  
  5464.  
  5465. show_start()
  5466. {
  5467. DATE=`LC_ALL=C date +'%b %d %H:%M:%S'`
  5468. echo "$DATE ** Starting Arno's Iptables Firewall v$MY_VERSION **" >> $FIREWALL_LOG
  5469. echo "** Starting Arno's Iptables Firewall v$MY_VERSION **" |logger -t firewall -p kern.info
  5470. }
  5471.  
  5472.  
  5473. show_restart()
  5474. {
  5475. DATE=`LC_ALL=C date +'%b %d %H:%M:%S'`
  5476. echo "$DATE ** Restarting Arno's Iptables Firewall v$MY_VERSION **" >> $FIREWALL_LOG
  5477. echo "** Restarting Arno's Iptables Firewall v$MY_VERSION **" |logger -t firewall -p kern.info
  5478. }
  5479.  
  5480.  
  5481. show_stop()
  5482. {
  5483. DATE=`LC_ALL=C date +'%b %d %H:%M:%S'`
  5484. printf "$DATE \033[40m\033[1;32mStopping Arno's Iptables Firewall v$MY_VERSION\033[0m\n"
  5485. echo "$DATE ** Stopping Arno's Iptables Firewall v$MY_VERSION **" >> $FIREWALL_LOG
  5486. echo "** Stopping Arno's Iptables Firewall v$MY_VERSION **" |logger -t firewall -p kern.info
  5487. }
  5488.  
  5489.  
  5490. show_stop_blocked()
  5491. {
  5492. DATE=`LC_ALL=C date +'%b %d %H:%M:%S'`
  5493. printf "$DATE \033[40m\033[1;31mFIREWALL DISABLED & BLOCKING ALL INTERNET TRAFFIC!\033[0m\n"
  5494. }
  5495.  
  5496.  
  5497. # Now show the final message
  5498. show_applied()
  5499. {
  5500. echo ""
  5501. DATE=`LC_ALL=C date +'%b %d %H:%M:%S'`
  5502.  
  5503. if [ "$IPTABLES_ERROR" = "1" ]; then
  5504. printf "$DATE \033[40m\033[1;31mWARNING: Not all firewall rules are applied.\033[0m\n" >&2
  5505. echo "$DATE ** WARNING: Not all firewall rules are applied **" >> $FIREWALL_LOG
  5506. echo "** WARNING: Not all firewall rules are applied **" |logger -t firewall -p kern.info
  5507. else
  5508. printf "$DATE \033[40m\033[1;32mAll firewall rules applied.\033[0m\n"
  5509. echo "$DATE ** All firewall rules applied **" >> $FIREWALL_LOG
  5510. echo "** All firewall rules applied **" |logger -t firewall -p kern.info
  5511. fi
  5512. }
  5513.  
  5514.  
  5515. show_disabled()
  5516. {
  5517. DATE=`LC_ALL=C date +'%b %d %H:%M:%S'`
  5518. printf "$DATE \033[40m\033[1;31mFIREWALL DISABLED!\033[0m\n"
  5519. }
  5520.  
  5521.  
  5522. show_help()
  5523. {
  5524. echo "Usage: `basename $0` { start | stop | restart | force-reload | status }" >&2
  5525. echo "start = Start firewall" >&2
  5526. echo "stop = Stop firewall" >&2
  5527. echo "stop-block = Stop firewall & block all internet packets" >&2
  5528. echo "restart = Restart firewall" >&2
  5529. echo "force-reload = Reload blocked hosts (blackhole) file" >&2
  5530. echo "check-conf = Check configuration file (only)" >&2
  5531. echo "status [-t {table}] [chain] = View firewall ruleset ([chain] & [-t {table}] are optional)" >&2
  5532. }
  5533.  
  5534.  
  5535. main_stop()
  5536. {
  5537. plugins_stop;
  5538. if [ "$1" = "block" ]; then
  5539. stop_block_firewall;
  5540. else
  5541. stop_firewall;
  5542. fi
  5543. }
  5544.  
  5545.  
  5546. main_start()
  5547. {
  5548.  
  5549. load_modules;
  5550. setup_misc;
  5551. setup_kernel_settings;
  5552. init_firewall_chains;
  5553. setup_default_policies;
  5554. setup_hostblock_chain;
  5555. setup_firewall_rules;
  5556. }
  5557.  
  5558.  
  5559. main_restart()
  5560. {
  5561. plugins_stop restart;
  5562. load_modules;
  5563. # setup_misc;
  5564. setup_kernel_settings;
  5565. reinit_firewall_chains;
  5566. setup_default_policies;
  5567. setup_hostblock_chain;
  5568. setup_firewall_rules;
  5569. }
  5570.  
  5571.  
  5572. start_restart()
  5573. {
  5574. # Show uname information
  5575. echo "Platform: $(uname -s -r -m)"
  5576.  
  5577. config_check;
  5578.  
  5579. # Check whether we are actually stopped
  5580. case $(check_for_base_chains) in
  5581. no) main_start;;
  5582. yes) main_restart;;
  5583. other) main_stop; main_start;;
  5584. esac
  5585.  
  5586. show_applied;
  5587. }
  5588.  
  5589.  
  5590. # int main (char *argv)
  5591. #######################
  5592.  
  5593. # Check commandline supplied argument:
  5594. case "$1" in
  5595. 'start' ) sanity_check;
  5596. show_start;
  5597. start_restart;;
  5598. 'restart' ) sanity_check;
  5599. show_restart;
  5600. start_restart;;
  5601. 'force-reload' ) sanity_check;
  5602. config_check;
  5603. setup_hostblock_chain;
  5604. show_applied;;
  5605. 'stop' ) sanity_check;
  5606. show_stop;
  5607. main_stop;
  5608. show_disabled;;
  5609. 'stop-block' ) sanity_check;
  5610. show_stop;
  5611. main_stop block;
  5612. show_stop_blocked;;
  5613. 'status' ) shift;
  5614. show_status $@;;
  5615. 'check-conf' ) config_check;;
  5616. * ) printf "\033[40m\033[1;31mERROR: Bad or missing parameter(s)\033[0m\n" >&2;
  5617. show_help;;
  5618. esac
  5619.  
  5620. exit $IPTABLES_ERROR
  5621.  
  5622.  
Advertisement
Add Comment
Please, Sign In to add comment