Project

General

Profile

Download (6.46 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	diag_states_summary.php
4
	Copyright (C) 2013-2015 Electric Sheep Fencing, LP
5
	Copyright (C) 2010-2014 Jim Pingle
6

    
7
	Portions borrowed from diag_dump_states.php:
8
	Copyright (C) 2005-2009 Scott Ullrich
9
	Copyright (C) 2005 Colin Smith
10
	All rights reserved.
11

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

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

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

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

    
34
/*
35
	pfSense_BUILDER_BINARIES:	/sbin/pfctl
36
	pfSense_MODULE: filter
37
*/
38

    
39
##|+PRIV
40
##|*IDENT=page-diagnostics-statessummary
41
##|*NAME=Diagnostics: States Summary page
42
##|*DESCR=Allow access to the 'Diagnostics: States Summary' page.
43
##|*MATCH=diag_states_summary.php*
44
##|-PRIV
45

    
46
exec("/sbin/pfctl -s state", $states);
47

    
48
$srcipinfo = array();
49
$dstipinfo = array();
50
$allipinfo = array();
51
$pairipinfo = array();
52

    
53
function addipinfo(&$iparr, $ip, $proto, $srcport, $dstport) {
54
	$iparr[$ip]['seen']++;
55
	$iparr[$ip]['protos'][$proto]['seen']++;
56
	if (!empty($srcport)) {
57
		$iparr[$ip]['protos'][$proto]['srcports'][$srcport]++;
58
	}
59
	if (!empty($dstport)) {
60
		$iparr[$ip]['protos'][$proto]['dstports'][$dstport]++;
61
	}
62
}
63

    
64
$row = 0;
65
if(count($states) > 0) {
66
	foreach($states as $line) {
67
		$line_split = preg_split("/\s+/", $line);
68
		$iface = array_shift($line_split);
69
		$proto = array_shift($line_split);
70
		$state = array_pop($line_split);
71
		$info  = implode(" ", $line_split);
72

    
73
		/* Handle NAT cases
74
			Replaces an external IP + NAT by the internal IP */
75
		if (strpos($info, ') ->') !== FALSE) {
76
			/* Outbound NAT */
77
			$info = preg_replace('/(\S+) \((\S+)\)/U', "$2", $info);
78
		} elseif (strpos($info, ') <-') !== FALSE) {
79
			/* Inbound NAT/Port Forward */
80
			$info = preg_replace('/(\S+) \((\S+)\)/U', "$1", $info);
81
		}
82

    
83
		/* break up info and extract $srcip and $dstip */
84
		$ends = preg_split("/\<?-\>?/", $info);
85

    
86
		if (strpos($info, '->') === FALSE) {
87
			$srcinfo = $ends[count($ends) - 1];
88
			$dstinfo = $ends[0];
89
		} else {
90
			$srcinfo = $ends[0];
91
			$dstinfo = $ends[count($ends) - 1];
92
		}
93

    
94
		/* Handle IPv6 */
95
		$parts = explode(":", $srcinfo);
96
		$partcount = count($parts);
97
		if ($partcount <= 2) {
98
			$srcip = trim($parts[0]);
99
			$srcport = trim($parts[1]);
100
		} else {
101
			preg_match("/([0-9a-f:]+)(\[([0-9]+)\])?/i", $srcinfo, $matches);
102
			$srcip = $matches[1];
103
			$srcport = trim($matches[3]);
104
		}
105

    
106
		$parts = explode(":", $dstinfo);
107
		$partcount = count($parts);
108
		if ($partcount <= 2) {
109
			$dstip = trim($parts[0]);
110
			$dstport = trim($parts[1]);
111
		} else {
112
			preg_match("/([0-9a-f:]+)(\[([0-9]+)\])?/i", $dstinfo, $matches);
113
			$dstip = $matches[1];
114
			$dstport = trim($matches[3]);
115
		}
116

    
117
		addipinfo($srcipinfo, $srcip, $proto, $srcport, $dstport);
118
		addipinfo($dstipinfo, $dstip, $proto, $srcport, $dstport);
119
		addipinfo($pairipinfo, "{$srcip} -> {$dstip}", $proto, $srcport, $dstport);
120

    
121
		addipinfo($allipinfo, $srcip, $proto, $srcport, $dstport);
122
		addipinfo($allipinfo, $dstip, $proto, $srcport, $dstport);
123
	}
124
}
125

    
126
function sort_by_ip($a, $b) {
127
	return ip2ulong($a) < ip2ulong($b) ? -1 : 1;
128
}
129

    
130
function build_port_info($portarr, $proto) {
131
	if (!$portarr)
132
		return '';
133
	$ports = array();
134
	asort($portarr);
135
	foreach (array_reverse($portarr, TRUE) as $port => $count) {
136
		$service = getservbyport($port, strtolower($proto));
137
		$port = "{$proto}/{$port}";
138
		if ($service)
139
			$port = "{$port} ({$service})";
140
		$ports[] = "{$port}: {$count}";
141
	}
142
	return implode($ports, ', ');
143
}
144

    
145
function print_summary_table($label, $iparr, $sort = TRUE)
146
{
147
	if ($sort)
148
		uksort($iparr, "sort_by_ip");
149

    
150
?>
151
	<div class="panel panel-default">
152
		<div class="panel-heading">
153
			<h2 class="panel-title"><?=$label?></h2>
154
		</div>
155
		<div class="panel-body">
156
			<div class="table-responsive">
157
				<table class="table table-hover table-condensed table-striped">
158
					<thead>
159
						<tr>
160
							<th ><?=gettext("IP");?></th>
161
							<th class="text-center"># <?=gettext("States");?></th>
162
							<th ><?=gettext("Proto");?></th>
163
							<th class="text-center"># <?=gettext("States");?></th>
164
							<th class="text-center"><?=gettext("Src Ports");?></th>
165
							<th class="text-center"><?=gettext("Dst Ports");?></th>
166
						</tr>
167
					</thead>
168
					<tbody>
169
<?php foreach($iparr as $ip => $ipinfo):
170
	$protocolCount = count($ipinfo['protos']);
171
	$rowSpan = '';
172
	$i = 0;
173

    
174
	if ($protocolCount > 1)
175
		$rowSpan = ' rowspan="' . $protocolCount . '"';
176
?>
177
						<tr>
178
							<td<?= $rowSpan ?>><?php echo $ip; ?></td>
179
							<td<?= $rowSpan ?> class="text-center"><?php echo $ipinfo['seen']; ?></td>
180

    
181
<?php foreach($ipinfo['protos'] as $proto => $protoinfo): ?>
182
<?php if ($protocolCount > 1 && $i > 0): ?>
183
							</tr><tr>
184
<?php endif; ?>
185
							<td><?php echo $proto; ?></td>
186
							<td class="text-center" ><?php echo $protoinfo['seen']; ?></td>
187
							<td class="text-center" ><span title="<?php echo build_port_info($protoinfo['srcports'], $proto); ?>"><?php echo count($protoinfo['srcports']); ?></span></td>
188
							<td class="text-center" ><span title="<?php echo build_port_info($protoinfo['dstports'], $proto); ?>"><?php echo count($protoinfo['dstports']); ?></span></td>
189
<?php $i++; endforeach; ?>
190
						</tr>
191
<?php endforeach; ?>
192
					</tbody>
193
				</table>
194
			</div>
195
		</div>
196
	</div>
197
<?php
198
}
199

    
200
$pgtitle = array(gettext("Diagnostics"),gettext("State Table Summary"));
201
require_once("guiconfig.inc");
202
include("head.inc");
203

    
204
print_summary_table(gettext("By Source IP"), $srcipinfo);
205
print_summary_table(gettext("By Destination IP"), $dstipinfo);
206
print_summary_table(gettext("Total per IP"), $allipinfo);
207
print_summary_table(gettext("By IP Pair"), $pairipinfo, FALSE);
208

    
209
include("foot.inc");
(37-37/241)