]>
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 # Call safe system_output function to get all available routes.
150 my @all_routes = &General
::system_output
("/sbin/route", "-n");
152 # Add Green Firewall Interface
153 push(@network, $netsettings{'GREEN_ADDRESS'});
154 push(@masklen, "255.255.255.255" );
155 push(@colour, ${Header
::colourfw
} );
157 # Add Green Network to Array
158 push(@network, $netsettings{'GREEN_NETADDRESS'});
159 push(@masklen, $netsettings{'GREEN_NETMASK'} );
160 push(@colour, ${Header
::colourgreen
} );
162 # Add Green Routes to Array
163 my @routes = grep (/$netsettings{'GREEN_DEV'}/, @all_routes);
164 foreach my $route (@routes) {
166 my @temp = split(/[\t ]+/, $route);
167 push(@network, $temp[0]);
168 push(@masklen, $temp[2]);
169 push(@colour, ${Header
::colourgreen
} );
172 # Add Blue Firewall Interface
173 push(@network, $netsettings{'BLUE_ADDRESS'});
174 push(@masklen, "255.255.255.255" );
175 push(@colour, ${Header
::colourfw
} );
178 if ($netsettings{'BLUE_DEV'}) {
179 push(@network, $netsettings{'BLUE_NETADDRESS'});
180 push(@masklen, $netsettings{'BLUE_NETMASK'} );
181 push(@colour, ${Header
::colourblue
} );
183 # Add Blue Routes to Array
184 @routes = grep(/$netsettings{'BLUE_DEV'}/, @all_routes);
185 foreach my $route (@routes) {
187 my @temp = split(/[\t ]+/, $route);
188 push(@network, $temp[0]);
189 push(@masklen, $temp[2]);
190 push(@colour, ${Header
::colourblue
} );
194 # Add Orange Firewall Interface
195 push(@network, $netsettings{'ORANGE_ADDRESS'});
196 push(@masklen, "255.255.255.255" );
197 push(@colour, ${Header
::colourfw
} );
200 if ($netsettings{'ORANGE_DEV'}) {
201 push(@network, $netsettings{'ORANGE_NETADDRESS'});
202 push(@masklen, $netsettings{'ORANGE_NETMASK'} );
203 push(@colour, ${Header
::colourorange
} );
204 # Add Orange Routes to Array
205 @routes = grep(/$netsettings{'ORANGE_DEV'}/, @all_routes);
206 foreach my $route (@routes) {
208 my @temp = split(/[\t ]+/, $route);
209 push(@network, $temp[0]);
210 push(@masklen, $temp[2]);
211 push(@colour, ${Header
::colourorange
} );
215 # Highlight multicast connections.
216 push(@network, "224.0.0.0");
217 push(@masklen, "239.0.0.0");
218 push(@colour, $colour_multicast);
220 # Add OpenVPN net and RED/BLUE/ORANGE entry (when appropriate)
221 if (-e
"${General::swroot}/ovpn/settings") {
222 my %ovpnsettings = ();
223 &General
::readhash
("${General::swroot}/ovpn/settings", \
%ovpnsettings);
224 my @tempovpnsubnet = split("\/",$ovpnsettings{'DOVPN_SUBNET'});
227 push(@network, $tempovpnsubnet[0]);
228 push(@masklen, $tempovpnsubnet[1]);
229 push(@colour, ${Header
::colourovpn
} );
231 # add BLUE:port / proto
232 if (($ovpnsettings{'ENABLED_BLUE'} eq 'on') && $netsettings{'BLUE_DEV'}) {
233 push(@network, $netsettings{'BLUE_ADDRESS'} );
234 push(@masklen, '255.255.255.255' );
235 push(@colour, ${Header
::colourovpn
});
238 # add ORANGE:port / proto
239 if (($ovpnsettings{'ENABLED_ORANGE'} eq 'on') && $netsettings{'ORANGE_DEV'}) {
240 push(@network, $netsettings{'ORANGE_ADDRESS'} );
241 push(@masklen, '255.255.255.255' );
242 push(@colour, ${Header
::colourovpn
} );
246 # Add OpenVPN net for custom OVPNs
247 if (-e
"${General::swroot}/ovpn/ccd.conf") {
248 open(OVPNSUB
, "${General::swroot}/ovpn/ccd.conf");
249 my @ovpnsub = <OVPNSUB
>;
253 my ($network, $mask) = split '/', (split ',', $_)[2];
255 $mask = ipv4_cidr2msk
($mask) unless &General
::validip
($mask);
257 push(@network, $network);
258 push(@masklen, $mask);
259 push(@colour, ${Header
::colourovpn
});
263 open(IPSEC
, "${General::swroot}/vpn/config");
267 foreach my $line (@ipsec) {
268 my @vpn = split(',', $line);
270 my @subnets = split(/\|/, $vpn[12]);
271 for my $subnet (@subnets) {
272 my ($network, $mask) = split("/", $subnet);
274 if (!&General
::validip
($mask)) {
275 $mask = ipv4_cidr2msk
($mask);
278 push(@network, $network);
279 push(@masklen, $mask);
280 push(@colour, ${Header
::colourvpn
});
284 if (-e
"${General::swroot}/ovpn/n2nconf") {
285 open(OVPNN2N
, "${General::swroot}/ovpn/ovpnconfig");
286 my @ovpnn2n = <OVPNN2N
>;
289 foreach my $line (@ovpnn2n) {
290 my @ovpn = split(',', $line);
291 next if ($ovpn[4] ne 'net');
293 my ($network, $mask) = split("/", $ovpn[12]);
294 if (!&General
::validip
($mask)) {
295 $mask = ipv4_cidr2msk
($mask);
298 push(@network, $network);
299 push(@masklen, $mask);
300 push(@colour, ${Header
::colourovpn
});
305 &Header
::openpage
($Lang::tr
{'connections'}, 1, '');
306 &Header
::openbigbox
('100%', 'left');
307 &Header
::openbox
('100%', 'left', $Lang::tr
{'connection tracking'});
311 <table style='width:100%'>
313 <td style='text-align:center;'>
314 <b>$Lang::tr{'legend'} :</b>
316 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourgreen}; font-weight:bold;'>
317 <b>$Lang::tr{'lan'}</b>
319 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourred};'>
320 <b>$Lang::tr{'internet'}</b>
322 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourorange};'>
323 <b>$Lang::tr{'dmz'}</b>
325 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourblue};'>
326 <b>$Lang::tr{'wireless'}</b>
328 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourfw};'>
331 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourvpn};'>
332 <b>$Lang::tr{'vpn'}</b>
334 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourovpn};'>
335 <b>$Lang::tr{'OpenVPN'}</b>
337 <td style='text-align:center; color:#FFFFFF; background-color:$colour_multicast;'>
345 if ($SORT_FIELD and $SORT_ORDER) {
346 my @sort_field_name = (
347 $Lang::tr
{'source ip'},
348 $Lang::tr
{'destination ip'},
349 $Lang::tr
{'source port'},
350 $Lang::tr
{'destination port'},
351 $Lang::tr
{'protocol'},
352 $Lang::tr
{'connection'}.' '.$Lang::tr
{'status'},
353 $Lang::tr
{'expires'}.' ('.$Lang::tr
{'seconds'}.')',
354 $Lang::tr
{'download'},
358 if (lc($SORT_ORDER) eq "a") {
359 $sort_order_name = $Lang::tr
{'sort ascending'};
361 $sort_order_name = $Lang::tr
{'sort descending'};
365 <div style="font-weight:bold;margin:10px;font-size: 70%">
366 $sort_order_name: $sort_field_name[$SORT_FIELD-1]
372 # Print table header.
374 <table style='width:100%'>
376 <th style='text-align:center'>
377 <a href="?sort_field=5&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
378 <a href="?sort_field=5&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
380 <th style='text-align:center' colspan='2'>
381 <a href="?sort_field=1&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
382 <a href="?sort_field=1&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
383
384 <a href="?sort_field=3&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
385 <a href="?sort_field=3&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
388 <th style='text-align:center' colspan='2'>
389 <a href="?sort_field=2&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
390 <a href="?sort_field=2&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
391
392 <a href="?sort_field=4&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
393 <a href="?sort_field=4&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
396 <th style='text-align:center'>
397 <a href="?sort_field=8&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
398 <a href="?sort_field=8&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
399
400 <a href="?sort_field=9&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
401 <a href="?sort_field=9&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
403 <th style='text-align:center'>
404 <a href="?sort_field=6&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
405 <a href="?sort_field=6&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
407 <th style='text-align:center'>
408 <a href="?sort_field=7&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
409 <a href="?sort_field=7&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
413 <th style='text-align:center'>
414 $Lang::tr{'protocol'}
416 <th style='text-align:center' colspan='2'>
417 $Lang::tr{'source ip and port'}
419 <th style='text-align:center'>
422 <th style='text-align:center' colspan='2'>
423 $Lang::tr{'dest ip and port'}
425 <th style='text-align:center'>
428 <th style='text-align:center'>
429 $Lang::tr{'download'} /
430 <br>$Lang::tr{'upload'}
432 <th style='text-align:center'>
433 $Lang::tr{'connection'}<br>$Lang::tr{'status'}
435 <th style='text-align:center'>
436 $Lang::tr{'expires'}<br>($Lang::tr{'seconds'})
441 foreach my $line (@conntrack) {
442 my @conn = split(' ', $line);
444 # The first bit is the l3 protocol.
445 my $l3proto = $conn[0];
447 # Skip everything that is not IPv4.
448 if ($l3proto ne 'ipv4') {
452 # L4 protocol (tcp, udp, ...).
453 my $l4proto = $conn[2];
455 # Translate unknown protocols.
456 if ($l4proto eq 'unknown') {
457 my $l4protonum = $conn[3];
458 if ($l4protonum eq '2') {
460 } elsif ($l4protonum eq '4') {
461 $l4proto = 'IPv4 Encap';
462 } elsif ($l4protonum eq '33') {
464 } elsif ($l4protonum eq '41') {
465 $l4proto = 'IPv6 Encap';
466 } elsif ($l4protonum eq '50') {
468 } elsif ($l4protonum eq '51') {
470 } elsif ($l4protonum eq '132') {
473 $l4proto = $l4protonum;
476 $l4proto = uc($l4proto);
479 # Source and destination.
493 if ($l4proto eq 'TCP') {
497 # Kick out everything that is not IPv4.
498 foreach my $item (@conn) {
499 my ($key, $val) = split('=', $item);
531 push(@packets, $val);
539 my $sip_colour = ipcolour
($sip);
540 # use colour of destination network for DNAT
541 my $dip_colour = $dip ne $dip_ret ? ipcolour
($dip_ret) : ipcolour
($dip);
545 $sserv = uc(getservbyport($sport, lc($l4proto)));
550 $dserv = uc(getservbyport($dport, lc($l4proto)));
553 my $bytes_in = format_bytes
($bytes[0]);
554 my $bytes_out = format_bytes
($bytes[1]);
556 # enumerate location information
557 my $srcccode = &Location
::Functions
::lookup_country_code
($sip_ret);
558 my $src_flag_icon = &Location
::Functions
::get_flag_icon
($srcccode);
559 my $dstccode = &Location
::Functions
::lookup_country_code
($dip_ret);
560 my $dst_flag_icon = &Location
::Functions
::get_flag_icon
($dstccode);
563 $ttl = format_time
($ttl);
566 if ($sip_ret && $sip ne $sip_ret) {
567 $sip_extra = "<span style='color:#FFFFFF;'>></span> ";
568 $sip_extra .= "<a href='/cgi-bin/ipinfo.cgi?ip=$sip_ret'>";
569 $sip_extra .= " <span style='color:#FFFFFF;'>$sip_ret</span>";
570 $sip_extra .= "</a>";
574 if ($dip_ret && $dip ne $dip_ret) {
575 $dip_extra = "<span style='color:#FFFFFF;'>></span> ";
576 $dip_extra .= "<a href='/cgi-bin/ipinfo.cgi?ip=$dip_ret'>";
577 $dip_extra .= " <span style='color:#FFFFFF;'>$dip_ret</span>";
578 $dip_extra .= "</a>";
583 if ($sport ne $sport_ret) {
585 if ($sport_ret < 1024) {
586 $sserv_ret = uc(getservbyport($sport_ret, lc($l4proto)));
589 $sport_extra = "<span style='color:#FFFFFF;'>></span> ";
590 $sport_extra .= "<a href='http://isc.sans.org/port_details.php?port=$sport_ret' target='top' title='$sserv_ret'>";
591 $sport_extra .= " <span style='color:#FFFFFF;'>$sport_ret</span>";
592 $sport_extra .= "</a>";
596 if ($dport ne $dport_ret) {
598 if ($dport_ret < 1024) {
599 $dserv_ret = uc(getservbyport($dport_ret, lc($l4proto)));
602 $dport_extra = "<span style='color:#FFFFFF;'>></span> ";
603 $dport_extra .= "<a href='http://isc.sans.org/port_details.php?port=$dport_ret' target='top' title='$dserv_ret'>";
604 $dport_extra .= " <span style='color:#FFFFFF;'>$dport_ret</span>";
605 $dport_extra .= "</a>";
610 <td style='text-align:center'>$l4proto</td>
611 <td style='text-align:center; background-color:$sip_colour;'>
612 <a href='/cgi-bin/ipinfo.cgi?ip=$sip'>
613 <span style='color:#FFFFFF;'>$sip</span>
617 <td style='text-align:center; background-color:$sip_colour;'>
618 <a href='http://isc.sans.org/port_details.php?port=$sport' target='top' title='$sserv'>
619 <span style='color:#FFFFFF;'>$sport</span>
623 <td style='text-align:center; background-color:$sip_colour;'>
624 <a href='country.cgi#$srcccode'><img src='$src_flag_icon' border='0' align='absmiddle' alt='$srcccode' title='$srcccode' /></a>
626 <td style='text-align:center; background-color:$dip_colour;'>
627 <a href='/cgi-bin/ipinfo.cgi?ip=$dip'>
628 <span style='color:#FFFFFF;'>$dip</span>
632 <td style='text-align:center; background-color:$dip_colour;'>
633 <a href='http://isc.sans.org/port_details.php?port=$dport' target='top' title='$dserv'>
634 <span style='color:#FFFFFF;'>$dport</span>
638 <td style='text-align:center; background-color:$sip_colour;'>
639 <a href='country.cgi#$dstccode'><img src='$dst_flag_icon' border='0' align='absmiddle' alt='$dstccode' title='$dstccode' /></a>
641 <td style='text-align:center'>
642 $bytes_in / $bytes_out
644 <td style='text-align:center'>$state</td>
645 <td style='text-align:center'>$ttl</td>
650 # Close the main table.
654 &Header
::closebigbox
();
655 &Header
::closepage
();
657 sub format_bytes
($) {
659 my @units = ("B", "k", "M", "G", "T");
661 foreach my $unit (@units) {
663 return sprintf("%d%s", $bytes, $unit);
669 return sprintf("%d%s", $bytes, $units[$#units]);
675 my $seconds = $time % 60;
676 my $minutes = $time / 60;
679 if ($minutes >= 60) {
680 $hours = $minutes / 60;
684 return sprintf("%3d:%02d:%02d", $hours, $minutes, $seconds);
689 my $colour = ${Header
::colourred
};
694 foreach my $line (@network) {
695 if ($network[$id] eq '') {
698 if (!$found && ipv4_in_network
($network[$id], $masklen[$id], $ip) ) {
700 $colour = $colour[$id];