| 1 | # | ||
| 2 | # $Id: Frame.pm,v 1.3.2.8 2006/06/04 13:56:23 gomor Exp $ | ||
| 3 | # | ||
| 4 | package Net::Packet::Frame; | ||
| 5 | use warnings; | ||
| 6 | use strict; | ||
| 7 | use Carp; | ||
| 8 | |||
| 9 | require Class::Gomor::Array; | ||
| 10 | our @ISA = qw(Class::Gomor::Array); | ||
| 11 | |||
| 12 | require Net::Packet; | ||
| 13 | require Net::Packet::Dump; | ||
| 14 | require Net::Packet::ETH; | ||
| 15 | require Net::Packet::ARP; | ||
| 16 | require Net::Packet::IPv4; | ||
| 17 | require Net::Packet::IPv6; | ||
| 18 | require Net::Packet::TCP; | ||
| 19 | require Net::Packet::UDP; | ||
| 20 | require Net::Packet::ICMPv4; | ||
| 21 | require Net::Packet::Layer7; | ||
| 22 | require Net::Packet::NULL; | ||
| 23 | require Net::Packet::RAW; | ||
| 24 | require Net::Packet::SLL; | ||
| 25 | |||
| 26 | use Time::HiRes qw(gettimeofday); | ||
| 27 | use Net::Packet::Env qw($Env); | ||
| 28 | use Net::Packet::Consts qw(:dump :layer :arp); | ||
| 29 | |||
| 30 | our @AS = qw( | ||
| 31 | env | ||
| 32 | raw | ||
| 33 | padding | ||
| 34 | l2 | ||
| 35 | l3 | ||
| 36 | l4 | ||
| 37 | l7 | ||
| 38 | reply | ||
| 39 | timestamp | ||
| 40 | noPadding | ||
| 41 | ); | ||
| 42 | __PACKAGE__->cgBuildIndices; | ||
| 43 | __PACKAGE__->cgBuildAccessorsScalar(\@AS); | ||
| 44 | |||
| 45 | no strict 'vars'; | ||
| 46 | |||
| 47 | sub _gettimeofday { | ||
| 48 | my ($sec, $usec) = gettimeofday(); | ||
| 49 | sprintf("%d.%06d", $sec, $usec); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub new { | ||
| 53 | my $self = shift->SUPER::new( | ||
| 54 | timestamp => _gettimeofday(), | ||
| 55 | env => $Env, | ||
| 56 | noPadding => 0, | ||
| 57 | @_, | ||
| 58 | ); | ||
| 59 | |||
| 60 | my $env = $self->[$__env]; | ||
| 61 | |||
| 62 | if (! $env->noFrameAutoDesc && ! $env->desc) { | ||
| 63 | if ($self->[$__l2]) { | ||
| 64 | require Net::Packet::DescL2; | ||
| 65 | $env->desc(Net::Packet::DescL2->new); | ||
| 66 | $self->cgDebugPrint(1, "DescL2 object created"); | ||
| 67 | } | ||
| 68 | elsif ($self->[$__l3]) { | ||
| 69 | require Net::Packet::DescL3; | ||
| 70 | $env->desc(Net::Packet::DescL3->new( | ||
| 71 | target => $self->[$__l3]->dst, | ||
| 72 | )); | ||
| 73 | $self->cgDebugPrint(1, "DescL3 object created"); | ||
| 74 | } | ||
| 75 | elsif ($self->[$__l4]) { | ||
| 76 | confess("@{[(caller(0))[3]]}: you must manually create a DescL4 ". | ||
| 77 | "object\n"); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 81 | if (! $env->noFrameAutoDump && ! $env->dump) { | ||
| 82 | require Net::Packet::Dump; | ||
| 83 | my $dumpFilter = ($env->dump && $env->dump->filter); | ||
| 84 | $env->dump( | ||
| 85 | Net::Packet::Dump->new( | ||
| 86 | filter => $dumpFilter || $self->getFilter, | ||
| 87 | ), | ||
| 88 | ); | ||
| 89 | $self->cgDebugPrint(1, "Dump object created"); | ||
| 90 | } | ||
| 91 | |||
| 92 | $self->[$__raw] ? $self->unpack : $self->pack; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub getLengthFromL7 { | ||
| 96 | my $self = shift; | ||
| 97 | $self->[$__l7] ? $self->[$__l7]->getLength : 0; | ||
| 98 | } | ||
| 99 | sub getLengthFromL4 { | ||
| 100 | my $self = shift; | ||
| 101 | my $len = 0; | ||
| 102 | $len += $self->[$__l4]->getLength if $self->[$__l4]; | ||
| 103 | $len += $self->getLengthFromL7; | ||
| 104 | $len || 0; | ||
| 105 | } | ||
| 106 | sub getLengthFromL3 { | ||
| 107 | my $self = shift; | ||
| 108 | my $len = 0; | ||
| 109 | $len += $self->[$__l3]->getLength if $self->[$__l3]; | ||
| 110 | $len += $self->getLengthFromL4; | ||
| 111 | $len || 0; | ||
| 112 | } | ||
| 113 | sub getLengthFromL2 { | ||
| 114 | my $self = shift; | ||
| 115 | my $len = 0; | ||
| 116 | $len += $self->[$__l2]->getLength if $self->[$__l2]; | ||
| 117 | $len += $self->getLengthFromL3; | ||
| 118 | $len || 0; | ||
| 119 | } | ||
| 120 | sub getLength { shift->getLengthFromL3 } | ||
| 121 | |||
| 122 | sub _unpackFromL3 { | ||
| 123 | my $self = shift; | ||
| 124 | |||
| 125 | my $nextLayer; | ||
| 126 | while (1) { | ||
| 127 | my $l3; | ||
| 128 | |||
| 129 | # First, try IPv4 | ||
| 130 | $l3 = Net::Packet::IPv4->new(raw => $self->[$__raw]) or return undef; | ||
| 131 | unless ($l3->version == 4) { | ||
| 132 | # Then IPv6 | ||
| 133 | $l3 = Net::Packet::IPv6->new(raw => $self->[$__raw]) or return undef; | ||
| 134 | unless ($l3->version == 6) { | ||
| 135 | # Then ARP | ||
| 136 | $l3 = Net::Packet::ARP->new(raw => $self->[$__raw]) or return undef; | ||
| 137 | unless ($l3->hType eq NP_ARP_HTYPE_ETH) { | ||
| 138 | carp("@{[(caller(0))[3]]}: unknown frame, unable to unpack\n"); | ||
| 139 | return undef; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | if ($l3->encapsulate eq NP_LAYER_UNKNOWN) { | ||
| 145 | carp("@{[(caller(0))[3]]}: unknown Layer4 protocol\n"); | ||
| 146 | last; | ||
| 147 | } | ||
| 148 | |||
| 149 | $self->[$__l3] = $l3; | ||
| 150 | |||
| 151 | last if $self->[$__l3]->encapsulate eq NP_LAYER_NONE; | ||
| 152 | $nextLayer = NP_LAYER. $self->[$__l3]->encapsulate; | ||
| 153 | |||
| 154 | $self->[$__l4] = $nextLayer->new(raw => $self->[$__l3]->payload) | ||
| 155 | or return undef; | ||
| 156 | |||
| 157 | # Here, no check; it is just raw layer 7 application data | ||
| 158 | last if $self->[$__l4]->encapsulate eq NP_LAYER_NONE; | ||
| 159 | $nextLayer = NP_LAYER. $self->[$__l4]->encapsulate; | ||
| 160 | |||
| 161 | $self->[$__l7] = $nextLayer->new(raw => $self->[$__l4]->payload) | ||
| 162 | or return undef; | ||
| 163 | |||
| 164 | last; | ||
| 165 | } | ||
| 166 | |||
| 167 | $self; | ||
| 168 | } | ||
| 169 | |||
| 170 | sub unpack { | ||
| 171 | my $self = shift; | ||
| 172 | |||
| 173 | my $whichLink = { | ||
| 174 | NP_DUMP_LINK_NULL() => | ||
| 175 | sub { Net::Packet::NULL->new(raw => $self->[$__raw]) }, | ||
| 176 | NP_DUMP_LINK_EN10MB() => | ||
| 177 | sub { Net::Packet::ETH->new(raw => $self->[$__raw]) }, | ||
| 178 | NP_DUMP_LINK_RAW() => | ||
| 179 | sub { Net::Packet::RAW->new(raw => $self->[$__raw]) }, | ||
| 180 | NP_DUMP_LINK_SLL() => | ||
| 181 | sub { Net::Packet::SLL->new(raw => $self->[$__raw]) }, | ||
| 182 | }; | ||
| 183 | |||
| 184 | my $nextLayer; | ||
| 185 | while (1) { | ||
| 186 | unless (exists $whichLink->{$self->[$__env]->dump->link}) { | ||
| 187 | carp("Unable to unpack Frame for this datalink type: ". | ||
| 188 | "@{[$self->[$__env]->dump->link]}\n"); | ||
| 189 | last; | ||
| 190 | } | ||
| 191 | |||
| 192 | my $l2 = $whichLink->{$self->[$__env]->dump->link}() or return undef; | ||
| 193 | |||
| 194 | $self->[$__l2] = $l2; | ||
| 195 | |||
| 196 | # For example, with a raw Datalink type (RAW.pm), | ||
| 197 | # we don't know what is encapsulated | ||
| 198 | if ($self->[$__l2]->encapsulate eq NP_LAYER_UNKNOWN) { | ||
| 199 | return $self->_unpackFromL3; | ||
| 200 | } | ||
| 201 | |||
| 202 | last if $self->[$__l2]->encapsulate eq NP_LAYER_NONE; | ||
| 203 | $nextLayer = NP_LAYER. $self->[$__l2]->encapsulate; | ||
| 204 | |||
| 205 | $self->[$__l3] = $nextLayer->new(raw => $l2->payload) | ||
| 206 | or return undef; | ||
| 207 | |||
| 208 | if ($self->[$__l3]->encapsulate eq NP_LAYER_UNKNOWN) { | ||
| 209 | carp("@{[(caller(0))[3]]}: unknown Layer4 protocol\n"); | ||
| 210 | last; | ||
| 211 | } | ||
| 212 | |||
| 213 | $self->_fixWithIpLen if $self->isIpv4; | ||
| 214 | $self->_getArpPadding if $self->isArp; | ||
| 215 | |||
| 216 | last if $self->[$__l3]->encapsulate eq NP_LAYER_NONE; | ||
| 217 | $nextLayer = NP_LAYER. $self->[$__l3]->encapsulate; | ||
| 218 | |||
| 219 | $self->[$__l4] = $nextLayer->new(raw => $self->[$__l3]->payload) | ||
| 220 | or return undef; | ||
| 221 | |||
| 222 | last if $self->[$__l4]->encapsulate eq NP_LAYER_NONE; | ||
| 223 | $nextLayer = NP_LAYER. $self->[$__l4]->encapsulate; | ||
| 224 | |||
| 225 | $self->[$__l7] = $nextLayer->new(raw => $self->[$__l4]->payload) | ||
| 226 | or return undef; | ||
| 227 | |||
| 228 | last; | ||
| 229 | } | ||
| 230 | |||
| 231 | $self; | ||
| 232 | } | ||
| 233 | |||
| 234 | sub pack { | ||
| 235 | my $self = shift; | ||
| 236 | |||
| 237 | # They all need info about other layers, to do their work | ||
| 238 | if ($self->[$__l2]) { | ||
| 239 | $self->[$__l2]->computeLengths($self) or return undef; | ||
| 240 | $self->[$__l2]->computeChecksums($self) or return undef; | ||
| 241 | $self->[$__l2]->pack or return undef; | ||
| 242 | } | ||
| 243 | if ($self->[$__l3]) { | ||
| 244 | $self->[$__l3]->computeLengths($self) or return undef; | ||
| 245 | $self->[$__l3]->computeChecksums($self) or return undef; | ||
| 246 | $self->[$__l3]->pack or return undef; | ||
| 247 | } | ||
| 248 | if ($self->[$__l4]) { | ||
| 249 | $self->[$__l4]->computeLengths($self) or return undef; | ||
| 250 | $self->[$__l4]->computeChecksums($self) or return undef; | ||
| 251 | $self->[$__l4]->pack or return undef; | ||
| 252 | } | ||
| 253 | if ($self->[$__l7]) { | ||
| 254 | $self->[$__l7]->computeLengths($self) or return undef; | ||
| 255 | $self->[$__l7]->computeChecksums($self) or return undef; | ||
| 256 | $self->[$__l7]->pack or return undef; | ||
| 257 | } | ||
| 258 | |||
| 259 | my $raw; | ||
| 260 | $raw .= $self->[$__l2]->raw if $self->[$__l2]; | ||
| 261 | $raw .= $self->[$__l3]->raw if $self->[$__l3]; | ||
| 262 | $raw .= $self->[$__l4]->raw if $self->[$__l4]; | ||
| 263 | $raw .= $self->[$__l7]->raw if $self->[$__l7]; | ||
| 264 | |||
| 265 | if ($raw) { | ||
| 266 | $self->[$__raw] = $raw; | ||
| 267 | |||
| 268 | $self->_padFrame unless $self->[$__noPadding]; | ||
| 269 | } | ||
| 270 | |||
| 271 | $self; | ||
| 272 | } | ||
| 273 | |||
| 274 | sub _padFrame { | ||
| 275 | my $self = shift; | ||
| 276 | |||
| 277 | # Pad this frame, this we send at layer 2 | ||
| 278 | if ($self->[$__l2] && $self->[$__env]->desc->isDescL2) { | ||
| 279 | my $rawLength = length($self->[$__raw]); | ||
| 280 | if ($rawLength < 60) { | ||
| 281 | $self->[$__padding] = ('G' x (60 - $rawLength)); | ||
| 282 | $self->[$__raw] = $self->[$__raw].$self->[$__padding]; | ||
| 283 | } | ||
| 284 | } | ||
| 285 | } | ||
| 286 | |||
| 287 | # Will wipe out the trailing memory disclosure found in the packet | ||
| 288 | # and put it into padding instance data | ||
| 289 | sub _fixWithIpLen { | ||
| 290 | my $self = shift; | ||
| 291 | |||
| 292 | my $oldLen = length($self->[$__l3]->payload); | ||
| 293 | |||
| 294 | my $truncated = | ||
| 295 | substr($self->[$__l3]->payload, 0, $self->[$__l3]->getPayloadLength); | ||
| 296 | my $truncLen = length($truncated); | ||
| 297 | my $padding = | ||
| 298 | substr($self->[$__l3]->payload, $truncLen, $oldLen - $truncLen); | ||
| 299 | |||
| 300 | $self->[$__l3]->payload($truncated); | ||
| 301 | $self->[$__padding] = $padding; | ||
| 302 | } | ||
| 303 | |||
| 304 | # Same as previous, but ARP version | ||
| 305 | sub _getArpPadding { | ||
| 306 | my $self = shift; | ||
| 307 | |||
| 308 | my $len = length($self->[$__raw]); | ||
| 309 | ($len > 42) | ||
| 310 | ? do { $self->[$__padding] = substr($self->[$__raw], 42, $len - 42) } | ||
| 311 | : do { $self->[$__padding] = ''}; | ||
| 312 | } | ||
| 313 | |||
| 314 | sub send { | ||
| 315 | my $self = shift; | ||
| 316 | |||
| 317 | my $env = $self->[$__env]; | ||
| 318 | |||
| 319 | if ($env->dump && ! $env->dump->isRunning) { | ||
| 320 | $env->dump->start; | ||
| 321 | $self->cgDebugPrint(1, "Dump object started"); | ||
| 322 | } | ||
| 323 | |||
| 324 | if ($env->debug >= 3) { | ||
| 325 | if ($self->isEth) { | ||
| 326 | $self->cgDebugPrint(3, | ||
| 327 | "send: l2: type:". sprintf("0x%x", $self->l2->type). ", ". | ||
| 328 | "@{[$self->l2->src]} => @{[$self->l2->dst]}" | ||
| 329 | ); | ||
| 330 | } | ||
| 331 | |||
| 332 | if ($self->isIp) { | ||
| 333 | $self->cgDebugPrint(3, | ||
| 334 | "send: l3: protocol:@{[$self->l3->protocol]}, ". | ||
| 335 | "size:@{[$self->getLength]}, ". | ||
| 336 | "@{[$self->l3->src]} => @{[$self->l3->dst]}" | ||
| 337 | ); | ||
| 338 | } | ||
| 339 | elsif ($self->isArp) { | ||
| 340 | $self->cgDebugPrint(3, | ||
| 341 | "send: l3: @{[$self->l3->src]} => @{[$self->l3->dst]}" | ||
| 342 | ); | ||
| 343 | } | ||
| 344 | |||
| 345 | if ($self->isTcp || $self->isUdp) { | ||
| 346 | $self->cgDebugPrint(3, | ||
| 347 | "send: l4: @{[$self->l4->is]}, ". | ||
| 348 | "@{[$self->l4->src]} => @{[$self->l4->dst]}" | ||
| 349 | ); | ||
| 350 | } | ||
| 351 | } | ||
| 352 | |||
| 353 | $self->[$__timestamp] = _gettimeofday(); | ||
| 354 | $env->desc->send($self->[$__raw]); | ||
| 355 | } | ||
| 356 | |||
| 357 | sub reSend { my $self = shift; $self->send unless $self->[$__reply] } | ||
| 358 | |||
| 359 | sub getFilter { | ||
| 360 | my $self = shift; | ||
| 361 | |||
| 362 | my $filter; | ||
| 363 | |||
| 364 | # L4 filtering | ||
| 365 | if ($self->[$__l4]) { | ||
| 366 | if ($self->isTcp) { | ||
| 367 | $filter .= "(tcp and". | ||
| 368 | " src port @{[$self->[$__l4]->dst]}". | ||
| 369 | " and dst port @{[$self->[$__l4]->src]})"; | ||
| 370 | } | ||
| 371 | elsif ($self->isUdp) { | ||
| 372 | $filter .= "(udp and". | ||
| 373 | " src port @{[$self->[$__l4]->dst]}". | ||
| 374 | " and dst port @{[$self->[$__l4]->src]})"; | ||
| 375 | } | ||
| 376 | elsif ($self->isIcmpv4) { | ||
| 377 | $filter .= "(icmp)"; | ||
| 378 | } | ||
| 379 | $filter .= " or icmp"; | ||
| 380 | } | ||
| 381 | |||
| 382 | # L3 filtering | ||
| 383 | if ($self->[$__l3]) { | ||
| 384 | $filter .= " and " if $filter; | ||
| 385 | |||
| 386 | if ($self->isIpv4) { | ||
| 387 | $filter .= "(src host @{[$self->[$__l3]->dst]}". | ||
| 388 | " and dst host @{[$self->[$__l3]->src]}) ". | ||
| 389 | " or ". | ||
| 390 | "(icmp and dst host @{[$self->[$__l3]->src]})"; | ||
| 391 | } | ||
| 392 | elsif ($self->isIpv6) { | ||
| 393 | $filter .= "(ip6 and src host @{[$self->[$__l3]->dst]}". | ||
| 394 | " and dst host @{[$self->[$__l3]->src]})"; | ||
| 395 | } | ||
| 396 | elsif ($self->isArp) { | ||
| 397 | $filter .= "(arp and src host @{[$self->[$__l3]->dstIp]}". | ||
| 398 | " and dst host @{[$self->[$__l3]->srcIp]})"; | ||
| 399 | } | ||
| 400 | } | ||
| 401 | |||
| 402 | $filter; | ||
| 403 | } | ||
| 404 | |||
| 405 | sub recv { | ||
| 406 | my $self = shift; | ||
| 407 | |||
| 408 | $self->[$__env]->dump->nextAll if $self->[$__env]->dump->isRunning; | ||
| 409 | |||
| 410 | # We already have the reply | ||
| 411 | return undef if $self->[$__reply]; | ||
| 412 | |||
| 413 | croak("@{[(caller(0))[3]]}: \$self->env->dump variable not set\n") | ||
| 414 | unless $self->[$__env]->dump; | ||
| 415 | |||
| 416 | if ($self->[$__l4] && $self->[$__l4]->can('recv')) { | ||
| 417 | $self->[$__reply] = $self->[$__l4]->recv($self); | ||
| 418 | } | ||
| 419 | elsif ($self->[$__l3] && $self->[$__l3]->can('recv')) { | ||
| 420 | $self->[$__reply] = $self->[$__l3]->recv($self); | ||
| 421 | } | ||
| 422 | else { | ||
| 423 | carp("@{[(caller(0))[3]]}: not implemented for this Layer\n"); | ||
| 424 | return undef; | ||
| 425 | } | ||
| 426 | |||
| 427 | $self->[$__reply] | ||
| 428 | ? do { $self->cgDebugPrint(1, "Reply received"); return $self->[$__reply]} | ||
| 429 | : return undef; | ||
| 430 | } | ||
| 431 | |||
| 432 | # | ||
| 433 | # Helpers | ||
| 434 | # | ||
| 435 | |||
| 436 | sub _isL2 { my $self = shift; $self->[$__l2] && $self->[$__l2]->is eq shift() } | ||
| 437 | sub _isL3 { my $self = shift; $self->[$__l3] && $self->[$__l3]->is eq shift() } | ||
| 438 | sub _isL4 { my $self = shift; $self->[$__l4] && $self->[$__l4]->is eq shift() } | ||
| 439 | sub _isL7 { my $self = shift; $self->[$__l7] && $self->[$__l7]->is eq shift() } | ||
| 440 | sub isEth { shift->_isL2(NP_LAYER_ETH) } | ||
| 441 | sub isRaw { shift->_isL2(NP_LAYER_RAW) } | ||
| 442 | sub isNull { shift->_isL2(NP_LAYER_NULL) } | ||
| 443 | sub isSll { shift->_isL2(NP_LAYER_SLL) } | ||
| 444 | sub isArp { shift->_isL3(NP_LAYER_ARP) } | ||
| 445 | sub isIpv4 { shift->_isL3(NP_LAYER_IPv4) } | ||
| 446 | sub isIpv6 { shift->_isL3(NP_LAYER_IPv6) } | ||
| 447 | sub isVlan { shift->_isL3(NP_LAYER_VLAN) } | ||
| 448 | sub isTcp { shift->_isL4(NP_LAYER_TCP) } | ||
| 449 | sub isUdp { shift->_isL4(NP_LAYER_UDP) } | ||
| 450 | sub isIcmpv4 { shift->_isL4(NP_LAYER_ICMPv4) } | ||
| 451 | sub is7 { shift->_isL7(NP_LAYER_7) } | ||
| 452 | sub isIp { my $self = shift; $self->isIpv4 || $self->isIpv6 } | ||
| 453 | sub isIcmp { my $self = shift; $self->isIcmpv4 } # XXX: || v6 | ||
| 454 | |||
| 455 | 1; | ||
| 456 | |||
| 457 | __END__ |