firewall_virtual_ip.php 11.5 KB
Newer Older
Ad Schellevis's avatar
Ad Schellevis committed
1
<?php
2

Ad Schellevis's avatar
Ad Schellevis committed
3
/*
4
	Copyright (C) 2014-2015 Deciso B.V.
Ad Schellevis's avatar
Ad Schellevis committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
	Copyright (C) 2005 Bill Marquette <bill.marquette@gmail.com>.
	Copyright (C) 2003-2005 Manuel Kasper <mk@neon1.net>.
	Copyright (C) 2004-2005 Scott Ullrich <geekgod@pfsense.com>.
	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.
*/

32
require_once("guiconfig.inc");
33
require_once("functions.inc");
Ad Schellevis's avatar
Ad Schellevis committed
34 35 36 37 38 39 40 41 42 43 44 45
require_once("filter.inc");
require_once("shaper.inc");

if (!is_array($config['virtualip']['vip'])) {
	$config['virtualip']['vip'] = array();
}
$a_vip = &$config['virtualip']['vip'];

if ($_POST) {
	$pconfig = $_POST;

	if ($_POST['apply']) {
46 47
		if (file_exists('/tmp/.firewall_virtual_ip.apply')) {
                        $toapplylist = unserialize(file_get_contents('/tmp/.firewall_virtual_ip.apply'));
Ad Schellevis's avatar
Ad Schellevis committed
48 49 50 51
			foreach ($toapplylist as $vid => $ovip) {
				if (!empty($ovip))
					interface_vip_bring_down($ovip);
				if ($a_vip[$vid]) {
52 53 54 55 56 57 58 59 60 61 62
					switch ($a_vip[$vid]['mode']) {
					case "ipalias":
						interface_ipalias_configure($a_vip[$vid]);
						break;
					case "proxyarp":
						interface_proxyarp_configure($a_vip[$vid]['interface']);
						break;
					case "carp":
						interface_carp_configure($a_vip[$vid]);
						break;
					default:
Ad Schellevis's avatar
Ad Schellevis committed
63 64
						break;
					}
65 66
				}
			}
67
			@unlink('/tmp/.firewall_virtual_ip.apply');
Ad Schellevis's avatar
Ad Schellevis committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
		}
		$retval = 0;
		$retval |= filter_configure();
		$savemsg = get_std_save_message($retval);

		clear_subsystem_dirty('vip');
	}
}

