diff options
author | jim-p <jimp@pfsense.org> | 2012-04-10 16:42:49 -0400 |
---|---|---|
committer | jim-p <jimp@pfsense.org> | 2012-04-10 16:42:49 -0400 |
commit | c9e04cd59054cf839af96cdf71cfc4cf58ccabaf (patch) | |
tree | 8a1681c7e8c013c6de3e1753f5d44a7aab1b99cd | |
parent | 9d14f8089f453778084bdb50455e1cfffbe98221 (diff) | |
download | pfsense-c9e04cd59054cf839af96cdf71cfc4cf58ccabaf.zip pfsense-c9e04cd59054cf839af96cdf71cfc4cf58ccabaf.tar.gz |
Add alias support to static routes (needs some testing) Ticket #2239
-rw-r--r-- | etc/inc/filter.inc | 37 | ||||
-rw-r--r-- | etc/inc/services.inc | 12 | ||||
-rw-r--r-- | etc/inc/system.inc | 5 | ||||
-rw-r--r-- | etc/inc/util.inc | 36 | ||||
-rwxr-xr-x | usr/local/www/system_routes_edit.php | 96 |
5 files changed, 136 insertions, 50 deletions
diff --git a/etc/inc/filter.inc b/etc/inc/filter.inc index 90502d3..dc7ba96 100644 --- a/etc/inc/filter.inc +++ b/etc/inc/filter.inc @@ -743,11 +743,9 @@ function filter_get_direct_networks_list() { $networks_arr[] = $subnet; } } - if(is_array($config['staticroutes']['route'])) { - foreach($config['staticroutes']['route'] as $netent) { - if(is_ipaddr($netent['network'])) { - $networks_arr[] = $netent['network']; - } + foreach(get_staticroutes(true) as $netent) { + if(is_subnet($netent)) { + $networks_arr[] = $netent; } } if(!empty($networks_arr)) { @@ -1376,17 +1374,15 @@ function filter_nat_rules_generate() { $tonathosts = ""; $numberofnathosts = 0; - if(is_array($config['staticroutes']['route'])) { - foreach ($config['staticroutes']['route'] as $route) { - $netip = explode("/", $route['network']); - if (isset($GatewaysList[$route['gateway']])) { - $gateway =& $GatewaysList[$route['gateway']]; - $gatewayip = $gateway['gateway']; - $interfacegw = $gateway['interface']; - if(!interface_has_gateway($gateway['interface']) && is_private_ip($netip[0])) { - $numberofnathosts++; - $tonathosts .= "{$route['network']} "; - } + foreach (get_staticroutes() as $route) { + $netip = explode("/", $route['network']); + if (isset($GatewaysList[$route['gateway']])) { + $gateway =& $GatewaysList[$route['gateway']]; + $gatewayip = $gateway['gateway']; + $interfacegw = $gateway['interface']; + if(!interface_has_gateway($gateway['interface']) && is_private_ip($netip[0])) { + $numberofnathosts++; + $tonathosts .= "{$route['network']} "; } } } @@ -1690,6 +1686,11 @@ function filter_generate_user_rule_arr($rule) { return $ret; } +function filter_expand_alias_array($alias_name) { + $expansion = filter_expand_alias($alias_name); + return explode(" ", preg_replace('/\s+/', ' ', trim($expansion))); +} + function filter_generate_address(& $rule, $target = "source", $isnat = false) { global $FilterIflist, $config; $src = ""; @@ -2459,8 +2460,8 @@ EOD; * interface in question to avoid problems with complicated routing * topologies */ - if(isset($config['filter']['bypassstaticroutes']) && is_array($config['staticroutes']['route']) && count($config['staticroutes']['route'])) { - foreach ($config['staticroutes']['route'] as $route) { + if(isset($config['filter']['bypassstaticroutes'])) { + foreach (get_staticroutes() as $route) { $friendly = $GatewaysList[$route['gateway']]['friendlyiface']; if(is_array($FilterIflist[$friendly])) { $oc = $FilterIflist[$friendly]; diff --git a/etc/inc/services.inc b/etc/inc/services.inc index 057f361..db4677f 100644 --- a/etc/inc/services.inc +++ b/etc/inc/services.inc @@ -503,13 +503,11 @@ function services_dhcrelay_configure() { } } if (!isset($destif)) { - if (is_array($config['staticroutes']['route'])) { - foreach ($config['staticroutes']['route'] as $rtent) { - if (ip_in_subnet($srvip, $rtent['network'])) { - $a_gateways = return_gateways_array(true); - $destif = $a_gateways[$rtent['gateway']]['interface']; - break; - } + foreach (get_staticroutes() as $rtent) { + if (ip_in_subnet($srvip, $rtent['network'])) { + $a_gateways = return_gateways_array(true); + $destif = $a_gateways[$rtent['gateway']]['interface']; + break; } } } diff --git a/etc/inc/system.inc b/etc/inc/system.inc index dc09526..d6a2790 100644 --- a/etc/inc/system.inc +++ b/etc/inc/system.inc @@ -376,10 +376,11 @@ function system_routing_configure($interface = "") { } } - if (is_array($config['staticroutes']['route'])) { + $static_routes = get_staticroutes(); + if (count($static_routes)) { $gateways_arr = return_gateways_array(); - foreach ($config['staticroutes']['route'] as $rtent) { + foreach ($static_routes as $rtent) { $gatewayip = ""; if (empty($gateways_arr[$rtent['gateway']])) { log_error("Static Routes: Gateway IP could not be found for {$rtent['network']}"); diff --git a/etc/inc/util.inc b/etc/inc/util.inc index 507eb0a..1646827 100644 --- a/etc/inc/util.inc +++ b/etc/inc/util.inc @@ -1503,4 +1503,40 @@ function array_merge_recursive_unique($array0, $array1) { return $result; } +function get_staticroutes($returnsubnetsonly = false) { + global $config; + require_once('filter.inc'); + $allstaticroutes = array(); + $allsubnets = array(); + + /* Bail if there are no routes, but return an array always so callers don't have to check. */ + if (!is_array($config['staticroutes']['route'])) + return array(); + + /* Loop through routes and expand aliases as we find them. */ + foreach ($config['staticroutes']['route'] as $route) { + if (is_alias($route['network'])) { + $subnets = filter_expand_alias_array($route['network']); + foreach ($subnets as $net) { + if (is_ipaddr($net)) + $net .= "/32"; + /* This must be a hostname, we can't use it. */ + if (!is_subnet($net)) + continue; + $temproute = $route; + $temproute['network'] = $net; + $allstaticroutes[] = $temproute; + $allsubnets[] = $net; + } + } elseif (is_subnet($route['network'])) { + $allstaticroutes[] = $route; + $allsubnets[] = $route['network']; + } + } + if ($returnsubnetsonly) { + return $allsubnets; + } else { + return $allstaticroutes; + } +} ?> diff --git a/usr/local/www/system_routes_edit.php b/usr/local/www/system_routes_edit.php index 3ca0de9..8415c36 100755 --- a/usr/local/www/system_routes_edit.php +++ b/usr/local/www/system_routes_edit.php @@ -92,7 +92,7 @@ if ($_POST) { do_input_validation($_POST, $reqdfields, $reqdfieldsn, &$input_errors); - if (($_POST['network'] && !is_ipaddr($_POST['network']))) { + if (($_POST['network'] && !is_ipaddr($_POST['network']) && !is_alias($_POST['network']))) { $input_errors[] = gettext("A valid destination network must be specified."); } if (($_POST['network_subnet'] && !is_numeric($_POST['network_subnet']))) { @@ -104,38 +104,63 @@ if ($_POST) { } /* check for overlaps */ - $osn = gen_subnet($_POST['network'], $_POST['network_subnet']) . "/" . $_POST['network_subnet']; - foreach ($a_routes as $route) { - if (isset($id) && ($a_routes[$id]) && ($a_routes[$id] === $route)) - continue; + $current_targets = get_staticroutes(true); + $new_targets = array(); + if (is_ipaddr($_POST['network'])) { + $osn = gen_subnet($_POST['network'], $_POST['network_subnet']) . "/" . $_POST['network_subnet']; + $new_targets[] = $osn; + } elseif (is_alias($_POST['network'])) { + $osn = $_POST['network']; + foreach (filter_expand_alias_array($_POST['network']) as $tgt) { + if (is_ipaddr($tgt)) + $tgt .= "/32"; + if (!is_subnet($tgt)) + continue; + $new_targets[] = $tgt; + } + } - if ($route['network'] == $osn) { - $input_errors[] = gettext("A route to this destination network already exists."); - break; + if (!isset($id)) + $id = count($a_routes); + $oroute = $a_routes[$id]; + if (!empty($oroute)) { + $old_targets = array(); + if (is_alias($oroute['network'])) { + foreach (filter_expand_alias_array($oroute['network']) as $tgt) { + if (is_ipaddr($tgt)) + $tgt .= "/32"; + if (!is_subnet($tgt)) + continue; + $old_targets[] = $tgt; + } + } else { + $old_targets[] = $oroute['network']; } } + $overlaps = array_intersect($current_targets, $new_targets); + $overlaps = array_diff($overlaps, $old_targets); + if (count($overlaps)) { + $input_errors[] = gettext("A route to these destination networks already exists") . ": " . implode(", ", $overlaps); + } + if (!$input_errors) { $route = array(); $route['network'] = $osn; $route['gateway'] = $_POST['gateway']; $route['descr'] = $_POST['descr']; - if (!isset($id)) - $id = count($a_routes); - if (file_exists("{$g['tmp_path']}/.system_routes.apply")) - $toapplylist = unserialize(file_get_contents("{$g['tmp_path']}/.system_routes.apply")); - else - $toapplylist = array(); - $oroute = $a_routes[$id]; - + if (file_exists("{$g['tmp_path']}/.system_routes.apply")) + $toapplylist = unserialize(file_get_contents("{$g['tmp_path']}/.system_routes.apply")); + else + $toapplylist = array(); $a_routes[$id] = $route; if (!empty($oroute)) { - $osn = explode('/', $oroute['network']); - $sn = explode('/', $route['network']); - if ($oroute['network'] <> $route['network']) - $toapplylist[] = "/sbin/route delete {$oroute['network']}"; + $delete_targets = array_diff($old_targets, $new_targets); + if (count($delete_targets)) + foreach ($delete_targets as $dts) + $toapplylist[] = "/sbin/route delete {$dts}"; } file_put_contents("{$g['tmp_path']}/.system_routes.apply", serialize($toapplylist)); staticroutes_sort(); @@ -151,11 +176,14 @@ if ($_POST) { $pgtitle = array(gettext("System"),gettext("Static Routes"),gettext("Edit route")); include("head.inc"); - ?> <body link="#0000CC" vlink="#0000CC" alink="#0000CC"> -<?php include("fbegin.inc"); ?> +<script type="text/javascript" src="/javascript/autosuggest.js"> +</script> +<script type="text/javascript" src="/javascript/suggestions.js"> +</script> +<?php include("fbegin.inc");?> <?php if ($input_errors) print_input_errors($input_errors); ?> <form action="system_routes_edit.php" method="post" name="iform" id="iform"> <table width="100%" border="0" cellpadding="6" cellspacing="0"> @@ -165,7 +193,7 @@ include("head.inc"); <tr> <td width="22%" valign="top" class="vncellreq"><?=gettext("Destination network"); ?></td> <td width="78%" class="vtable"> - <input name="network" type="text" class="formfld unknown" id="network" size="20" value="<?=htmlspecialchars($pconfig['network']);?>"> + <input name="network" type="text" class="formfldalias" id="network" size="20" value="<?=htmlspecialchars($pconfig['network']);?>"> / <select name="network_subnet" class="formselect" id="network_subnet"> <?php for ($i = 32; $i >= 1; $i--): ?> @@ -329,6 +357,28 @@ include("head.inc"); report_failure(); } } + <?php + $isfirst = 0; + $aliases = ""; + $addrisfirst = 0; + $aliasesaddr = ""; + if($config['aliases']['alias'] <> "" and is_array($config['aliases']['alias'])) + foreach($config['aliases']['alias'] as $alias_name) { + switch ($alias_name['type']) { + case "host": + case "network": + if($addrisfirst == 1) $aliasesaddr .= ","; + $aliasesaddr .= "'" . $alias_name['name'] . "'"; + $addrisfirst = 1; + break; + default: + break; + } + } + ?> + var addressarray=new Array(<?php echo $aliasesaddr; ?>); + var oTextbox1 = new AutoSuggestControl(document.getElementById("network"), new StateSuggestions(addressarray)); + </script> <?php include("fend.inc"); ?> </body> |