]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - html/cgi-bin/connections.cgi
Update connections.cgi: Show byte counters.
[people/pmueller/ipfire-2.x.git] / html / cgi-bin / connections.cgi
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2012 IPFire Team <info@ipfire.org> #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 use strict;
23
24 use Net::IPv4Addr qw( :all );
25 use Switch;
26
27 # enable only the following on debugging purpose
28 #use warnings;
29 #use CGI::Carp 'fatalsToBrowser';
30
31 require '/var/ipfire/general-functions.pl';
32 require "${General::swroot}/lang.pl";
33 require "${General::swroot}/header.pl";
34
35 &Header::showhttpheaders();
36
37 my @network=();
38 my @masklen=();
39 my @colour=();
40
41 my %netsettings=();
42 &General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
43
44 #workaround to suppress a warning when a variable is used only once
45 my @dummy = ( ${Header::table1colour} );
46 undef (@dummy);
47
48 # Read the connection tracking table.
49 open(CONNTRACK, "/usr/local/bin/getconntracktable | sort -k 5,5 --numeric-sort --reverse |") or die "Unable to read conntrack table";
50 my @conntrack = <CONNTRACK>;
51 close(CONNTRACK);
52
53 # Collect data for the @network array.
54
55 # Add Firewall Localhost 127.0.0.1
56 push(@network, '127.0.0.1');
57 push(@masklen, '255.255.255.255');
58 push(@colour, ${Header::colourfw});
59
60 if (open(IP, "${General::swroot}/red/local-ipaddress")) {
61 my $redip = <IP>;
62 close(IP);
63
64 chomp $redip;
65 push(@network, $redip);
66 push(@masklen, '255.255.255.255');
67 push(@colour, ${Header::colourfw});
68 }
69
70 # Add STATIC RED aliases
71 if ($netsettings{'RED_DEV'}) {
72 my $aliasfile = "${General::swroot}/ethernet/aliases";
73 open(ALIASES, $aliasfile) or die 'Unable to open aliases file.';
74 my @aliases = <ALIASES>;
75 close(ALIASES);
76
77 # We have a RED eth iface
78 if ($netsettings{'RED_TYPE'} eq 'STATIC') {
79 # We have a STATIC RED eth iface
80 foreach my $line (@aliases) {
81 chomp($line);
82 my @temp = split(/\,/,$line);
83 if ($temp[0]) {
84 push(@network, $temp[0]);
85 push(@masklen, $netsettings{'RED_NETMASK'} );
86 push(@colour, ${Header::colourfw} );
87 }
88 }
89 }
90 }
91
92 # Add Green Firewall Interface
93 push(@network, $netsettings{'GREEN_ADDRESS'});
94 push(@masklen, "255.255.255.255" );
95 push(@colour, ${Header::colourfw} );
96
97 # Add Green Network to Array
98 push(@network, $netsettings{'GREEN_NETADDRESS'});
99 push(@masklen, $netsettings{'GREEN_NETMASK'} );
100 push(@colour, ${Header::colourgreen} );
101
102 # Add Green Routes to Array
103 my @routes = `/sbin/route -n | /bin/grep $netsettings{'GREEN_DEV'}`;
104 foreach my $route (@routes) {
105 chomp($route);
106 my @temp = split(/[\t ]+/, $route);
107 push(@network, $temp[0]);
108 push(@masklen, $temp[2]);
109 push(@colour, ${Header::colourgreen} );
110 }
111
112 # Add Blue Firewall Interface
113 push(@network, $netsettings{'BLUE_ADDRESS'});
114 push(@masklen, "255.255.255.255" );
115 push(@colour, ${Header::colourfw} );
116
117 # Add Blue Network
118 if ($netsettings{'BLUE_DEV'}) {
119 push(@network, $netsettings{'BLUE_NETADDRESS'});
120 push(@masklen, $netsettings{'BLUE_NETMASK'} );
121 push(@colour, ${Header::colourblue} );
122
123 # Add Blue Routes to Array
124 @routes = `/sbin/route -n | /bin/grep $netsettings{'BLUE_DEV'}`;
125 foreach my $route (@routes) {
126 chomp($route);
127 my @temp = split(/[\t ]+/, $route);
128 push(@network, $temp[0]);
129 push(@masklen, $temp[2]);
130 push(@colour, ${Header::colourblue} );
131 }
132 }
133
134 # Add Orange Network
135 if ($netsettings{'ORANGE_DEV'}) {
136 push(@network, $netsettings{'ORANGE_NETADDRESS'});
137 push(@masklen, $netsettings{'ORANGE_NETMASK'} );
138 push(@colour, ${Header::colourorange} );
139 # Add Orange Routes to Array
140 @routes = `/sbin/route -n | /bin/grep $netsettings{'ORANGE_DEV'}`;
141 foreach my $route (@routes) {
142 chomp($route);
143 my @temp = split(/[\t ]+/, $route);
144 push(@network, $temp[0]);
145 push(@masklen, $temp[2]);
146 push(@colour, ${Header::colourorange} );
147 }
148 }
149
150 # Add OpenVPN net and RED/BLUE/ORANGE entry (when appropriate)
151 if (-e "${General::swroot}/ovpn/settings") {
152 my %ovpnsettings = ();
153 &General::readhash("${General::swroot}/ovpn/settings", \%ovpnsettings);
154 my @tempovpnsubnet = split("\/",$ovpnsettings{'DOVPN_SUBNET'});
155
156 # add OpenVPN net
157 push(@network, $tempovpnsubnet[0]);
158 push(@masklen, $tempovpnsubnet[1]);
159 push(@colour, ${Header::colourovpn} );
160
161 # add BLUE:port / proto
162 if (($ovpnsettings{'ENABLED_BLUE'} eq 'on') && $netsettings{'BLUE_DEV'}) {
163 push(@network, $netsettings{'BLUE_ADDRESS'} );
164 push(@masklen, '255.255.255.255' );
165 push(@colour, ${Header::colourovpn});
166 }
167
168 # add ORANGE:port / proto
169 if (($ovpnsettings{'ENABLED_ORANGE'} eq 'on') && $netsettings{'ORANGE_DEV'}) {
170 push(@network, $netsettings{'ORANGE_ADDRESS'} );
171 push(@masklen, '255.255.255.255' );
172 push(@colour, ${Header::colourovpn} );
173 }
174 }
175
176 open(IPSEC, "/var/ipfire/vpn/config");
177 my @ipsec = <IPSEC>;
178 close(IPSEC);
179
180 foreach my $line (@ipsec) {
181 my @vpn = split(',', $line);
182 my ($network, $mask) = split("/", $vpn[12]);
183
184 if (!&General::validip($mask)) {
185 $mask = ipv4_cidr2msk($mask);
186 }
187
188 push(@network, $network);
189 push(@masklen, $mask);
190 push(@colour, ${Header::colourvpn});
191 }
192
193 # Show the page.
194 &Header::openpage($Lang::tr{'connections'}, 1, '');
195 &Header::openbigbox('100%', 'left');
196 &Header::openbox('100%', 'left', $Lang::tr{'connection tracking'});
197
198 # Print legend.
199 print <<END;
200 <table width='100%'>
201 <tr>
202 <td align='center'>
203 <b>$Lang::tr{'legend'} : </b>
204 </td>
205 <td align='center' bgcolor='${Header::colourgreen}'>
206 <b><font color='#FFFFFF'>$Lang::tr{'lan'}</font></b>
207 </td>
208 <td align='center' bgcolor='${Header::colourred}'>
209 <b><font color='#FFFFFF'>$Lang::tr{'internet'}</font></b>
210 </td>
211 <td align='center' bgcolor='${Header::colourorange}'>
212 <b><font color='#FFFFFF'>$Lang::tr{'dmz'}</font></b>
213 </td>
214 <td align='center' bgcolor='${Header::colourblue}'>
215 <b><font color='#FFFFFF'>$Lang::tr{'wireless'}</font></b>
216 </td>
217 <td align='center' bgcolor='${Header::colourfw}'>
218 <b><font color='#FFFFFF'>IPFire</font></b>
219 </td>
220 <td align='center' bgcolor='${Header::colourvpn}'>
221 <b><font color='#FFFFFF'>$Lang::tr{'vpn'}</font></b>
222 </td>
223 <td align='center' bgcolor='${Header::colourovpn}'>
224 <b><font color='#FFFFFF'>$Lang::tr{'OpenVPN'}</font></b>
225 </td>
226 </tr>
227 </table>
228 <br>
229 END
230
231 # Print table header.
232 print <<END;
233 <table width='100%'>
234 <tr>
235 <th align='center'>
236 $Lang::tr{'protocol'}
237 </th>
238 <th align='center'>
239 $Lang::tr{'source ip and port'}
240 </th>
241 <th>&nbsp;</th>
242 <th align='center'>
243 $Lang::tr{'dest ip and port'}
244 </th>
245 <th>&nbsp;</th>
246 <th align='center'>
247 $Lang::tr{'download'} /
248 <br>$Lang::tr{'upload'}
249 </th>
250 <th align='center'>
251 $Lang::tr{'connection'}<br>$Lang::tr{'status'}
252 </th>
253 <th align='center'>
254 $Lang::tr{'expires'}<br>($Lang::tr{'seconds'})
255 </th>
256 </tr>
257 END
258
259 foreach my $line (@conntrack) {
260 my @conn = split(' ', $line);
261
262 # The first bit is the l3 protocol.
263 my $l3proto = $conn[0];
264
265 # Skip everything that is not IPv4.
266 if ($l3proto ne 'ipv4') {
267 next;
268 }
269
270 # L4 protocol (tcp, udp, ...).
271 my $l4proto = $conn[2];
272
273 if ($l4proto eq 'unknown') {
274 $l4proto = '';
275 }
276
277 # Source and destination.
278 my $sip;
279 my $dip;
280 my $sport;
281 my $dport;
282 my @packets;
283 my @bytes;
284
285 my $ttl = $conn[4];
286 my $state;
287 if ($l4proto eq 'tcp') {
288 $state = $conn[5];
289 }
290
291 # Kick out everything that is not IPv4.
292 foreach my $item (@conn) {
293 my ($key, $val) = split('=', $item);
294
295 switch ($key) {
296 case "src" {
297 $sip = $val;
298 }
299 case "dst" {
300 $dip = $val;
301 }
302 case "sport" {
303 $sport = $val;
304 }
305 case "dport" {
306 $dport = $val;
307 }
308 case "packets" {
309 push(@packets, $val);
310 }
311 case "bytes" {
312 push(@bytes, $val);
313 }
314 }
315 }
316
317 my $sip_colour = ipcolour($sip);
318 my $dip_colour = ipcolour($dip);
319
320 my $sserv = '';
321 if ($sport < 1024) {
322 $sserv = uc(getservbyport($sport, lc($l4proto)));
323 if ($sserv ne '') {
324 $sserv = "&nbsp;($sserv)";
325 }
326 }
327
328 my $dserv = '';
329 if ($dport < 1024) {
330 $dserv = uc(getservbyport($dport, lc($l4proto)));
331 if ($dserv ne '') {
332 $dserv = "&nbsp;($dserv)";
333 }
334 }
335
336 my $bytes_in = format_bytes($bytes[0]);
337 my $bytes_out = format_bytes($bytes[1]);
338
339 # Format TTL
340 $ttl = format_time($ttl);
341
342 print <<END;
343 <tr>
344 <td align='center'>$l4proto</td>
345 <td align='center' bgcolor='$sip_colour'>
346 <a href='/cgi-bin/ipinfo.cgi?ip=$sip'>
347 <font color='#FFFFFF'>$sip</font>
348 </a>
349 </td>
350 <td align='center' bgcolor='$sip_colour'>
351 <a href='http://isc.sans.org/port_details.php?port=$sport' target='top'>
352 <font color='#FFFFFF'>$sport$sserv</font>
353 </a>
354 </td>
355 <td align='center' bgcolor='$dip_colour'>
356 <a href='/cgi-bin/ipinfo.cgi?ip=$dip'>
357 <font color='#FFFFFF'>$dip</font>
358 </a>
359 </td>
360 <td align='center' bgcolor='$dip_colour'>
361 <a href='http://isc.sans.org/port_details.php?port=$dport' target='top'>
362 <font color='#FFFFFF'>$dport$dserv</font>
363 </a>
364 </td>
365 <td align='center'>
366 $bytes_in / $bytes_out
367 </td>
368 <td align='center'>$state</td>
369 <td align='center'>$ttl</td>
370 </tr>
371 END
372 }
373
374 # Close the main table.
375 print "</table>";
376
377 &Header::closebox();
378 &Header::closebigbox();
379 &Header::closepage();
380
381 sub format_bytes($) {
382 my $bytes = shift;
383 my @units = ("B", "k", "M", "G", "T");
384
385 foreach my $unit (@units) {
386 if ($bytes < 1024) {
387 return sprintf("%d%s", $bytes, $unit);
388 }
389
390 $bytes /= 1024;
391 }
392
393 return sprintf("%d%s", $bytes, $units[$#units]);
394 }
395
396 sub format_time($) {
397 my $time = shift;
398
399 my $seconds = $time % 60;
400 my $minutes = $time / 60;
401
402 my $hours = 0;
403 if ($minutes >= 60) {
404 $hours = $minutes / 60;
405 $minutes %= 60;
406 }
407
408 return sprintf("%3d:%02d:%02d", $hours, $minutes, $seconds);
409 }
410
411 sub ipcolour($) {
412 my $id = 0;
413 my $colour = ${Header::colourred};
414 my ($ip) = $_[0];
415 my $found = 0;
416
417 foreach my $line (@network) {
418 if ($network[$id] eq '') {
419 $id++;
420 } else {
421 if (!$found && ipv4_in_network($network[$id], $masklen[$id], $ip) ) {
422 $found = 1;
423 $colour = $colour[$id];
424 }
425 $id++;
426 }
427 }
428
429 return $colour;
430 }
431
432 1;