]>
git.ipfire.org Git - people/mfischer/ipfire-2.x.git/blob - html/cgi-bin/connections.cgi
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2007-2012 IPFire Team <info@ipfire.org> #
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. #
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. #
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/>. #
20 ###############################################################################
23 use experimental
'smartmatch';
25 use Net
::IPv4Addr
qw( :all );
28 # enable only the following on debugging purpose
30 #use CGI::Carp 'fatalsToBrowser';
32 require '/var/ipfire/general-functions.pl';
33 require "${General::swroot}/lang.pl";
34 require "${General::swroot}/header.pl";
35 require "${General::swroot}/location-functions.pl";
37 my $colour_multicast = "#A0A0A0";
39 # sort arguments for connection tracking table
40 # the sort field. eg. 1=src IP, 2=dst IP, 3=src port, 4=dst port
42 # the sort order. (a)scending orr (d)escending
49 # retrieve query arguments
50 # note: let a-z A-Z and 0-9 pass as value only
51 if (length ($ENV{'QUERY_STRING'}) > 0){
54 my $buffer = $ENV{'QUERY_STRING'};
55 my @pairs = split(/&/, $buffer);
56 foreach my $pair (@pairs){
57 ($name, $value) = split(/=/, $pair);
58 $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; # e.g. "%20" => " "
59 $value =~ s/[^a-zA-Z0-9]*//g; # a-Z 0-9 will pass
60 $cgiin{$name} = $value;
64 &Header
::showhttpheaders
();
71 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
73 # output cgi query arrguments to browser on debug
75 &Header
::openbox
('100%', 'center', 'DEBUG');
77 foreach my $line (sort keys %cgiin) {
78 print "$line = '$cgiin{$line}'<br />\n";
81 print " Count: $debugCount\n";
85 #workaround to suppress a warning when a variable is used only once
86 my @dummy = ( ${Header
::table1colour
} );
89 # check sorting arguments
90 if ( $cgiin{'sort_field'} ~~ [ '1','2','3','4','5','6','7','8','9' ] ) {
91 $SORT_FIELD = $cgiin{'sort_field'};
93 if ( $cgiin{'sort_order'} ~~ [ 'a','d','A','D' ] ) {
94 $SORT_ORDER = lc($cgiin{'sort_order'});
98 # Read and sort the connection tracking table
100 if ($SORT_FIELD and $SORT_ORDER) {
101 # field sorting when sorting arguments are sane
102 open(CONNTRACK
, "/usr/local/bin/getconntracktable | /usr/local/bin/consort.sh $SORT_FIELD $SORT_ORDER |") or die "Unable to read conntrack table";
104 # default sorting with no query arguments
105 open(CONNTRACK
, "/usr/local/bin/getconntracktable | sort -k 5,5 --numeric-sort --reverse |") or die "Unable to read conntrack table";
107 my @conntrack = <CONNTRACK
>;
110 # Collect data for the @network array.
112 # Add Firewall Localhost 127.0.0.1
113 push(@network, '127.0.0.1');
114 push(@masklen, '255.255.255.255');
115 push(@colour, ${Header
::colourfw
});
117 if (open(IP
, "${General::swroot}/red/local-ipaddress")) {
122 push(@network, $redip);
123 push(@masklen, '255.255.255.255');
124 push(@colour, ${Header
::colourfw
});
127 # Add STATIC RED aliases
128 if ($netsettings{'RED_DEV'}) {
129 my $aliasfile = "${General::swroot}/ethernet/aliases";
130 open(ALIASES
, $aliasfile) or die 'Unable to open aliases file.';
131 my @aliases = <ALIASES
>;
134 # We have a RED eth iface
135 if ($netsettings{'RED_TYPE'} eq 'STATIC') {
136 # We have a STATIC RED eth iface
137 foreach my $line (@aliases) {
139 my @temp = split(/\,/,$line);
141 push(@network, $temp[0]);
142 push(@masklen, $netsettings{'RED_NETMASK'} );
143 push(@colour, ${Header
::colourfw
} );
149 # Add Green Firewall Interface
150 push(@network, $netsettings{'GREEN_ADDRESS'});
151 push(@masklen, "255.255.255.255" );
152 push(@colour, ${Header
::colourfw
} );
154 # Add Green Network to Array
155 push(@network, $netsettings{'GREEN_NETADDRESS'});
156 push(@masklen, $netsettings{'GREEN_NETMASK'} );
157 push(@colour, ${Header
::colourgreen
} );
159 # Add Green Routes to Array
160 my @routes = `/sbin/route -n | /bin/grep $netsettings{'GREEN_DEV'}`;
161 foreach my $route (@routes) {
163 my @temp = split(/[\t ]+/, $route);
164 push(@network, $temp[0]);
165 push(@masklen, $temp[2]);
166 push(@colour, ${Header
::colourgreen
} );
169 # Add Blue Firewall Interface
170 push(@network, $netsettings{'BLUE_ADDRESS'});
171 push(@masklen, "255.255.255.255" );
172 push(@colour, ${Header
::colourfw
} );
175 if ($netsettings{'BLUE_DEV'}) {
176 push(@network, $netsettings{'BLUE_NETADDRESS'});
177 push(@masklen, $netsettings{'BLUE_NETMASK'} );
178 push(@colour, ${Header
::colourblue
} );
180 # Add Blue Routes to Array
181 @routes = `/sbin/route -n | /bin/grep $netsettings{'BLUE_DEV'}`;
182 foreach my $route (@routes) {
184 my @temp = split(/[\t ]+/, $route);
185 push(@network, $temp[0]);
186 push(@masklen, $temp[2]);
187 push(@colour, ${Header
::colourblue
} );
191 # Add Orange Firewall Interface
192 push(@network, $netsettings{'ORANGE_ADDRESS'});
193 push(@masklen, "255.255.255.255" );
194 push(@colour, ${Header
::colourfw
} );
197 if ($netsettings{'ORANGE_DEV'}) {
198 push(@network, $netsettings{'ORANGE_NETADDRESS'});
199 push(@masklen, $netsettings{'ORANGE_NETMASK'} );
200 push(@colour, ${Header
::colourorange
} );
201 # Add Orange Routes to Array
202 @routes = `/sbin/route -n | /bin/grep $netsettings{'ORANGE_DEV'}`;
203 foreach my $route (@routes) {
205 my @temp = split(/[\t ]+/, $route);
206 push(@network, $temp[0]);
207 push(@masklen, $temp[2]);
208 push(@colour, ${Header
::colourorange
} );
212 # Highlight multicast connections.
213 push(@network, "224.0.0.0");
214 push(@masklen, "239.0.0.0");
215 push(@colour, $colour_multicast);
217 # Add OpenVPN net and RED/BLUE/ORANGE entry (when appropriate)
218 if (-e
"${General::swroot}/ovpn/settings") {
219 my %ovpnsettings = ();
220 &General
::readhash
("${General::swroot}/ovpn/settings", \
%ovpnsettings);
221 my @tempovpnsubnet = split("\/",$ovpnsettings{'DOVPN_SUBNET'});
224 push(@network, $tempovpnsubnet[0]);
225 push(@masklen, $tempovpnsubnet[1]);
226 push(@colour, ${Header
::colourovpn
} );
228 # add BLUE:port / proto
229 if (($ovpnsettings{'ENABLED_BLUE'} eq 'on') && $netsettings{'BLUE_DEV'}) {
230 push(@network, $netsettings{'BLUE_ADDRESS'} );
231 push(@masklen, '255.255.255.255' );
232 push(@colour, ${Header
::colourovpn
});
235 # add ORANGE:port / proto
236 if (($ovpnsettings{'ENABLED_ORANGE'} eq 'on') && $netsettings{'ORANGE_DEV'}) {
237 push(@network, $netsettings{'ORANGE_ADDRESS'} );
238 push(@masklen, '255.255.255.255' );
239 push(@colour, ${Header
::colourovpn
} );
243 # Add OpenVPN net for custom OVPNs
244 if (-e
"${General::swroot}/ovpn/ccd.conf") {
245 open(OVPNSUB
, "${General::swroot}/ovpn/ccd.conf");
246 my @ovpnsub = <OVPNSUB
>;
250 my ($network, $mask) = split '/', (split ',', $_)[2];
252 $mask = ipv4_cidr2msk
($mask) unless &General
::validip
($mask);
254 push(@network, $network);
255 push(@masklen, $mask);
256 push(@colour, ${Header
::colourovpn
});
260 open(IPSEC
, "${General::swroot}/vpn/config");
264 foreach my $line (@ipsec) {
265 my @vpn = split(',', $line);
267 my @subnets = split(/\|/, $vpn[12]);
268 for my $subnet (@subnets) {
269 my ($network, $mask) = split("/", $subnet);
271 if (!&General
::validip
($mask)) {
272 $mask = ipv4_cidr2msk
($mask);
275 push(@network, $network);
276 push(@masklen, $mask);
277 push(@colour, ${Header
::colourvpn
});
281 if (-e
"${General::swroot}/ovpn/n2nconf") {
282 open(OVPNN2N
, "${General::swroot}/ovpn/ovpnconfig");
283 my @ovpnn2n = <OVPNN2N
>;
286 foreach my $line (@ovpnn2n) {
287 my @ovpn = split(',', $line);
288 next if ($ovpn[4] ne 'net');
290 my ($network, $mask) = split("/", $ovpn[12]);
291 if (!&General
::validip
($mask)) {
292 $mask = ipv4_cidr2msk
($mask);
295 push(@network, $network);
296 push(@masklen, $mask);
297 push(@colour, ${Header
::colourovpn
});
302 &Header
::openpage
($Lang::tr
{'connections'}, 1, '');
303 &Header
::openbigbox
('100%', 'left');
304 &Header
::openbox
('100%', 'left', $Lang::tr
{'connection tracking'});
308 <table style='width:100%'>
310 <td style='text-align:center;'>
311 <b>$Lang::tr{'legend'} :</b>
313 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourgreen}; font-weight:bold;'>
314 <b>$Lang::tr{'lan'}</b>
316 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourred};'>
317 <b>$Lang::tr{'internet'}</b>
319 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourorange};'>
320 <b>$Lang::tr{'dmz'}</b>
322 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourblue};'>
323 <b>$Lang::tr{'wireless'}</b>
325 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourfw};'>
328 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourvpn};'>
329 <b>$Lang::tr{'vpn'}</b>
331 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourovpn};'>
332 <b>$Lang::tr{'OpenVPN'}</b>
334 <td style='text-align:center; color:#FFFFFF; background-color:$colour_multicast;'>
342 if ($SORT_FIELD and $SORT_ORDER) {
343 my @sort_field_name = (
344 $Lang::tr
{'source ip'},
345 $Lang::tr
{'destination ip'},
346 $Lang::tr
{'source port'},
347 $Lang::tr
{'destination port'},
348 $Lang::tr
{'protocol'},
349 $Lang::tr
{'connection'}.' '.$Lang::tr
{'status'},
350 $Lang::tr
{'expires'}.' ('.$Lang::tr
{'seconds'}.')',
351 $Lang::tr
{'download'},
355 if (lc($SORT_ORDER) eq "a") {
356 $sort_order_name = $Lang::tr
{'sort ascending'};
358 $sort_order_name = $Lang::tr
{'sort descending'};
362 <div style="font-weight:bold;margin:10px;font-size: 70%">
363 $sort_order_name: $sort_field_name[$SORT_FIELD-1]
369 # Print table header.
371 <table style='width:100%'>
373 <th style='text-align:center'>
374 <a href="?sort_field=5&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
375 <a href="?sort_field=5&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
377 <th style='text-align:center' colspan='2'>
378 <a href="?sort_field=1&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
379 <a href="?sort_field=1&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
380
381 <a href="?sort_field=3&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
382 <a href="?sort_field=3&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
385 <th style='text-align:center' colspan='2'>
386 <a href="?sort_field=2&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
387 <a href="?sort_field=2&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
388
389 <a href="?sort_field=4&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
390 <a href="?sort_field=4&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
393 <th style='text-align:center'>
394 <a href="?sort_field=8&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
395 <a href="?sort_field=8&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
396
397 <a href="?sort_field=9&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
398 <a href="?sort_field=9&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
400 <th style='text-align:center'>
401 <a href="?sort_field=6&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
402 <a href="?sort_field=6&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
404 <th style='text-align:center'>
405 <a href="?sort_field=7&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
406 <a href="?sort_field=7&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
410 <th style='text-align:center'>
411 $Lang::tr{'protocol'}
413 <th style='text-align:center' colspan='2'>
414 $Lang::tr{'source ip and port'}
416 <th style='text-align:center'>
419 <th style='text-align:center' colspan='2'>
420 $Lang::tr{'dest ip and port'}
422 <th style='text-align:center'>
425 <th style='text-align:center'>
426 $Lang::tr{'download'} /
427 <br>$Lang::tr{'upload'}
429 <th style='text-align:center'>
430 $Lang::tr{'connection'}<br>$Lang::tr{'status'}
432 <th style='text-align:center'>
433 $Lang::tr{'expires'}<br>($Lang::tr{'seconds'})
438 foreach my $line (@conntrack) {
439 my @conn = split(' ', $line);
441 # The first bit is the l3 protocol.
442 my $l3proto = $conn[0];
444 # Skip everything that is not IPv4.
445 if ($l3proto ne 'ipv4') {
449 # L4 protocol (tcp, udp, ...).
450 my $l4proto = $conn[2];
452 # Translate unknown protocols.
453 if ($l4proto eq 'unknown') {
454 my $l4protonum = $conn[3];
455 if ($l4protonum eq '2') {
457 } elsif ($l4protonum eq '4') {
458 $l4proto = 'IPv4 Encap';
459 } elsif ($l4protonum eq '33') {
461 } elsif ($l4protonum eq '41') {
462 $l4proto = 'IPv6 Encap';
463 } elsif ($l4protonum eq '50') {
465 } elsif ($l4protonum eq '51') {
467 } elsif ($l4protonum eq '132') {
470 $l4proto = $l4protonum;
473 $l4proto = uc($l4proto);
476 # Source and destination.
490 if ($l4proto eq 'TCP') {
494 # Kick out everything that is not IPv4.
495 foreach my $item (@conn) {
496 my ($key, $val) = split('=', $item);
528 push(@packets, $val);
536 my $sip_colour = ipcolour
($sip);
537 # use colour of destination network for DNAT
538 my $dip_colour = $dip ne $dip_ret ? ipcolour
($dip_ret) : ipcolour
($dip);
542 $sserv = uc(getservbyport($sport, lc($l4proto)));
547 $dserv = uc(getservbyport($dport, lc($l4proto)));
550 my $bytes_in = format_bytes
($bytes[0]);
551 my $bytes_out = format_bytes
($bytes[1]);
553 # enumerate location information
554 my $srcccode = &Location
::Functions
::lookup_country_code
($sip_ret);
555 my $src_flag_icon = &Location
::Functions
::get_flag_icon
($srcccode);
556 my $dstccode = &Location
::Functions
::lookup_country_code
($dip_ret);
557 my $dst_flag_icon = &Location
::Functions
::get_flag_icon
($dstccode);
560 $ttl = format_time
($ttl);
563 if ($sip_ret && $sip ne $sip_ret) {
564 $sip_extra = "<span style='color:#FFFFFF;'>></span> ";
565 $sip_extra .= "<a href='/cgi-bin/ipinfo.cgi?ip=$sip_ret'>";
566 $sip_extra .= " <span style='color:#FFFFFF;'>$sip_ret</span>";
567 $sip_extra .= "</a>";
571 if ($dip_ret && $dip ne $dip_ret) {
572 $dip_extra = "<span style='color:#FFFFFF;'>></span> ";
573 $dip_extra .= "<a href='/cgi-bin/ipinfo.cgi?ip=$dip_ret'>";
574 $dip_extra .= " <span style='color:#FFFFFF;'>$dip_ret</span>";
575 $dip_extra .= "</a>";
580 if ($sport ne $sport_ret) {
582 if ($sport_ret < 1024) {
583 $sserv_ret = uc(getservbyport($sport_ret, lc($l4proto)));
586 $sport_extra = "<span style='color:#FFFFFF;'>></span> ";
587 $sport_extra .= "<a href='http://isc.sans.org/port_details.php?port=$sport_ret' target='top' title='$sserv_ret'>";
588 $sport_extra .= " <span style='color:#FFFFFF;'>$sport_ret</span>";
589 $sport_extra .= "</a>";
593 if ($dport ne $dport_ret) {
595 if ($dport_ret < 1024) {
596 $dserv_ret = uc(getservbyport($dport_ret, lc($l4proto)));
599 $dport_extra = "<span style='color:#FFFFFF;'>></span> ";
600 $dport_extra .= "<a href='http://isc.sans.org/port_details.php?port=$dport_ret' target='top' title='$dserv_ret'>";
601 $dport_extra .= " <span style='color:#FFFFFF;'>$dport_ret</span>";
602 $dport_extra .= "</a>";
607 <td style='text-align:center'>$l4proto</td>
608 <td style='text-align:center; background-color:$sip_colour;'>
609 <a href='/cgi-bin/ipinfo.cgi?ip=$sip'>
610 <span style='color:#FFFFFF;'>$sip</span>
614 <td style='text-align:center; background-color:$sip_colour;'>
615 <a href='http://isc.sans.org/port_details.php?port=$sport' target='top' title='$sserv'>
616 <span style='color:#FFFFFF;'>$sport</span>
620 <td style='text-align:center; background-color:$sip_colour;'>
621 <a href='country.cgi#$srcccode'><img src='$src_flag_icon' border='0' align='absmiddle' alt='$srcccode' title='$srcccode' /></a>
623 <td style='text-align:center; background-color:$dip_colour;'>
624 <a href='/cgi-bin/ipinfo.cgi?ip=$dip'>
625 <span style='color:#FFFFFF;'>$dip</span>
629 <td style='text-align:center; background-color:$dip_colour;'>
630 <a href='http://isc.sans.org/port_details.php?port=$dport' target='top' title='$dserv'>
631 <span style='color:#FFFFFF;'>$dport</span>
635 <td style='text-align:center; background-color:$sip_colour;'>
636 <a href='country.cgi#$dstccode'><img src='$dst_flag_icon' border='0' align='absmiddle' alt='$dstccode' title='$dstccode' /></a>
638 <td style='text-align:center'>
639 $bytes_in / $bytes_out
641 <td style='text-align:center'>$state</td>
642 <td style='text-align:center'>$ttl</td>
647 # Close the main table.
651 &Header
::closebigbox
();
652 &Header
::closepage
();
654 sub format_bytes
($) {
656 my @units = ("B", "k", "M", "G", "T");
658 foreach my $unit (@units) {
660 return sprintf("%d%s", $bytes, $unit);
666 return sprintf("%d%s", $bytes, $units[$#units]);
672 my $seconds = $time % 60;
673 my $minutes = $time / 60;
676 if ($minutes >= 60) {
677 $hours = $minutes / 60;
681 return sprintf("%3d:%02d:%02d", $hours, $minutes, $seconds);
686 my $colour = ${Header
::colourred
};
691 foreach my $line (@network) {
692 if ($network[$id] eq '') {
695 if (!$found && ipv4_in_network
($network[$id], $masklen[$id], $ip) ) {
697 $colour = $colour[$id];