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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * Cisco NetFlow protocol
33 * http://www.cisco.com/c/en/us/td/docs/net_mgmt/netflow_collection_engine/3-6/user/guide/format.html#wp1005892
36 #include <sys/cdefs.h>
38 __RCSID("$NetBSD: print-cnfp.c,v 1.5 2015/03/31 21:59:35 christos Exp $");
41 #define NETDISSECT_REWORKED
46 #include <tcpdump-stdinc.h>
51 #include "interface.h"
52 #include "addrtoname.h"
59 uint16_t version
; /* version number */
60 uint16_t count
; /* # of records */
67 struct in_addr src_ina
;
68 struct in_addr dst_ina
;
69 struct in_addr nhop_ina
;
70 uint16_t input
; /* SNMP index of input interface */
71 uint16_t output
; /* SNMP index of output interface */
72 uint32_t packets
; /* packets in the flow */
73 uint32_t octets
; /* layer 3 octets in the packets of the flow */
74 uint32_t start_time
; /* sys_uptime value at start of flow */
75 uint32_t last_time
; /* sys_uptime value when last packet of flow was received */
76 uint16_t srcport
; /* TCP/UDP source port or equivalent */
77 uint16_t dstport
; /* TCP/UDP source port or equivalent */
78 uint16_t pad1
; /* pad */
79 uint8_t proto
; /* IP protocol type */
80 uint8_t tos
; /* IP type of service */
81 uint8_t tcp_flags
; /* cumulative OR of TCP flags */
82 uint8_t pad
[3]; /* padding */
83 uint32_t reserved
; /* unused */
87 uint16_t version
; /* version number */
88 uint16_t count
; /* # of records */
92 uint32_t sequence
; /* flow sequence number */
93 uint8_t engine_type
; /* type of flow-switching engine */
94 uint8_t engine_id
; /* slot number of the flow-switching engine */
95 uint16_t sampling_interval
; /* sampling mode and interval */
99 struct in_addr src_ina
;
100 struct in_addr dst_ina
;
101 struct in_addr nhop_ina
;
102 uint16_t input
; /* SNMP index of input interface */
103 uint16_t output
; /* SNMP index of output interface */
104 uint32_t packets
; /* packets in the flow */
105 uint32_t octets
; /* layer 3 octets in the packets of the flow */
106 uint32_t start_time
; /* sys_uptime value at start of flow */
107 uint32_t last_time
; /* sys_uptime value when last packet of flow was received */
108 uint16_t srcport
; /* TCP/UDP source port or equivalent */
109 uint16_t dstport
; /* TCP/UDP source port or equivalent */
110 uint8_t pad1
; /* pad */
111 uint8_t tcp_flags
; /* cumulative OR of TCP flags */
112 uint8_t proto
; /* IP protocol type */
113 uint8_t tos
; /* IP type of service */
114 uint16_t src_as
; /* AS number of the source */
115 uint16_t dst_as
; /* AS number of the destination */
116 uint8_t src_mask
; /* source address mask bits */
117 uint8_t dst_mask
; /* destination address prefix mask bits */
119 struct in_addr peer_nexthop
; /* v6: IP address of the nexthop within the peer (FIB)*/
123 uint16_t version
; /* version number */
124 uint16_t count
; /* # of records */
125 uint32_t msys_uptime
;
128 uint32_t sequence
; /* v5 flow sequence number */
129 uint32_t reserved
; /* v5 only */
133 struct in_addr src_ina
;
134 struct in_addr dst_ina
;
135 struct in_addr nhop_ina
;
136 uint16_t input
; /* SNMP index of input interface */
137 uint16_t output
; /* SNMP index of output interface */
138 uint32_t packets
; /* packets in the flow */
139 uint32_t octets
; /* layer 3 octets in the packets of the flow */
140 uint32_t start_time
; /* sys_uptime value at start of flow */
141 uint32_t last_time
; /* sys_uptime value when last packet of flow was received */
142 uint16_t srcport
; /* TCP/UDP source port or equivalent */
143 uint16_t dstport
; /* TCP/UDP source port or equivalent */
144 uint8_t pad1
; /* pad */
145 uint8_t tcp_flags
; /* cumulative OR of TCP flags */
146 uint8_t proto
; /* IP protocol type */
147 uint8_t tos
; /* IP type of service */
148 uint16_t src_as
; /* AS number of the source */
149 uint16_t dst_as
; /* AS number of the destination */
150 uint8_t src_mask
; /* source address mask bits */
151 uint8_t dst_mask
; /* destination address prefix mask bits */
153 struct in_addr peer_nexthop
; /* v6: IP address of the nexthop within the peer (FIB)*/
157 cnfp_v1_print(netdissect_options
*ndo
, const u_char
*cp
)
159 register const struct nfhdr_v1
*nh
;
160 register const struct nfrec_v1
*nr
;
161 struct protoent
*pent
;
167 nh
= (const struct nfhdr_v1
*)cp
;
170 ver
= EXTRACT_16BITS(&nh
->version
);
171 nrecs
= EXTRACT_32BITS(&nh
->count
);
174 * This is seconds since the UN*X epoch, and is followed by
175 * nanoseconds. XXX - format it, rather than just dumping the
176 * raw seconds-since-the-Epoch.
178 t
= EXTRACT_32BITS(&nh
->utc_sec
);
181 ND_PRINT((ndo
, "NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver
,
182 EXTRACT_32BITS(&nh
->msys_uptime
)/1000,
183 EXTRACT_32BITS(&nh
->msys_uptime
)%1000,
184 EXTRACT_32BITS(&nh
->utc_sec
), EXTRACT_32BITS(&nh
->utc_nsec
)));
186 nr
= (const struct nfrec_v1
*)&nh
[1];
188 ND_PRINT((ndo
, "%2u recs", nrecs
));
190 for (; nrecs
!= 0; nr
++, nrecs
--) {
195 * Make sure we have the entire record.
198 ND_PRINT((ndo
, "\n started %u.%03u, last %u.%03u",
199 EXTRACT_32BITS(&nr
->start_time
)/1000,
200 EXTRACT_32BITS(&nr
->start_time
)%1000,
201 EXTRACT_32BITS(&nr
->last_time
)/1000,
202 EXTRACT_32BITS(&nr
->last_time
)%1000));
204 asbuf
[0] = buf
[0] = '\0';
205 ND_PRINT((ndo
, "\n %s%s%s:%u ", intoa(nr
->src_ina
.s_addr
), buf
, asbuf
,
206 EXTRACT_16BITS(&nr
->srcport
)));
208 ND_PRINT((ndo
, "> %s%s%s:%u ", intoa(nr
->dst_ina
.s_addr
), buf
, asbuf
,
209 EXTRACT_16BITS(&nr
->dstport
)));
211 ND_PRINT((ndo
, ">> %s\n ", intoa(nr
->nhop_ina
.s_addr
)));
213 pent
= getprotobynumber(nr
->proto
);
214 if (!pent
|| ndo
->ndo_nflag
)
215 ND_PRINT((ndo
, "%u ", nr
->proto
));
217 ND_PRINT((ndo
, "%s ", pent
->p_name
));
219 /* tcp flags for tcp only */
220 if (pent
&& pent
->p_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
;
251 struct protoent
*pent
;
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 pent
= getprotobynumber(nr
->proto
);
311 if (!pent
|| ndo
->ndo_nflag
)
312 ND_PRINT((ndo
, "%u ", nr
->proto
));
314 ND_PRINT((ndo
, "%s ", pent
->p_name
));
316 /* tcp flags for tcp only */
317 if (pent
&& pent
->p_proto
== IPPROTO_TCP
) {
319 flags
= nr
->tcp_flags
;
320 ND_PRINT((ndo
, "%s%s%s%s%s%s%s",
321 flags
& TH_FIN
? "F" : "",
322 flags
& TH_SYN
? "S" : "",
323 flags
& TH_RST
? "R" : "",
324 flags
& TH_PUSH
? "P" : "",
325 flags
& TH_ACK
? "A" : "",
326 flags
& TH_URG
? "U" : "",
331 ND_PRINT((ndo
, "tos %u, %u (%u octets) %s",
333 EXTRACT_32BITS(&nr
->packets
),
334 EXTRACT_32BITS(&nr
->octets
), buf
));
339 ND_PRINT((ndo
, "[|cnfp]"));
344 cnfp_v6_print(netdissect_options
*ndo
, const u_char
*cp
)
346 register const struct nfhdr_v6
*nh
;
347 register const struct nfrec_v6
*nr
;
348 struct protoent
*pent
;
354 nh
= (const struct nfhdr_v6
*)cp
;
357 ver
= EXTRACT_16BITS(&nh
->version
);
358 nrecs
= EXTRACT_32BITS(&nh
->count
);
361 * This is seconds since the UN*X epoch, and is followed by
362 * nanoseconds. XXX - format it, rather than just dumping the
363 * raw seconds-since-the-Epoch.
365 t
= EXTRACT_32BITS(&nh
->utc_sec
);
368 ND_PRINT((ndo
, "NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver
,
369 EXTRACT_32BITS(&nh
->msys_uptime
)/1000,
370 EXTRACT_32BITS(&nh
->msys_uptime
)%1000,
371 EXTRACT_32BITS(&nh
->utc_sec
), EXTRACT_32BITS(&nh
->utc_nsec
)));
373 ND_PRINT((ndo
, "#%u, ", EXTRACT_32BITS(&nh
->sequence
)));
374 nr
= (const struct nfrec_v6
*)&nh
[1];
376 ND_PRINT((ndo
, "%2u recs", nrecs
));
378 for (; nrecs
!= 0; nr
++, nrecs
--) {
383 * Make sure we have the entire record.
386 ND_PRINT((ndo
, "\n started %u.%03u, last %u.%03u",
387 EXTRACT_32BITS(&nr
->start_time
)/1000,
388 EXTRACT_32BITS(&nr
->start_time
)%1000,
389 EXTRACT_32BITS(&nr
->last_time
)/1000,
390 EXTRACT_32BITS(&nr
->last_time
)%1000));
392 asbuf
[0] = buf
[0] = '\0';
393 snprintf(buf
, sizeof(buf
), "/%u", nr
->src_mask
);
394 snprintf(asbuf
, sizeof(asbuf
), ":%u",
395 EXTRACT_16BITS(&nr
->src_as
));
396 ND_PRINT((ndo
, "\n %s%s%s:%u ", intoa(nr
->src_ina
.s_addr
), buf
, asbuf
,
397 EXTRACT_16BITS(&nr
->srcport
)));
399 snprintf(buf
, sizeof(buf
), "/%d", nr
->dst_mask
);
400 snprintf(asbuf
, sizeof(asbuf
), ":%u",
401 EXTRACT_16BITS(&nr
->dst_as
));
402 ND_PRINT((ndo
, "> %s%s%s:%u ", intoa(nr
->dst_ina
.s_addr
), buf
, asbuf
,
403 EXTRACT_16BITS(&nr
->dstport
)));
405 ND_PRINT((ndo
, ">> %s\n ", intoa(nr
->nhop_ina
.s_addr
)));
407 pent
= getprotobynumber(nr
->proto
);
408 if (!pent
|| ndo
->ndo_nflag
)
409 ND_PRINT((ndo
, "%u ", nr
->proto
));
411 ND_PRINT((ndo
, "%s ", pent
->p_name
));
413 /* tcp flags for tcp only */
414 if (pent
&& pent
->p_proto
== IPPROTO_TCP
) {
416 flags
= nr
->tcp_flags
;
417 ND_PRINT((ndo
, "%s%s%s%s%s%s%s",
418 flags
& TH_FIN
? "F" : "",
419 flags
& TH_SYN
? "S" : "",
420 flags
& TH_RST
? "R" : "",
421 flags
& TH_PUSH
? "P" : "",
422 flags
& TH_ACK
? "A" : "",
423 flags
& TH_URG
? "U" : "",
428 snprintf(buf
, sizeof(buf
), "(%u<>%u encaps)",
429 (EXTRACT_16BITS(&nr
->flags
) >> 8) & 0xff,
430 (EXTRACT_16BITS(&nr
->flags
)) & 0xff);
431 ND_PRINT((ndo
, "tos %u, %u (%u octets) %s",
433 EXTRACT_32BITS(&nr
->packets
),
434 EXTRACT_32BITS(&nr
->octets
), buf
));
439 ND_PRINT((ndo
, "[|cnfp]"));
444 cnfp_print(netdissect_options
*ndo
, const u_char
*cp
)
449 * First 2 bytes are the version number.
452 ver
= EXTRACT_16BITS(cp
);
456 cnfp_v1_print(ndo
, cp
);
460 cnfp_v5_print(ndo
, cp
);
464 cnfp_v6_print(ndo
, cp
);
468 ND_PRINT((ndo
, "NetFlow v%x", ver
));
474 ND_PRINT((ndo
, "[|cnfp]"));