if ($_GET['act'] == "del") {
	if ($a_vip[$_GET['id']]) {
		/* make sure no inbound NAT mappings reference this entry */
		if (is_array($config['nat']['rule'])) {
			foreach ($config['nat']['rule'] as $rule) {
				if($rule['destination']['address'] <> "") {
					if ($rule['destination']['address'] == $a_vip[$_GET['id']]['subnet']) {
						$input_errors[] = gettext("This entry cannot be deleted because it is still referenced by at least one NAT mapping.");
						break;
					}
				}
			}
		}

		if (is_ipaddrv6($a_vip[$_GET['id']]['subnet'])) {
			$is_ipv6 = true;
			$subnet = gen_subnetv6($a_vip[$_GET['id']]['subnet'], $a_vip[$_GET['id']]['subnet_bits']);
			$if_subnet_bits = get_interface_subnetv6($a_vip[$_GET['id']]['interface']);
			$if_subnet = gen_subnetv6(get_interface_ipv6($a_vip[$_GET['id']]['interface']), $if_subnet_bits);
		} else {
			$is_ipv6 = false;
			$subnet = gen_subnet($a_vip[$_GET['id']]['subnet'], $a_vip[$_GET['id']]['subnet_bits']);
			$if_subnet_bits = get_interface_subnet($a_vip[$_GET['id']]['interface']);
			$if_subnet = gen_subnet(get_interface_ip($a_vip[$_GET['id']]['interface']), $if_subnet_bits);
		}

		$subnet .= "/" . $a_vip[$_GET['id']]['subnet_bits'];
		$if_subnet .= "/" . $if_subnet_bits;

		if (is_array($config['gateways']['gateway_item']))
			foreach($config['gateways']['gateway_item'] as $gateway) {
				if ($a_vip[$_GET['id']]['interface'] != $gateway['interface'])
					continue;
				if ($is_ipv6 && $gateway['ipprotocol'] == 'inet')
					continue;
				if (!$is_ipv6 && $gateway['ipprotocol'] == 'inet6')
					continue;
				if (ip_in_subnet($gateway['gateway'], $if_subnet))
					continue;

				if (ip_in_subnet($gateway['gateway'], $subnet)) {
					$input_errors[] = gettext("This entry cannot be deleted because it is still referenced by at least one Gateway.");
					break;
				}
			}

		if ($a_vip[$_GET['id']]['mode'] == "ipalias") {
			$subnet = gen_subnet($a_vip[$_GET['id']]['subnet'], $a_vip[$_GET['id']]['subnet_bits']) . "/" . $a_vip[$_GET['id']]['subnet_bits'];
			$found_if = false;
			$found_carp = false;
			$found_other_alias = false;

			if ($subnet == $if_subnet)
				$found_if = true;
131

Ad Schellevis's avatar
Ad Schellevis committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
			$vipiface = $a_vip[$_GET['id']]['interface'];
			foreach ($a_vip as $vip_id => $vip) {
				if ($vip_id == $_GET['id'])
					continue;

				if ($vip['interface'] == $vipiface && ip_in_subnet($vip['subnet'], $subnet))
					if ($vip['mode'] == "carp")
						$found_carp = true;
					else if ($vip['mode'] == "ipalias")
						$found_other_alias = true;
			}

			if ($found_carp === true && $found_other_alias === false && $found_if === false)
				$input_errors[] = gettext("This entry cannot be deleted because it is still referenced by a CARP IP with the description") . " {$vip['descr']}.";
		}
147

Ad Schellevis's avatar
Ad Schellevis committed
148
		if (!$input_errors) {
149
			if (session_status() == PHP_SESSION_NONE) {
Ad Schellevis's avatar
Ad Schellevis committed
150
				session_start();
151
                        }
Ad Schellevis's avatar
Ad Schellevis committed
152 153 154 155 156
			$user = getUserEntry($_SESSION['Username']);
			if (is_array($user) && userHasPrivilege($user, "user-config-readonly")) {
				header("Location: firewall_virtual_ip.php");
				exit;
			}
157
			session_write_close();
Ad Schellevis's avatar
Ad Schellevis committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

			// Special case since every proxyarp vip is handled by the same daemon.
			if ($a_vip[$_GET['id']]['mode'] == "proxyarp") {
				$viface = $a_vip[$_GET['id']]['interface'];
				unset($a_vip[$_GET['id']]);
				interface_proxyarp_configure($viface);
			} else {
				interface_vip_bring_down($a_vip[$_GET['id']]);
				unset($a_vip[$_GET['id']]);
			}
			if (count($config['virtualip']['vip']) == 0)
				unset($config['virtualip']['vip']);
			write_config();
			header("Location: firewall_virtual_ip.php");
			exit;
		}
	}
} else if ($_GET['changes'] == "mods" && is_numericint($_GET['id']))
	$id = $_GET['id'];

$pgtitle = array(gettext("Firewall"),gettext("Virtual IP Addresses"));
include("head.inc");

Ad Schellevis's avatar
Ad Schellevis committed
181
$main_buttons = array(
182
	array('href'=>'firewall_virtual_ip_edit.php', 'label'=>'Add'),
Ad Schellevis's avatar
Ad Schellevis committed
183 184
);

Ad Schellevis's avatar
Ad Schellevis committed
185
?>
Ad Schellevis's avatar
Ad Schellevis committed
186
<body>
Ad Schellevis's avatar
Ad Schellevis committed
187
<?php include("fbegin.inc"); ?>
Ad Schellevis's avatar
Ad Schellevis committed
188 189

	<section class="page-content-main">
190
		<div class="container-fluid">
Ad Schellevis's avatar
Ad Schellevis committed
191
			<div class="row">
192 193 194

				<?php
					if ($input_errors)
Ad Schellevis's avatar
Ad Schellevis committed
195 196
						print_input_errors($input_errors);
					else
197 198
					if ($savemsg)
						print_info_box($savemsg);
Ad Schellevis's avatar
Ad Schellevis committed
199 200 201 202
					else
					if (is_subsystem_dirty('vip'))
						print_info_box_np(gettext("The VIP configuration has been changed.")."<br />".gettext("You must apply the changes in order for them to take effect."));
				?>
203

Ad Schellevis's avatar
Ad Schellevis committed
204
			    <section class="col-xs-12">
205 206 207


					 <?php
Ad Schellevis's avatar
Ad Schellevis committed
208 209 210 211 212 213
						        /* active tabs */
						        $tab_array = array();
						        $tab_array[] = array(gettext("Virtual IPs"), true, "firewall_virtual_ip.php");
						        $tab_array[] = array(gettext("CARP Settings"), false, "system_hasync.php");
						        display_top_tabs($tab_array);
						  ?>
214 215 216 217 218


						<div class="tab-content content-box col-xs-12">


Ad Schellevis's avatar
Ad Schellevis committed
219
		                        <form action="firewall_virtual_ip.php" method="post" name="iform" id="iform">
220 221
						<input type="hidden" id="id" name="id" value="<?php echo htmlspecialchars($id); ?>" />

