]> git.ipfire.org Git - people/teissler/ipfire-2.x.git/blobdiff - html/cgi-bin/connections.cgi
tor.cgi: Burst bandwidth may never be less than rate.
[people/teissler/ipfire-2.x.git] / html / cgi-bin / connections.cgi
index 43927f553ae4fae5cd26ab48fe624c8f152647fd..d566cf7ebb29b98bcfdb8bdfe38ea7847eae678a 100644 (file)
@@ -1,23 +1,28 @@
 #!/usr/bin/perl
-#
-# (c) 2001 Jack Beglinger <jackb_guppy@yahoo.com>
-#
-# (c) 2003 Dave Roberts <countzerouk@hotmail.com> - colour coded netfilter/iptables rewrite for 1.3
-#
-# (c) 2006 Franck - add sorting+filtering capability
-#
-# $Id: connections.cgi,v 1.6.2.12 2006/02/27 19:48:46 franck78 Exp $
-#
-
-# Setup GREEN, ORANGE, IPCOP, VPN CIDR networks, masklengths and colours only once
+###############################################################################
+#                                                                             #
+# IPFire.org - A linux based firewall                                         #
+# Copyright (C) 2007-2012  IPFire Team  <info@ipfire.org>                     #
+#                                                                             #
+# This program is free software: you can redistribute it and/or modify        #
+# it under the terms of the GNU General Public License as published by        #
+# the Free Software Foundation, either version 3 of the License, or           #
+# (at your option) any later version.                                         #
+#                                                                             #
+# This program is distributed in the hope that it will be useful,             #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of              #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
+# GNU General Public License for more details.                                #
+#                                                                             #
+# You should have received a copy of the GNU General Public License           #
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
 
-my @network=();
-my @masklen=();
-my @colour=();
+use strict;
 
 use Net::IPv4Addr qw( :all );
-
-use strict;
+use Switch;
 
 # enable only the following on debugging purpose
 #use warnings;
@@ -27,27 +32,117 @@ require '/var/ipfire/general-functions.pl';
 require "${General::swroot}/lang.pl";
 require "${General::swroot}/header.pl";
 
+my $colour_multicast = "#A0A0A0";
+
+# sort arguments for connection tracking table
+# the sort field. eg. 1=src IP, 2=dst IP, 3=src port, 4=dst port
+my $SORT_FIELD = 0;
+# the sort order. (a)scending orr (d)escending
+my $SORT_ORDER = 0;
+# cgi query arguments
+my %cgiin;
+# debug mode
+my $debug = 0;
+
+# retrieve query arguments
+# note: let a-z A-Z and 0-9 pass as value only
+if (length ($ENV{'QUERY_STRING'}) > 0){
+       my $name;
+       my $value;
+       my $buffer = $ENV{'QUERY_STRING'};
+       my @pairs = split(/&/, $buffer);
+       foreach my $pair (@pairs){
+               ($name, $value) = split(/=/, $pair);
+               $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; # e.g. "%20" => " "
+               $value =~ s/[^a-zA-Z0-9]*//g; # a-Z 0-9 will pass
+               $cgiin{$name} = $value; 
+       }
+}
+
+&Header::showhttpheaders();
+
+my @network=();
+my @masklen=();
+my @colour=();
+
+my %netsettings=();
+&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
+
+# output cgi query arrguments to browser on debug
+if ( $debug ){
+       &Header::openbox('100%', 'center', 'DEBUG');
+       my $debugCount = 0;
+       foreach my $line (sort keys %cgiin) {
+               print "$line = '$cgiin{$line}'<br />\n";
+               $debugCount++;
+       }
+       print "&nbsp;Count: $debugCount\n";
+       &Header::closebox();
+}
+
 #workaround to suppress a warning when a variable is used only once
 my @dummy = ( ${Header::table1colour} );
 undef (@dummy);
 
-# Read various files
+# check sorting arguments
+if ( $cgiin{'sort_field'} ~~ [ '1','2','3','4','5','6','7','8','9' ] ) {
+       $SORT_FIELD = $cgiin{'sort_field'};
 
-my %netsettings=();
-&General::readhash("${General::swroot}/ethernet/settings", \%netsettings);
+       if ( $cgiin{'sort_order'} ~~ [ 'a','d','A','D' ] ) {
+               $SORT_ORDER = lc($cgiin{'sort_order'});
+       }
+}
+
+# Read and sort the connection tracking table
+# do sorting 
+if ($SORT_FIELD and $SORT_ORDER) { 
+       # field sorting when sorting arguments are sane
+       open(CONNTRACK, "/usr/local/bin/getconntracktable | /usr/local/bin/consort.sh $SORT_FIELD $SORT_ORDER |") or die "Unable to read conntrack table";
+} else {
+       # default sorting with no query arguments
+       open(CONNTRACK, "/usr/local/bin/getconntracktable | sort -k 5,5 --numeric-sort --reverse |") or die "Unable to read conntrack table";
+}
+my @conntrack = <CONNTRACK>;
+close(CONNTRACK);
 
-open (ACTIVE, "/proc/net/ip_conntrack") or die 'Unable to open ip_conntrack';
-my @active = <ACTIVE>;
-close (ACTIVE);
+# Collect data for the @network array.
+
+# Add Firewall Localhost 127.0.0.1
+push(@network, '127.0.0.1');
+push(@masklen, '255.255.255.255');
+push(@colour, ${Header::colourfw});
 
