1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
#!/usr/bin/perl
#
# Module: vyatta-show-conntrack.pl
#
# **** License ****
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# 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.
#
# This code was originally developed by Vyatta, Inc.
# Portions created by Vyatta are Copyright (C) 2010 Vyatta, Inc.
# All Rights Reserved.
#
# Author: Gaurav Sinha
# Date: Oct 2011
# Description: Script to show conntrack entries based on the input
# show command.
#
# **** End License ****
#
use Getopt::Long;
use XML::Simple;
use Data::Dumper;
use POSIX;
use lib "/opt/vyatta/share/perl5";
use Vyatta::ConntrackUtil;
use Vyatta::Misc;
use warnings;
use strict;
use Switch;
use Vyatta::TypeChecker;
my $format = "%-10s %-22s %-22s %-12s %-20s\n";
my $format_IPv6 = "%-10s %-40s %-40s %-12s %-20s\n";
sub print_xml {
my ($data, $cache, $family) = @_;
my $flow = 0;
my %flowh;
my $tcount = 0;
while (1) {
my $meta = 0;
last if ! defined $data->{flow}[$flow];
my $flow_ref = $data->{flow}[$flow];
my $flow_type = $flow_ref->{type};
my (%src, %dst, %sport, %dport, %proto, %protonum, $timeout_ref, $connection_id_ref,
$state_connection_ref);
while (1) {
my $meta_ref = $flow_ref->{meta}[$meta];
last if ! defined $meta_ref;
my $dir = $meta_ref->{direction};
if ($dir eq 'original' or $dir eq 'reply') {
my $l3_ref = $meta_ref->{layer3}[0];
my $l4_ref = $meta_ref->{layer4}[0];
if (defined $l3_ref) {
$src{$dir} = $l3_ref->{src}[0];
$dst{$dir} = $l3_ref->{dst}[0];
if (defined $l4_ref) {
$sport{$dir} = $l4_ref->{sport}[0];
$dport{$dir} = $l4_ref->{dport}[0];
$proto{$dir} = $l4_ref->{protoname};
$protonum{$dir} = $l4_ref->{protonum};
}
}
} elsif ($dir eq 'independent') {
$timeout_ref = $meta_ref->{timeout}[0];
$connection_id_ref = $meta_ref->{id}[0];
$state_connection_ref = $meta_ref->{state}[0];
}
$meta++;
}
my ($proto, $protonum, $in_src, $in_dst, $out_src, $out_dst, $connection_id,
$timeout, $state_connection);
$proto = $proto{original};
$protonum = $protonum{original};
$in_src = "$src{original}";
$in_src .= ":$sport{original}" if defined $sport{original};
$in_dst = "$dst{original}";
$in_dst .= ":$dport{original}" if defined $dport{original};
$connection_id = "$connection_id_ref";
$timeout = "$timeout_ref";
if ($state_connection_ref) {
$state_connection = "$state_connection_ref";
}
# not using these for now
$out_src = "|$dst{reply}|";
$out_src .= ":$dport{reply}" if defined $dport{reply};
$out_dst = "|$src{reply}|";
$out_dst .= ":$sport{reply}" if defined $sport{reply};
my $protocol = $proto . ' [' . $protonum . ']';
if ($state_connection) {
switch($state_connection) {
case ("ESTABLISHED") {
$protocol .= " ES";
}
case ("SYN_SENT") {
$protocol .= " SS";
}
case ("SYN_RECV") {
$protocol .= " SR";
}
case ("FIN_WAIT") {
$protocol .= " FW";
}
case ("CLOSE_WAIT") {
$protocol .= " CW";
}
case ("LAST_ACK") {
$protocol .= " LA";
}
case ("TIME_WAIT") {
$protocol .= " TW";
}
case ("CLOSED") {
$protocol .= " CL";
}
case ("LISTEN") {
$protocol .= " LI";
}
}
}
if ( $family eq 'ipv6') {
#IPv6 Addresses can be 39 chars long, so chose the format as per family
printf($format_IPv6, $connection_id ,$in_src, $in_dst, $protocol, $timeout);
} else {
printf($format, $connection_id ,$in_src, $in_dst, $protocol, $timeout);
}
$flow++;
}
return $flow;
}
#
# main
#
my ($sourceIP, $destIP, $family);
GetOptions("source_IP=s" => \$sourceIP,
"dest_IP=s" => \$destIP,
"family=s" => \$family,
);
my $xs = XML::Simple->new(ForceArray => 1, KeepRoot => 0);
my ($xml1, $xml2, $data);
#build command string using <command_prefix><command>.
my $command_prefix = "sudo conntrack -L";
my $command = " --family $family";
my ($sourcePort, $destPort);
if ($family eq "ipv4") {
if ((defined $sourceIP) and $sourceIP =~ m/:/) {
#IP address and port entered, are of the form IP:port
my @address = split(/:/, $sourceIP);
$sourceIP = $address[0];
$sourcePort = $address[1];
if ((defined $sourceIP) and ($sourceIP eq "any")) {
$sourceIP = "0.0.0.0";
}
# Check if IP address is a valid IPv4 address
my( $success, $err ) = isValidPortNumber($sourcePort);
if (!(isIpAddress($sourceIP)) and !($sourceIP eq "0.0.0.0")) {
if(!defined($success)) {
#both IP and port are invalid
die "Please enter a valid source IPv4 address and port \n";
} else {
#only IP is invalid
die "Please enter a valid source IPv4 address\n";
}
}
if(!defined($success)) {
#port is invalid
die "Please enter a valid source port \n";
}
$command .= " --orig-port-src $sourcePort";
}
if ((defined $destIP) and $destIP =~ m/:/) {
my @address = split(/:/, $destIP);
$destIP = $address[0];
$destPort = $address[1];
if ((defined $destIP) and ($destIP eq "any")) {
$destIP = "0.0.0.0";
}
my( $success, $err ) = isValidPortNumber($destPort);
if (!(isIpAddress($destIP))and !($destIP eq "0.0.0.0")) {
if(!defined($success)) {
#both IP and port are invalid
die "Please enter a valid destination IPv4 address and port \n";
} else {
#only IP is invalid
die "Please enter a valid destination IPv4 address\n";
}
}
if(!defined($success)) {
#port is invalid
die "Please enter a valid destination port \n";
}
$command .= " --orig-port-dst $destPort";
}
if ((defined $sourceIP) and ($sourceIP eq "any")) {
$sourceIP = "0.0.0.0";
}
if ((defined $destIP) and ($destIP eq "any")) {
$destIP = "0.0.0.0";
}
if ((defined $sourceIP) and !($sourceIP eq "0.0.0.0")) {
# Check if IP address is a valid IPv4 address
if (!(isIpAddress($sourceIP))) {
die "Please enter a valid source IPv4 address\n";
}
#If IP is any, do not add anything to command.
$command .= " -s $sourceIP";
}
if ((defined $destIP) and !($destIP eq "0.0.0.0")) {
# Check if IP address is a valid IPv4 address
if (!(isIpAddress($destIP))) {
die "Please enter a valid destination IPv4 address\n";
}
$command .= " -d $destIP";
}
} else {
#IPv6 code.
if ((defined $sourceIP) and ($sourceIP ne "0:0:0:0:0:0:0:0")) {
if ((($sourceIP =~ m/^\[/) and (!($sourceIP =~ m/]/))) or
(!($sourceIP =~ m/^\[/) and (($sourceIP =~ m/]/)))) {
die "Please use prescribed format for source IP: [IPv6-address]:port \n";
}
if (($sourceIP =~ m/^\[/) and ($sourceIP =~ m/]/)) {
# [IPv6-address]:port
my @address = split(/]/, $sourceIP);
if (@address) {
if(!$address[0] or !$address[1]) {
die "Please use prescribed format for source IP: [IPv6-address]:port \n";
}
$sourceIP = substr($address[0], 1);
$sourcePort = substr($address[1], 1);
my( $success, $err ) = isValidPortNumber($sourcePort);
if ($sourceIP ne "any") {
if (validateType('ipv6', $sourceIP, 'quiet')) {
if ($sourceIP =~ m/[^ABCDEFabcdef0123456789:\[\]]/) {
die "Please enter a valid source IPv6 address\n";
}
} else {
if(!defined($success)) {
die "Please enter a valid source IPv6 address and port \n";
}
}
}
if(!defined($success)) {
die "Please enter a valid source port \n";
}
$command .= " --orig-port-src $sourcePort";
}
} else {
#IPv6-address without port
if ($sourceIP ne "any") {
if (validateType('ipv6', $sourceIP, 'quiet')) {
if ($sourceIP =~ m/[^ABCDEFabcdef0123456789:\[\]]/) {
die "Please enter a valid source IPv6 address\n";
}
} else {
die "Please enter a valid source IPv6 address\n";
}
}
}
}
if ((defined $destIP) and ($destIP ne "0:0:0:0:0:0:0:0")) {
if ((($destIP =~ m/^\[/) and (!($destIP =~ m/]/))) or
(!($destIP =~ m/^\[/) and (($destIP =~ m/]/)))) {
die "Please use prescribed format for destination IP: [IPv6-address]:port \n";
}
if (($destIP =~ m/^\[/) and ($destIP =~ m/]/)) {
my @address = split(/]/, $destIP);
if (@address) {
$destIP = substr($address[0], 1);
$destPort = substr($address[1], 1);
my( $success, $err ) = isValidPortNumber($destPort);
if ($destIP ne "any") {
if (validateType('ipv6', $destIP, 'quiet')) {
if ($destIP =~ m/[^ABCDEFabcdef0123456789:\[\]]/) {
die "Please enter a valid destination IPv6 address\n";
}
} else {
if(!defined($success)) {
die "Please enter a valid destination IPv6 address and port \n";
}
}
}
if(!defined($success)) {
die "Please enter a valid destination port \n";
}
#$command .= " --orig-port-dst $destPort";
}
} else {
#IPv6-address without port
if ($destIP ne "any") {
if (validateType('ipv6', $destIP, 'quiet')) {
if ($destIP =~ m/[^ABCDEFabcdef0123456789:\[\]]/) {
die "Please enter a valid destination IPv6 address\n";
}
} else {
die "Please enter a valid destination IPv6 address\n";
}
}
}
}
# Support "any" keyword
if ((defined $destIP) and ($destIP eq "any")) {
$destIP = "0:0:0:0:0:0:0:0";
}
if ((defined $sourceIP) and ($sourceIP eq "any")) {
$sourceIP = "0:0:0:0:0:0:0:0";
}
if (($sourceIP) and ($sourceIP ne "0:0:0:0:0:0:0:0")) {
$command .= " -s $sourceIP";
}
if (($destIP) and ($destIP ne "0:0:0:0:0:0:0:0")) {
$command .= " -d $destIP";
}
}
$command .= " -o xml";
print "TCP state codes: SS - SYN SENT, SR - SYN RECEIVED, ES - ESTABLISHED,\n";
print " FW - FIN WAIT, CW - CLOSE WAIT, LA - LAST ACK,\n";
print " TW - TIME WAIT, CL - CLOSE, LI - LISTEN\n\n";
#IPv6 Addresses can be 39 chars long, so chose the format as per family
if ($family eq 'ipv4') {
printf($format, 'CONN ID', 'Source', 'Destination', 'Protocol', 'TIMEOUT');
} else {
printf($format_IPv6, 'CONN ID', 'Source', 'Destination', 'Protocol', 'TIMEOUT');
}
if ((defined($destPort)) or (defined($sourcePort))) {
my $command_final = $command_prefix." -p tcp".$command;
$xml1 = `$command_final 2> /dev/null`;
#Execute the command for UDP as well.
$command_final = $command_prefix." -p udp".$command;
$xml2 = `$command_final 2> /dev/null`;
} else {
my $command_final = $command_prefix.$command;
$xml1 = `$command_final 2> /dev/null`;
}
if ($xml1) {
$data = $xs->XMLin($xml1);
print_xml($data, "", $family);
}
if ($xml2) {
$data = $xs->XMLin($xml2);
print_xml($data, "", $family);
}
if (!($xml1) and !($xml2)) {
if (!(Vyatta::ConntrackUtil::check_for_conntrack_hooks())) {
#Connection tracking is being used
die "\nWarning: Connection tracking is not enabled\n\n";
}
}
# end of file
|