| 1 | ############################################################################ | ||
| 2 | # TCP | ||
| 3 | # opens und closes connections and feeds data into them | ||
| 4 | # reorders packets based on sequence number | ||
| 5 | ############################################################################ | ||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | package Net::Inspect::L4::TCP; | ||
| 9 | use base 'Net::Inspect::Flow'; | ||
| 10 | use fields qw(conn); | ||
| 11 | use Net::Inspect::Debug qw( debug trace ); | ||
| 12 | |||
| 13 | # field conn | ||
| 14 | # hash indexed by {saddr,sport,daddr,dport} with values | ||
| 15 | # [ \@d0,\@d1,$conn ] | ||
| 16 | # $conn - connection object | ||
| 17 | # \@d0,\@d1 - information about data from client(0) or server(1). | ||
| 18 | # [$sn,\%pkt,\@buf,$state] | ||
| 19 | # $sn - all packets received up to this sequence number | ||
| 20 | # \%pkt - packets which are not added to \@buf (out of order, missing | ||
| 21 | # packet in between): sn -> [pkt,time] | ||
| 22 | # \@buf - ordered data which are not yet forwarded to attached flows [pkt,time] | ||
| 23 | # $state - bitmask ( 0b0000FfSs : Fin+ack|Fin|Syn+ack|Syn) | ||
| 24 | |||
| 25 | sub new { | ||
| 26 | my ($class,$flow) = @_; | ||
| 27 | my $self = $class->SUPER::new($flow); | ||
| 28 | $self->{conn} = {}; | ||
| 29 | return $self; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub pktin { | ||
| 33 | my Net::Inspect::L4::TCP $self = shift; | ||
| 34 | my ($pkt,$meta) = @_; | ||
| 35 | return if $meta->{proto} != 6; # does only handle tcp | ||
| 36 | |||
| 37 | # extract TCP header | ||
| 38 | my ($sport,$dport,$sn,$asn,$doc,$window,$cksum,$up) | ||
| 39 | = unpack('nnNNnnnn',$pkt); | ||
| 40 | |||
| 41 | # payload | ||
| 42 | my $do = ( $doc >> 12 )*4; | ||
| 43 | my $buf = substr($pkt,$do); | ||
| 44 | |||
| 45 | # flags | ||
| 46 | my $urg = $doc & 0b100000 && 1; | ||
| 47 | my $ack = $doc & 0b010000 && 1; | ||
| 48 | my $psh = $doc & 0b001000 && 1; | ||
| 49 | my $rst = $doc & 0b000100 && 1; | ||
| 50 | my $syn = $doc & 0b000010 && 1; | ||
| 51 | my $fin = $doc & 0b000001 && 1; | ||
| 52 | |||
| 53 | debug("%s%s%ssn=%u len=%d", | ||
| 54 | $syn ? 'SYN ':'', | ||
| 55 | $fin ? "FIN ":'', | ||
| 56 | $ack ? "ACK=$asn ":'', | ||
| 57 | $sn, | ||
| 58 | length($buf)); | ||
| 59 | |||
| 60 | my $saddr = $meta->{saddr}; | ||
| 61 | my $daddr = $meta->{daddr}; | ||
| 62 | |||
| 63 | # find or create connection | ||
| 64 | my ($conn,$dir); | ||
| 65 | if ( $conn = $self->{conn}{$saddr,$sport,$daddr,$dport} ) { | ||
| 66 | $dir = 0; | ||
| 67 | debug("found conn $conn $saddr.$sport(%04b) -> $daddr.$dport(%04b)", | ||
| 68 | $conn->[0][3],$conn->[1][3] ); | ||
| 69 | } elsif ( $conn = $self->{conn}{$daddr,$dport,$saddr,$sport} ) { | ||
| 70 | $dir = 1; | ||
| 71 | # saddr should point to client all time.. | ||
| 72 | ($saddr,$sport,$daddr,$dport) = ($daddr,$dport,$saddr,$sport); | ||
| 73 | debug("found conn $conn $saddr.$sport(%04b) <- $daddr.$dport(%04b)", | ||
| 74 | $conn->[0][3],$conn->[1][3] ); | ||
| 75 | } else { | ||
| 76 | $dir = 0 | ||
| 77 | } | ||
| 78 | |||
| 79 | # new connection, ISN = $sn | ||
| 80 | if ( $syn ) { | ||
| 81 | if ( $fin ) { | ||
| 82 | # invalid combination | ||
| 83 | trace("FIN+SYN($dir) for connection $saddr.$sport -> $daddr.$dport - ignoring"); | ||
| 84 | return 1; | ||
| 85 | } | ||
| 86 | |||
| 87 | debug("SYN($dir) for new flow $saddr.$sport -> $daddr.$dport"); | ||
| 88 | if ( $conn and $conn->[$dir][3] & 0b0001 ) { | ||
| 89 | # SYN already received, duplicate? | ||
| 90 | trace("duplicate SYN($dir) for connection $saddr.$sport -> $daddr.$dport - ignoring"); | ||
| 91 | return 1; | ||
| 92 | } | ||
| 93 | if ( ! $conn ) { | ||
| 94 | my $rv = $self->syn({ | ||
| 95 | time => $meta->{time}, | ||
| 96 | saddr => $saddr, | ||
| 97 | sport => $sport, | ||
| 98 | daddr => $daddr, | ||
| 99 | dport => $dport, | ||
| 100 | dir => $dir, | ||
| 101 | proto => 6, | ||
| 102 | }); | ||
| 103 | if ( defined $rv && ! $rv ) { | ||
| 104 | # we don't want a connection for this | ||
| 105 | debug("ignored syn $saddr.$sport -> $daddr.$dport because of syn-hook"); | ||
| 106 | return 1; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | # dok for structure see top of file | ||
| 111 | if ( ! $conn ) { | ||
| 112 | $conn = [ [ undef,{},[],0], [ undef,{},[],0], undef ]; | ||
| 113 | # register new connection | ||
| 114 | debug("register conn $saddr.$sport -> $daddr.$dport $conn"); | ||
| 115 | $self->{conn}{$saddr,$sport,$daddr,$dport} = $conn; | ||
| 116 | } | ||
| 117 | |||
| 118 | # set ISN and state for direction | ||
| 119 | $conn->[$dir][0] = $sn+1; | ||
| 120 | $conn->[$dir][3] = 0b0001; | ||
| 121 | } | ||
| 122 | |||
| 123 | # must have $conn here or ignore packet | ||
| 124 | if ( ! $conn ) { | ||
| 125 | debug("no established connection $saddr.$sport <-> $daddr.$dport"); | ||
| 126 | return 1; | ||
| 127 | } | ||
| 128 | |||
| 129 | if ( $buf ne '' or $fin ) { | ||
| 130 | |||
| 131 | # add buf to packets | ||
| 132 | $conn->[$dir][1]{$sn} = [$buf,$meta->{time}] if $buf ne ''; | ||
| 133 | |||
| 134 | # set FIN flag - no more data expected from peer after this point | ||
| 135 | # but outstanding packets might still come in | ||
| 136 | if ( $fin ) { | ||
| 137 | if ( not $conn->[$dir][3] & 0b0100 ) { | ||
| 138 | debug("shutdown dir $dir $saddr.$sport -> $daddr.$dport"); | ||
| 139 | $conn->[$dir][3] |= 0b0100; # fin received | ||
| 140 | # must increase sn for ACK | ||
| 141 | $conn->[$dir][1]{ ( $sn+length($buf) ) % 2**32 } = [ '',$meta->{time} ]; | ||
| 142 | } else { | ||
| 143 | # probably duplicate | ||
| 144 | debug("ignore duplicate FIN($dir) $saddr.$sport -> $daddr.$dport"); | ||
| 145 | return 1; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | if ( $ack ) { | ||
| 151 | my $odir = $dir?0:1; | ||
| 152 | |||
| 153 | # reorder and concat packets up to acknowledged value and forward | ||
| 154 | # to attached flows | ||
| 155 | my $pkts = $conn->[$odir][1]; | ||
| 156 | my $eof = 0; | ||
| 157 | if ( %$pkts ) { | ||
| 158 | my $xsn = $conn->[$odir][0]; | ||
| 159 | my $obuf = $conn->[$odir][2]; | ||
| 160 | while ( $xsn != $asn and $pkts->{$xsn} ) { | ||
| 161 | my $pkt = delete $pkts->{$xsn}; | ||
| 162 | # merge packets which came later but are earlier in the stream | ||
| 163 | # into $pkt | ||
| 164 | while (@$obuf) { | ||
| 165 | last if $obuf->[-1][1] < $pkt->[1]; # earlier in time | ||
| 166 | $pkt->[0] = pop(@$obuf)->[0].$pkt->[0]; | ||
| 167 | } | ||
| 168 | push @$obuf,$pkt; | ||
| 169 | |||
| 170 | if ( $pkt->[0] ne '' ) { | ||
| 171 | $xsn = ( $xsn + length($pkt->[0])) % 2**32; | ||
| 172 | |||
| 173 | } else { | ||
| 174 | debug("got ACK for FIN($odir)"); | ||
| 175 | $xsn = ( $xsn + 1 ) % 2**32; | ||
| 176 | |||
| 177 | # eof - set to 2 if both sides closed | ||
| 178 | $eof = $conn->[$dir][3] & 0b1000 ? 2:1; | ||
| 179 | |||
| 180 | # upper flow needs to process all remaining data on eof, so | ||
| 181 | # pack them together | ||
| 182 | if (@$obuf>1) { | ||
| 183 | $pkt = shift(@$obuf); | ||
| 184 | $pkt->[0] .= $_->[0] for(@$obuf); | ||
| 185 | @$obuf = $pkt; | ||
| 186 | } | ||
| 187 | |||
| 188 | # ignore data after fin | ||
| 189 | if ( %$pkts) { | ||
| 190 | trace("ignoring packets after FIN($odir) $saddr.$sport -> $daddr.$dport"); | ||
| 191 | %$pkts = (); | ||
| 192 | } | ||
| 193 | |||
| 194 | last; | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | # lost packets or ack points in the middle of packet | ||
| 199 | if ( $xsn != $asn ) { | ||
| 200 | trace("lost packets before ACK($odir)=$asn SN($odir)=$xsn $saddr.$sport -> $daddr.$dport"); | ||
| 201 | delete $self->{conn}{$saddr,$sport,$daddr,$dport}; | ||
| 202 | if ( my $obj = $conn->[2] ) { | ||
| 203 | $obj->fatal( "lost packets before ACK($odir)=$asn, SN($odir)=$xsn", | ||
| 204 | $dir,$meta->{time}); | ||
| 205 | } | ||
| 206 | return 1; | ||
| 207 | } | ||
| 208 | |||
| 209 | # update sn etc | ||
| 210 | $conn->[$odir][0] = $xsn; | ||
| 211 | $conn->[$odir][3] |= 0b1000 if $eof; | ||
| 212 | |||
| 213 | # forward data | ||
| 214 | if ( my $obj = $conn->[2] ) { | ||
| 215 | while ( my $buf = shift(@$obuf) ) { | ||
| 216 | my $n = $obj->in($odir,$buf->[0],$eof,$buf->[1]); | ||
| 217 | |||
| 218 | if ( ! defined $n ) { | ||
| 219 | # error processing -> close | ||
| 220 | # don't call fatal, hook probably reported error already | ||
| 221 | trace("error processing data in hook in $saddr.$sport -> $daddr.$dport"); | ||
| 222 | delete $self->{conn}{$saddr,$sport,$daddr,$dport}; | ||
| 223 | return 1; | ||
| 224 | } elsif ( $n == length($buf->[0]) ) { | ||
| 225 | # everything processed | ||
| 226 | next; | ||
| 227 | } elsif ( $eof ) { | ||
| 228 | $obj->fatal("handler did not eat all data on eof",$dir,$meta->{time}); | ||
| 229 | return 1; | ||
| 230 | } else { | ||
| 231 | # keep bytes in $buf which were not processed | ||
| 232 | substr($buf->[0],0,$n,''); | ||
| 233 | debug("keep %d bytes in buffer for $odir",length($buf->[1])); | ||
| 234 | unshift @$obuf,$buf; | ||
| 235 | last; | ||
| 236 | } | ||
| 237 | } | ||
| 238 | } | ||
| 239 | |||
| 240 | # eof: if other side fin+acked too reap connection | ||
| 241 | if ( $eof == 2 ) { | ||
| 242 | debug("connection $saddr.$sport -> $daddr.$dport closed"); | ||
| 243 | delete $self->{conn}{$saddr,$sport,$daddr,$dport}; | ||
| 244 | return 1; | ||
| 245 | } | ||
| 246 | } | ||
| 247 | |||
| 248 | # check for ACK to SYN | ||
| 249 | my $osn = $conn->[$odir][0]; | ||
| 250 | if ( ! defined $osn ) { | ||
| 251 | # got ack w/o syn? | ||
| 252 | trace("received ACK w/o SYN for dir=$odir $saddr.$sport -> $daddr.$dport"); | ||
| 253 | return 1; | ||
| 254 | } | ||
| 255 | |||
| 256 | # ACK for SYN? | ||
| 257 | if ( not $conn->[$odir][3] & 0b0010 ) { | ||
| 258 | # check that ACK matches ISN from SYN | ||
| 259 | if ( $osn % 2**32 != $asn ) { | ||
| 260 | trace("got ACK($asn) which does not match SYN($osn)"); | ||
| 261 | return 1; | ||
| 262 | } | ||
| 263 | # ack ok | ||
| 264 | $conn->[$odir][3] |= 0b0010; | ||
| 265 | debug("got ACK for SYN($odir)"); | ||
| 266 | |||
| 267 | # got other side syn+ack too? -> new connection | ||
| 268 | if ( $conn->[$dir][3] & 0b0010 ) { | ||
| 269 | # make connection object | ||
| 270 | my $obj = $self->new_connection({ | ||
| 271 | time => $meta->{time}, | ||
| 272 | saddr => $saddr, | ||
| 273 | sport => $sport, | ||
| 274 | daddr => $daddr, | ||
| 275 | dport => $dport, | ||
| 276 | }); | ||
| 277 | if ( ! $obj ) { | ||
| 278 | # hook says ignore connection | ||
| 279 | debug("ignoring connection $saddr.$sport -> $daddr.$dport because of hook"); | ||
| 280 | delete $self->{conn}{$saddr,$sport,$daddr,$dport}; | ||
| 281 | return 1; | ||
| 282 | } | ||
| 283 | debug("created object for $saddr.$sport -> $daddr.$dport $obj"); | ||
| 284 | $conn->[2] = $obj; | ||
| 285 | } | ||
| 286 | } | ||
| 287 | } | ||
| 288 | |||
| 289 | return 1; | ||
| 290 | } | ||
| 291 | |||
| 292 | sub syn { shift->{upper_flow}->syn(@_) } | ||
| 293 | sub new_connection { shift->{upper_flow}->new_connection(@_) } | ||
| 294 | |||
| 295 | sub expire { | ||
| 296 | my ($self,$time) = @_; | ||
| 297 | while (my ($k,$c) = each %{$self->{conn}} ) { | ||
| 298 | $c->[2] or next; | ||
| 299 | $c->[2]->expire($time) or next; | ||
| 300 | delete $self->{conn}{$k} | ||
| 301 | } | ||
| 302 | } | ||
| 303 | |||
| 304 | |||
| 305 | 1; | ||
| 306 | |||
| 307 | __END__ |