-my @vpn = ('none');
-open (ACTIVE, "/proc/net/ipsec_eroute") and @vpn = <ACTIVE>;
-close (ACTIVE);
+if (open(IP, "${General::swroot}/red/local-ipaddress")) {
+       my $redip = <IP>;
+       close(IP);
 
-my $aliasfile = "${General::swroot}/ethernet/aliases";
-open(ALIASES, $aliasfile) or die 'Unable to open aliases file.';
-my @aliases = <ALIASES>;
-close(ALIASES);
+       chomp $redip;
+       push(@network, $redip);
+       push(@masklen, '255.255.255.255');
+       push(@colour, ${Header::colourfw});
+}
+
+# Add STATIC RED aliases
+if ($netsettings{'RED_DEV'}) {
+       my $aliasfile = "${General::swroot}/ethernet/aliases";
+       open(ALIASES, $aliasfile) or die 'Unable to open aliases file.';
+       my @aliases = <ALIASES>;
+       close(ALIASES);
+
+       # We have a RED eth iface
+       if ($netsettings{'RED_TYPE'} eq 'STATIC') {
+               # We have a STATIC RED eth iface
+               foreach my $line (@aliases) {
+                       chomp($line);
+                       my @temp = split(/\,/,$line);
+                       if ($temp[0]) {
+                               push(@network, $temp[0]);
+                               push(@masklen, $netsettings{'RED_NETMASK'} );
+                               push(@colour, ${Header::colourfw} );
+                       }
+               }
+       }
+}
 
 # Add Green Firewall Interface
 push(@network, $netsettings{'GREEN_ADDRESS'});
@@ -69,45 +164,33 @@ foreach my $route (@routes) {
        push(@colour, ${Header::colourgreen} );
 }
 
-# Add Firewall Localhost 127.0.0.1
-push(@network, '127.0.0.1');
-push(@masklen, '255.255.255.255' );
+# Add Blue Firewall Interface
+push(@network, $netsettings{'BLUE_ADDRESS'});
+push(@masklen, "255.255.255.255" );
 push(@colour, ${Header::colourfw} );
 
