1
|
<?php
|
2
|
/*
|
3
|
* firewall_nat.php
|
4
|
*
|
5
|
* part of pfSense (https://www.pfsense.org)
|
6
|
* Copyright (c) 2004-2013 BSD Perimeter
|
7
|
* Copyright (c) 2013-2016 Electric Sheep Fencing
|
8
|
* Copyright (c) 2014-2024 Rubicon Communications, LLC (Netgate)
|
9
|
* All rights reserved.
|
10
|
*
|
11
|
* originally based on m0n0wall (http://m0n0.ch/wall)
|
12
|
* Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
|
13
|
* All rights reserved.
|
14
|
*
|
15
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
16
|
* you may not use this file except in compliance with the License.
|
17
|
* You may obtain a copy of the License at
|
18
|
*
|
19
|
* http://www.apache.org/licenses/LICENSE-2.0
|
20
|
*
|
21
|
* Unless required by applicable law or agreed to in writing, software
|
22
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
23
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
24
|
* See the License for the specific language governing permissions and
|
25
|
* limitations under the License.
|
26
|
*/
|
27
|
|
28
|
##|+PRIV
|
29
|
##|*IDENT=page-firewall-nat-portforward
|
30
|
##|*NAME=Firewall: NAT: Port Forward
|
31
|
##|*DESCR=Allow access to the 'Firewall: NAT: Port Forward' page.
|
32
|
##|*MATCH=firewall_nat.php*
|
33
|
##|-PRIV
|
34
|
|
35
|
require_once("guiconfig.inc");
|
36
|
require_once("util.inc");
|
37
|
require_once("functions.inc");
|
38
|
require_once("filter.inc");
|
39
|
require_once("shaper.inc");
|
40
|
require_once("itemid.inc");
|
41
|
require_once("firewall_nat.inc");
|
42
|
|
43
|
init_config_arr(array('nat', 'rule'));
|
44
|
$a_nat = &$config['nat']['rule'];
|
45
|
$rdr_lcltype_flags = [SPECIALNET_IFADDR];
|
46
|
$rdr_srctype_flags = [SPECIALNET_ANY, SPECIALNET_CLIENTS, SPECIALNET_IFADDR, SPECIALNET_IFNET];
|
47
|
$rdr_dsttype_flags = [SPECIALNET_ANY, SPECIALNET_SELF, SPECIALNET_CLIENTS, SPECIALNET_IFADDR, SPECIALNET_IFNET, SPECIALNET_VIPS];
|
48
|
|
49
|
// Process $_POST/$_REQUEST =======================================================================
|
50
|
if ($_REQUEST['savemsg']) {
|
51
|
$savemsg = $_REQUEST['savemsg'];
|
52
|
}
|
53
|
|
54
|
if (array_key_exists('order-store', $_REQUEST) && have_natpfruleint_access($natent['interface'])) {
|
55
|
reorderNATrules($_POST);
|
56
|
} else if ($_POST['apply'] && have_natpfruleint_access($natent['interface'])) {
|
57
|
$retval = applyNATrules();
|
58
|
} else if (($_POST['act'] == "del" || isset($_POST['del_x'])) && have_natpfruleint_access($natent['interface'])) {
|
59
|
if ($a_nat[$_POST['id']] || (is_array($_POST['rule']) && count($_POST['rule']))) {
|
60
|
deleteNATrule($_POST);
|
61
|
}
|
62
|
} elseif (($_POST['act'] == "toggle" || isset($_POST['toggle_x'])) && have_natpfruleint_access($natent['interface'])) {
|
63
|
if ($a_nat[$_POST['id']] || (is_array($_POST['rule']) && count($_POST['rule']))) {
|
64
|
toggleNATrule($_POST);
|
65
|
}
|
66
|
}
|
67
|
|
68
|
// Construct the page =============================================================================
|
69
|
$pgtitle = array(gettext("Firewall"), gettext("NAT"), gettext("Port Forward"));
|
70
|
$pglinks = array("", "@self", "@self");
|
71
|
include("head.inc");
|
72
|
|
73
|
if ($_POST['apply']) {
|
74
|
print_apply_result_box($retval);
|
75
|
}
|
76
|
|
77
|
if (is_subsystem_dirty('natconf') && have_natpfruleint_access($natent['interface'])) {
|
78
|
print_apply_box(gettext('The NAT configuration has been changed.') . '<br />' .
|
79
|
gettext('The changes must be applied for them to take effect.'));
|
80
|
}
|
81
|
|
82
|
$tab_array = array();
|
83
|
$tab_array[] = array(gettext("Port Forward"), true, "firewall_nat.php");
|
84
|
$tab_array[] = array(gettext("1:1"), false, "firewall_nat_1to1.php");
|
85
|
$tab_array[] = array(gettext("Outbound"), false, "firewall_nat_out.php");
|
86
|
$tab_array[] = array(gettext("NPt"), false, "firewall_nat_npt.php");
|
87
|
display_top_tabs($tab_array);
|
88
|
|
89
|
$columns_in_table = 13;
|
90
|
?>
|
91
|
<!-- Allow table to scroll when dragging outside of the display window -->
|
92
|
<style>
|
93
|
.table-responsive {
|
94
|
clear: both;
|
95
|
overflow-x: visible;
|
96
|
margin-bottom: 0px;
|
97
|
}
|
98
|
</style>
|
99
|
|
100
|
<form action="firewall_nat.php" method="post" name="iform">
|
101
|
<div class="panel panel-default">
|
102
|
<div class="panel-heading"><h2 class="panel-title"><?=gettext('Rules')?></h2></div>
|
103
|
<div class="panel-body table-responsive">
|
104
|
<table id="ruletable" class="table table-striped table-hover table-condensed">
|
105
|
<thead>
|
106
|
<tr>
|
107
|
<th style="padding-left:10px;"> <input type="checkbox" id="selectAll" name="selectAll" /></th>
|
108
|
<th><!-- Icon --></th>
|
109
|
<th><!-- Rule type --></th>
|
110
|
<th><?=gettext("Interface")?></th>
|
111
|
<th><?=gettext("Protocol")?></th>
|
112
|
<th><?=gettext("Source Address")?></th>
|
113
|
<th><?=gettext("Source Ports")?></th>
|
114
|
<th><?=gettext("Dest. Address")?></th>
|
115
|
<th><?=gettext("Dest. Ports")?></th>
|
116
|
<th><?=gettext("NAT IP")?></th>
|
117
|
<th><?=gettext("NAT Ports")?></th>
|
118
|
<th><?=gettext("Description")?></th>
|
119
|
<th><?=gettext("Actions")?></th>
|
120
|
</tr>
|
121
|
</thead>
|
122
|
<tbody class='user-entries'>
|
123
|
<?php
|
124
|
|
125
|
$nnats = $i = 0;
|
126
|
$separators = $config['nat']['separator'];
|
127
|
|
128
|
// Get a list of separator rows and use it to call the display separator function only for rows which there are separator(s).
|
129
|
// More efficient than looping through the list of separators on every row.
|
130
|
$seprows = separator_rows($separators);
|
131
|
|
132
|
foreach ($a_nat as $natent):
|
133
|
|
134
|
// Display separator(s) for section beginning at rule n
|
135
|
if ($seprows[$nnats]) {
|
136
|
display_separator($separators, $nnats, $columns_in_table);
|
137
|
}
|
138
|
|
139
|
$localport = $natent['local-port'];
|
140
|
|
141
|
list($dstbeginport, $dstendport) = explode("-", $natent['destination']['port']);
|
142
|
|
143
|
if ($dstendport && is_port($localport)) {
|
144
|
$localendport = $natent['local-port'] + $dstendport - $dstbeginport;
|
145
|
$localport .= '-' . $localendport;
|
146
|
}
|
147
|
|
148
|
$alias = rule_columns_with_alias(
|
149
|
$natent['source']['address'],
|
150
|
pprint_port($natent['source']['port']),
|
151
|
$natent['destination']['address'],
|
152
|
pprint_port($natent['destination']['port']),
|
153
|
$natent['target'],
|
154
|
$localport
|
155
|
);
|
156
|
|
157
|
if (isset($natent['disabled'])) {
|
158
|
$iconfn = "pass_d";
|
159
|
$trclass = 'class="disabled"';
|
160
|
} else {
|
161
|
$iconfn = "pass";
|
162
|
$trclass = '';
|
163
|
}
|
164
|
|
165
|
$specialnets = get_specialnet('', $rdr_lcltype_flags);
|
166
|
if (array_key_exists($natent['target'], $specialnets)) {
|
167
|
$natent['target'] = $specialnets[$natent['target']];
|
168
|
}
|
169
|
?>
|
170
|
|
171
|
<tr id="fr<?=$nnats;?>" <?=$trclass?> onClick="fr_toggle(<?=$nnats;?>)" ondblclick="document.location='firewall_nat_edit.php?id=<?=$i;?>';">
|
172
|
<td >
|
173
|
<?php if (have_natpfruleint_access($natent['interface'])): ?>
|
174
|
<input type="checkbox" id="frc<?=$nnats;?>" onClick="fr_toggle(<?=$nnats;?>)" name="rule[]" value="<?=$i;?>"/>
|
175
|
<?php endif; ?>
|
176
|
</td>
|
177
|
<td>
|
178
|
<?php if (have_natpfruleint_access($natent['interface'])): ?>
|
179
|
<a href="?act=toggle&id=<?=$i?>" usepost>
|
180
|
<i class="fa-solid fa-check" title="<?=gettext("click to toggle enabled/disabled status")?>"></i>
|
181
|
</a>
|
182
|
<?php endif; ?>
|
183
|
<?php if (isset($natent['nordr'])) { ?>
|
184
|
<i class="fa-regular fa-hand text-danger" title="<?=gettext("Negated: This rule excludes NAT from a later rule")?>"></i>
|
185
|
<?php } ?>
|
186
|
</td>
|
187
|
<td>
|
188
|
<?php
|
189
|
if ($natent['associated-rule-id'] == "pass"):
|
190
|
?>
|
191
|
<i class="fa-solid fa-play" title="<?=gettext("All traffic matching this NAT entry is passed")?>"></i>
|
192
|
<?php
|
193
|
elseif (!empty($natent['associated-rule-id'])):
|
194
|
?>
|
195
|
<i class="fa-solid fa-random" title="<?=sprintf(gettext("Firewall rule ID %s is managed by this rule"), htmlspecialchars($natent['associated-rule-id']))?>"></i>
|
196
|
<?php
|
197
|
endif;
|
198
|
?>
|
199
|
</td>
|
200
|
<td>
|
201
|
<?=$textss?>
|
202
|
<?php
|
203
|
if (!$natent['interface']) {
|
204
|
echo htmlspecialchars(convert_friendly_interface_to_friendly_descr("wan"));
|
205
|
} else {
|
206
|
echo htmlspecialchars(convert_friendly_interface_to_friendly_descr($natent['interface']));
|
207
|
}
|
208
|
?>
|
209
|
<?=$textse?>
|
210
|
</td>
|
211
|
|
212
|
<td>
|
213
|
<?=$textss?><?=strtoupper($natent['protocol'])?><?=$textse?>
|
214
|
</td>
|
215
|
|
216
|
<td>
|
217
|
|
218
|
|
219
|
<?php
|
220
|
if (isset($alias['src'])):
|
221
|
?>
|
222
|
<a href="/firewall_aliases_edit.php?id=<?=$alias['src']?>" data-toggle="popover" data-trigger="hover focus" title="<?=gettext('Alias details')?>" data-content="<?=alias_info_popup($alias['src'])?>" data-html="true">
|
223
|
<?php
|
224
|
endif;
|
225
|
?>
|
226
|
<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_address($natent['source'], $rdr_srctype_flags)))?>
|
227
|
<?php
|
228
|
if (isset($alias['src'])):
|
229
|
?>
|
230
|
</a>
|
231
|
<?php
|
232
|
endif;
|
233
|
?>
|
234
|
</td>
|
235
|
<td>
|
236
|
<?php
|
237
|
if (isset($alias['srcport'])):
|
238
|
?>
|
239
|
<a href="/firewall_aliases_edit.php?id=<?=$alias['srcport']?>" data-toggle="popover" data-trigger="hover focus" title="<?=gettext('Alias details')?>" data-content="<?=alias_info_popup($alias['srcport'])?>" data-html="true">
|
240
|
<?php
|
241
|
endif;
|
242
|
?>
|
243
|
<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($natent['source']['port'])))?>
|
244
|
<?php
|
245
|
if (isset($alias['srcport'])):
|
246
|
?>
|
247
|
</a>
|
248
|
<?php
|
249
|
endif;
|
250
|
?>
|
251
|
</td>
|
252
|
|
253
|
<td>
|
254
|
<?php
|
255
|
if (isset($alias['dst'])):
|
256
|
?>
|
257
|
<a href="/firewall_aliases_edit.php?id=<?=$alias['dst']?>" data-toggle="popover" data-trigger="hover focus" title="<?=gettext('Alias details')?>" data-content="<?=alias_info_popup($alias['dst'])?>" data-html="true">
|
258
|
<?php
|
259
|
endif;
|
260
|
?>
|
261
|
<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_address($natent['destination'], $rdr_dsttype_flags)))?>
|
262
|
<?php
|
263
|
if (isset($alias['dst'])):
|
264
|
?>
|
265
|
</a>
|
266
|
<?php
|
267
|
endif;
|
268
|
?>
|
269
|
</td>
|
270
|
<td>
|
271
|
<?php
|
272
|
if (isset($alias['dstport'])):
|
273
|
?>
|
274
|
<a href="/firewall_aliases_edit.php?id=<?=$alias['dstport']?>" data-toggle="popover" data-trigger="hover focus" title="<?=gettext('Alias details')?>" data-content="<?=alias_info_popup($alias['dstport'])?>" data-html="true">
|
275
|
<?php
|
276
|
endif;
|
277
|
?>
|
278
|
<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($natent['destination']['port'])))?>
|
279
|
<?php
|
280
|
if (isset($alias['dstport'])):
|
281
|
?>
|
282
|
</a>
|
283
|
<?php
|
284
|
endif;
|
285
|
?>
|
286
|
</td>
|
287
|
<td>
|
288
|
<?php
|
289
|
if (isset($alias['target'])):
|
290
|
?>
|
291
|
<a href="/firewall_aliases_edit.php?id=<?=$alias['target']?>" data-toggle="popover" data-trigger="hover focus" title="<?=gettext('Alias details')?>" data-content="<?=alias_info_popup($alias['target'])?>" data-html="true" >
|
292
|
<?php
|
293
|
endif;
|
294
|
?>
|
295
|
|
296
|
<?=str_replace('_', '_<wbr>', htmlspecialchars($natent['target']))?>
|
297
|
<?php
|
298
|
if (isset($alias['target'])):
|
299
|
?>
|
300
|
</a>
|
301
|
<?php
|
302
|
endif;
|
303
|
?>
|
304
|
</td>
|
305
|
<td>
|
306
|
<?php
|
307
|
if (isset($alias['targetport'])):
|
308
|
?>
|
309
|
<a href="/firewall_aliases_edit.php?id=<?=$alias['targetport']?>" data-toggle="popover" data-trigger="hover focus" title="<?=gettext('Alias details')?>" data-content="<?=alias_info_popup($alias['targetport'])?>" data-html="true">
|
310
|
<?php
|
311
|
endif;
|
312
|
?>
|
313
|
<?=str_replace('_', '_<wbr>', htmlspecialchars(pprint_port($localport)))?>
|
314
|
<?php
|
315
|
if (isset($alias['targetport'])):
|
316
|
?>
|
317
|
</a>
|
318
|
<?php
|
319
|
endif;
|
320
|
?>
|
321
|
</td>
|
322
|
|
323
|
<td>
|
324
|
<?=htmlspecialchars($natent['descr'])?>
|
325
|
</td>
|
326
|
<td>
|
327
|
<?php if (have_natpfruleint_access($natent['interface'])): ?>
|
328
|
<a class="fa-solid fa-pencil" title="<?=gettext("Edit rule"); ?>" href="firewall_nat_edit.php?id=<?=$i?>"></a>
|
329
|
<a class="fa-regular fa-clone" title="<?=gettext("Add a new NAT based on this one")?>" href="firewall_nat_edit.php?dup=<?=$i?>"></a>
|
330
|
<a class="fa-solid fa-trash-can" title="<?=gettext("Delete rule")?>" href="firewall_nat.php?act=del&id=<?=$i?>" usepost></a>
|
331
|
<?php else: ?>
|
332
|
-
|
333
|
<?php endif; ?>
|
334
|
</td>
|
335
|
</tr>
|
336
|
<?php
|
337
|
$i++;
|
338
|
$nnats++;
|
339
|
|
340
|
endforeach;
|
341
|
|
342
|
// There can be separator(s) after the last rule listed.
|
343
|
if ($seprows[$nnats]) {
|
344
|
display_separator($separators, $nnats, $columns_in_table);
|
345
|
}
|
346
|
?>
|
347
|
</tbody>
|
348
|
</table>
|
349
|
</div>
|
350
|
</div>
|
351
|
|
352
|
<?php if (have_natpfruleint_access($natent['interface'])): ?>
|
353
|
<nav class="action-buttons">
|
354
|
<a href="firewall_nat_edit.php?after=-1" class="btn btn-sm btn-success" title="<?=gettext('Add rule to the top of the list')?>">
|
355
|
<i class="fa-solid fa-turn-up icon-embed-btn"></i>
|
356
|
<?=gettext('Add')?>
|
357
|
</a>
|
358
|
<a href="firewall_nat_edit.php" class="btn btn-sm btn-success" title="<?=gettext('Add rule to the end of the list')?>">
|
359
|
<i class="fa-solid fa-turn-down icon-embed-btn"></i>
|
360
|
<?=gettext('Add')?>
|
361
|
</a>
|
362
|
<button id="del_x" name="del_x" type="submit" class="btn btn-danger btn-sm" disabled title="<?=gettext('Delete selected rules')?>">
|
363
|
<i class="fa-solid fa-trash-can icon-embed-btn"></i>
|
364
|
<?=gettext("Delete"); ?>
|
365
|
</button>
|
366
|
<button id="toggle_x" name="toggle_x" type="submit" class="btn btn-primary btn-sm" disabled value="<?=gettext("Toggle selected rules"); ?>" title="<?=gettext('Toggle selected rules')?>">
|
367
|
<i class="fa-solid fa-ban icon-embed-btn"></i>
|
368
|
<?=gettext("Toggle"); ?>
|
369
|
</button>
|
370
|
<button type="submit" id="order-store" name="order-store" class="btn btn-primary btn-sm" disabled title="<?=gettext('Save rule order')?>">
|
371
|
<i class="fa-solid fa-save icon-embed-btn"></i>
|
372
|
<?=gettext("Save")?>
|
373
|
</button>
|
374
|
<button type="submit" id="addsep" name="addsep" class="btn btn-sm btn-warning" title="<?=gettext('Add separator')?>">
|
375
|
<i class="fa-solid fa-plus icon-embed-btn"></i>
|
376
|
<?=gettext("Separator")?>
|
377
|
</button>
|
378
|
</nav>
|
379
|
<?php endif; ?>
|
380
|
</form>
|
381
|
|
382
|
<script type="text/javascript">
|
383
|
//<![CDATA[
|
384
|
//Need to create some variables here so that jquery/pfSenseHelpers.js can read them
|
385
|
iface = "<?=strtolower($if)?>";
|
386
|
cncltxt = '<?=gettext("Cancel")?>';
|
387
|
svtxt = '<?=gettext("Save")?>';
|
388
|
svbtnplaceholder = '<?=gettext("Enter a description, Save, then drag to final location.")?>';
|
389
|
configsection = "nat";
|
390
|
dirty = false;
|
391
|
|
392
|
events.push(function() {
|
393
|
|
394
|
<?php if(!isset($config['system']['webgui']['roworderdragging'])): ?>
|
395
|
// Make rules sortable
|
396
|
$('table tbody.user-entries').sortable({
|
397
|
cursor: 'grabbing',
|
398
|
update: function(event, ui) {
|
399
|
$('#order-store').removeAttr('disabled');
|
400
|
dirty = true;
|
401
|
reindex_rules(ui.item.parent('tbody'));
|
402
|
dirty = true;
|
403
|
}
|
404
|
});
|
405
|
<?php endif; ?>
|
406
|
|
407
|
// Check all of the rule checkboxes so that their values are posted
|
408
|
$('#order-store').click(function () {
|
409
|
$('[id^=frc]').prop('checked', true);
|
410
|
|
411
|
// Save the separator bar configuration
|
412
|
save_separators();
|
413
|
|
414
|
// Suppress the "Do you really want to leave the page" message
|
415
|
saving = true;
|
416
|
|
417
|
});
|
418
|
|
419
|
$('[id^=fr]').click(function () {
|
420
|
buttonsmode('frc', ['del_x', 'toggle_x']);
|
421
|
});
|
422
|
|
423
|
// Globals
|
424
|
saving = false;
|
425
|
dirty = false;
|
426
|
|
427
|
// provide a warning message if the user tries to change page before saving
|
428
|
// Unfortunately the custom message is not supported in modern browsers, but he user wil lat
|
429
|
// least see a generic warning message
|
430
|
$(window).bind('beforeunload', function(){
|
431
|
if (!saving && dirty) {
|
432
|
return ("<?=gettext('One or more Port Forward rules have been moved but have not yet been saved')?>");
|
433
|
} else {
|
434
|
return undefined;
|
435
|
}
|
436
|
});
|
437
|
|
438
|
$('#selectAll').click(function() {
|
439
|
var checkedStatus = this.checked;
|
440
|
$('#ruletable tbody tr').find('td:first :checkbox').each(function() {
|
441
|
$(this).prop('checked', checkedStatus);
|
442
|
});
|
443
|
buttonsmode('frc', ['del_x', 'toggle_x']);
|
444
|
});
|
445
|
});
|
446
|
//]]>
|
447
|
</script>
|
448
|
<?php
|
449
|
|
450
|
if (count($a_nat) > 0) {
|
451
|
?>
|
452
|
<!-- Legend -->
|
453
|
<div>
|
454
|
<dl class="dl-horizontal responsive">
|
455
|
<dt><?=gettext('Legend')?></dt> <dd></dd>
|
456
|
<dt><i class="fa-solid fa-play"></i></dt> <dd><?=gettext('Pass')?></dd>
|
457
|
<dt><i class="fa-solid fa-random"></i></dt> <dd><?=gettext('Linked rule')?></dd>
|
458
|
</dl>
|
459
|
</div>
|
460
|
|
461
|
<?php
|
462
|
}
|
463
|
|
464
|
include("foot.inc");
|