. 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"); } 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 */ $tcpidletimeout = 9000*2; if ($config['filter']['tcpidletimeout']) $tcpidletimeout = $config['filter']['tcpidletimeout']*2; mwexec("/sbin/sysctl net.inet.ipf.fr_tcpidletimeout={$tcpidletimeout}"); mwexec("/sbin/sysctl net.inet.ipf.fr_tcphalfclosed=480"); mwexec("/sbin/sysctl net.inet.ipf.fr_udpacktimeout=480"); /* SIP... */ /* 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); /* set up MSS clamping */ $wanif = get_real_wan_interface(); if ($config['interfaces']['wan']['mtu']) $mssclamp = $config['interfaces']['wan']['mtu'] - 40; else if ($config['interfaces']['wan']['ipaddr'] == "pppoe") $mssclamp = 1452; else $mssclamp = 0; mwexec("/sbin/sysctl net.inet.ipf.fr_mssif={$wanif}"); mwexec("/sbin/sysctl net.inet.ipf.fr_mssclamp={$mssclamp}"); 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_nat_rules_generate_if($if, $src, $dst, $target, $portmap = true) { global $config; if ($target) $tgt = $target . "/32"; else $tgt = "0/32"; $natrule = "map $if $src $dst -> {$tgt} proxy port ftp 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'] . "/" . $g['pptp_subnet'], "", 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); } } } /* DIAG: add ipv6 NAT, if requested */ if (isset($config['diag']['ipv6nat']['enable'])) { $natrules .= "rdr $wanif 0/0 port 0 -> " . "{$config['diag']['ipv6nat']['ipaddr']} port 0 ipv6\n"; } /* 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(); if ($curwanip) $natrules .= "map $wanif from $curwanip/32 to any port = 53 -> 0.0.0.0/32\n"; } 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() { global $config, $g; $wancfg = $config['interfaces']['wan']; $lancfg = $config['interfaces']['lan']; $pptpdcfg = $config['pptpd']; $lanif = $lancfg['if']; $wanif = get_real_wan_interface(); $curwanip = get_current_wan_address(); /* rule groups (optional interfaces: see below) */ $ifgroups = array("lan" => 100, "wan" => 200); $lanip = $lancfg['ipaddr']; $lansa = gen_subnet($lancfg['ipaddr'], $lancfg['subnet']); $lansn = $lancfg['subnet']; /* 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 (is_ipaddr($config['interfaces'][$oc['bridge']]['ipaddr'])) { $oic['ip'] = $config['interfaces'][$oc['bridge']]['ipaddr']; $oic['sn'] = $config['interfaces'][$oc['bridge']]['subnet']; $oic['sa'] = gen_subnet($oic['ip'], $oic['sn']); } } $oic['bridge'] = 1; $oic['bridge_if'] = $oc['bridge']; } else { $oic['ip'] = $oc['ipaddr']; $oic['sn'] = $oc['subnet']; $oic['sa'] = gen_subnet($oic['ip'], $oic['sn']); } $optcfg['opt' . $i] = $oic; $ifgroups['opt' . $i] = ($i * 100) + 200; } } if ($pptpdcfg['mode'] == "server") { $pptpip = $pptpdcfg['localip']; $pptpsa = $pptpdcfg['remoteip']; $pptpsn = $g['pptp_subnet']; } /* 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) { 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); /* 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); } /* block private networks on WAN? */ if (isset($config['interfaces']['wan']['blockpriv'])) { $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 .= <<