Ad Schellevis's avatar
Ad Schellevis committed
222 223
		                        <div class="table-responsive">
			                        <table class="table table-striped table-sort">
224
			                        <thead>
Ad Schellevis's avatar
Ad Schellevis committed
225 226 227 228 229 230 231
						                <tr>
						                  <td width="30%" class="listhdrr"><?=gettext("Virtual IP address");?></td>
						                  <td width="10%" class="listhdrr"><?=gettext("Interface");?></td>
						                  <td width="10%" class="listhdrr"><?=gettext("Type");?></td>
						                  <td width="40%" class="listhdr"><?=gettext("Description");?></td>
						                  <td width="10%" class="list"></td>
										</tr>
232 233
			                        </thead>
			                        <tbody>
Ad Schellevis's avatar
Ad Schellevis committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
								<?php
									$interfaces = get_configured_interface_with_descr(false, true);
									$interfaces['lo0'] = "Localhost";
								?>
									  <?php $i = 0; foreach ($a_vip as $vipent): ?>
									  <?php if($vipent['subnet'] <> "" or $vipent['range'] <> "" or
									        $vipent['subnet_bits'] <> "" or (isset($vipent['range']['from']) && $vipent['range']['from'] <> "")): ?>
						                <tr>
						                  <td class="listlr" ondblclick="document.location='firewall_virtual_ip_edit.php?id=<?=$i;?>';">
											<?php	if (($vipent['type'] == "single") || ($vipent['type'] == "network"))
														if($vipent['subnet_bits'])
															echo "{$vipent['subnet']}/{$vipent['subnet_bits']}";
													if ($vipent['type'] == "range")
														echo "{$vipent['range']['from']}-{$vipent['range']['to']}";
											?>
											<?php if($vipent['mode'] == "carp") echo " (vhid {$vipent['vhid']})"; ?>
						                  </td>
						                  <td class="listr" ondblclick="document.location='firewall_virtual_ip_edit.php?id=<?=$i;?>';">
						                    <?=htmlspecialchars($interfaces[$vipent['interface']]);?>&nbsp;
						                  </td>
						                  <td class="listr" align="center" ondblclick="document.location='firewall_virtual_ip_edit.php?id=<?=$i;?>';">
						                    <?php if($vipent['mode'] == "proxyarp") echo "Proxy ARP"; elseif($vipent['mode'] == "carp") echo "CARP"; elseif($vipent['mode'] == "other") echo "Other"; elseif($vipent['mode'] == "ipalias") echo "IP Alias";?>
						                  </td>
						                  <td class="listbg" ondblclick="document.location='firewall_virtual_ip_edit.php?id=<?=$i;?>';">
						                    <?=htmlspecialchars($vipent['descr']);?>&nbsp;
						                  </td>
						                  <td class="list nowrap">
						                    <table border="0" cellspacing="0" cellpadding="1" summary="icons">
						                      <tr>
						                        <td valign="middle">
							                         <a href="firewall_virtual_ip_edit.php?id=<?=$i;?>" class="btn btn-default"><span class="glyphicon glyphicon-edit" title="<?=gettext("Edit");?>"></span></a>
265

Ad Schellevis's avatar
Ad Schellevis committed
266 267 268 269 270 271 272 273
													<a href="firewall_virtual_ip.php?act=del&amp;tab=<?=$tab;?>&amp;id=<?=$i;?>" class="btn btn-default"  onclick="return confirm('<?=gettext("Do you really want to delete this entry?");?>')"><span class="glyphicon glyphicon-remove"></span></a>
												</td>
						                      </tr>
						                    </table>
						                  </td>
						                </tr>
										<?php endif; ?>
										<?php $i++; endforeach; ?>
274
			                        </tbody>
Ad Schellevis's avatar
Ad Schellevis committed
275 276
									</table>
		                        </div>
Ad Schellevis's avatar
Ad Schellevis committed
277
		                        <div class="container-fluid">
Ad Schellevis's avatar
Ad Schellevis committed
278
		                        <p><span class="vexpl"><span class="text-danger"><strong><?=gettext("Note:");?><br />
279 280
                      </strong></span><?=gettext("The virtual IP addresses defined on this page may be used in");?><a href="firewall_nat.php"> <?=gettext("NAT"); ?> </a><?=gettext("mappings.");?><br />
                      <?=gettext("You can check the status of your CARP Virtual IPs and interfaces ");?><a href="carp_status.php"><?=gettext("here");?></a>.</span></p>
Ad Schellevis's avatar
Ad Schellevis committed
281
		                        </div>
Ad Schellevis's avatar
Ad Schellevis committed
282 283 284 285 286 287
		                        </form>
						</div>
			    </section>
			</div>
		</div>
	</section>
288 289

<?php include("foot.inc"); ?>