3 # Copyright (c) 2009, 2010 Nicira Networks.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at:
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
20 open(FLOWS, ">&=3");# or die "failed to open fd 3 for writing: $!\n";
21 open(PACKETS, ">&=4");# or die "failed to open fd 4 for writing: $!\n";
23 # Print pcap file header.
24 print PACKETS pack('NnnNNNN',
25 0xa1b2c3d4, # magic number
29 0, # time stamp accuracy
33 output(DL_HEADER => '802.2');
35 for my $dl_header qw(802.2+SNAP Ethernet) {
36 my %a = (DL_HEADER => $dl_header);
37 for my $dl_vlan qw(none zero nonzero) {
38 my %b = (%a, DL_VLAN => $dl_vlan);
41 output(%b, DL_TYPE => 'non-ip');
43 for my $ip_options qw(no yes) {
44 my %c = (%b, DL_TYPE => 'ip', IP_OPTIONS => $ip_options);
45 for my $ip_fragment qw(no first middle last) {
46 my %d = (%c, IP_FRAGMENT => $ip_fragment);
47 for my $tp_proto qw(TCP TCP+options UDP ICMP other) {
48 output(%d, TP_PROTO => $tp_proto);
60 $flow{DL_SRC} = "00:02:e3:0f:80:a4";
61 $flow{DL_DST} = "00:1a:92:40:ac:05";
64 $flow{NW_SRC} = '0.0.0.0';
65 $flow{NW_DST} = '0.0.0.0';
68 if (defined($attrs{DL_VLAN})) {
69 my (%vlan_map) = ('none' => 0xffff,
72 $flow{DL_VLAN} = $vlan_map{$attrs{DL_VLAN}};
74 $flow{DL_VLAN} = 0xffff; # OFP_VLAN_NONE
76 if ($attrs{DL_HEADER} eq '802.2') {
77 $flow{DL_TYPE} = 0x5ff; # OFP_DL_TYPE_NOT_ETH_TYPE
78 } elsif ($attrs{DL_TYPE} eq 'ip') {
79 $flow{DL_TYPE} = 0x0800; # ETH_TYPE_IP
80 $flow{NW_SRC} = '10.0.2.15';
81 $flow{NW_DST} = '192.168.1.20';
83 if ($attrs{TP_PROTO} eq 'other') {
85 } elsif ($attrs{TP_PROTO} eq 'TCP' ||
86 $attrs{TP_PROTO} eq 'TCP+options') {
87 $flow{NW_PROTO} = 6; # IP_TYPE_TCP
90 } elsif ($attrs{TP_PROTO} eq 'UDP') {
91 $flow{NW_PROTO} = 17; # IP_TYPE_UDP
94 } elsif ($attrs{TP_PROTO} eq 'ICMP') {
95 $flow{NW_PROTO} = 1; # IP_TYPE_ICMP
96 $flow{TP_SRC} = 8; # echo request
97 $flow{TP_DST} = 0; # code
101 if ($attrs{IP_FRAGMENT} ne 'no') {
102 $flow{TP_SRC} = $flow{TP_DST} = 0;
104 } elsif ($attrs{DL_TYPE} eq 'non-ip') {
105 $flow{DL_TYPE} = 0x5678;
112 $packet .= pack_ethaddr($flow{DL_DST});
113 $packet .= pack_ethaddr($flow{DL_SRC});
114 $packet .= pack('n', 0) if $attrs{DL_HEADER} =~ /^802.2/;
115 if ($attrs{DL_HEADER} eq '802.2') {
116 $packet .= pack('CCC', 0x42, 0x42, 0x03); # LLC for 802.1D STP.
118 if ($attrs{DL_HEADER} eq '802.2+SNAP') {
119 $packet .= pack('CCC', 0xaa, 0xaa, 0x03); # LLC for SNAP.
120 $packet .= pack('CCC', 0, 0, 0); # SNAP OUI.
122 if ($attrs{DL_VLAN} ne 'none') {
123 $packet .= pack('nn', 0x8100, $flow{DL_VLAN});
125 $packet .= pack('n', $flow{DL_TYPE});
126 if ($attrs{DL_TYPE} eq 'ip') {
127 my $ip = pack('CCnnnCCnNN',
128 (4 << 4) | 5, # version, hdrlen
129 $flow{NW_TOS}, # type of service
130 0, # total length (filled in later)
134 $flow{NW_PROTO}, # protocol
138 if ($attrs{IP_OPTIONS} eq 'yes') {
139 substr($ip, 0, 1) = pack('C', (4 << 4) | 8);
140 $ip .= pack('CCnnnCCCx',
150 if ($attrs{IP_FRAGMENT} ne 'no') {
151 my (%frag_map) = ('first' => 0x2000, # more frags, ofs 0
152 'middle' => 0x2111, # more frags, ofs 0x888
153 'last' => 0x0222); # last frag, ofs 0x1110
155 = pack('n', $frag_map{$attrs{IP_FRAGMENT}});
158 if ($attrs{TP_PROTO} =~ '^TCP') {
159 my $tcp = pack('nnNNnnnn',
160 $flow{TP_SRC}, # source port
161 $flow{TP_DST}, # dest port
164 (5 << 12) | 0x02 | 0x10, # hdrlen, SYN, ACK
167 12893); # urgent pointer
168 if ($attrs{TP_PROTO} eq 'TCP+options') {
169 substr($tcp, 12, 2) = pack('n', (6 << 12) | 0x02 | 0x10);
170 $tcp .= pack('CCn', 2, 4, 1975); # MSS option
174 } elsif ($attrs{TP_PROTO} eq 'UDP') {
176 my $udp = pack('nnnn', $flow{TP_SRC}, $flow{TP_DST}, $len, 0);
177 $udp .= chr($len) while length($udp) < $len;
179 } elsif ($attrs{TP_PROTO} eq 'ICMP') {
185 931); # sequence number
186 } elsif ($attrs{TP_PROTO} eq 'other') {
187 $ip .= 'other header';
192 substr($ip, 2, 2) = pack('n', length($ip));
196 substr($packet, 12, 2) = pack('n', length($packet))
197 if $attrs{DL_HEADER} =~ /^802.2/;
199 print join(' ', map("$_=$attrs{$_}", keys(%attrs))), "\n";
200 print join(' ', map("$_=$flow{$_}", keys(%flow))), "\n";
203 print FLOWS pack('Nn',
206 print FLOWS pack_ethaddr($flow{DL_SRC});
207 print FLOWS pack_ethaddr($flow{DL_DST});
208 print FLOWS pack('nCxnCCxxNNnn',
214 inet_aton($flow{NW_SRC}),
215 inet_aton($flow{NW_DST}),
219 print PACKETS pack('NNNN',
220 0, # timestamp seconds
221 0, # timestamp microseconds
222 length($packet), # bytes saved
223 length($packet)), # total length
229 my $xx = '([0-9a-fA-F][0-9a-fA-F])';
230 my (@octets) = /$xx:$xx:$xx:$xx:$xx:$xx/;
231 @octets == 6 or die $_;
233 $out .= pack('C', hex($_)) foreach @octets;
239 my ($a, $b, $c, $d) = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
240 defined $d or die $_;
241 return ($a << 24) | ($b << 16) | ($c << 8) | $d;