Project

General

Profile

Download (50.2 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	firewall_rules_edit.php
5
	part of pfSense (https://www.pfsense.org)
6
	Copyright (C) 2005 Scott Ullrich (sullrich@gmail.com)
7
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
8

    
9
	originally part of m0n0wall (http://m0n0.ch/wall)
10
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
11
	All rights reserved.
12

    
13
	Redistribution and use in source and binary forms, with or without
14
	modification, are permitted provided that the following conditions are met:
15

    
16
	1. Redistributions of source code must retain the above copyright notice,
17
	   this list of conditions and the following disclaimer.
18

    
19
	2. Redistributions in binary form must reproduce the above copyright
20
	   notice, this list of conditions and the following disclaimer in the
21
	   documentation and/or other materials provided with the distribution.
22

    
23
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
27
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
	POSSIBILITY OF SUCH DAMAGE.
33
*/
34
/*
35
	pfSense_MODULE:	filter
36
*/
37

    
38
##|+PRIV
39
##|*IDENT=page-firewall-rules-edit
40
##|*NAME=Firewall: Rules: Edit page
41
##|*DESCR=Allow access to the 'Firewall: Rules: Edit' page.
42
##|*MATCH=firewall_rules_edit.php*
43
##|-PRIV
44

    
45
require("guiconfig.inc");
46
require_once("filter.inc");
47
require("shaper.inc");
48

    
49
$referer = (isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '/firewall_rules.php');
50

    
51
function is_posnumericint($arg) {
52
	// Note that to be safe we do not allow any leading zero - "01", "007"
53
	return (is_numericint($arg) && $arg[0] != '0' && $arg > 0);
54
}
55

    
56
function is_aoadv_used($rule_config) {
57
	// Note that the user could set "tag" or "tagged" to the string "0", which is valid but empty().
58
	// And if the user enters "0" in other fields, we want to present an error message, and keep the Advanced Options section open.
59
	if ((isset($rule_config['allowopts'])) ||
60
		(isset($rule_config['disablereplyto'])) ||
61
		($rule_config['tag'] != "") ||
62
		($rule_config['tagged'] != "") ||
63
		($rule_config['max'] != "") ||
64
		($rule_config['max-src-nodes'] != "") ||
65
		($rule_config['max-src-conn'] != "") ||
66
		($rule_config['max-src-states'] != "") ||
67
		($rule_config['max-src-conn-rate'] != "") ||
68
		($rule_config['max-src-conn-rates'] != "") ||
69
		($rule_config['statetimeout'] != ""))
70
		return true;
71
	return false;
72
}
73

    
74
$ostypes = array();
75
exec('/sbin/pfctl -s osfp | /usr/bin/tr \'\t\' \' \'', $ostypes);
76

    
77
if (count($ostypes) > 2) {
78
	// Remove header rows from pfctl output
79
	array_shift($ostypes);
80
	array_shift($ostypes);
81
} else {
82
	// Fall back to a default list
83
	$ostypes = array(
84
		"AIX",
85
		"Linux",
86
		"FreeBSD",
87
		"NetBSD",
88
		"OpenBSD",
89
		"Solaris",
90
		"MacOS",
91
		"Windows",
92
		"Novell",
93
		"NMAP"
94
	);
95
}
96

    
97
$specialsrcdst = explode(" ", "any (self) pptp pppoe l2tp openvpn");
98
$ifdisp = get_configured_interface_with_descr();
99
foreach ($ifdisp as $kif => $kdescr) {
100
	$specialsrcdst[] = "{$kif}";
101
	$specialsrcdst[] = "{$kif}ip";
102
}
103

    
104
if (!is_array($config['filter']['rule'])) {
105
	$config['filter']['rule'] = array();
106
}
107
filter_rules_sort();
108
$a_filter = &$config['filter']['rule'];
109

    
110
if (is_numericint($_GET['id']))
111
	$id = $_GET['id'];
112
if (isset($_POST['id']) && is_numericint($_POST['id']))
113
	$id = $_POST['id'];
114

    
115
if (is_numericint($_GET['after']) || $_GET['after'] == "-1")
116
	$after = $_GET['after'];
117
if (isset($_POST['after']) && (is_numericint($_POST['after']) || $_POST['after'] == "-1"))
118
	$after = $_POST['after'];
119

    
120
if (isset($_GET['dup']) && is_numericint($_GET['dup'])) {
121
		$id = $_GET['dup'];
122
		$after = $_GET['dup'];
123
}
124

    
125
if (isset($id) && $a_filter[$id]) {
126
	$pconfig['interface'] = $a_filter[$id]['interface'];
127

    
128
	if (isset($a_filter[$id]['id']))
129
		$pconfig['ruleid'] = $a_filter[$id]['id'];
130

    
131
	if ( isset($a_filter[$id]['created']) && is_array($a_filter[$id]['created']) )
132
		$pconfig['created'] = $a_filter[$id]['created'];
133

    
134
	if ( isset($a_filter[$id]['updated']) && is_array($a_filter[$id]['updated']) )
135
		$pconfig['updated'] = $a_filter[$id]['updated'];
136

    
137
	if (!isset($a_filter[$id]['type']))
138
		$pconfig['type'] = "pass";
139
	else
140
		$pconfig['type'] = $a_filter[$id]['type'];
141

    
142
	if (isset($a_filter[$id]['floating']) || $if == "FloatingRules") {
143
		$pconfig['floating'] = $a_filter[$id]['floating'];
144
		if (isset($a_filter[$id]['interface']) && $a_filter[$id]['interface'] != "")
145
			$pconfig['interface'] = $a_filter[$id]['interface'];
146
	}
147

    
148
	if (isset($a_filter['floating']))
149
		$pconfig['floating'] = "yes";
150

    
151
	if (isset($a_filter[$id]['direction']))
152
		$pconfig['direction'] = $a_filter[$id]['direction'];
153

    
154
	if (isset($a_filter[$id]['ipprotocol']))
155
		$pconfig['ipprotocol'] = $a_filter[$id]['ipprotocol'];
156

    
157
	if (isset($a_filter[$id]['protocol']))
158
		$pconfig['proto'] = $a_filter[$id]['protocol'];
159
	else
160
		$pconfig['proto'] = "any";
161

    
162
	if ($a_filter[$id]['protocol'] == "icmp")
163
		$pconfig['icmptype'] = $a_filter[$id]['icmptype'];
164

    
165
	address_to_pconfig($a_filter[$id]['source'], $pconfig['src'],
166
		$pconfig['srcmask'], $pconfig['srcnot'],
167
		$pconfig['srcbeginport'], $pconfig['srcendport']);
168

    
169
	if($a_filter[$id]['os'] != "")
170
		$pconfig['os'] = $a_filter[$id]['os'];
171

    
172
	address_to_pconfig($a_filter[$id]['destination'], $pconfig['dst'],
173
		$pconfig['dstmask'], $pconfig['dstnot'],
174
		$pconfig['dstbeginport'], $pconfig['dstendport']);
175

    
176
	if ($a_filter[$id]['dscp'] != "")
177
		$pconfig['dscp'] = $a_filter[$id]['dscp'];
178

    
179
	$pconfig['disabled'] = isset($a_filter[$id]['disabled']);
180
	$pconfig['log'] = isset($a_filter[$id]['log']);
181
	$pconfig['descr'] = $a_filter[$id]['descr'];
182

    
183
	if (isset($a_filter[$id]['tcpflags_any']))
184
		$pconfig['tcpflags_any'] = true;
185
	else {
186
		if (isset($a_filter[$id]['tcpflags1']) && $a_filter[$id]['tcpflags1'] != "")
187
			$pconfig['tcpflags1'] = $a_filter[$id]['tcpflags1'];
188
		if (isset($a_filter[$id]['tcpflags2']) && $a_filter[$id]['tcpflags2'] != "")
189
			$pconfig['tcpflags2'] = $a_filter[$id]['tcpflags2'];
190
	}
191

    
192
	if (isset($a_filter[$id]['tag']) && $a_filter[$id]['tag'] != "")
193
		$pconfig['tag'] = $a_filter[$id]['tag'];
194
	if (isset($a_filter[$id]['tagged']) && $a_filter[$id]['tagged'] != "")
195
		$pconfig['tagged'] = $a_filter[$id]['tagged'];
196
	if (isset($a_filter[$id]['quick']) && $a_filter[$id]['quick'])
197
		$pconfig['quick'] = $a_filter[$id]['quick'];
198
	if (isset($a_filter[$id]['allowopts']))
199
		$pconfig['allowopts'] = true;
200
	if (isset($a_filter[$id]['disablereplyto']))
201
		$pconfig['disablereplyto'] = true;
202

    
203
	/* advanced */
204
	$pconfig['max'] = $a_filter[$id]['max'];
205
	$pconfig['max-src-nodes'] = $a_filter[$id]['max-src-nodes'];
206
	$pconfig['max-src-conn'] = $a_filter[$id]['max-src-conn'];
207
	$pconfig['max-src-states'] = $a_filter[$id]['max-src-states'];
208
	$pconfig['statetype'] = $a_filter[$id]['statetype'];
209
	$pconfig['statetimeout'] = $a_filter[$id]['statetimeout'];
210
	$pconfig['nopfsync'] = isset($a_filter[$id]['nopfsync']);
211

    
212
	/* advanced - nosync */
213
	$pconfig['nosync'] = isset($a_filter[$id]['nosync']);
214

    
215
	/* advanced - new connection per second banning*/
216
	$pconfig['max-src-conn-rate'] = $a_filter[$id]['max-src-conn-rate'];
217
	$pconfig['max-src-conn-rates'] = $a_filter[$id]['max-src-conn-rates'];
218

    
219
	/* Multi-WAN next-hop support */
220
	$pconfig['gateway'] = $a_filter[$id]['gateway'];
221

    
222
	/* Shaper support */
223
	$pconfig['defaultqueue'] = (($a_filter[$id]['ackqueue'] == "none") ? '' : $a_filter[$id]['defaultqueue']);
224
	$pconfig['ackqueue'] = (($a_filter[$id]['ackqueue'] == "none") ? '' : $a_filter[$id]['ackqueue']);
225
	$pconfig['dnpipe'] = (($a_filter[$id]['dnpipe'] == "none") ? '' : $a_filter[$id]['dnpipe']);
226
	$pconfig['pdnpipe'] = (($a_filter[$id]['pdnpipe'] == "none") ? '' : $a_filter[$id]['pdnpipe']);
227
	$pconfig['l7container'] = (($a_filter[$id]['l7container'] == "none") ? '' : $a_filter[$id]['l7container']);
228

    
229
	//schedule support
230
	$pconfig['sched'] = (($a_filter[$id]['sched'] == "none") ? '' : $a_filter[$id]['sched']);
231
	$pconfig['vlanprio'] = (($a_filter[$id]['vlanprio'] == "none") ? '' : $a_filter[$id]['vlanprio']);
232
	$pconfig['vlanprioset'] = (($a_filter[$id]['vlanprioset'] == "none") ? '' : $a_filter[$id]['vlanprioset']);
233
	if (!isset($_GET['dup']) || !is_numericint($_GET['dup']))
234
		$pconfig['associated-rule-id'] = $a_filter[$id]['associated-rule-id'];
235

    
236
	$pconfig['tracker'] = $a_filter[$id]['tracker'];
237

    
238
} else {
239
	/* defaults */
240
	if ($_GET['if'])
241
		$pconfig['interface'] = $_GET['if'];
242
	$pconfig['type'] = "pass";
243
	$pconfig['src'] = "any";
244
	$pconfig['dst'] = "any";
245
}
246
/* Allow the FloatingRules to work */
247
$if = $pconfig['interface'];
248

    
249
if (isset($_GET['dup']) && is_numericint($_GET['dup']))
250
	unset($id);
251

    
252
read_altq_config(); /* XXX: */
253
$qlist =& get_unique_queue_list();
254
read_dummynet_config(); /* XXX: */
255
$dnqlist =& get_unique_dnqueue_list();
256
read_layer7_config();
257
$l7clist =& get_l7_unique_list();
258
$a_gatewaygroups = return_gateway_groups_array();
259

    
260
if ($_POST) {
261
	unset($input_errors);
262

    
263
	if( isset($a_filter[$id]['associated-rule-id']) ) {
264
		$_POST['proto'] = $pconfig['proto'];
265
		if ($pconfig['proto'] == "icmp")
266
			$_POST['icmptype'] = $pconfig['icmptype'];
267
	}
268

    
269
	if (($_POST['ipprotocol'] != "") && ($_POST['gateway'] != "")) {
270
		if(is_array($config['gateways']['gateway_group'])) {
271
			foreach($config['gateways']['gateway_group'] as $gw_group) {
272
				if($gw_group['name'] == $_POST['gateway']) {
273
					$family = $a_gatewaygroups[$_POST['gateway']]['ipprotocol'];
274
					if($_POST['ipprotocol'] == $family) {
275
						continue;
276
					}
277
					if(($_POST['ipprotocol'] == "inet46") && ($_POST['ipprotocol'] != $family)) {
278
						$input_errors[] = gettext("You can not assign a gateway to a rule that applies to IPv4 and IPv6");
279
					}
280
					if(($_POST['ipprotocol'] == "inet6") && ($_POST['ipprotocol'] != $family)) {
281
						$input_errors[] = gettext("You can not assign an IPv4 gateway group on IPv6 Address Family rule");
282
					}
283
					if(($_POST['ipprotocol'] == "inet") && ($_POST['ipprotocol'] != $family)) {
284
						$input_errors[] = gettext("You can not assign an IPv6 gateway group on IPv4 Address Family rule");
285
					}
286
				}
287
			}
288
		}
289
	}
290
	if (($_POST['ipprotocol'] != "") && ($_POST['gateway'] != "") && (is_ipaddr(lookup_gateway_ip_by_name($_POST['gateway'])))) {
291
		if(($_POST['ipprotocol'] == "inet46") && ($_POST['gateway'] != "")) {
292
			$input_errors[] = gettext("You can not assign a gateway to a rule that applies to IPv4 and IPv6");
293
		}
294
		if(($_POST['ipprotocol'] == "inet6") && (!is_ipaddrv6(lookup_gateway_ip_by_name($_POST['gateway'])))) {
295
			$input_errors[] = gettext("You can not assign an IPv4 Gateway to an IPv6 Filter rule");
296
		}
297
		if(($_POST['ipprotocol'] == "inet") && (!is_ipaddrv4(lookup_gateway_ip_by_name($_POST['gateway'])))) {
298
			$input_errors[] = gettext("You can not assign an IPv6 Gateway to an IPv4 Filter rule");
299
		}
300
	}
301

    
302
	if (($_POST['proto'] == "icmp") && ($_POST['icmptype'] != "")){
303
		if($_POST['ipprotocol'] == "inet46")
304
			$input_errors[] =  gettext("You can not assign a ICMP type to a rule that applies to IPv4 and IPv6");
305
	}
306

    
307
	if (($_POST['proto'] != "tcp") && ($_POST['proto'] != "udp") && ($_POST['proto'] != "tcp/udp")) {
308
		$_POST['srcbeginport'] = 0;
309
		$_POST['srcendport'] = 0;
310
		$_POST['dstbeginport'] = 0;
311
		$_POST['dstendport'] = 0;
312
	} else {
313
		if ($_POST['srcbeginport_cust'] && !$_POST['srcbeginport'])
314
			$_POST['srcbeginport'] = trim($_POST['srcbeginport_cust']);
315
		if ($_POST['srcendport_cust'] && !$_POST['srcendport'])
316
			$_POST['srcendport'] = trim($_POST['srcendport_cust']);
317
		if ($_POST['srcbeginport'] == "any") {
318
			$_POST['srcbeginport'] = 0;
319
			$_POST['srcendport'] = 0;
320
		} else {
321
			if (!$_POST['srcendport'])
322
				$_POST['srcendport'] = $_POST['srcbeginport'];
323
		}
324
		if ($_POST['srcendport'] == "any")
325
			$_POST['srcendport'] = $_POST['srcbeginport'];
326

    
327
		if ($_POST['dstbeginport_cust'] && !$_POST['dstbeginport'])
328
			$_POST['dstbeginport'] = trim($_POST['dstbeginport_cust']);
329
		if ($_POST['dstendport_cust'] && !$_POST['dstendport'])
330
			$_POST['dstendport'] = trim($_POST['dstendport_cust']);
331

    
332
		if ($_POST['dstbeginport'] == "any") {
333
			$_POST['dstbeginport'] = 0;
334
			$_POST['dstendport'] = 0;
335
		} else {
336
			if (!$_POST['dstendport'])
337
				$_POST['dstendport'] = $_POST['dstbeginport'];
338
		}
339
		if ($_POST['dstendport'] == "any")
340
			$_POST['dstendport'] = $_POST['dstbeginport'];
341
	}
342

    
343
	if (is_specialnet($_POST['srctype'])) {
344
		$_POST['src'] = $_POST['srctype'];
345
		$_POST['srcmask'] = 0;
346
	} else if ($_POST['srctype'] == "single") {
347
		if (is_ipaddrv6($_POST['src']))
348
			$_POST['srcmask'] = 128;
349
		else
350
			$_POST['srcmask'] = 32;
351
	}
352
	if (is_specialnet($_POST['dsttype'])) {
353
		$_POST['dst'] = $_POST['dsttype'];
354
		$_POST['dstmask'] = 0;
355
	}  else if ($_POST['dsttype'] == "single") {
356
		if (is_ipaddrv6($_POST['dst']))
357
			$_POST['dstmask'] = 128;
358
		else
359
			$_POST['dstmask'] = 32;
360
	}
361

    
362
	$pconfig = $_POST;
363

    
364
	/* input validation */
365
	$reqdfields = explode(" ", "type proto");
366
	if ( isset($a_filter[$id]['associated-rule-id'])===false ) {
367
		$reqdfields[] = "src";
368
		$reqdfields[] = "dst";
369
	}
370
	$reqdfieldsn = explode(",", "Type,Protocol");
371
	if ( isset($a_filter[$id]['associated-rule-id'])===false ) {
372
		$reqdfieldsn[] = "Source";
373
		$reqdfieldsn[] = "Destination";
374
	}
375

    
376
	if($_POST['statetype'] == "modulate state" or $_POST['statetype'] == "synproxy state") {
377
		if( $_POST['proto'] != "tcp" )
378
			$input_errors[] = sprintf(gettext("%s is only valid with protocol TCP."),$_POST['statetype']);
379
		if(($_POST['statetype'] == "synproxy state") && ($_POST['gateway'] != ""))
380
			$input_errors[] = sprintf(gettext("%s is only valid if the gateway is set to 'default'."),$_POST['statetype']);
381
	}
382

    
383
	if ( isset($a_filter[$id]['associated-rule-id'])===false &&
384
	(!(is_specialnet($_POST['srctype']) || ($_POST['srctype'] == "single"))) ) {
385
		$reqdfields[] = "srcmask";
386
		$reqdfieldsn[] = "Source bit count";
387
	}
388
	if ( isset($a_filter[$id]['associated-rule-id'])===false &&
389
	(!(is_specialnet($_POST['dsttype']) || ($_POST['dsttype'] == "single"))) ) {
390
		$reqdfields[] = "dstmask";
391
		$reqdfieldsn[] = gettext("Destination bit count");
392
	}
393

    
394
	do_input_validation($_POST, $reqdfields, $reqdfieldsn, $input_errors);
395

    
396
	if (!$_POST['srcbeginport']) {
397
		$_POST['srcbeginport'] = 0;
398
		$_POST['srcendport'] = 0;
399
	}
400
	if (!$_POST['dstbeginport']) {
401
		$_POST['dstbeginport'] = 0;
402
		$_POST['dstendport'] = 0;
403
	}
404

    
405
	if ($_POST['srcbeginport'] && !is_portoralias($_POST['srcbeginport']))
406
		$input_errors[] = sprintf(gettext("%s is not a valid start source port. It must be a port alias or integer between 1 and 65535."),$_POST['srcbeginposrt']);
407
	if ($_POST['srcendport'] && !is_portoralias($_POST['srcendport']))
408
			$input_errors[] = sprintf(gettext("%s  is not a valid end source port. It must be a port alias or integer between 1 and 65535."),$_POST['srcendport']);
409
	if ($_POST['dstbeginport'] && !is_portoralias($_POST['dstbeginport']))
410
			$input_errors[] = sprintf(gettext("%s is not a valid start destination port. It must be a port alias or integer between 1 and 65535."),$_POST['dstbeginport']);
411
	if ($_POST['dstendport'] && !is_portoralias($_POST['dstendport']))
412
			$input_errors[] = sprintf(gettext("%s is not a valid end destination port. It must be a port alias or integer between 1 and 65535."),$_POST['dstendport']);
413
	if ( !$_POST['srcbeginport_cust'] && $_POST['srcendport_cust'])
414
		if (is_alias($_POST['srcendport_cust']))
415
			$input_errors[] = 'If you put port alias in Source port range to: field you must put the same port alias in from: field';
416
	if ( $_POST['srcbeginport_cust'] && $_POST['srcendport_cust']){
417
		if (is_alias($_POST['srcendport_cust']) && is_alias($_POST['srcendport_cust']) && $_POST['srcbeginport_cust'] != $_POST['srcendport_cust'])
418
			$input_errors[] = 'The same port alias must be used in Source port range from: and to: fields';
419
		if ((is_alias($_POST['srcbeginport_cust']) && (!is_alias($_POST['srcendport_cust']) && $_POST['srcendport_cust']!='')) ||
420
			((!is_alias($_POST['srcbeginport_cust']) && $_POST['srcbeginport_cust']!='') && is_alias($_POST['srcendport_cust'])))
421
			$input_errors[] = 'You cannot specify numbers and port aliases at the same time in Source port range from: and to: field';
422
	}
423
	if ( !$_POST['dstbeginport_cust'] && $_POST['dstendport_cust'])
424
		if (is_alias($_POST['dstendport_cust']))
425
			$input_errors[] = 'If you put port alias in Destination port range to: field you must put the same port alias in from: field';
426
	if ( $_POST['dstbeginport_cust'] && $_POST['dstendport_cust']){
427
		if (is_alias($_POST['dstendport_cust']) && is_alias($_POST['dstendport_cust']) && $_POST['dstbeginport_cust'] != $_POST['dstendport_cust'])
428
			$input_errors[] = 'The same port alias must be used in Destination port range from: and to: fields';
429
		if ((is_alias($_POST['dstbeginport_cust']) && (!is_alias($_POST['dstendport_cust']) && $_POST['dstendport_cust']!='')) ||
430
			((!is_alias($_POST['dstbeginport_cust']) && $_POST['dstbeginport_cust']!='') && is_alias($_POST['dstendport_cust'])))
431
			$input_errors[] = 'You cannot specify numbers and port aliases at the same time in Destination port range from: and to: field';
432
	}
433

    
434
	if ($_POST['src'])
435
		$_POST['src'] = trim($_POST['src']);
436
	if ($_POST['dst'])
437
		$_POST['dst'] = trim($_POST['dst']);
438

    
439
	/* if user enters an alias and selects "network" then disallow. */
440
	if($_POST['srctype'] == "network") {
441
		if(is_alias($_POST['src']))
442
			$input_errors[] = gettext("You must specify single host or alias for alias entries.");
443
	}
444
	if($_POST['dsttype'] == "network") {
445
		if(is_alias($_POST['dst']))
446
			$input_errors[] = gettext("You must specify single host or alias for alias entries.");
447
	}
448

    
449
	if (!is_specialnet($_POST['srctype'])) {
450
		if (($_POST['src'] && !is_ipaddroralias($_POST['src']))) {
451
			$input_errors[] = sprintf(gettext("%s is not a valid source IP address or alias."),$_POST['src']);
452
		}
453
		if (($_POST['srcmask'] && !is_numericint($_POST['srcmask']))) {
454
			$input_errors[] = gettext("A valid source bit count must be specified.");
455
		}
456
	}
457
	if (!is_specialnet($_POST['dsttype'])) {
458
		if (($_POST['dst'] && !is_ipaddroralias($_POST['dst']))) {
459
			$input_errors[] = sprintf(gettext("%s is not a valid destination IP address or alias."),$_POST['dst']);
460
		}
461
		if (($_POST['dstmask'] && !is_numericint($_POST['dstmask']))) {
462
			$input_errors[] = gettext("A valid destination bit count must be specified.");
463
		}
464
	}
465
	if((is_ipaddr($_POST['src']) && is_ipaddr($_POST['dst']))) {
466
		if(!validate_address_family($_POST['src'], $_POST['dst']))
467
			$input_errors[] = sprintf(gettext("The Source IP address %s Address Family differs from the destination %s."), $_POST['src'], $_POST['dst']);
468
		if((is_ipaddrv6($_POST['src']) || is_ipaddrv6($_POST['dst'])) && ($_POST['ipprotocol'] == "inet"))
469
			$input_errors[] = gettext("You can not use IPv6 addresses in IPv4 rules.");
470
		if((is_ipaddrv4($_POST['src']) || is_ipaddrv4($_POST['dst'])) && ($_POST['ipprotocol'] == "inet6"))
471
			$input_errors[] = gettext("You can not use IPv4 addresses in IPv6 rules.");
472
	}
473

    
474
	if((is_ipaddr($_POST['src']) || is_ipaddr($_POST['dst'])) && ($_POST['ipprotocol'] == "inet46"))
475
		$input_errors[] = gettext("You can not use a IPv4 or IPv6 address in combined IPv4 + IPv6 rules.");
476

    
477
	if ($_POST['srcbeginport'] > $_POST['srcendport']) {
478
		/* swap */
479
		$tmp = $_POST['srcendport'];
480
		$_POST['srcendport'] = $_POST['srcbeginport'];
481
		$_POST['srcbeginport'] = $tmp;
482
	}
483
	if ($_POST['dstbeginport'] > $_POST['dstendport']) {
484
		/* swap */
485
		$tmp = $_POST['dstendport'];
486
		$_POST['dstendport'] = $_POST['dstbeginport'];
487
		$_POST['dstbeginport'] = $tmp;
488
	}
489
	if ($_POST['os']) {
490
		if( $_POST['proto'] != "tcp" )
491
			$input_errors[] = gettext("OS detection is only valid with protocol TCP.");
492
		if (!in_array($_POST['os'], $ostypes))
493
			$input_errors[] = gettext("Invalid OS detection selection. Please select a valid OS.");
494
	}
495

    
496
	if ($_POST['ackqueue'] != "") {
497
		if ($_POST['defaultqueue'] == "" )
498
			$input_errors[] = gettext("You have to select a queue when you select an acknowledge queue too.");
499
		else if ($_POST['ackqueue'] == $_POST['defaultqueue'])
500
			$input_errors[] = gettext("Acknowledge queue and Queue cannot be the same.");
501
	}
502
	if (isset($_POST['floating']) && $_POST['pdnpipe'] != "" && (empty($_POST['direction']) || $_POST['direction'] == "any"))
503
		$input_errors[] = gettext("You can not use limiters in Floating rules without choosing a direction.");
504
	if (isset($_POST['floating']) && $_POST['gateway'] != "" && (empty($_POST['direction']) || $_POST['direction'] == "any"))
505
		$input_errors[] = gettext("You can not use gateways in Floating rules without choosing a direction.");
506
	if ($_POST['pdnpipe'] && $_POST['pdnpipe'] != "") {
507
		if ($_POST['dnpipe'] == "" )
508
			$input_errors[] = gettext("You must select a queue for the In direction before selecting one for Out too.");
509
		else if ($_POST['pdnpipe'] == $_POST['dnpipe'])
510
			$input_errors[] = gettext("In and Out Queue cannot be the same.");
511
		else if ($dnqlist[$_POST['pdnpipe']][0] == "?" && $dnqlist[$_POST['dnpipe']][0] != "?")
512
			$input_errors[] = gettext("You cannot select one queue and one virtual interface for IN and Out. Both must be from the same type.");
513
		else if ($dnqlist[$_POST['dnpipe']][0] == "?" && $dnqlist[$_POST['pdnpipe']][0] != "?")
514
			$input_errors[] = gettext("You cannot select one queue and one virtual interface for IN and Out. Both must be from the same type.");
515
		if ($_POST['direction'] == "out" && empty($_POST['gateway']))
516
			$input_errors[] = gettext("Please select a gateway, normally the interface selected gateway, so the limiters work correctly");
517
	}
518
	if( !empty($_POST['ruleid']) && !ctype_digit($_POST['ruleid']))
519
		$input_errors[] = gettext('ID must be an integer');
520
	if($_POST['l7container'] && $_POST['l7container'] != "") {
521
		if(!($_POST['proto'] == "tcp" || $_POST['proto'] == "udp" || $_POST['proto'] == "tcp/udp"))
522
			$input_errors[] = gettext("You can only select a layer7 container for TCP and/or UDP protocols");
523
		if ($_POST['type'] != "pass")
524
			$input_errors[] = gettext("You can only select a layer7 container for Pass type rules.");
525
	}
526

    
527
	if (!in_array($_POST['proto'], array("tcp","tcp/udp"))) {
528
		if (!empty($_POST['max-src-conn']))
529
			$input_errors[] = gettext("You can only specify the maximum number of established connections per host (advanced option) for TCP protocol.");
530
		if (!empty($_POST['max-src-conn-rate']) || !empty($_POST['max-src-conn-rates']))
531
			$input_errors[] = gettext("You can only specify the maximum new connections per host / per second(s) (advanced option) for TCP protocol.");
532
		if (!empty($_POST['statetimeout']))
533
			$input_errors[] = gettext("You can only specify the state timeout (advanced option) for TCP protocol.");
534
	}
535

    
536
	if ($_POST['type'] != "pass") {
537
		if (!empty($_POST['max']))
538
			$input_errors[] = gettext("You can only specify the maximum state entries (advanced option) for Pass type rules.");
539
		if (!empty($_POST['max-src-nodes']))
540
			$input_errors[] = gettext("You can only specify the maximum number of unique source hosts (advanced option) for Pass type rules.");
541
		if (!empty($_POST['max-src-conn']))
542
			$input_errors[] = gettext("You can only specify the maximum number of established connections per host (advanced option) for Pass type rules.");
543
		if (!empty($_POST['max-src-states']))
544
			$input_errors[] = gettext("You can only specify the maximum state entries per host (advanced option) for Pass type rules.");
545
		if (!empty($_POST['max-src-conn-rate']) || !empty($_POST['max-src-conn-rates']))
546
			$input_errors[] = gettext("You can only specify the maximum new connections per host / per second(s) (advanced option) for Pass type rules.");
547
		if (!empty($_POST['statetimeout']))
548
			$input_errors[] = gettext("You can only specify the state timeout (advanced option) for Pass type rules.");
549
	}
550

    
551
	if (($_POST['statetype'] == "none") && (empty($_POST['l7container']))) {
552
		if (!empty($_POST['max']))
553
			$input_errors[] = gettext("You cannot specify the maximum state entries (advanced option) if statetype is none and no L7 container is selected.");
554
		if (!empty($_POST['max-src-nodes']))
555
			$input_errors[] = gettext("You cannot specify the maximum number of unique source hosts (advanced option) if statetype is none and no L7 container is selected.");
556
		if (!empty($_POST['max-src-conn']))
557
			$input_errors[] = gettext("You cannot specify the maximum number of established connections per host (advanced option) if statetype is none and no L7 container is selected.");
558
		if (!empty($_POST['max-src-states']))
559
			$input_errors[] = gettext("You cannot specify the maximum state entries per host (advanced option) if statetype is none and no L7 container is selected.");
560
		if (!empty($_POST['max-src-conn-rate']) || !empty($_POST['max-src-conn-rates']))
561
			$input_errors[] = gettext("You cannot specify the maximum new connections per host / per second(s) (advanced option) if statetype is none and no L7 container is selected.");
562
		if (!empty($_POST['statetimeout']))
563
			$input_errors[] = gettext("You cannot specify the state timeout (advanced option) if statetype is none and no L7 container is selected.");
564
	}
565

    
566
	if (($_POST['max'] != "") && !is_posnumericint($_POST['max']))
567
		$input_errors[] = gettext("Maximum state entries (advanced option) must be a positive integer");
568

    
569
	if (($_POST['max-src-nodes'] != "") && !is_posnumericint($_POST['max-src-nodes']))
570
		$input_errors[] = gettext("Maximum number of unique source hosts (advanced option) must be a positive integer");
571

    
572
	if (($_POST['max-src-conn'] != "") && !is_posnumericint($_POST['max-src-conn']))
573
		$input_errors[] = gettext("Maximum number of established connections per host (advanced option) must be a positive integer");
574

    
575
	if (($_POST['max-src-states'] != "") && !is_posnumericint($_POST['max-src-states']))
576
		$input_errors[] = gettext("Maximum state entries per host (advanced option) must be a positive integer");
577

    
578
	if (($_POST['max-src-conn-rate'] != "") && !is_posnumericint($_POST['max-src-conn-rate']))
579
		$input_errors[] = gettext("Maximum new connections per host / per second(s) (advanced option) must be a positive integer");
580

    
581
	if (($_POST['statetimeout'] != "") && !is_posnumericint($_POST['statetimeout']))
582
		$input_errors[] = gettext("State timeout (advanced option) must be a positive integer");
583

    
584
	if ((($_POST['max-src-conn-rate'] != "" and $_POST['max-src-conn-rates'] == "")) ||
585
		(($_POST['max-src-conn-rate'] == "" and $_POST['max-src-conn-rates'] != "")))
586
		$input_errors[] = gettext("Both maximum new connections per host and the interval (per second(s)) must be specified");
587

    
588
	if (!$_POST['tcpflags_any']) {
589
		$settcpflags = array();
590
		$outoftcpflags = array();
591
		foreach ($tcpflags as $tcpflag) {
592
			if ($_POST['tcpflags1_' . $tcpflag] == "on")
593
				$settcpflags[] = $tcpflag;
594
			if ($_POST['tcpflags2_' . $tcpflag] == "on")
595
				$outoftcpflags[] = $tcpflag;
596
		}
597
		if (empty($outoftcpflags) && !empty($settcpflags))
598
			$input_errors[] = gettext("If you specify TCP flags that should be set you should specify out of which flags as well.");
599
	}
600

    
601
	// Allow extending of the firewall edit page and include custom input validation
602
	pfSense_handle_custom_code("/usr/local/pkg/firewall_rules/input_validation");
603

    
604
	if (!$input_errors) {
605
		$filterent = array();
606
		$filterent['id'] = $_POST['ruleid']>0?$_POST['ruleid']:'';
607

    
608
		$filterent['tracker'] = empty($_POST['tracker']) ? (int)microtime(true) : $_POST['tracker'];
609

    
610
		$filterent['type'] = $_POST['type'];
611
		if (isset($_POST['interface'] ))
612
			$filterent['interface'] = $_POST['interface'];
613

    
614
		if (isset($_POST['ipprotocol'] ))
615
			$filterent['ipprotocol'] = $_POST['ipprotocol'];
616

    
617
		if ($_POST['tcpflags_any']) {
618
			$filterent['tcpflags_any'] = true;
619
		} else {
620
			$settcpflags = array();
621
			$outoftcpflags = array();
622
			foreach ($tcpflags as $tcpflag) {
623
				if ($_POST['tcpflags1_' . $tcpflag] == "on")
624
					$settcpflags[] = $tcpflag;
625
				if ($_POST['tcpflags2_' . $tcpflag] == "on")
626
					$outoftcpflags[] = $tcpflag;
627
			}
628
			if (!empty($outoftcpflags)) {
629
				$filterent['tcpflags2'] = join(",", $outoftcpflags);
630
				if (!empty($settcpflags))
631
					$filterent['tcpflags1'] = join(",", $settcpflags);
632
			}
633
		}
634

    
635
		if (isset($_POST['tag']))
636
			$filterent['tag'] = $_POST['tag'];
637
		if (isset($_POST['tagged']))
638
			$filterent['tagged'] = $_POST['tagged'];
639
		if ($if == "FloatingRules" || isset($_POST['floating'])) {
640
			$filterent['direction'] = $_POST['direction'];
641
			if (isset($_POST['quick']) && $_POST['quick'] != "")
642
				$filterent['quick'] = $_POST['quick'];
643
			$filterent['floating'] = "yes";
644
			if (isset($_POST['interface']) && count($_POST['interface']) > 0)  {
645
				$filterent['interface'] = implode(",", $_POST['interface']);
646
			}
647
		}
648

    
649
		/* Advanced options */
650
		if ($_POST['allowopts'] == "yes")
651
			$filterent['allowopts'] = true;
652
		else
653
			unset($filterent['allowopts']);
654
		if ($_POST['disablereplyto'] == "yes")
655
			$filterent['disablereplyto'] = true;
656
		else
657
			unset($filterent['disablereplyto']);
658
		$filterent['max'] = $_POST['max'];
659
		$filterent['max-src-nodes'] = $_POST['max-src-nodes'];
660
		$filterent['max-src-conn'] = $_POST['max-src-conn'];
661
		$filterent['max-src-states'] = $_POST['max-src-states'];
662
		$filterent['statetimeout'] = $_POST['statetimeout'];
663
		$filterent['statetype'] = $_POST['statetype'];
664
		$filterent['os'] = $_POST['os'];
665
		if($_POST['nopfsync'] != "")
666
			$filterent['nopfsync'] = true;
667
		else
668
			unset($filterent['nopfsync']);
669

    
670
		/* Nosync directive - do not xmlrpc sync this item */
671
		if($_POST['nosync'] != "")
672
			$filterent['nosync'] = true;
673
		else
674
			unset($filterent['nosync']);
675

    
676
		/* unless both values are provided, unset the values - ticket #650 */
677
		if($_POST['max-src-conn-rate'] != "" and $_POST['max-src-conn-rates'] != "") {
678
			$filterent['max-src-conn-rate'] = $_POST['max-src-conn-rate'];
679
			$filterent['max-src-conn-rates'] = $_POST['max-src-conn-rates'];
680
		} else {
681
			unset($filterent['max-src-conn-rate']);
682
			unset($filterent['max-src-conn-rates']);
683
		}
684

    
685
		if ($_POST['proto'] != "any")
686
			$filterent['protocol'] = $_POST['proto'];
687
		else
688
			unset($filterent['protocol']);
689

    
690
		if ($_POST['proto'] == "icmp") {
691
			if ($filterent['ipprotocol'] == 'inet6' && $_POST['icmp6type'])
692
				$filterent['icmptype'] = $_POST['icmp6type'];
693
			else if ($filterent['ipprotocol'] != 'inet6' && $_POST['icmptype'])
694
				$filterent['icmptype'] = $_POST['icmptype'];
695
			else
696
				unset($filterent['icmptype']);
697
		} else
698
			unset($filterent['icmptype']);
699

    
700
		pconfig_to_address($filterent['source'], $_POST['src'],
701
			$_POST['srcmask'], $_POST['srcnot'],
702
			$_POST['srcbeginport'], $_POST['srcendport']);
703

    
704
		pconfig_to_address($filterent['destination'], $_POST['dst'],
705
			$_POST['dstmask'], $_POST['dstnot'],
706
			$_POST['dstbeginport'], $_POST['dstendport']);
707

    
708
		if ($_POST['disabled'])
709
			$filterent['disabled'] = true;
710
		else
711
			unset($filterent['disabled']);
712

    
713
		if ($_POST['dscp'])
714
			$filterent['dscp'] = $_POST['dscp'];
715

    
716
		if ($_POST['log'])
717
			$filterent['log'] = true;
718
		else
719
			unset($filterent['log']);
720
		strncpy($filterent['descr'], $_POST['descr'], 52);
721

    
722
		if ($_POST['gateway'] != "") {
723
			$filterent['gateway'] = $_POST['gateway'];
724
		}
725

    
726
		if ($_POST['defaultqueue'] != "") {
727
			$filterent['defaultqueue'] = $_POST['defaultqueue'];
728
			if ($_POST['ackqueue'] != "")
729
				$filterent['ackqueue'] = $_POST['ackqueue'];
730
		}
731

    
732
		if ($_POST['dnpipe'] != "") {
733
			$filterent['dnpipe'] = $_POST['dnpipe'];
734
			if ($_POST['pdnpipe'] != "")
735
				$filterent['pdnpipe'] = $_POST['pdnpipe'];
736
		}
737

    
738
		if ($_POST['l7container'] != "") {
739
			$filterent['l7container'] = $_POST['l7container'];
740
		}
741

    
742
		if ($_POST['sched'] != "") {
743
			$filterent['sched'] = $_POST['sched'];
744
		}
745

    
746
		if ($_POST['vlanprio'] != "") {
747
			$filterent['vlanprio'] = $_POST['vlanprio'];
748
		}
749
		if ($_POST['vlanprioset'] != "") {
750
			$filterent['vlanprioset'] = $_POST['vlanprioset'];
751
		}
752

    
753
		// If we have an associated nat rule, make sure the source and destination doesn't change
754
		if( isset($a_filter[$id]['associated-rule-id']) ) {
755
			$filterent['interface'] = $a_filter[$id]['interface'];
756
			if (isset($a_filter[$id]['protocol']))
757
				$filterent['protocol'] = $a_filter[$id]['protocol'];
758
			else if (isset($filterent['protocol']))
759
				unset($filterent['protocol']);
760
			if ($a_filter[$id]['protocol'] == "icmp" && $a_filter[$id]['icmptype'])
761
				$filterent['icmptype'] = $a_filter[$id]['icmptype'];
762
			else if (isset($filterent['icmptype']))
763
				unset($filterent['icmptype']);
764

    
765
			$filterent['source'] = $a_filter[$id]['source'];
766
			$filterent['destination'] = $a_filter[$id]['destination'];
767
			$filterent['associated-rule-id'] = $a_filter[$id]['associated-rule-id'];
768
		}
769

    
770
		if ( isset($a_filter[$id]['created']) && is_array($a_filter[$id]['created']) )
771
			$filterent['created'] = $a_filter[$id]['created'];
772

    
773
		$filterent['updated'] = make_config_revision_entry();
774

    
775
		// Allow extending of the firewall edit page and include custom input validation
776
		pfSense_handle_custom_code("/usr/local/pkg/firewall_rules/pre_write_config");
777

    
778
		if (isset($id) && $a_filter[$id])
779
			$a_filter[$id] = $filterent;
780
		else {
781
			$filterent['created'] = make_config_revision_entry();
782
			if (is_numeric($after))
783
				array_splice($a_filter, $after+1, 0, array($filterent));
784
			else
785
				$a_filter[] = $filterent;
786
		}
787

    
788
		filter_rules_sort();
789

    
790
		if (write_config())
791
			mark_subsystem_dirty('filter');
792

    
793
		if (isset($_POST['floating']))
794
			header("Location: firewall_rules.php?if=FloatingRules");
795
		else
796
			header("Location: firewall_rules.php?if=" . htmlspecialchars($_POST['interface']));
797
		exit;
798
	}
799
}
800

    
801
$pgtitle = array(gettext("Firewall"),gettext("Rules"),gettext("Edit"));
802
$shortcut_section = "firewall";
803

    
804
$closehead = false;
805

    
806
$page_filename = "firewall_rules_edit.php";
807
include("head.inc");
808

    
809
if ($input_errors)
810
	print_input_errors($input_errors);
811

    
812
require('classes/Form.class.php');
813
$form = new Form;
814
$section = new Form_Section('Edit Firewall rule');
815

    
816
if (isset($id))
817
{
818
	$form->addGlobal(new Form_Input(
819
		'id',
820
		'ID',
821
		'hidden',
822
		$id
823
	));
824
}
825

    
826
if (isset($a_filter[$id]))
827
{
828
	$form->addGlobal(new Form_Input(
829
		'tracker',
830
		'Tracker',
831
		'hidden',
832
		$pconfig['tracker']
833
	));
834
}
835

    
836
$form->addGlobal(new Form_Input(
837
	'after',
838
	'After',
839
	'hidden',
840
	$after
841
));
842

    
843
$form->addGlobal(new Form_Input(
844
	'ruleid',
845
	'Ruleid',
846
	'hidden',
847
	$pconfig['ruleid']
848
));
849

    
850
// Allow extending of the firewall edit page and include custom input validation
851
pfSense_handle_custom_code("/usr/local/pkg/firewall_rules/htmlphpearly");
852

    
853
$values = array(
854
	'pass' => 'Pass',
855
	'block' => 'Block',
856
	'reject' => 'Reject',
857
);
858

    
859
if ($if == "FloatingRules" || isset($pconfig['floating']))
860
	$values['match'] = 'Match';
861

    
862
$section->addInput(new Form_Select(
863
	'type',
864
	'Action',
865
	$pconfig['type'],
866
	$values
867
))->setHelp('Choose what to do with packets that match the criteria specified '.
868
	'below.<br/>Hint: the difference between block and reject is that with '.
869
	'reject, a packet (TCP RST or ICMP port unreachable for UDP) is returned '.
870
	'to the sender, whereas with block the packet is dropped silently. In '.
871
	'either case, the original packet is discarded.');
872

    
873
$section->addInput(new Form_Checkbox(
874
	'disabled',
875
	'Disabled',
876
	'Disable this rule',
877
	$pconfig['disabled']
878
))->setHelp('Set this option to disable this rule without removing it from the '.
879
	'list.');
880

    
881
if ($if == "FloatingRules" || isset($pconfig['floating']))
882
{
883
	$section->addInput(new Form_Checkbox(
884
		'floating',
885
		'Quick',
886
		'Apply the action immediately on match.',
887
		$pconfig['quick']
888
	))->setHelp('Set this option if you need to apply this action to traffic that '.
889
		'matches this rule immediately.');
890
}
891

    
892
$edit_disabled = isset($pconfig['associated-rule-id']);
893

    
894
if ($edit_disabled)
895
{
896
	$extra = '';
897
	foreach( $config['nat']['rule'] as $index => $nat_rule )
898
	{
899
		if ($nat_rule['associated-rule-id'] === $pconfig['associated-rule-id'] )
900
			$extra = '<br/><a href="firewall_nat_edit.php?id='. $index .'">'. gettext('View the NAT rule') .'</a>';
901
	}
902

    
903
	$section->add(new Form_Group(
904
		'Associated filter rule'
905
	))->setHelp('Note: This is associated to a NAT rule.<br/>You cannot edit '.
906
		'the interface, protocol, source, or destination of associated filter '.
907
		'rules.'. $extra);
908

    
909
	$section->addGlobal(new Form_Input(
910
		'associated-rule-id',
911
		'Associated Rule ID',
912
		'hidden',
913
		$pconfig['associated-rule-id']
914
	));
915

    
916
	if (!empty($pconfig['interface']))
917
	{
918
		$section->addInput(new Form_Input(
919
			'interface',
920
			'Interface',
921
			'hidden',
922
			$pconfig['interface']
923
		));
924
	}
925
}
926

    
927
$interfaces = array();
928

    
929
/* add group interfaces */
930
if (is_array($config['ifgroups']['ifgroupentry']))
931
	foreach ($config['ifgroups']['ifgroupentry'] as $ifgen)
932
		if (have_ruleint_access($ifgen['ifname']))
933
			$interfaces[$ifgen['ifname']] = $ifgen['ifname'];
934

    
935
foreach (get_configured_interface_with_descr() as $ifent => $ifdesc)
936
{
937
	if (have_ruleint_access($ifent))
938
		$interfaces[$ifent] = $ifdesc;
939
}
940

    
941
if ($config['l2tp']['mode'] == "server" && have_ruleint_access("l2tp"))
942
	$interfaces['l2tp'] = 'L2TP VPN';
943

    
944
if ($config['pptpd']['mode'] == "server" && have_ruleint_access("pptp"))
945
	$interfaces['pptp'] = 'PPTP VPN';
946

    
947
if (is_pppoe_server_enabled() && have_ruleint_access("pppoe"))
948
	$interfaces['pppoe'] = "PPPoE Server";
949

    
950
/* add ipsec interfaces */
951
if (isset($config['ipsec']['enable']) || isset($config['ipsec']['client']['enable']) && have_ruleint_access("enc0"))
952
	$interfaces["enc0"] = "IPsec";
953

    
954
/* add openvpn/tun interfaces */
955
if  ($config['openvpn']["openvpn-server"] || $config['openvpn']["openvpn-client"])
956
	$interfaces["openvpn"] = "OpenVPN";
957

    
958
$section->addInput($input = new Form_Select(
959
	'interface',
960
	'Interface',
961
	$pconfig['interface'],
962
	$interfaces,
963
	($if == "FloatingRules" || isset($pconfig['floating']))
964
))->setHelp('Choose on which interface packets must come in to match this '.
965
	'rule.');
966

    
967
if ($if == "FloatingRules" || isset($pconfig['floating']))
968
	$input->setHelp('Choose the interface(s) for this rule.');
969

    
970
if ($if == "FloatingRules" || isset($pconfig['floating']))
971
{
972
	$section->addInput(new Form_Select(
973
		'direction',
974
		'Direction',
975
		$pconfig['direction'],
976
		array(
977
			'any' => 'any',
978
			'in' => 'in',
979
			'out' => 'out',
980
		)
981
	));
982

    
983
	$section->addInput(new Form_Input(
984
		'floating',
985
		'Floating',
986
		'hidden',
987
		'floating'
988
	));
989
}
990

    
991
$section->addInput(new Form_Select(
992
	'ipprotocol',
993
	'TCP/IP Version',
994
	$pconfig['ipprotocol'],
995
	array(
996
		'inet' => 'IPv4',
997
		'inet6' => 'IPv6',
998
		'inet46' => 'IPv4+IPv6',
999
	)
1000
))->setHelp('Select the Internet Protocol version this rule applies to');
1001

    
1002
$section->addInput(new Form_Select(
1003
	'proto',
1004
	'Protocol',
1005
	$pconfig['ipprotocol'],
1006
	array(
1007
		'tcp' => 'TCP',
1008
		'udp' => 'UDP',
1009
		'tcp/udp' => 'TCP/UDP',
1010
		'icmp' => 'ICMP',
1011
		'esp' => 'ESP',
1012
		'ah' => 'AH',
1013
		'gre' => 'GRE',
1014
		'ipv6' => 'IPV6',
1015
		'igmp' => 'IGMP',
1016
		'pim' => 'PIM',
1017
		'ospf' => 'OSPF',
1018
		'sctp' => 'SCTP',
1019
		'any' => 'any',
1020
		'carp' => 'CARP',
1021
		'pfsync' => 'PFSYNC',
1022
	)
1023
))->setHelp('Select the Internet Protocol version this rule applies to');
1024

    
1025
$section->addInput(new Form_Select(
1026
	'icmptype',
1027
	'ICMP type',
1028
	$pconfig['icmptype'],
1029
	$icmptypes
1030
))->setHelp('If you selected ICMP for the protocol above, you may specify an ICMP type here.');
1031

    
1032
$section->addInput(new Form_Select(
1033
	'icmp6type',
1034
	'ICMPv6 type',
1035
	$pconfig['icmptype'],
1036
	$icmp6types
1037
))->setHelp('If you selected ICMP for the protocol above, you may specify an ICMP type here.');
1038

    
1039
$form->add($section);
1040

    
1041
// Source and destination share a lot of logic. Loop over the two
1042
foreach (['src' => 'Source', 'dst' => 'Destination'] as $type => $name)
1043
{
1044
	$section = new Form_Section($name .' details');
1045

    
1046
	$group = new Form_Group($name);
1047
	$group->add(new Form_Checkbox(
1048
		$type .'not',
1049
		$name .' not',
1050
		'Invert the sense of the match.',
1051
		$pconfig[$type.'not']
1052
	));
1053

    
1054
	$ruleType = $pconfig[$type];
1055
	if (is_specialnet($pconfig[$type]))
1056
		$ruleType = 'network';
1057
	elseif ((is_ipaddrv6($pconfig[$type]) && $pconfig[$type.'mask'] == 128) ||
1058
			(is_ipaddrv4($pconfig[$type]) && $pconfig[$type.'mask'] == 32) ||
1059
			(is_alias($pconfig[$type])))
1060
		$ruleType = 'single';
1061

    
1062
	$ruleValues = array(
1063
		'any' => 'any',
1064
		'single' => 'Single host or alias',
1065
		'network' => 'Network',
1066
	);
1067
	if (isset($a_filter[$id]['floating']) || $if == "FloatingRules")
1068
		$ruleValues['(self)'] = 'This Firewall (self)';
1069
	if (have_ruleint_access("pptp"))
1070
		$ruleValues['pptp'] = 'PPTP clients';
1071
	if (have_ruleint_access("pppoe"))
1072
		$ruleValues['pppoe'] = 'PPoE clients';
1073
	if (have_ruleint_access("l2tp"))
1074
		$ruleValues['l2tp'] = 'L2TP clients';
1075

    
1076
	foreach ($ifdisp as $ifent => $ifdesc)
1077
	{
1078
		if (!have_ruleint_access($ifent))
1079
			continue;
1080

    
1081
		$ruleValues[$ifent] = $ifdesc.' net';
1082
		$ruleValues[$ifent.'ip'] = $ifdesc.' address';
1083
	}
1084

    
1085
	$group->add(new Form_Select(
1086
		$type . 'type',
1087
		$name .' Type',
1088
		$ruleType,
1089
		$ruleValues
1090
	));
1091

    
1092
	$group->add(new Form_IpAddress(
1093
		$type,
1094
		$name .' Address',
1095
		$pconfig[$type]
1096
	))->addMask($type .'mask', $pconfig[$type.'mask']);
1097

    
1098
	$section->add($group);
1099

    
1100
	$portValues = ['any' => 'any'];
1101
	foreach ($wkports as $port => $portName)
1102
		$portValues[$port] = $portName.' ('. $port .')';
1103

    
1104
	$group = new Form_Group($name .' port range');
1105
	$group->add($input = new Form_Select(
1106
		$type .'beginport',
1107
		$name .' port begin',
1108
		$pconfig[$type .'beginport'],
1109
		$portValues
1110
	));
1111

    
1112
	if ($type == 'src')
1113
		$input->setHelp('Specify the source port or port range for this rule. This is '.
1114
			'usually random and almost never equal to the destination port range (and '.
1115
			'should usually be <b>any</b><br />Hint: you can leave the <i>to</i> field '.
1116
			'empty if you only want to filter a single port.');
1117
	else
1118
		$input->setHelp('Specify the destination port or port range for this rule. <br />'.
1119
			'Hint: you can leave the <i>to</i> field empty if you only want to filter a '.
1120
			'single port.');
1121

    
1122
	$group->add(new Form_Input(
1123
		$type .'beginport_cust',
1124
		$name .' port begin custom',
1125
		'number',
1126
		(isset($portValues[ $pconfig[$type .'beginport'] ]) ? null : $pconfig[$type .'beginport']),
1127
		['min' => 1, 'max' => 65535]
1128
	));
1129

    
1130
	$group->add(new Form_Select(
1131
		$type .'endport',
1132
		$name .' port end',
1133
		$pconfig[$type .'endport'],
1134
		$portValues
1135
	));
1136

    
1137
	$group->add(new Form_Input(
1138
		$type .'endport_cust',
1139
		$name .' port end custom',
1140
		'number',
1141
		(isset($portValues[ $pconfig[$type .'endport'] ]) ? null : $pconfig[$type .'endport']),
1142
		['min' => 1, 'max' => 65535]
1143
	));
1144

    
1145
	$section->add($group);
1146
	$form->add($section);
1147
}
1148

    
1149
$section = new Form_Section('Extra options');
1150
$section->addInput(new Form_Checkbox(
1151
	'log',
1152
	'Log',
1153
	'Log packets that are handled by this rule',
1154
	$pconfig['log']
1155
))->setHelp('Hint: the firewall has limited local log space. Don"t turn on logging '.
1156
	'for everything. If you want to do a lot of logging, consider using a remote '.
1157
	'syslog server (see the <a href="diag_logs_settings.php">Diagnostics: System logs: '.
1158
	'Settings</a> page).');
1159

    
1160
$section->addInput(new Form_Input(
1161
	'descr',
1162
	'Description',
1163
	'text',
1164
	$pconfig['descr']
1165
))->setHelp('You may enter a description here for your reference.');
1166

    
1167
$form->add($section);
1168
$section = new Form_Section('Advanced options');
1169

    
1170
$section->addInput(new Form_Select(
1171
	'os',
1172
	'Source OS',
1173
	$pconfig['os'],
1174
	['' => 'any'] + $ostypes
1175
))->setHelp('Note: this only works for TCP rules. General OS choice matches all subtypes.');
1176

    
1177
$section->addInput(new Form_Select(
1178
	'dscp',
1179
	'Diffserv Code Point',
1180
	$pconfig['dscp'],
1181
	array_combine($firewall_rules_dscp_types, $firewall_rules_dscp_types)
1182
));
1183

    
1184
$section->addInput(new Form_Checkbox(
1185
	'allowopts',
1186
	'Allow IP options',
1187
	'Allow packets with IP options to pass. Otherwise they are blocked by '.
1188
	'default. This is usually only seen with multicast traffic.',
1189
	$pconfig['allowopts']
1190
));
1191

    
1192
$section->addInput(new Form_Checkbox(
1193
	'disablereplyto',
1194
	'Disable reply-to',
1195
	'Disable auto generated reply-to for this rule.',
1196
	$pconfig['disablereplyto']
1197
));
1198

    
1199
$section->addInput(new Form_Input(
1200
	'tag',
1201
	'Tag',
1202
	'text',
1203
	$pconfig['tag']
1204
))->setHelp('You can mark a packet matching this rule and use this mark to match '.
1205
	'on other NAT/filter rules. It is called <b>Policy filtering</b>.');
1206

    
1207
$section->addInput(new Form_Input(
1208
	'tagged',
1209
	'Tagged',
1210
	'text',
1211
	$pconfig['tagged']
1212
))->setHelp('You can match packet on a mark placed before on another rule.');
1213

    
1214
$section->addInput(new Form_Input(
1215
	'max',
1216
	'Max. states',
1217
	'number',
1218
	$pconfig['max']
1219
))->setHelp('Maximum state entries this rule can create.');
1220

    
1221
$section->addInput(new Form_Input(
1222
	'max-src-nodes',
1223
	'Max. src nodes',
1224
	'number',
1225
	$pconfig['max-src-nodes']
1226
))->setHelp('Maximum number of unique source hosts.');
1227

    
1228
$section->addInput(new Form_Input(
1229
	'max-src-conn',
1230
	'Max. connections',
1231
	'number',
1232
	$pconfig['max-src-conn']
1233
))->setHelp('Maximum number of established connections per host (TCP only).');
1234

    
1235
$section->addInput(new Form_Input(
1236
	'max-src-states',
1237
	'Max. src. states',
1238
	'number',
1239
	$pconfig['max-src-states']
1240
))->setHelp('Maximum state entries per host.');
1241

    
1242
$section->addInput(new Form_Input(
1243
	'max-src-conn-rate',
1244
	'Max. src. conn. Rate',
1245
	'number',
1246
	$pconfig['max-src-conn-rate']
1247
))->setHelp('Maximum state entries per host');
1248

    
1249
$section->addInput(new Form_Input(
1250
	'max-src-conn-rates',
1251
	'Max. src. conn. Rates',
1252
	'number',
1253
	$pconfig['max-src-conn-rates'],
1254
	['min' => 1, 'max' => 255]
1255
))->setHelp('Maximum new connections per host / per second(s) (TCP only)');
1256

    
1257
$section->addInput(new Form_Input(
1258
	'statetimeout',
1259
	'State timeout',
1260
	'number',
1261
	$pconfig['statetimeout'],
1262
	['min' => 1, 'max' => 3600]
1263
))->setHelp('State Timeout in seconds (TCP only)');
1264

    
1265
$form->add($section);
1266
$section = new Form_Section('TCP Flags');
1267

    
1268
$setflags = explode(',', $pconfig['tcpflags1']);
1269
$outofflags = explode(',', $pconfig['tcpflags2']);
1270

    
1271
foreach ($tcpflags as $tcpflag)
1272
{
1273
	$section->addInput(new Form_Checkbox(
1274
		'tcpflags1_'. $tcpflag,
1275
		'Set '. strtoupper($tcpflag),
1276
		null,
1277
		(array_search($tcpflag, $setflags) !== false),
1278
		'on'
1279
	));
1280

    
1281
	$section->addInput(new Form_Checkbox(
1282
		'tcpflags2_'. $tcpflag,
1283
		'Out of '. strtoupper($tcpflag),
1284
		null,
1285
		(array_search($tcpflag, $setflags) !== false),
1286
		'on'
1287
	));
1288
}
1289

    
1290
$section->addInput(new Form_Checkbox(
1291
	'tcpflags_any',
1292
	'Any',
1293
	'Any flags',
1294
	$pconfig['tcpflags_any'],
1295
	'on'
1296
))->setHelp('Use this to choose TCP flags that must be set or cleared for this rule to match.');
1297

    
1298
$form->add($section);
1299
$section = new Form_Section('State Type');
1300

    
1301
$section->addInput(new Form_Checkbox(
1302
	'nopfsync',
1303
	'No pfSync',
1304
	'Prevent states created by this rule to be sync\'ed over pfsync.',
1305
	$pconfig['nopfsync']
1306
));
1307

    
1308
$section->addInput(new Form_Select(
1309
	'statetype',
1310
	'State type',
1311
	$pconfig['statetype'],
1312
	array(
1313
		'keep state' => 'Keep: works with all IP protocols',
1314
		'sloppy state' => 'Sloppy: works with all IP protocols',
1315
		'synproxy state' => 'Synproxy: proxies incoming TCP connections to help protect servers from spoofed TCP SYN floods. This option includes the functionality of keep state and modulate state combined',
1316
		'none' => 'None: Do not use state mechanisms to keep track.  This is only useful if you\'re doing advanced queueing in certain situations',
1317
	)
1318
))->setHelp('Select which type of state tracking mechanism you would like to use.  If in doubt, use keep state.');
1319

    
1320
$section->addInput(new Form_Checkbox(
1321
	'nosync',
1322
	'No XMLRPC Sync',
1323
	'Prevent the rule on Master from automatically syncing to other CARP members',
1324
	$pconfig['nosync']
1325
))->setHelp('This does NOT prevent the rule from being overwritten on Slave.');
1326

    
1327
$vlanprio = array("none", "be", "bk", "ee", "ca", "vi", "vo", "ic", "nc");
1328
$section->addInput(new Form_Select(
1329
	'vlanprio',
1330
	'VLAN Prio',
1331
	$pconfig['vlanprio'],
1332
	$vlanprio
1333
))->setHelp('Choose 802.1p priority to match on');
1334

    
1335
$section->addInput(new Form_Select(
1336
	'vlanprioset',
1337
	'VLAN Prio Set',
1338
	$pconfig['vlanprioset'],
1339
	$vlanprio
1340
))->setHelp('Choose 802.1p priority to apply');
1341

    
1342
$schedules = array('none'); //leave none to leave rule enabled all the time
1343
foreach ((array)$config['schedules']['schedule'] as $schedule)
1344
{
1345
	if ($schedule['name'] != "")
1346
		$schedules[] = $schedule['name'];
1347
}
1348

    
1349
$section->addInput(new Form_Select(
1350
	'sched',
1351
	'Schedule',
1352
	$pconfig['sched'],
1353
	$schedules
1354
))->setHelp('Leave as \'none\' to leave the rule enabled all the time');
1355

    
1356
$gateways = array('default' => 'default');
1357
foreach (return_gateways_array() as $gwname => $gw)
1358
{
1359
	if (($pconfig['ipprotocol'] == "inet46"))
1360
		continue;
1361
	if (($pconfig['ipprotocol'] == "inet6") && !(($gw['ipprotocol'] == "inet6") || (is_ipaddrv6($gw['gateway']))))
1362
		continue;
1363
	if (($pconfig['ipprotocol'] == "inet") && !(($gw['ipprotocol'] == "inet") || (is_ipaddrv4($gw['gateway']))))
1364
		continue;
1365
	if ($gw == "")
1366
		continue;
1367

    
1368
	$gateways[ $gwname ] = $gw['name'] . (empty($gw['gateway'])? '' : ' - '. $gateway_addr_str);
1369
}
1370

    
1371
foreach ((array)$a_gatewaygroups as $gwg_name => $gwg_data)
1372
{
1373
	if ((empty($pconfig['ipprotocol'])) || ($pconfig['ipprotocol'] == $gwg_data['ipprotocol']))
1374
		$gateways[ $gwg_name ] = $gwg_name;
1375
}
1376

    
1377
$section->addInput(new Form_Select(
1378
	'gateway',
1379
	'Gateway',
1380
	$pconfig['gateway'],
1381
	$gateways
1382
))->setHelp('Leave as \'default\' to use the system routing table. Or choose a '.
1383
	'gateway to utilize policy based routing.');
1384

    
1385
$group = new Form_Group('In / Out pipe');
1386

    
1387
$group->add(new Form_Select(
1388
	'dnpipe',
1389
	'DNpipe',
1390
	$pconfig['dnpipe'],
1391
	array('' => 'none') + array_keys($dnqlist)
1392
));
1393

    
1394
$group->add(new Form_Select(
1395
	'pdnpipe',
1396
	'PDNpipe',
1397
	$pconfig['pdnpipe'],
1398
	array('' => 'none') + array_keys($dnqlist)
1399
));
1400

    
1401
$section->add($group)->setHelp('Choose the Out queue/Virtual interface only if '.
1402
	'you have also selected In. The Out selection is applied to traffic leaving '.
1403
	'the interface where the rule is created, In is applied to traffic coming '.
1404
	'into the chosen interface.<br />If you are creating a floating rule, if the '.
1405
	'direction is In then the same rules apply, if the direction is out the '.
1406
	'selections are reverted Out is for incoming and In is for outgoing.'
1407
);
1408

    
1409
$group = new Form_Group('Ackqueue / Queue');
1410

    
1411
$qlist = array_keys($qlist);
1412
foreach ($qlist as $idx => $q)
1413
{
1414
	if (isset($ifdisp[$q]))
1415
		$qlist[$idx] = $ifdisp[$q];
1416
}
1417

    
1418
$group->add(new Form_Select(
1419
	'ackqueue',
1420
	'Ackqueue',
1421
	$pconfig['ackqueue'],
1422
	$qlist
1423
));
1424

    
1425
$group->add(new Form_Select(
1426
	'defaultqueue',
1427
	'Default Queue',
1428
	$pconfig['defaultqueue'],
1429
	$qlist
1430
));
1431

    
1432
$section->add($group)->setHelp('Choose the Acknowledge Queue only if you have '.
1433
	'selected Queue.'
1434
);
1435

    
1436
$section->addInput(new Form_Select(
1437
	'l7container',
1438
	'Layer7',
1439
	$pconfig['l7container'],
1440
	array_keys($l7clist)
1441
))->setHelp('Choose a Layer7 container to apply application protocol inspection '.
1442
	'rules. These are valid for TCP and UDP protocols only.');
1443

    
1444
$has_created_time = (isset($a_filter[$id]['created']) && is_array($a_filter[$id]['created']));
1445
$has_updated_time = (isset($a_filter[$id]['updated']) && is_array($a_filter[$id]['updated']));
1446

    
1447

    
1448
if ($has_created_time || $has_updated_time)
1449
{
1450
	$form->add($section);
1451
	$section = new Form_Section('Rule Information');
1452

    
1453
	if ($has_created_time)
1454
	{
1455
		$section->addInput(new Form_StaticText(
1456
			'Created',
1457
			date('n/j/y H:i:s', $a_filter[$id]['created']['time']) . gettext('by') .'<b>'. $a_filter[$id]['created']['username'] .'</b>'
1458
		));
1459
	}
1460

    
1461
	if ($has_updated_time)
1462
	{
1463
		$section->addInput(new Form_StaticText(
1464
			'Updated',
1465
			date('n/j/y H:i:s', $a_filter[$id]['updated']['time']) . gettext('by') .'<b>'. $a_filter[$id]['updated']['username'] .'</b>'
1466
		));
1467
	}
1468
}
1469

    
1470
$form->add($section);
1471
echo $form;
1472

    
1473
include("foot.inc");
(61-61/241)