]>
The Tcpdump Group git mirrors - tcpdump/blob - print-cnfp.c
1 /* $OpenBSD: print-cnfp.c,v 1.2 1998/06/25 20:26:59 mickey Exp $ */
4 * Copyright (c) 1998 Michael Shalayeff
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Michael Shalayeff.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 /* \summary: Cisco NetFlow protocol printer */
36 * Cisco NetFlow protocol
40 * http://www.cisco.com/c/en/us/td/docs/net_mgmt/netflow_collection_engine/3-6/user/guide/format.html#wp1005892
47 #include <netdissect-stdinc.h>
52 #include "netdissect.h"
53 #include "addrtoname.h"
60 uint16_t version
; /* version number */
61 uint16_t count
; /* # of records */
68 struct in_addr src_ina
;
69 struct in_addr dst_ina
;
70 struct in_addr nhop_ina
;
71 uint16_t input
; /* SNMP index of input interface */
72 uint16_t output
; /* SNMP index of output interface */
73 uint32_t packets
; /* packets in the flow */
74 uint32_t octets
; /* layer 3 octets in the packets of the flow */
75 uint32_t start_time
; /* sys_uptime value at start of flow */
76 uint32_t last_time
; /* sys_uptime value when last packet of flow was received */
77 uint16_t srcport
; /* TCP/UDP source port or equivalent */
78 uint16_t dstport
; /* TCP/UDP source port or equivalent */
79 uint16_t pad1
; /* pad */
80 uint8_t proto
; /* IP protocol type */
81 uint8_t tos
; /* IP type of service */
82 uint8_t tcp_flags
; /* cumulative OR of TCP flags */
83 uint8_t pad
[3]; /* padding */
84 uint32_t reserved
; /* unused */
88 uint16_t version
; /* version number */
89 uint16_t count
; /* # of records */
93 uint32_t sequence
; /* flow sequence number */
94 uint8_t engine_type
; /* type of flow-switching engine */
95 uint8_t engine_id
; /* slot number of the flow-switching engine */
96 uint16_t sampling_interval
; /* sampling mode and interval */
100 struct in_addr src_ina
;
101 struct in_addr dst_ina
;
102 struct in_addr nhop_ina
;
103 uint16_t input
; /* SNMP index of input interface */
104 uint16_t output
; /* SNMP index of output interface */
105 uint32_t packets
; /* packets in the flow */
106 uint32_t octets
; /* layer 3 octets in the packets of the flow */
107 uint32_t start_time
; /* sys_uptime value at start of flow */
108 uint32_t last_time
; /* sys_uptime value when last packet of flow was received */
109 uint16_t srcport
; /* TCP/UDP source port or equivalent */
110 uint16_t dstport
; /* TCP/UDP source port or equivalent */
111 uint8_t pad1
; /* pad */
112 uint8_t tcp_flags
; /* cumulative OR of TCP flags */
113 uint8_t proto
; /* IP protocol type */
114 uint8_t tos
; /* IP type of service */
115 uint16_t src_as
; /* AS number of the source */
116 uint16_t dst_as
; /* AS number of the destination */
117 uint8_t src_mask
; /* source address mask bits */
118 uint8_t dst_mask
; /* destination address prefix mask bits */
120 struct in_addr peer_nexthop
; /* v6: IP address of the nexthop within the peer (FIB)*/
124 uint16_t version
; /* version number */
125 uint16_t count
; /* # of records */
126 uint32_t msys_uptime
;
129 uint32_t sequence
; /* v5 flow sequence number */
130 uint32_t reserved
; /* v5 only */
134 struct in_addr src_ina
;
135 struct in_addr dst_ina
;
136 struct in_addr nhop_ina
;
137 uint16_t input
; /* SNMP index of input interface */
138 uint16_t output
; /* SNMP index of output interface */
139 uint32_t packets
; /* packets in the flow */
140 uint32_t octets
; /* layer 3 octets in the packets of the flow */
141 uint32_t start_time
; /* sys_uptime value at start of flow */
142 uint32_t last_time
; /* sys_uptime value when last packet of flow was received */
143 uint16_t srcport
; /* TCP/UDP source port or equivalent */
144 uint16_t dstport
; /* TCP/UDP source port or equivalent */
145 uint8_t pad1
; /* pad */
146 uint8_t tcp_flags
; /* cumulative OR of TCP flags */
147 uint8_t proto
; /* IP protocol type */
148 uint8_t tos
; /* IP type of service */
149 uint16_t src_as
; /* AS number of the source */
150 uint16_t dst_as
; /* AS number of the destination */
151 uint8_t src_mask
; /* source address mask bits */
152 uint8_t dst_mask
; /* destination address prefix mask bits */
154 struct in_addr peer_nexthop
; /* v6: IP address of the nexthop within the peer (FIB)*/
158 cnfp_v1_print(netdissect_options
*ndo
, const u_char
*cp
)
160 register const struct nfhdr_v1
*nh
;
161 register const struct nfrec_v1
*nr
;
168 nh
= (const struct nfhdr_v1
*)cp
;
171 ver
= EXTRACT_16BITS(&nh
->version
);
172 nrecs
= EXTRACT_32BITS(&nh
->count
);
175 * This is seconds since the UN*X epoch, and is followed by
176 * nanoseconds. XXX - format it, rather than just dumping the
177 * raw seconds-since-the-Epoch.
179 t
= EXTRACT_32BITS(&nh
->utc_sec
);
182 ND_PRINT((ndo
, "NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver
,
183 EXTRACT_32BITS(&nh
->msys_uptime
)/1000,
184 EXTRACT_32BITS(&nh
->msys_uptime
)%1000,
185 EXTRACT_32BITS(&nh
->utc_sec
), EXTRACT_32BITS(&nh
->utc_nsec
)));
187 nr
= (const struct nfrec_v1
*)&nh
[1];
189 ND_PRINT((ndo
, "%2u recs", nrecs
));
191 for (; nrecs
!= 0; nr
++, nrecs
--) {
196 * Make sure we have the entire record.
199 ND_PRINT((ndo
, "\n started %u.%03u, last %u.%03u",
200 EXTRACT_32BITS(&nr
->start_time
)/1000,
201 EXTRACT_32BITS(&nr
->start_time
)%1000,
202 EXTRACT_32BITS(&nr
->last_time
)/1000,
203 EXTRACT_32BITS(&nr
->last_time
)%1000));
205 asbuf
[0] = buf
[0] = '\0';
206 ND_PRINT((ndo
, "\n %s%s%s:%u ", intoa(nr
->src_ina
.s_addr
), buf
, asbuf
,
207 EXTRACT_16BITS(&nr
->srcport
)));
209 ND_PRINT((ndo
, "> %s%s%s:%u ", intoa(nr
->dst_ina
.s_addr
), buf
, asbuf
,
210 EXTRACT_16BITS(&nr
->dstport
)));
212 ND_PRINT((ndo
, ">> %s\n ", intoa(nr
->nhop_ina
.s_addr
)));
214 if (!ndo
->ndo_nflag
&& (p_name
= netdb_protoname(nr
->proto
)) != NULL
)
215 ND_PRINT((ndo
, "%s ", p_name
));
217 ND_PRINT((ndo
, "%u ", nr
->proto
));
219 /* tcp flags for tcp only */
220 if (nr
->proto
== IPPROTO_TCP
) {
222 flags
= nr
->tcp_flags
;
223 ND_PRINT((ndo
, "%s%s%s%s%s%s%s",
224 flags
& TH_FIN
? "F" : "",
225 flags
& TH_SYN
? "S" : "",
226 flags
& TH_RST
? "R" : "",
227 flags
& TH_PUSH
? "P" : "",
228 flags
& TH_ACK
? "A" : "",
229 flags
& TH_URG
? "U" : "",
234 ND_PRINT((ndo
, "tos %u, %u (%u octets) %s",
236 EXTRACT_32BITS(&nr
->packets
),
237 EXTRACT_32BITS(&nr
->octets
), buf
));
242 ND_PRINT((ndo
, "[|cnfp]"));
247 cnfp_v5_print(netdissect_options
*ndo
, const u_char
*cp
)
249 register const struct nfhdr_v5
*nh
;
250 register const struct nfrec_v5
*nr
;
257 nh
= (const struct nfhdr_v5
*)cp
;
260 ver
= EXTRACT_16BITS(&nh
->version
);
261 nrecs
= EXTRACT_32BITS(&nh
->count
);
264 * This is seconds since the UN*X epoch, and is followed by
265 * nanoseconds. XXX - format it, rather than just dumping the
266 * raw seconds-since-the-Epoch.
268 t
= EXTRACT_32BITS(&nh
->utc_sec
);
271 ND_PRINT((ndo
, "NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver
,
272 EXTRACT_32BITS(&nh
->msys_uptime
)/1000,
273 EXTRACT_32BITS(&nh
->msys_uptime
)%1000,
274 EXTRACT_32BITS(&nh
->utc_sec
), EXTRACT_32BITS(&nh
->utc_nsec
)));
276 ND_PRINT((ndo
, "#%u, ", EXTRACT_32BITS(&nh
->sequence
)));
277 nr
= (const struct nfrec_v5
*)&nh
[1];
279 ND_PRINT((ndo
, "%2u recs", nrecs
));
281 for (; nrecs
!= 0; nr
++, nrecs
--) {
286 * Make sure we have the entire record.
289 ND_PRINT((ndo
, "\n started %u.%03u, last %u.%03u",
290 EXTRACT_32BITS(&nr
->start_time
)/1000,
291 EXTRACT_32BITS(&nr
->start_time
)%1000,
292 EXTRACT_32BITS(&nr
->last_time
)/1000,
293 EXTRACT_32BITS(&nr
->last_time
)%1000));
295 asbuf
[0] = buf
[0] = '\0';
296 snprintf(buf
, sizeof(buf
), "/%u", nr
->src_mask
);
297 snprintf(asbuf
, sizeof(asbuf
), ":%u",
298 EXTRACT_16BITS(&nr
->src_as
));
299 ND_PRINT((ndo
, "\n %s%s%s:%u ", intoa(nr
->src_ina
.s_addr
), buf
, asbuf
,
300 EXTRACT_16BITS(&nr
->srcport
)));
302 snprintf(buf
, sizeof(buf
), "/%d", nr
->dst_mask
);
303 snprintf(asbuf
, sizeof(asbuf
), ":%u",
304 EXTRACT_16BITS(&nr
->dst_as
));
305 ND_PRINT((ndo
, "> %s%s%s:%u ", intoa(nr
->dst_ina
.s_addr
), buf
, asbuf
,
306 EXTRACT_16BITS(&nr
->dstport
)));
308 ND_PRINT((ndo
, ">> %s\n ", intoa(nr
->nhop_ina
.s_addr
)));
310 if (!ndo
->ndo_nflag
&& (p_name
= netdb_protoname(nr
->proto
)) != NULL
)
311 ND_PRINT((ndo
, "%s ", p_name
));
313 ND_PRINT((ndo
, "%u ", nr
->proto
));
315 /* tcp flags for tcp only */
316 if (nr
->proto
== IPPROTO_TCP
) {
318 flags
= nr
->tcp_flags
;
319 ND_PRINT((ndo
, "%s%s%s%s%s%s%s",
320 flags
& TH_FIN
? "F" : "",
321 flags
& TH_SYN
? "S" : "",
322 flags
& TH_RST
? "R" : "",
323 flags
& TH_PUSH
? "P" : "",
324 flags
& TH_ACK
? "A" : "",
325 flags
& TH_URG
? "U" : "",
330 ND_PRINT((ndo
, "tos %u, %u (%u octets) %s",
332 EXTRACT_32BITS(&nr
->packets
),
333 EXTRACT_32BITS(&nr
->octets
), buf
));
338 ND_PRINT((ndo
, "[|cnfp]"));
343 cnfp_v6_print(netdissect_options
*ndo
, const u_char
*cp
)
345 register const struct nfhdr_v6
*nh
;
346 register const struct nfrec_v6
*nr
;
353 nh
= (const struct nfhdr_v6
*)cp
;
356 ver
= EXTRACT_16BITS(&nh
->version
);
357 nrecs
= EXTRACT_32BITS(&nh
->count
);
360 * This is seconds since the UN*X epoch, and is followed by
361 * nanoseconds. XXX - format it, rather than just dumping the
362 * raw seconds-since-the-Epoch.
364 t
= EXTRACT_32BITS(&nh
->utc_sec
);
367 ND_PRINT((ndo
, "NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver
,
368 EXTRACT_32BITS(&nh
->msys_uptime
)/1000,
369 EXTRACT_32BITS(&nh
->msys_uptime
)%1000,
370 EXTRACT_32BITS(&nh
->utc_sec
), EXTRACT_32BITS(&nh
->utc_nsec
)));
372 ND_PRINT((ndo
, "#%u, ", EXTRACT_32BITS(&nh
->sequence
)));
373 nr
= (const struct nfrec_v6
*)&nh
[1];
375 ND_PRINT((ndo
, "%2u recs", nrecs
));
377 for (; nrecs
!= 0; nr
++, nrecs
--) {
382 * Make sure we have the entire record.
385 ND_PRINT((ndo
, "\n started %u.%03u, last %u.%03u",
386 EXTRACT_32BITS(&nr
->start_time
)/1000,
387 EXTRACT_32BITS(&nr
->start_time
)%1000,
388 EXTRACT_32BITS(&nr
->last_time
)/1000,
389 EXTRACT_32BITS(&nr
->last_time
)%1000));
391 asbuf
[0] = buf
[0] = '\0';
392 snprintf(buf
, sizeof(buf
), "/%u", nr
->src_mask
);
393 snprintf(asbuf
, sizeof(asbuf
), ":%u",
394 EXTRACT_16BITS(&nr
->src_as
));
395 ND_PRINT((ndo
, "\n %s%s%s:%u ", intoa(nr
->src_ina
.s_addr
), buf
, asbuf
,
396 EXTRACT_16BITS(&nr
->srcport
)));
398 snprintf(buf
, sizeof(buf
), "/%d", nr
->dst_mask
);
399 snprintf(asbuf
, sizeof(asbuf
), ":%u",
400 EXTRACT_16BITS(&nr
->dst_as
));
401 ND_PRINT((ndo
, "> %s%s%s:%u ", intoa(nr
->dst_ina
.s_addr
), buf
, asbuf
,
402 EXTRACT_16BITS(&nr
->dstport
)));
404 ND_PRINT((ndo
, ">> %s\n ", intoa(nr
->nhop_ina
.s_addr
)));
406 if (!ndo
->ndo_nflag
&& (p_name
= netdb_protoname(nr
->proto
)) != NULL
)
407 ND_PRINT((ndo
, "%s ", p_name
));
409 ND_PRINT((ndo
, "%u ", nr
->proto
));
411 /* tcp flags for tcp only */
412 if (nr
->proto
== IPPROTO_TCP
) {
414 flags
= nr
->tcp_flags
;
415 ND_PRINT((ndo
, "%s%s%s%s%s%s%s",
416 flags
& TH_FIN
? "F" : "",
417 flags
& TH_SYN
? "S" : "",
418 flags
& TH_RST
? "R" : "",
419 flags
& TH_PUSH
? "P" : "",
420 flags
& TH_ACK
? "A" : "",
421 flags
& TH_URG
? "U" : "",
426 snprintf(buf
, sizeof(buf
), "(%u<>%u encaps)",
427 (EXTRACT_16BITS(&nr
->flags
) >> 8) & 0xff,
428 (EXTRACT_16BITS(&nr
->flags
)) & 0xff);
429 ND_PRINT((ndo
, "tos %u, %u (%u octets) %s",
431 EXTRACT_32BITS(&nr
->packets
),
432 EXTRACT_32BITS(&nr
->octets
), buf
));
437 ND_PRINT((ndo
, "[|cnfp]"));
442 cnfp_print(netdissect_options
*ndo
, const u_char
*cp
)
447 * First 2 bytes are the version number.
450 ver
= EXTRACT_16BITS(cp
);
454 cnfp_v1_print(ndo
, cp
);
458 cnfp_v5_print(ndo
, cp
);
462 cnfp_v6_print(ndo
, cp
);
466 ND_PRINT((ndo
, "NetFlow v%x", ver
));
472 ND_PRINT((ndo
, "[|cnfp]"));