. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* include all configuration functions */ require_once("functions.inc"); function filter_resync() { global $config, $g; mwexec("/sbin/ipf -y"); mwexec("/sbin/ipf -6 -y"); } function filter_ipmon_start() { global $config, $g; mwexec("/sbin/ipmon -sD"); } function filter_configure() { global $config, $g; if ($g['booting']) echo "Configuring firewall... "; /* set TCP timeouts */ if ($config['filter']['tcpidletimeout'] && $g['booting']) { $tcpidletimeout = $config['filter']['tcpidletimeout']*2; /* ipfilter now needs to be disabled briefly in order to change the timeout value :( */ mwexec("/sbin/ipf -D"); mwexec("/sbin/sysctl net.inet.ipf.fr_tcpidletimeout={$tcpidletimeout}"); mwexec("/sbin/ipf -E"); } /* set portrange sysctls - m0n0wall doesn't need many ports for itself, and we'd rather let ipnat have the rest for better source port randomization */ mwexec("/sbin/sysctl net.inet.ip.portrange.first=64536"); mwexec("/sbin/sysctl net.inet.ip.portrange.hifirst=64536"); /* generate ipnat rules */ $ipnatrules = filter_nat_rules_generate(); /* load ipnat rules */ $fd = popen("/sbin/ipnat -C -f - > /dev/null 2>&1", "w"); if (!$fd) { printf("Cannot open /sbin/ipnat in filter_configure()\n"); return 1; } fwrite($fd, $ipnatrules); pclose($fd); /* generate ipf rules */ $ipfrules = filter_rules_generate(); $fd = popen("/sbin/ipf -Fa -f - > /dev/null 2>&1", "w"); if (!$fd) { printf("Cannot open /sbin/ipf in filter_configure()\n"); return 1; } fwrite($fd, $ipfrules); pclose($fd); /* generate ipf rules for IPv6 */ $ipfrules_ipv6 = filter_rules_generate_ipv6(); $fd = popen("/sbin/ipf -6 -Fa -f - > /dev/null 2>&1", "w"); if (!$fd) { printf("Cannot open /sbin/ipf in filter_configure()\n"); return 1; } fwrite($fd, $ipfrules_ipv6); pclose($fd); if ($g['booting']) echo "done\n"; return 0; } function filter_flush_nat_table() { global $config, $g; return mwexec("/sbin/ipnat -F"); } function filter_flush_state_table() { global $config, $g; return mwexec("/sbin/ipf -FS"); } function filter_flush_state_table_ipv6() { if (ipv6enabled()) { return mwexec("/sbin/ipf -6 -FS"); } else { return 0; } } function filter_nat_rules_generate_if($if, $src, $dst, $target, $portmap = true) { global $config; if ($target) $tgt = $target . "/32"; else $tgt = "0/32"; if (strpos($src, "from") !== false) /* must explicitly match destination port when using from/to matching with proxy rules */ $natrule = "map $if $src $dst port = 21 -> {$tgt} proxy port 21 ftp/tcp\n"; else $natrule = "map $if $src $dst -> {$tgt} proxy port 21 ftp/tcp\n"; if ($portmap) { $rangelow = 1024; $rangehigh = 64535; if ($config['nat']['portrange-low'] && $config['nat']['portrange-high']) { $rangelow = $config['nat']['portrange-low']; $rangehigh = $config['nat']['portrange-high']; } $natrule .= "map $if $src $dst -> {$tgt} portmap tcp/udp $rangelow:$rangehigh\n"; } $natrule .= "map $if $src $dst -> {$tgt}\n"; return $natrule; } function filter_nat_rules_generate() { global $config, $g; $wancfg = $config['interfaces']['wan']; $lancfg = $config['interfaces']['lan']; $pptpdcfg = $config['pptpd']; $wanif = get_real_wan_interface(); $lansa = gen_subnet($lancfg['ipaddr'], $lancfg['subnet']); $natrules = ""; /* any 1:1 mappings? */ if (is_array($config['nat']['onetoone'])) { foreach ($config['nat']['onetoone'] as $natent) { if (!is_numeric($natent['subnet'])) $sn = 32; else $sn = $natent['subnet']; if (!$natent['interface'] || ($natent['interface'] == "wan")) $natif = $wanif; else $natif = $config['interfaces'][$natent['interface']]['if']; $natrules .= "bimap {$natif} {$natent['internal']}/{$sn} -> {$natent['external']}/{$sn}\n"; } } /* outbound rules - advanced or standard */ if (isset($config['nat']['advancedoutbound']['enable'])) { /* advanced outbound rules */ if (is_array($config['nat']['advancedoutbound']['rule'])) { foreach ($config['nat']['advancedoutbound']['rule'] as $obent) { $dst = ""; $src = ""; if (!isset($obent['destination']['any'])) { $src = "from "; if (isset($obent['destination']['not'])) $dst = "! to "; else $dst = "to "; $dst .= $obent['destination']['network']; } $src .= $obent['source']['network']; if (!$obent['interface'] || ($obent['interface'] == "wan")) $natif = $wanif; else $natif = $config['interfaces'][$obent['interface']]['if']; $natrules .= filter_nat_rules_generate_if($natif, $src, $dst, $obent['target'], !isset($obent['noportmap'])); } } } else { /* standard outbound rules (one for each interface) */ $natrules .= filter_nat_rules_generate_if($wanif, $lansa . "/" . $lancfg['subnet'], "", null); /* optional interfaces */ for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) { $optcfg = $config['interfaces']['opt' . $i]; if (isset($optcfg['enable']) && !$optcfg['bridge']) { $optsa = gen_subnet($optcfg['ipaddr'], $optcfg['subnet']); $natrules .= filter_nat_rules_generate_if($wanif, $optsa . "/" . $optcfg['subnet'], "", null); } } /* PPTP subnet */ if ($pptpdcfg['mode'] == "server") { $natrules .= filter_nat_rules_generate_if($wanif, $pptpdcfg['remoteip'] . "/" . subnet_size_to_cidr($pptpdcfg['nunits'] ? $pptpdcfg['nunits'] : 16), "", null); } /* static routes */ if (is_array($config['staticroutes']['route'])) { foreach ($config['staticroutes']['route'] as $route) { if ($route['interface'] != "wan") $natrules .= filter_nat_rules_generate_if($wanif, $route['network'], "", null); } } } /* if the DNS forwarder is enabled, we need to add a map rule for outgoing DNS queries (source IP = WAN, destination port = 53). Otherwise, since Dnsmasq now picks port at random, they could clash with inbound mappings, leading to sporadic query timeouts. The map rule (without portmap) ensures that an entry in the NAT table is generated and prevents rdr rules from taking effect. */ if (isset($config['dnsmasq']['enable'])) { $curwanip = get_current_wan_address(false); if ($curwanip) $natrules .= "map $wanif from $curwanip/32 to any port = 53 -> 0.0.0.0/32 tcp/udp\n"; } /* inbound rules */ if (isset($config['nat']['rule'])) { foreach ($config['nat']['rule'] as $rule) { $extport = explode("-", $rule['external-port']); $target = alias_expand_host($rule['target']); if (!$target) continue; /* unresolvable alias */ if ($rule['external-address']) $extaddr = $rule['external-address'] . "/32"; else $extaddr = "0/0"; if (!$rule['interface'] || ($rule['interface'] == "wan")) $natif = $wanif; else $natif = $config['interfaces'][$rule['interface']]['if']; if ((!$extport[1]) || ($extport[0] == $extport[1])) { $natrules .= "rdr $natif {$extaddr} port {$extport[0]} -> {$target} " . "port {$rule['local-port']} {$rule['protocol']}"; } else { $natrules .= "rdr $natif {$extaddr} port {$extport[0]}-{$extport[1]} " . "-> {$target} " . "port {$rule['local-port']} {$rule['protocol']}"; } $natrules .= "\n"; } } if ($pptpdcfg['mode'] == "redir" && $pptpdcfg['redir']) { $natrules .= << {$pptpdcfg['redir']} port 0 gre rdr $wanif 0/0 port 1723 -> {$pptpdcfg['redir']} port 1723 tcp EOD; } return $natrules; } function filter_rules_generate_ipv6() { global $config, $g; if (ipv6enabled() && $config['interfaces']['lan']['ipaddr6']) { return filter_rules_generate(true); } else { return << 100, "wan" => 200); $lanip = $lancfg[$ipv6 ? 'ipaddr6' : 'ipaddr']; $lansn = $lancfg[$ipv6 ? 'subnet6' : 'subnet']; if ($ipv6) { if ($lanip == "6to4") { $lanip = calc_6to4_address("lan"); $lansn = 64; } if (!$lanip) { $lanip = "::1"; /* dummy until 6to4 address is known */ $lansa = "::"; } else { $lansa = gen_subnet6($lanip, $lansn); } } else { $lansa = gen_subnet($lanip, $lansn); } /* optional interfaces */ $optcfg = array(); for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) { $oc = $config['interfaces']['opt' . $i]; if (isset($oc['enable']) && $oc['if']) { $oic = array(); $oic['if'] = $oc['if']; if ($oc['bridge']) { if (!strstr($oc['bridge'], "opt") || isset($config['interfaces'][$oc['bridge']]['enable'])) { if ($ipv6 ? is_ipaddr6($config['interfaces'][$oc['bridge']]['ipaddr6']) : is_ipaddr($config['interfaces'][$oc['bridge']]['ipaddr'])) { $oic['ip'] = $config['interfaces'][$oc['bridge']][$ipv6 ? 'ipaddr6' : 'ipaddr']; $oic['sn'] = $config['interfaces'][$oc['bridge']][$ipv6 ? 'subnet6' : 'subnet']; if ($ipv6) $oic['sa'] = gen_subnet6($oic['ip'], $oic['sn']); else $oic['sa'] = gen_subnet($oic['ip'], $oic['sn']); } } $oic['bridge'] = 1; $oic['bridge_if'] = $oc['bridge']; } else { if ($ipv6) { if ($oc['ipaddr6'] == "6to4") { $oic['ip'] = calc_6to4_address('opt' . $i); $oic['sn'] = 64; } else { $oic['ip'] = $oc['ipaddr6']; $oic['sn'] = $oc['subnet6']; } /* IPv6: ignore interface if no address is set or 6to4 address not yet known */ if (!$oic['ip']) continue; } else { $oic['ip'] = $oc['ipaddr']; $oic['sn'] = $oc['subnet']; } if ($ipv6) $oic['sa'] = gen_subnet6($oic['ip'], $oic['sn']); else $oic['sa'] = gen_subnet($oic['ip'], $oic['sn']); $oic['ipv6ra'] = $oc['ipv6ra']; } $optcfg['opt' . $i] = $oic; $ifgroups['opt' . $i] = ($i * 100) + 200; } } if ($pptpdcfg['mode'] == "server" && !$ipv6) { $pptpip = $pptpdcfg['localip']; $pptpsa = $pptpdcfg['remoteip']; $pptpsn = subnet_size_to_cidr($pptpdcfg['nunits'] ? $pptpdcfg['nunits'] : 16); } if ($ipv6) { /* Make sure that group numbers are unique */ foreach ($ifgroups as $key => $val) { $ifgroups[$key] += 10000; } } /* default block logging? */ if (!isset($config['syslog']['nologdefaultblock'])) $log = "log"; else $log = ""; $ipfrules = << $oc) { if ((isset($config['dhcpd'][$on]['enable']) && !$oc['bridge']) || ($oc['bridge'] && isset($config['dhcpd'][$oc['bridge_if']]['enable']))) { $ipfrules .= << $oc) { $ipfrules .= "# allow link-local traffic on {$on}"; $ipfrules .= "pass in quick on {$oc['if']} from fe80::/10 to fe80::/10\n"; $ipfrules .= "pass out quick on {$oc['if']} from fe80::/10 to fe80::/10\n"; if ((isset($config['dhcpd'][$on]['enable']) && !$oc['bridge']) || ($oc['bridge'] && isset($config['dhcpd'][$oc['bridge_if']]['enable']))) { $ipfrules .= << $oc) { if ($oc['bridge'] && $oc['bridge_if'] == "lan") { $lanbridge = true; break; } } if (!$lanbridge || !isset($config['bridge']['filteringbridge'])) $ipfrules .= filter_rules_spoofcheck_generate('lan', $lanif, $lansa, $lansn, $log, $ipv6); /* OPT spoof check */ foreach ($optcfg as $on => $oc) { /* omit for bridged interfaces when the filtering bridge is on */ $isbridged = false; foreach ($optcfg as $on2 => $oc2) { if ($oc2['bridge'] && $oc2['bridge_if'] == $on) { $isbridged = true; break; } } if ($oc['ip'] && !(($oc['bridge'] || $isbridged) && isset($config['bridge']['filteringbridge']))) $ipfrules .= filter_rules_spoofcheck_generate($on, $oc['if'], $oc['sa'], $oc['sn'], $log, $ipv6); } /* block private networks on WAN? */ if (isset($config['interfaces']['wan']['blockpriv'])) { if (!$ipv6) { $ipfrules .= << $oc) { if ($oc['ip']) $ipfrules .= filter_rules_ipsec_generate($oc['if'], $oc['ip']); } } $out_kf = ""; if (isset($config['filter']['allowipsecfrags'])) $out_kf = "keep frags"; /* XXX - the first section is only needed because ipf refuses to parse rules that have "flags S/SAFR" and proto "tcp/udp" set because UDP does not have flags, but we still want to offer the TCP/UDP protocol option to the user */ $ipfrules .= << $oc) { $ingroup = $ifgroups[$on]; $ipfrules .= <<