-# Add OpenVPN net and RED/BLUE/ORANGE entry (when appropriate)
-if (-e "${General::swroot}/ovpn/settings") {
-    my %ovpnsettings = ();    
-    &General::readhash("${General::swroot}/ovpn/settings", \%ovpnsettings);
-    my @tempovpnsubnet = split("\/",$ovpnsettings{'DOVPN_SUBNET'});
-
-    # add OpenVPN net
-               push(@network, $tempovpnsubnet[0]);
-               push(@masklen, $tempovpnsubnet[1]);
-               push(@colour, ${Header::colourovpn} );
+# Add Blue Network
+if ($netsettings{'BLUE_DEV'}) {
+       push(@network, $netsettings{'BLUE_NETADDRESS'});
+       push(@masklen, $netsettings{'BLUE_NETMASK'} );
+       push(@colour, ${Header::colourblue} );
 
-    if ( ($ovpnsettings{'ENABLED'} eq 'on') && open(IP, "${General::swroot}/red/local-ipaddress") ) {
-      # add RED:port / proto
-                 my $redip = <IP>;
-               close(IP);
-               chomp $redip;
-                 push(@network, $redip );
-               push(@masklen, '255.255.255.255' );
-               push(@colour, ${Header::colourovpn} );
-    }
-    if ( ($ovpnsettings{'ENABLED_BLUE'} eq 'on') && $netsettings{'BLUE_DEV'} ) {
-      # add BLUE:port / proto
-       push(@network, $netsettings{'BLUE_ADDRESS'} );
-       push(@masklen, '255.255.255.255' );
-               push(@colour, ${Header::colourovpn} );
-    }
-    if ( ($ovpnsettings{'ENABLED_ORANGE'} eq 'on') && $netsettings{'ORANGE_DEV'} ) {
-      # add ORANGE:port / proto
-       push(@network, $netsettings{'ORANGE_ADDRESS'} );
-       push(@masklen, '255.255.255.255' );
-               push(@colour, ${Header::colourovpn} );
-    }
+       # Add Blue Routes to Array
+       @routes = `/sbin/route -n | /bin/grep $netsettings{'BLUE_DEV'}`;
+       foreach my $route (@routes) {
+               chomp($route);
+               my @temp = split(/[\t ]+/, $route);
+               push(@network, $temp[0]);
+               push(@masklen, $temp[2]);
+               push(@colour, ${Header::colourblue} );
+       }
 }
 
+# Add Orange Firewall Interface
+push(@network, $netsettings{'ORANGE_ADDRESS'});
+push(@masklen, "255.255.255.255" );
+push(@colour, ${Header::colourfw} );
+
 # Add Orange Network
 if ($netsettings{'ORANGE_DEV'}) {
        push(@network, $netsettings{'ORANGE_NETADDRESS'});
@@ -116,7 +199,7 @@ if ($netsettings{'ORANGE_DEV'}) {
        # Add Orange Routes to Array
        @routes = `/sbin/route -n | /bin/grep $netsettings{'ORANGE_DEV'}`;
        foreach my $route (@routes) {
-               chomp($route);
+               chomp($route);
                my @temp = split(/[\t ]+/, $route);
                push(@network, $temp[0]);
                push(@masklen, $temp[2]);
@@ -124,419 +207,455 @@ if ($netsettings{'ORANGE_DEV'}) {
        }
 }
 
-# Add Blue Network
-if ($netsettings{'BLUE_DEV'}) {
-       push(@network, $netsettings{'BLUE_NETADDRESS'});
-       push(@masklen, $netsettings{'BLUE_NETMASK'} );
-       push(@colour, ${Header::colourblue} );
-       # Add Blue Routes to Array
-       @routes = `/sbin/route -n | /bin/grep $netsettings{'BLUE_DEV'}`;
-       foreach my $route (@routes) {
-               chomp($route);
-               my @temp = split(/[\t ]+/, $route);
-               push(@network, $temp[0]);
-               push(@masklen, $temp[2]);
-               push(@colour, ${Header::colourblue} );
+# Highlight multicast connections.
+push(@network, "224.0.0.0");
+push(@masklen, "239.0.0.0");
+push(@colour, $colour_multicast);
+
+# Add OpenVPN net and RED/BLUE/ORANGE entry (when appropriate)
+if (-e "${General::swroot}/ovpn/settings") {
+       my %ovpnsettings = ();
+       &General::readhash("${General::swroot}/ovpn/settings", \%ovpnsettings);
+       my @tempovpnsubnet = split("\/",$ovpnsettings{'DOVPN_SUBNET'});
+
+       # add OpenVPN net
+       push(@network, $tempovpnsubnet[0]);
+       push(@masklen, $tempovpnsubnet[1]);
+       push(@colour, ${Header::colourovpn} );
+
+       # add BLUE:port / proto
+       if (($ovpnsettings{'ENABLED_BLUE'} eq 'on') && $netsettings{'BLUE_DEV'}) {
+               push(@network, $netsettings{'BLUE_ADDRESS'} );
+               push(@masklen, '255.255.255.255' );
+               push(@colour, ${Header::colourovpn});
        }
-}
 
-# Add STATIC RED aliases
-if ($netsettings{'RED_DEV'}) {
-       # We have a RED eth iface
-       if ($netsettings{'RED_TYPE'} eq 'STATIC') {
-               # We have a STATIC RED eth iface
-               foreach my $line (@aliases)
-               {
-                       chomp($line);
-                       my @temp = split(/\,/,$line);
-                       if ( $temp[0] ) {
-                               push(@network, $temp[0]);
-                               push(@masklen, $netsettings{'RED_NETMASK'} );
-                               push(@colour, ${Header::colourfw} );
-                       }
-               }
+       # add ORANGE:port / proto
+       if (($ovpnsettings{'ENABLED_ORANGE'} eq 'on') && $netsettings{'ORANGE_DEV'}) {
+               push(@network, $netsettings{'ORANGE_ADDRESS'} );
+               push(@masklen, '255.255.255.255' );
+               push(@colour, ${Header::colourovpn} );
        }
 }
 
-# Add VPNs
-if ( $vpn[0] ne 'none' ) {
-       foreach my $line (@vpn) {
-               my @temp = split(/[\t ]+/,$line);
-               my @temp1 = split(/[\/:]+/,$temp[3]);
-               push(@network, $temp1[0]);
-               push(@masklen, ipv4_cidr2msk($temp1[1]));
-               push(@colour, ${Header::colourvpn} );
+open(IPSEC, "${General::swroot}/vpn/config");
+my @ipsec = <IPSEC>;
+close(IPSEC);
+
+foreach my $line (@ipsec) {
+       my @vpn = split(',', $line);
+       my ($network, $mask) = split("/", $vpn[12]);
+
+       if (!&General::validip($mask)) {
+               $mask = ipv4_cidr2msk($mask);
        }
-}
-if (open(IP, "${General::swroot}/red/local-ipaddress")) {
-       my $redip = <IP>;
-       close(IP);
-       chomp $redip;
-       push(@network, $redip);
-       push(@masklen, '255.255.255.255' );
-       push(@colour, ${Header::colourfw} );
+
+       push(@network, $network);
+       push(@masklen, $mask);
+       push(@colour, ${Header::colourvpn});
 }
 
+if (-e "${General::swroot}/ovpn/n2nconf") {
+       open(OVPNN2N, "${General::swroot}/ovpn/ovpnconfig");
+       my @ovpnn2n = <OVPNN2N>;
+       close(OVPNN2N);
 
-#Establish simple filtering&sorting boxes on top of table
-
-our %cgiparams;
-&Header::getcgihash(\%cgiparams);
-
-my @list_proto = ($Lang::tr{'all'}, 'icmp', 'udp', 'tcp');
-my @list_state = ($Lang::tr{'all'}, 'SYN_SENT', 'SYN_RECV', 'ESTABLISHED', 'FIN_WAIT',
-                   'CLOSE_WAIT', 'LAST_ACK', 'TIME_WAIT', 'CLOSE', 'LISTEN');
-my @list_mark = ($Lang::tr{'all'}, '[ASSURED]', '[UNREPLIED]');
-my @list_sort = ('orgsip','protocol', 'expires', 'status', 'orgdip', 'orgsp',
-                   'orgdp', 'exsip', 'exdip', 'exsp', 'exdp');
-
-# init or silently correct unknown value...
-if ( ! grep ( /^$cgiparams{'SEE_PROTO'}$/ , @list_proto )) { $cgiparams{'SEE_PROTO'} = $list_proto[0] };
-if ( ! grep ( /^$cgiparams{'SEE_STATE'}$/ , @list_state )) { $cgiparams{'SEE_STATE'} = $list_state[0] };
-if ( ! grep ( /^$cgiparams{'SEE_MARK'}$/  , @list_mark ))  { $cgiparams{'SEE_MARK'}  = $list_mark[0] };
-if ( ! grep ( /^$cgiparams{'SEE_SORT'}$/  , @list_sort ))  { $cgiparams{'SEE_SORT'}  = $list_sort[0] };
-# *.*.*.* or a valid IP
-if ( $cgiparams{'SEE_SRC'}  !~ /^(\*\.\*\.\*\.\*\.|\d+\.\d+\.\d+\.\d+)$/) {  $cgiparams{'SEE_SRC'} = '*.*.*.*' };
-if ( $cgiparams{'SEE_DEST'} !~ /^(\*\.\*\.\*\.\*\.|\d+\.\d+\.\d+\.\d+)$/) {  $cgiparams{'SEE_DEST'} = '*.*.*.*' };
-
-
-our %entries = ();     # will hold the lines analyzed correctly
-my $unknownlines = ''; # should be empty all the time...
-my $index = 0;         # just a counter to make unique entryies in entries
-
-foreach my $line (@active) {
-       my $protocol='';
-       my $expires='';
-       my $status='';
-       my $orgsip='';
-       my $orgdip='';
-       my $orgsp='';
-       my $orgdp='';
-       my $exsip='';
-       my $exdip='';
-       my $exsp='';
-       my $exdp='';
-       my $marked='';
-       my $use='';
-
-       chomp($line);
-       my @temp = split(' ',$line);
-
-       if ($temp[0] eq 'icmp') {
-               $protocol  = $temp[0];
-               $status    = $Lang::tr{'all'};
-               $orgsip   = substr $temp[3], 4;
-               $orgdip   = substr $temp[4], 4;
-               $marked   = $temp[8] eq '[UNREPLIED]' ? '[UNREPLIED]' : ' ';
-       }
-       if ($temp[0] eq 'udp') {
-               $protocol  = $temp[0];
-               $status  = $Lang::tr{'all'};
-               $orgsip = substr $temp[3], 4;
-               $orgdip  = substr $temp[4], 4;
-               $marked   = $temp[7] eq '[UNREPLIED]' ? '[UNREPLIED]' : defined ($temp[12]) ? $temp[11] : ' ';
-       }
-       if ($temp[0] eq 'tcp') {
-               $protocol  = $temp[0];
-               $status  = $temp[3];
-               $orgsip = substr $temp[4], 4;
-               $orgdip   = substr $temp[5], 4;
-               $marked   = $temp[8] eq '[UNREPLIED]' ? '[UNREPLIED]' : defined ($temp[13]) ? $temp[12] : ' ';
-       }
+       foreach my $line (@ovpnn2n) {
+               my @ovpn = split(',', $line);
+               next if ($ovpn[4] ne 'net');
 
-       # filter the line if we found a known proto
-       next if( !(
-                  (($cgiparams{'SEE_PROTO'}  eq $Lang::tr{'all'}) || ($protocol  eq $cgiparams{'SEE_PROTO'} ))
-               && (($cgiparams{'SEE_STATE'}  eq $Lang::tr{'all'}) || ($status    eq $cgiparams{'SEE_STATE'} ))
-               && (($cgiparams{'SEE_MARK'}   eq $Lang::tr{'all'}) || ($marked    eq $cgiparams{'SEE_MARK'}  ))
-               && (($cgiparams{'SEE_SRC'}    eq "*.*.*.*")        || ($orgsip    eq $cgiparams{'SEE_SRC'}   ))
-               && (($cgiparams{'SEE_DEST'}   eq "*.*.*.*")        || ($orgdip    eq $cgiparams{'SEE_DEST'}  ))
-               ));
-
-       if ($temp[0] eq 'icmp') {
-               my $offset = 0;
-               $protocol = $temp[0] . " (" . $temp[1] . ")";
-               $expires = $temp[2];
-               $status = ' ';
-               if ($temp[8] eq '[UNREPLIED]' ) {
-                       $offset = +1;
-               }       
-               $orgsip = substr $temp[3], 4;
-               $orgdip = substr $temp[4], 4;
-               $orgsp = &General::GetIcmpDescription(substr( $temp[5], 5)) . "/" . substr( $temp[6], 5);;
-               $orgdp = 'id=' . substr( $temp[7], 3);
-               $exsip = substr $temp[8 + $offset], 4;
-               $exdip = substr $temp[9 + $offset], 4;
-               $exsp = &General::GetIcmpDescription(substr( $temp[10 + $offset], 5)). "/" . substr( $temp[11 + $offset], 5); 
-               $exdp = 'id=' . substr( $temp[11 + $offset], 5);
-               $marked   = $temp[8] eq '[UNREPLIED]' ? '[UNREPLIED]' : ' ';
-               $use = substr( $temp[13 + $offset], 4 );
-       }
-       if ($temp[0] eq 'udp') {
-               my $offset = 0;
-               $marked = '';
-               $protocol = $temp[0] . " (" . $temp[1] . ")";
-               $expires = $temp[2];
-               $status = ' ';
-               $orgsip = substr $temp[3], 4;
-               $orgdip = substr $temp[4], 4;
-               $orgsp = substr $temp[5], 6;
-               $orgdp = substr $temp[6], 6;
-               if ($temp[7] eq '[UNREPLIED]') {
-                    $offset = 1;
-                   $marked = $temp[7];
-                   $use = substr $temp[12], 4;
-                } else {
-                   if ((substr $temp[11], 0, 3) eq 'use' ) {
-                       $marked = '';
-                       $use = substr $temp[11], 4;
-                   } else {
-                       $marked = $temp[11];
-                       $use = substr $temp[12], 4;
-                   }
+               my ($network, $mask) = split("/", $ovpn[12]);
+               if (!&General::validip($mask)) {
+                       $mask = ipv4_cidr2msk($mask);
                }
-               $exsip = substr $temp[7 + $offset], 4;
-               $exdip = substr $temp[8 + $offset], 4;
-               $exsp = substr $temp[9 + $offset], 6;
-               $exdp = substr $temp[10 + $offset], 6;
-       }
-       if ($temp[0] eq 'tcp') {
-               my $offset = 0;
-                $protocol = $temp[0] . " (" . $temp[1] . ")";
-                $expires = $temp[2];
-                $status = $temp[3];
-                $orgsip = substr $temp[4], 4;
-                $orgdip = substr $temp[5], 4;
-                $orgsp = substr $temp[6], 6;
-               $orgdp = substr $temp[7], 6;
-               if ($temp[8] eq '[UNREPLIED]') {
-                        $marked = $temp[8];
-                        $offset = 1;
-                } else {
-                        $marked = $temp[12];
-                }
-               $exsip = substr $temp[8 + $offset], 4;
-                $exdip = substr $temp[9 + $offset], 4;
-                $exsp = substr $temp[10 + $offset], 6;
-                $exdp = substr $temp[11 + $offset], 6;
-               $use = substr $temp[13], 4;
-        }
-       if ($temp[0] eq 'unknown') {
-                my $offset = 0;
-                $protocol = "??? (" . $temp[1] . ")";
-                $protocol = "esp (" . $temp[1] . ")" if ($temp[1] == 50);
-                $protocol = "ah (" . $temp[1] . ")" if ($temp[1] == 51);
-                $expires = $temp[2];
-                $status = ' ';
-                $orgsip = substr $temp[3], 4;
-                $orgdip = substr $temp[4], 4;
-                $orgsp = ' ';
-                $orgdp = ' ';
-                $exsip = substr $temp[5], 4;
-                $exdip = substr $temp[6], 4;
-                $exsp = ' ';
-                $exdp = ' ';
-                $marked = ' ';
-                $use = ' ';
-        }
-       if ($temp[0] eq 'gre') {
-                my $offset = 0;
-               $protocol = $temp[0] . " (" . $temp[1] . ")";
-                $expires = $temp[2];
-                $orgsip = substr $temp[5], 4;
-                $orgdip = substr $temp[6], 4;
-                $orgsp = ' ';
-               $orgdp = ' ';
-               $exsip = substr $temp[11], 4;
-                $exdip = substr $temp[12], 4;
-                $exsp = ' ';
-                $exdp = ' ';
-               $marked = $temp[17];
-               $use = $temp[18];
-       }
-       # Only from this point, lines have the same known format/field
-       # The floating fields [UNREPLIED] [ASSURED] etc are ok.
-
-       # Store the line in a hash array for sorting
-       if ( $protocol ) { # line is decoded ?
-               my @record = (  'index', $index++,
-                           'protocol', $protocol,
-                           'expires',  $expires,
-                           'status',   $status,
-                           'orgsip',   $orgsip,
-                           'orgdip',   $orgdip,
-                           'orgsp',    $orgsp,
-                           'orgdp',    $orgdp,
-                           'exsip',    $exsip,
-                           'exdip',    $exdip,
-                           'exsp',     $exsp,
-                           'exdp',     $exdp,
-                           'marked',   $marked,
-                           'use',      $use);
-               my $record = {};                                # create a reference to empty hash
-               %{$record} = @record;                   # populate that hash with @record
-               $entries{$record->{index}} = $record;   # add this to a hash of hashes      
-       } else { # it was not a known line
-               $unknownlines .= "<tr bgcolor='${Header::table1colour}'>";
-               $unknownlines .= "<td colspan='9'> unknown:$line></td></tr>";
+
+               push(@network, $network);
+               push(@masklen, $mask);
+               push(@colour, ${Header::colourovpn});
        }
 }
 
-# Build listbox objects
-my $menu_proto = &make_select ('SEE_PROTO', $cgiparams{'SEE_PROTO'}, @list_proto);
-my $menu_state = &make_select ('SEE_STATE', $cgiparams{'SEE_STATE'}, @list_state);
-my $menu_src   = &make_select ('SEE_SRC',   $cgiparams{'SEE_SRC'},   &get_known_ips('orgsip'));
-my $menu_dest  = &make_select ('SEE_DEST',  $cgiparams{'SEE_DEST'},  &get_known_ips('orgdip'));
-my $menu_mark  = &make_select ('SEE_MARK',  $cgiparams{'SEE_MARK'},  @list_mark);
-my $menu_sort  = &make_select ('SEE_SORT',  $cgiparams{'SEE_SORT'},  @list_sort);
-
-&Header::showhttpheaders();
+# Show the page.
 &Header::openpage($Lang::tr{'connections'}, 1, '');
 &Header::openbigbox('100%', 'left');
 &Header::openbox('100%', 'left', $Lang::tr{'connection tracking'});
 
+# Print legend.
+print <<END;
+       <table width='100%'>
+               <tr>
+                       <td align='center'>
+                               <b>$Lang::tr{'legend'} : </b>
+                       </td>
+                       <td align='center' bgcolor='${Header::colourgreen}'>
+                               <b><font color='#FFFFFF'>$Lang::tr{'lan'}</font></b>
+                       </td>
+                       <td align='center' bgcolor='${Header::colourred}'>
+                               <b><font color='#FFFFFF'>$Lang::tr{'internet'}</font></b>
+                       </td>
+                       <td align='center' bgcolor='${Header::colourorange}'>
+                               <b><font color='#FFFFFF'>$Lang::tr{'dmz'}</font></b>
+                       </td>
+                       <td align='center' bgcolor='${Header::colourblue}'>
+                               <b><font color='#FFFFFF'>$Lang::tr{'wireless'}</font></b>
+                       </td>
+                       <td align='center' bgcolor='${Header::colourfw}'>
+                               <b><font color='#FFFFFF'>IPFire</font></b>
+                       </td>
+                       <td align='center' bgcolor='${Header::colourvpn}'>
+                               <b><font color='#FFFFFF'>$Lang::tr{'vpn'}</font></b>
+                       </td>
+                       <td align='center' bgcolor='${Header::colourovpn}'>
+                               <b><font color='#FFFFFF'>$Lang::tr{'OpenVPN'}</font></b>
+                       </td>
+                       <td align='center' bgcolor='$colour_multicast'>
+                               <b><font color='#FFFFFF'>Multicast</font></b>
+                       </td>
+               </tr>
+       </table>
+       <br>
+END
+
+if ($SORT_FIELD and $SORT_ORDER) {
+       my @sort_field_name = (
+               $Lang::tr{'source ip'},
+               $Lang::tr{'destination ip'},
+               $Lang::tr{'source port'},
+               $Lang::tr{'destination port'},
+               $Lang::tr{'protocol'},
+               $Lang::tr{'connection'}.' '.$Lang::tr{'status'},
+               $Lang::tr{'expires'}.' ('.$Lang::tr{'seconds'}.')',
+               $Lang::tr{'download'},
+               $Lang::tr{'upload'}
+       );
+       my $sort_order_name;
+       if (lc($SORT_ORDER) eq "a") {
+               $sort_order_name = $Lang::tr{'sort ascending'};
+       } else {
+               $sort_order_name = $Lang::tr{'sort descending'};
+       }
+
 print <<END
-<table width='60%'>
-<tr><td align='center'><b>$Lang::tr{'legend'} : </b></td>
-    <td align='center' bgcolor='${Header::colourgreen}'><b><font color='#FFFFFF'>$Lang::tr{'lan'}</font></b></td>
-    <td align='center' bgcolor='${Header::colourred}'><b><font color='#FFFFFF'>$Lang::tr{'internet'}</font></b></td>
-    <td align='center' bgcolor='${Header::colourorange}'><b><font color='#FFFFFF'>$Lang::tr{'dmz'}</font></b></td>
-    <td align='center' bgcolor='${Header::colourblue}'><b><font color='#FFFFFF'>$Lang::tr{'wireless'}</font></b></td>
-    <td align='center' bgcolor='${Header::colourfw}'><b><font color='#FFFFFF'>IPFire</font></b></td>
-    <td align='center' bgcolor='${Header::colourvpn}'><b><font color='#FFFFFF'>$Lang::tr{'vpn'}</font></b></td>
-    <td align='center' bgcolor='${Header::colourovpn}'><b><font color='#FFFFFF'>$Lang::tr{'OpenVPN'}</font></b></td>
-</tr>
-</table>
-<br />
-<table cellpadding='2'>
-<tr><td align='center'><b>$Lang::tr{'protocol'}</b></td>
-    <td align='center'><b>$Lang::tr{'expires'}<br />($Lang::tr{'seconds'})</b></td>
-    <td align='center'><b>$Lang::tr{'connection'}<br />$Lang::tr{'status'}</b></td>
-    <td align='center'><b>$Lang::tr{'original'}<br />$Lang::tr{'source ip and port'}</b></td>
-    <td align='center'><b>$Lang::tr{'original'}<br />$Lang::tr{'dest ip and port'}</b></td>
-    <td align='center'><b>$Lang::tr{'expected'}<br />$Lang::tr{'source ip and port'}</b></td>
-    <td align='center'><b>$Lang::tr{'expected'}<br />$Lang::tr{'dest ip and port'}</b></td>
-    <td align='center'><b>$Lang::tr{'marked'}</b></td>
-    <td align='center'><b>$Lang::tr{'use'}</b></td>
-</tr>
-<tr><form method='post' action='$ENV{'SCRIPT_NAME'}'>
-    <td align='center'>$menu_proto</td>
-    <td></td>
-    <td align='center'>$menu_state</td>
-    <td align='center'>$menu_src</td>
-    <td align='center'>$menu_dest</td>
-    <td align='center'colspan='2'>$Lang::tr{'sort ascending'}:$menu_sort </td>
-    <td align='center'>$menu_mark</td>
-    <td align='center'><input type='submit' value='!' /></td>
-    </form>
-</tr>
+       <div style="font-weight:bold;margin:10px;font-size: 70%">
+               $sort_order_name: $sort_field_name[$SORT_FIELD-1]
+       </div>
 END
 ;
+}
+
+# Print table header.
+print <<END;
+       <table width='100%'>
+               <tr valign="top"">
+                       <th align='center'>
+                               <a href="?sort_field=5&sort_order=d"><img style="width:10px" src="/images/up.gif"></a>
+                               <a href="?sort_field=5&sort_order=a"><img style="width:10px" src="/images/down.gif"></a>
+                       </th>
+                       <th align='center' colspan="2">
+                               <a href="?sort_field=1&sort_order=d"><img style="width:10px" src="/images/up.gif"></a>
+                               <a href="?sort_field=1&sort_order=a"><img style="width:10px" src="/images/down.gif"></a>
+                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
+                               <a href="?sort_field=3&sort_order=d"><img style="width:10px" src="/images/up.gif"></a>
+                               <a href="?sort_field=3&sort_order=a"><img style="width:10px" src="/images/down.gif"></a>
+                       </th>
+                       <th align='center' colspan="2">
+                               <a href="?sort_field=2&sort_order=d"><img style="width:10px" src="/images/up.gif"></a>
+                               <a href="?sort_field=2&sort_order=a"><img style="width:10px" src="/images/down.gif"></a>
+                               &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
+                               <a href="?sort_field=4&sort_order=d"><img style="width:10px" src="/images/up.gif"></a>
+                               <a href="?sort_field=4&sort_order=a"><img style="width:10px" src="/images/down.gif"></a>
+                       </th>
+                       <th align='center'>
+                               <a href="?sort_field=8&sort_order=d"><img style="width:10px" src="/images/up.gif"></a>
+                               <a href="?sort_field=8&sort_order=a"><img style="width:10px" src="/images/down.gif"></a>
+                               &nbsp;&nbsp;&nbsp;&nbsp;
+                               <a href="?sort_field=9&sort_order=d"><img style="width:10px" src="/images/up.gif"></a>
+                               <a href="?sort_field=9&sort_order=a"><img style="width:10px" src="/images/down.gif"></a>
+                       </th>
+                       <th align='center'>
+                               <a href="?sort_field=6&sort_order=d"><img style="width:10px" src="/images/up.gif"></a>
+                               <a href="?sort_field=6&sort_order=a"><img style="width:10px" src="/images/down.gif"></a>
+                       </th>
+                       <th align='center'>
+                               <a href="?sort_field=7&sort_order=d"><img style="width:10px" src="/images/up.gif"></a>
+                               <a href="?sort_field=7&sort_order=a"><img style="width:10px" src="/images/down.gif"></a>
+                       </th>
+               </tr>
+               <tr valign="top"">
+                       <th align='center'>
+                               $Lang::tr{'protocol'}
+                       </th>
+                       <th align='center' colspan="2">
+                               $Lang::tr{'source ip and port'}
+                       </th>
+                       <th align='center' colspan="2">
+                               $Lang::tr{'dest ip and port'}
+                       </th>
+                       <th align='center'>
+                               $Lang::tr{'download'} /
+                               <br>$Lang::tr{'upload'}
+                       </th>
+                       <th align='center'>
+                               $Lang::tr{'connection'}<br>$Lang::tr{'status'}
+                       </th>
+                       <th align='center'>
+                               $Lang::tr{'expires'}<br>($Lang::tr{'seconds'})
+                       </th>
+               </tr>
+END
+
+foreach my $line (@conntrack) {
+       my @conn = split(' ', $line);
+
+       # The first bit is the l3 protocol.
+       my $l3proto = $conn[0];
+
+       # Skip everything that is not IPv4.
+       if ($l3proto ne 'ipv4') {
+               next;
+       }
+
+       # L4 protocol (tcp, udp, ...).
+       my $l4proto = $conn[2];
+
+       # Translate unknown protocols.
+       if ($l4proto eq 'unknown') {
+               my $l4protonum = $conn[3];
+               if ($l4protonum eq '2') {
+                       $l4proto = 'IGMP';
+               } elsif ($l4protonum eq '4') {
+                       $l4proto = 'IPv4 Encap';
+               } elsif ($l4protonum eq '33') {
+                       $l4proto = 'DCCP';
+               } elsif ($l4protonum eq '41') {
+                       $l4proto = 'IPv6 Encap';
+               } elsif ($l4protonum eq '50') {
+                       $l4proto = 'ESP';
+               } elsif ($l4protonum eq '51') {
+                       $l4proto = 'AH';
+               } elsif ($l4protonum eq '132') {
+                       $l4proto = 'SCTP';
+               } else {
+                       $l4proto = $l4protonum;
+               }
+       } else {
+               $l4proto = uc($l4proto);
+       }
+
+       # Source and destination.
+       my $sip;
+       my $sip_ret;
+       my $dip;
+       my $dip_ret;
+       my $sport;
+       my $sport_ret;
+       my $dport;
+       my $dport_ret;
+       my @packets;
+       my @bytes;
+
+       my $ttl = $conn[4];
+       my $state;
+       if ($l4proto eq 'TCP') {
+               $state = $conn[5];
+       }
+
+       # Kick out everything that is not IPv4.
+       foreach my $item (@conn) {
+               my ($key, $val) = split('=', $item);
+
+               switch ($key) {
+                       case "src" {
+                               if ($sip == "") {
+                                       $sip = $val;
+                               } else {
+                                       $dip_ret = $val;
+                               }
+                       }
+                       case "dst" {
+                               if ($dip == "") {
+                                       $dip = $val;
+                               } else {
+                                       $sip_ret = $val;
+                               }
+                       }
+                       case "sport" {
+                               if ($sport == "") {
+                                       $sport = $val;
+                               } else {
+                                       $dport_ret = $val;
+                               }
+                       }
+                       case "dport" {
+                               if ($dport == "") {
+                                       $dport = $val;
+                               } else {
+                                       $sport_ret = $val;
+                               }
+                       }
+                       case "packets" {
+                               push(@packets, $val);
+                       }
+                       case "bytes" {
+                               push(@bytes, $val);
+                       }
+               }
+       }
+
+       my $sip_colour = ipcolour($sip);
+       my $dip_colour = ipcolour($dip);
+
+       my $sserv = '';
+       if ($sport < 1024) {
+               $sserv = uc(getservbyport($sport, lc($l4proto)));
+       }
+
+       my $dserv = '';
+       if ($dport < 1024) {
+               $dserv = uc(getservbyport($dport, lc($l4proto)));
+       }
+
+       my $bytes_in = format_bytes($bytes[0]);
+       my $bytes_out = format_bytes($bytes[1]);
+
+       # Format TTL
+       $ttl = format_time($ttl);
+
+       my $sip_extra;
+       if ($sip ne $sip_ret) {
+               $sip_extra = "<font color='#FFFFFF'>&gt;</font> ";
+               $sip_extra .= "<a href='/cgi-bin/ipinfo.cgi?ip=$sip_ret'>";
+               $sip_extra .= " <font color='#FFFFFF'>$sip_ret</font>";
+               $sip_extra .= "</a>";
+       }
+
+       my $dip_extra;
+       if ($dip ne $dip_ret) {
+               $dip_extra = "<font color='#FFFFFF'>&gt;</font> ";
+               $dip_extra .= "<a href='/cgi-bin/ipinfo.cgi?ip=$dip_ret'>";
+               $dip_extra .= " <font color='#FFFFFF'>$dip_ret</font>";
+               $dip_extra .= "</a>";
+       }
+
+
+       my $sport_extra;
+       if ($sport ne $sport_ret) {
+               my $sserv_ret = '';
+               if ($sport_ret < 1024) {
+                       $sserv_ret = uc(getservbyport($sport_ret, lc($l4proto)));
+               }
+
+               $sport_extra = "<font color='#FFFFFF'>&gt;</font> ";
+               $sport_extra .= "<a href='http://isc.sans.org/port_details.php?port=$sport_ret' target='top' title='$sserv_ret'>";
+               $sport_extra .= " <font color='#FFFFFF'>$sport_ret</font>";
+               $sport_extra .= "</a>";
+       }
+
+       my $dport_extra;
+       if ($dport ne $dport_ret) {
+               my $dserv_ret = '';
+               if ($dport_ret < 1024) {
+                       $dserv_ret = uc(getservbyport($dport_ret, lc($l4proto)));
+               }
 
-foreach my $entry (sort sort_entries keys %entries) {
-
-       print "<tr bgcolor='${Header::table1colour}'>";
-       my $orgsipcolour = &ipcolour( $entries{$entry}->{orgsip} );
-       my $orgdipcolour = &ipcolour( $entries{$entry}->{orgdip} );
-       my $exsipcolour  = &ipcolour( $entries{$entry}->{exsip} );
-       my $exdipcolour  = &ipcolour( $entries{$entry}->{exdip} );
-       print <<END
-       <td align='center'>$entries{$entry}->{protocol}</td>
-       <td align='center'>$entries{$entry}->{expires}</td>
-       <td align='center'>$entries{$entry}->{status}</td>
-       <td align='center' bgcolor='$orgsipcolour'>
-           <a href='/cgi-bin/ipinfo.cgi?ip=$entries{$entry}->{orgsip}'>
-           <font color='#FFFFFF'>$entries{$entry}->{orgsip}</font>
-           </a><font color='#FFFFFF'>:$entries{$entry}->{orgsp}</font></td>
-       <td align='center' bgcolor='$orgdipcolour'>
-           <a href='/cgi-bin/ipinfo.cgi?ip=$entries{$entry}->{orgdip}'>
-           <font color='#FFFFFF'>$entries{$entry}->{orgdip}</font>
-           </a><font color='#FFFFFF'>:$entries{$entry}->{orgdp}</font></td>
-       <td align='center' bgcolor='$exsipcolour'>
-           <a href='/cgi-bin/ipinfo.cgi?ip=$entries{$entry}->{exsip}'>
-           <font color='#FFFFFF'>$entries{$entry}->{exsip}</font>
-           </a><font color='#FFFFFF'>:$entries{$entry}->{exsp}</font></td>
-       <td align='center' bgcolor='$exdipcolour'>
-           <a href='/cgi-bin/ipinfo.cgi?ip=$entries{$entry}->{exdip}'>
-           <font color='#FFFFFF'>$entries{$entry}->{exdip}</font>
-           </a><font color='#FFFFFF'>:$entries{$entry}->{exdp}</font></td>
-       <td align='center'>$entries{$entry}->{marked}</td>
-       <td align='center'>$entries{$entry}->{use}</td>
+               $dport_extra = "<font color='#FFFFFF'>&gt;</font> ";
+               $dport_extra .= "<a href='http://isc.sans.org/port_details.php?port=$dport_ret' target='top' title='$dserv_ret'>";
+               $dport_extra .= " <font color='#FFFFFF'>$dport_ret</font>";
+               $dport_extra .= "</a>";
+       }
+
+       print <<END;
+       <tr>
+               <td align='center'>$l4proto</td>
+               <td align='center' bgcolor='$sip_colour'>
+                       <a href='/cgi-bin/ipinfo.cgi?ip=$sip'>
+                               <font color='#FFFFFF'>$sip</font>
+                       </a>
+                       $sip_extra
+               </td>
+               <td align='center' bgcolor='$sip_colour'>
+                       <a href='http://isc.sans.org/port_details.php?port=$sport' target='top' title='$sserv'>
+                               <font color='#FFFFFF'>$sport</font>
+                       </a>
+                       $sport_extra
+               </td>
+               <td align='center' bgcolor='$dip_colour'>
+                       <a href='/cgi-bin/ipinfo.cgi?ip=$dip'>
+                               <font color='#FFFFFF'>$dip</font>
+                       </a>
+                       $dip_extra
+               </td>
+               <td align='center' bgcolor='$dip_colour'>
+                       <a href='http://isc.sans.org/port_details.php?port=$dport' target='top' title='$dserv'>
+                               <font color='#FFFFFF'>$dport</font>
+                       </a>
+                       $dport_extra
+               </td>
+               <td align='center'>
+                       $bytes_in / $bytes_out
+               </td>
+               <td align='center'>$state</td>
+               <td align='center'>$ttl</td>
        </tr>
 END
-;
 }
 
-print "$unknownlines</table>";
+# Close the main table.
+print "</table>";
 
 &Header::closebox();
 &Header::closebigbox();
 &Header::closepage();
 
-sub ipcolour($) {
-       my $id = 0;
-       my $line;
-       my $colour = ${Header::colourred};
-       my ($ip) = $_[0];
-        my $found = 0;
-        foreach $line (@network) {
-               if (!$found && ipv4_in_network( $network[$id] , $masklen[$id], $ip) ) {
-                       $found = 1;
-                       $colour = $colour[$id];
+sub format_bytes($) {
+       my $bytes = shift;
+       my @units = ("B", "k", "M", "G", "T");
+
+       foreach my $unit (@units) {
+               if ($bytes < 1024) {
+                       return sprintf("%d%s", $bytes, $unit);
                }
-               $id++;
-       }
-       return $colour
-}
 
-# Create a string containing a complete SELECT html object 
-# param1: name
-# param2: current value selected
-# param3: field list
-sub make_select ($,$,$) {
-       my $select_name = shift;
-       my $selected    = shift;
-       my $select = "<select name='$select_name'>";
-
-       foreach my $value (@_) {
-               my $check = $selected eq $value ? "selected='selected'" : '';
-               $select .= "<option $check value='$value'>$value";
+               $bytes /= 1024;
        }
-       $select .= "</select>";
-       return $select;
+
+       return sprintf("%d%s", $bytes, $units[$#units]);
 }
 
-# Build a list of IP obtained from the %entries hash
-# param1: IP field name
-sub get_known_ips ($) {
-       my $field = shift;
-       my $qs = $cgiparams{'SEE_SORT'};        # switch the sort order
-       $cgiparams{'SEE_SORT'} = $field;
+sub format_time($) {
+       my $time = shift;
+
+       my $seconds = $time % 60;
+       my $minutes = $time / 60;
 
-       my @liste=('*.*.*.*');
-       foreach my $entry ( sort sort_entries keys %entries) {
-               push (@liste, $entries{$entry}->{$field}) if (! grep (/^$entries{$entry}->{$field}$/,@liste) );
+       my $hours = 0;
+       if ($minutes >= 60) {
+               $hours = $minutes / 60;
+               $minutes %= 60;
        }
 
-       $cgiparams{'SEE_SORT'} = $qs;   #restore sort order
-        return @liste;
+       return sprintf("%3d:%02d:%02d", $hours, $minutes, $seconds);
 }
 
-# Used to sort the table containing the lines displayed.
-sub sort_entries { #Reverse is not implemented
-        my $qs=$cgiparams{'SEE_SORT'};
-        if ($qs =~ /orgsip|orgdip|exsip|exdip/) {
-               my @a = split(/\./,$entries{$a}->{$qs});
-               my @b = split(/\./,$entries{$b}->{$qs});
-               ($a[0]<=>$b[0]) ||
-               ($a[1]<=>$b[1]) ||
-               ($a[2]<=>$b[2]) ||
-               ($a[3]<=>$b[3]);
-       } elsif ($qs =~ /expire|orgsp|orgdp|exsp|exdp/) {
-               $entries{$a}->{$qs} <=> $entries{$b}->{$qs};
-       } else {
-               $entries{$a}->{$qs} cmp $entries{$b}->{$qs};
+sub ipcolour($) {
+       my $id = 0;
+       my $colour = ${Header::colourred};
+       my ($ip) = $_[0];
+       my $found = 0;
+
+       foreach my $line (@network) {
+               if ($network[$id] eq '') {
+                       $id++;
+               } else {
+                       if (!$found && ipv4_in_network($network[$id], $masklen[$id], $ip) ) {
+                               $found = 1;
+                               $colour = $colour[$id];
+                       }
+                       $id++;
+               }
        }
+
+       return $colour;
 }
 
 1;