]>
git.ipfire.org Git - 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 ###############################################################################
24 use Net
::IPv4Addr
qw( :all );
27 # enable only the following on debugging purpose
29 #use CGI::Carp 'fatalsToBrowser';
31 require '/var/ipfire/general-functions.pl';
32 require "${General::swroot}/lang.pl";
33 require "${General::swroot}/header.pl";
34 require "${General::swroot}/geoip-functions.pl";
36 my $colour_multicast = "#A0A0A0";
38 # sort arguments for connection tracking table
39 # the sort field. eg. 1=src IP, 2=dst IP, 3=src port, 4=dst port
41 # the sort order. (a)scending orr (d)escending
48 # retrieve query arguments
49 # note: let a-z A-Z and 0-9 pass as value only
50 if (length ($ENV{'QUERY_STRING'}) > 0){
53 my $buffer = $ENV{'QUERY_STRING'};
54 my @pairs = split(/&/, $buffer);
55 foreach my $pair (@pairs){
56 ($name, $value) = split(/=/, $pair);
57 $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; # e.g. "%20" => " "
58 $value =~ s/[^a-zA-Z0-9]*//g; # a-Z 0-9 will pass
59 $cgiin{$name} = $value;
63 &Header
::showhttpheaders
();
70 &General
::readhash
("${General::swroot}/ethernet/settings", \
%netsettings);
72 # output cgi query arrguments to browser on debug
74 &Header
::openbox
('100%', 'center', 'DEBUG');
76 foreach my $line (sort keys %cgiin) {
77 print "$line = '$cgiin{$line}'<br />\n";
80 print " Count: $debugCount\n";
84 #workaround to suppress a warning when a variable is used only once
85 my @dummy = ( ${Header
::table1colour
} );
88 # check sorting arguments
89 if ( $cgiin{'sort_field'} ~~ [ '1','2','3','4','5','6','7','8','9' ] ) {
90 $SORT_FIELD = $cgiin{'sort_field'};
92 if ( $cgiin{'sort_order'} ~~ [ 'a','d','A','D' ] ) {
93 $SORT_ORDER = lc($cgiin{'sort_order'});
97 # Read and sort the connection tracking table
99 if ($SORT_FIELD and $SORT_ORDER) {
100 # field sorting when sorting arguments are sane
101 open(CONNTRACK
, "/usr/local/bin/getconntracktable | /usr/local/bin/consort.sh $SORT_FIELD $SORT_ORDER |") or die "Unable to read conntrack table";
103 # default sorting with no query arguments
104 open(CONNTRACK
, "/usr/local/bin/getconntracktable | sort -k 5,5 --numeric-sort --reverse |") or die "Unable to read conntrack table";
106 my @conntrack = <CONNTRACK
>;
109 # Collect data for the @network array.
111 # Add Firewall Localhost 127.0.0.1
112 push(@network, '127.0.0.1');
113 push(@masklen, '255.255.255.255');
114 push(@colour, ${Header
::colourfw
});
116 if (open(IP
, "${General::swroot}/red/local-ipaddress")) {
121 push(@network, $redip);
122 push(@masklen, '255.255.255.255');
123 push(@colour, ${Header
::colourfw
});
126 # Add STATIC RED aliases
127 if ($netsettings{'RED_DEV'}) {
128 my $aliasfile = "${General::swroot}/ethernet/aliases";
129 open(ALIASES
, $aliasfile) or die 'Unable to open aliases file.';
130 my @aliases = <ALIASES
>;
133 # We have a RED eth iface
134 if ($netsettings{'RED_TYPE'} eq 'STATIC') {
135 # We have a STATIC RED eth iface
136 foreach my $line (@aliases) {
138 my @temp = split(/\,/,$line);
140 push(@network, $temp[0]);
141 push(@masklen, $netsettings{'RED_NETMASK'} );
142 push(@colour, ${Header
::colourfw
} );
148 # Add Green Firewall Interface
149 push(@network, $netsettings{'GREEN_ADDRESS'});
150 push(@masklen, "255.255.255.255" );
151 push(@colour, ${Header
::colourfw
} );
153 # Add Green Network to Array
154 push(@network, $netsettings{'GREEN_NETADDRESS'});
155 push(@masklen, $netsettings{'GREEN_NETMASK'} );
156 push(@colour, ${Header
::colourgreen
} );
158 # Add Green Routes to Array
159 my @routes = `/sbin/route -n | /bin/grep $netsettings{'GREEN_DEV'}`;
160 foreach my $route (@routes) {
162 my @temp = split(/[\t ]+/, $route);
163 push(@network, $temp[0]);
164 push(@masklen, $temp[2]);
165 push(@colour, ${Header
::colourgreen
} );
168 # Add Blue Firewall Interface
169 push(@network, $netsettings{'BLUE_ADDRESS'});
170 push(@masklen, "255.255.255.255" );
171 push(@colour, ${Header
::colourfw
} );
174 if ($netsettings{'BLUE_DEV'}) {
175 push(@network, $netsettings{'BLUE_NETADDRESS'});
176 push(@masklen, $netsettings{'BLUE_NETMASK'} );
177 push(@colour, ${Header
::colourblue
} );
179 # Add Blue Routes to Array
180 @routes = `/sbin/route -n | /bin/grep $netsettings{'BLUE_DEV'}`;
181 foreach my $route (@routes) {
183 my @temp = split(/[\t ]+/, $route);
184 push(@network, $temp[0]);
185 push(@masklen, $temp[2]);
186 push(@colour, ${Header
::colourblue
} );
190 # Add Orange Firewall Interface
191 push(@network, $netsettings{'ORANGE_ADDRESS'});
192 push(@masklen, "255.255.255.255" );
193 push(@colour, ${Header
::colourfw
} );
196 if ($netsettings{'ORANGE_DEV'}) {
197 push(@network, $netsettings{'ORANGE_NETADDRESS'});
198 push(@masklen, $netsettings{'ORANGE_NETMASK'} );
199 push(@colour, ${Header
::colourorange
} );
200 # Add Orange Routes to Array
201 @routes = `/sbin/route -n | /bin/grep $netsettings{'ORANGE_DEV'}`;
202 foreach my $route (@routes) {
204 my @temp = split(/[\t ]+/, $route);
205 push(@network, $temp[0]);
206 push(@masklen, $temp[2]);
207 push(@colour, ${Header
::colourorange
} );
211 # Highlight multicast connections.
212 push(@network, "224.0.0.0");
213 push(@masklen, "239.0.0.0");
214 push(@colour, $colour_multicast);
216 # Add OpenVPN net and RED/BLUE/ORANGE entry (when appropriate)
217 if (-e
"${General::swroot}/ovpn/settings") {
218 my %ovpnsettings = ();
219 &General
::readhash
("${General::swroot}/ovpn/settings", \
%ovpnsettings);
220 my @tempovpnsubnet = split("\/",$ovpnsettings{'DOVPN_SUBNET'});
223 push(@network, $tempovpnsubnet[0]);
224 push(@masklen, $tempovpnsubnet[1]);
225 push(@colour, ${Header
::colourovpn
} );
227 # add BLUE:port / proto
228 if (($ovpnsettings{'ENABLED_BLUE'} eq 'on') && $netsettings{'BLUE_DEV'}) {
229 push(@network, $netsettings{'BLUE_ADDRESS'} );
230 push(@masklen, '255.255.255.255' );
231 push(@colour, ${Header
::colourovpn
});
234 # add ORANGE:port / proto
235 if (($ovpnsettings{'ENABLED_ORANGE'} eq 'on') && $netsettings{'ORANGE_DEV'}) {
236 push(@network, $netsettings{'ORANGE_ADDRESS'} );
237 push(@masklen, '255.255.255.255' );
238 push(@colour, ${Header
::colourovpn
} );
242 # Add OpenVPN net for custom OVPNs
243 if (-e
"${General::swroot}/ovpn/ccd.conf") {
244 open(OVPNSUB
, "${General::swroot}/ovpn/ccd.conf");
245 my @ovpnsub = <OVPNSUB
>;
249 my ($network, $mask) = split '/', (split ',', $_)[2];
251 $mask = ipv4_cidr2msk
($mask) unless &General
::validip
($mask);
253 push(@network, $network);
254 push(@masklen, $mask);
255 push(@colour, ${Header
::colourovpn
});
259 open(IPSEC
, "${General::swroot}/vpn/config");
263 foreach my $line (@ipsec) {
264 my @vpn = split(',', $line);
266 my @subnets = split(/\|/, $vpn[12]);
267 for my $subnet (@subnets) {
268 my ($network, $mask) = split("/", $subnet);
270 if (!&General
::validip
($mask)) {
271 $mask = ipv4_cidr2msk
($mask);
274 push(@network, $network);
275 push(@masklen, $mask);
276 push(@colour, ${Header
::colourvpn
});
280 if (-e
"${General::swroot}/ovpn/n2nconf") {
281 open(OVPNN2N
, "${General::swroot}/ovpn/ovpnconfig");
282 my @ovpnn2n = <OVPNN2N
>;
285 foreach my $line (@ovpnn2n) {
286 my @ovpn = split(',', $line);
287 next if ($ovpn[4] ne 'net');
289 my ($network, $mask) = split("/", $ovpn[12]);
290 if (!&General
::validip
($mask)) {
291 $mask = ipv4_cidr2msk
($mask);
294 push(@network, $network);
295 push(@masklen, $mask);
296 push(@colour, ${Header
::colourovpn
});
301 &Header
::openpage
($Lang::tr
{'connections'}, 1, '');
302 &Header
::openbigbox
('100%', 'left');
303 &Header
::openbox
('100%', 'left', $Lang::tr
{'connection tracking'});
307 <table style='width:100%'>
309 <td style='text-align:center;'>
310 <b>$Lang::tr{'legend'} :</b>
312 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourgreen}; font-weight:bold;'>
313 <b>$Lang::tr{'lan'}</b>
315 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourred};'>
316 <b>$Lang::tr{'internet'}</b>
318 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourorange};'>
319 <b>$Lang::tr{'dmz'}</b>
321 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourblue};'>
322 <b>$Lang::tr{'wireless'}</b>
324 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourfw};'>
327 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourvpn};'>
328 <b>$Lang::tr{'vpn'}</b>
330 <td style='text-align:center; color:#FFFFFF; background-color:${Header::colourovpn};'>
331 <b>$Lang::tr{'OpenVPN'}</b>
333 <td style='text-align:center; color:#FFFFFF; background-color:$colour_multicast;'>
341 if ($SORT_FIELD and $SORT_ORDER) {
342 my @sort_field_name = (
343 $Lang::tr
{'source ip'},
344 $Lang::tr
{'destination ip'},
345 $Lang::tr
{'source port'},
346 $Lang::tr
{'destination port'},
347 $Lang::tr
{'protocol'},
348 $Lang::tr
{'connection'}.' '.$Lang::tr
{'status'},
349 $Lang::tr
{'expires'}.' ('.$Lang::tr
{'seconds'}.')',
350 $Lang::tr
{'download'},
354 if (lc($SORT_ORDER) eq "a") {
355 $sort_order_name = $Lang::tr
{'sort ascending'};
357 $sort_order_name = $Lang::tr
{'sort descending'};
361 <div style="font-weight:bold;margin:10px;font-size: 70%">
362 $sort_order_name: $sort_field_name[$SORT_FIELD-1]
368 # Print table header.
370 <table style='width:100%'>
372 <th style='text-align:center'>
373 <a href="?sort_field=5&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
374 <a href="?sort_field=5&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
376 <th style='text-align:center' colspan='2'>
377 <a href="?sort_field=1&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
378 <a href="?sort_field=1&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
379
380 <a href="?sort_field=3&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
381 <a href="?sort_field=3&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
384 <th style='text-align:center' colspan='2'>
385 <a href="?sort_field=2&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
386 <a href="?sort_field=2&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
387
388 <a href="?sort_field=4&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
389 <a href="?sort_field=4&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
392 <th style='text-align:center'>
393 <a href="?sort_field=8&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
394 <a href="?sort_field=8&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
395
396 <a href="?sort_field=9&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
397 <a href="?sort_field=9&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
399 <th style='text-align:center'>
400 <a href="?sort_field=6&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
401 <a href="?sort_field=6&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
403 <th style='text-align:center'>
404 <a href="?sort_field=7&sort_order=d"><img style="width:10px" src="/images/up.gif" alt=""></a>
405 <a href="?sort_field=7&sort_order=a"><img style="width:10px" src="/images/down.gif" alt=""></a>
409 <th style='text-align:center'>
410 $Lang::tr{'protocol'}
412 <th style='text-align:center' colspan='2'>
413 $Lang::tr{'source ip and port'}
415 <th style='text-align:center'>
418 <th style='text-align:center' colspan='2'>
419 $Lang::tr{'dest ip and port'}
421 <th style='text-align:center'>
424 <th style='text-align:center'>
425 $Lang::tr{'download'} /
426 <br>$Lang::tr{'upload'}
428 <th style='text-align:center'>
429 $Lang::tr{'connection'}<br>$Lang::tr{'status'}
431 <th style='text-align:center'>
432 $Lang::tr{'expires'}<br>($Lang::tr{'seconds'})
437 foreach my $line (@conntrack) {
438 my @conn = split(' ', $line);
440 # The first bit is the l3 protocol.
441 my $l3proto = $conn[0];
443 # Skip everything that is not IPv4.
444 if ($l3proto ne 'ipv4') {
448 # L4 protocol (tcp, udp, ...).
449 my $l4proto = $conn[2];
451 # Translate unknown protocols.
452 if ($l4proto eq 'unknown') {
453 my $l4protonum = $conn[3];
454 if ($l4protonum eq '2') {
456 } elsif ($l4protonum eq '4') {
457 $l4proto = 'IPv4 Encap';
458 } elsif ($l4protonum eq '33') {
460 } elsif ($l4protonum eq '41') {
461 $l4proto = 'IPv6 Encap';
462 } elsif ($l4protonum eq '50') {
464 } elsif ($l4protonum eq '51') {
466 } elsif ($l4protonum eq '132') {
469 $l4proto = $l4protonum;
472 $l4proto = uc($l4proto);
475 # Source and destination.
489 if ($l4proto eq 'TCP') {
493 # Kick out everything that is not IPv4.
494 foreach my $item (@conn) {
495 my ($key, $val) = split('=', $item);
527 push(@packets, $val);
535 my $sip_colour = ipcolour
($sip);
536 # use colour of destination network for DNAT
537 my $dip_colour = $dip ne $dip_ret ? ipcolour
($dip_ret) : ipcolour
($dip);
541 $sserv = uc(getservbyport($sport, lc($l4proto)));
546 $dserv = uc(getservbyport($dport, lc($l4proto)));
549 my $bytes_in = format_bytes
($bytes[0]);
550 my $bytes_out = format_bytes
($bytes[1]);
552 # enumerate GeoIP information
553 my $srcccode = &GeoIP
::lookup
($sip_ret);
554 my $src_flag_icon = &GeoIP
::get_flag_icon
($srcccode);
555 my $dstccode = &GeoIP
::lookup
($dip_ret);
556 my $dst_flag_icon = &GeoIP
::get_flag_icon
($dstccode);
559 $ttl = format_time
($ttl);
562 if ($sip_ret && $sip ne $sip_ret) {
563 $sip_extra = "<span style='color:#FFFFFF;'>></span> ";
564 $sip_extra .= "<a href='/cgi-bin/ipinfo.cgi?ip=$sip_ret'>";
565 $sip_extra .= " <span style='color:#FFFFFF;'>$sip_ret</span>";
566 $sip_extra .= "</a>";
570 if ($dip_ret && $dip ne $dip_ret) {
571 $dip_extra = "<span style='color:#FFFFFF;'>></span> ";
572 $dip_extra .= "<a href='/cgi-bin/ipinfo.cgi?ip=$dip_ret'>";
573 $dip_extra .= " <span style='color:#FFFFFF;'>$dip_ret</span>";
574 $dip_extra .= "</a>";
579 if ($sport ne $sport_ret) {
581 if ($sport_ret < 1024) {
582 $sserv_ret = uc(getservbyport($sport_ret, lc($l4proto)));
585 $sport_extra = "<span style='color:#FFFFFF;'>></span> ";
586 $sport_extra .= "<a href='http://isc.sans.org/port_details.php?port=$sport_ret' target='top' title='$sserv_ret'>";
587 $sport_extra .= " <span style='color:#FFFFFF;'>$sport_ret</span>";
588 $sport_extra .= "</a>";
592 if ($dport ne $dport_ret) {
594 if ($dport_ret < 1024) {
595 $dserv_ret = uc(getservbyport($dport_ret, lc($l4proto)));
598 $dport_extra = "<span style='color:#FFFFFF;'>></span> ";
599 $dport_extra .= "<a href='http://isc.sans.org/port_details.php?port=$dport_ret' target='top' title='$dserv_ret'>";
600 $dport_extra .= " <span style='color:#FFFFFF;'>$dport_ret</span>";
601 $dport_extra .= "</a>";
606 <td style='text-align:center'>$l4proto</td>
607 <td style='text-align:center; background-color:$sip_colour;'>
608 <a href='/cgi-bin/ipinfo.cgi?ip=$sip'>
609 <span style='color:#FFFFFF;'>$sip</span>
613 <td style='text-align:center; background-color:$sip_colour;'>
614 <a href='http://isc.sans.org/port_details.php?port=$sport' target='top' title='$sserv'>
615 <span style='color:#FFFFFF;'>$sport</span>
619 <td style='text-align:center; background-color:$sip_colour;'>
620 <a href='country.cgi#$srcccode'><img src='$src_flag_icon' border='0' align='absmiddle' alt='$srcccode' title='$srcccode' /></a>
622 <td style='text-align:center; background-color:$dip_colour;'>
623 <a href='/cgi-bin/ipinfo.cgi?ip=$dip'>
624 <span style='color:#FFFFFF;'>$dip</span>
628 <td style='text-align:center; background-color:$dip_colour;'>
629 <a href='http://isc.sans.org/port_details.php?port=$dport' target='top' title='$dserv'>
630 <span style='color:#FFFFFF;'>$dport</span>
634 <td style='text-align:center; background-color:$sip_colour;'>
635 <a href='country.cgi#$dstccode'><img src='$dst_flag_icon' border='0' align='absmiddle' alt='$dstccode' title='$dstccode' /></a>
637 <td style='text-align:center'>
638 $bytes_in / $bytes_out
640 <td style='text-align:center'>$state</td>
641 <td style='text-align:center'>$ttl</td>
646 # Close the main table.
650 &Header
::closebigbox
();
651 &Header
::closepage
();
653 sub format_bytes
($) {
655 my @units = ("B", "k", "M", "G", "T");
657 foreach my $unit (@units) {
659 return sprintf("%d%s", $bytes, $unit);
665 return sprintf("%d%s", $bytes, $units[$#units]);
671 my $seconds = $time % 60;
672 my $minutes = $time / 60;
675 if ($minutes >= 60) {
676 $hours = $minutes / 60;
680 return sprintf("%3d:%02d:%02d", $hours, $minutes, $seconds);
685 my $colour = ${Header
::colourred
};
690 foreach my $line (@network) {
691 if ($network[$id] eq '') {
694 if (!$found && ipv4_in_network
($network[$id], $masklen[$id], $ip) ) {
696 $colour = $colour[$id];