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 nd_uint16_t version
; /* version number */
61 nd_uint16_t count
; /* # of records */
62 nd_uint32_t msys_uptime
;
68 struct in_addr src_ina
;
69 struct in_addr dst_ina
;
70 struct in_addr nhop_ina
;
71 nd_uint16_t input
; /* SNMP index of input interface */
72 nd_uint16_t output
; /* SNMP index of output interface */
73 nd_uint32_t packets
; /* packets in the flow */
74 nd_uint32_t octets
; /* layer 3 octets in the packets of the flow */
75 nd_uint32_t start_time
; /* sys_uptime value at start of flow */
76 nd_uint32_t last_time
; /* sys_uptime value when last packet of flow was received */
77 nd_uint16_t srcport
; /* TCP/UDP source port or equivalent */
78 nd_uint16_t dstport
; /* TCP/UDP source port or equivalent */
79 nd_byte pad1
[2]; /* pad */
80 nd_uint8_t proto
; /* IP protocol type */
81 nd_uint8_t tos
; /* IP type of service */
82 nd_uint8_t tcp_flags
; /* cumulative OR of TCP flags */
83 nd_byte pad
[3]; /* padding */
84 nd_uint32_t reserved
; /* unused */
88 nd_uint16_t version
; /* version number */
89 nd_uint16_t count
; /* # of records */
90 nd_uint32_t msys_uptime
;
93 nd_uint32_t sequence
; /* flow sequence number */
94 nd_uint8_t engine_type
; /* type of flow-switching engine */
95 nd_uint8_t engine_id
; /* slot number of the flow-switching engine */
96 nd_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 nd_uint16_t input
; /* SNMP index of input interface */
104 nd_uint16_t output
; /* SNMP index of output interface */
105 nd_uint32_t packets
; /* packets in the flow */
106 nd_uint32_t octets
; /* layer 3 octets in the packets of the flow */
107 nd_uint32_t start_time
; /* sys_uptime value at start of flow */
108 nd_uint32_t last_time
; /* sys_uptime value when last packet of flow was received */
109 nd_uint16_t srcport
; /* TCP/UDP source port or equivalent */
110 nd_uint16_t dstport
; /* TCP/UDP source port or equivalent */
111 nd_byte pad1
; /* pad */
112 nd_uint8_t tcp_flags
; /* cumulative OR of TCP flags */
113 nd_uint8_t proto
; /* IP protocol type */
114 nd_uint8_t tos
; /* IP type of service */
115 nd_uint16_t src_as
; /* AS number of the source */
116 nd_uint16_t dst_as
; /* AS number of the destination */
117 nd_uint8_t src_mask
; /* source address mask bits */
118 nd_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 nd_uint16_t version
; /* version number */
125 nd_uint16_t count
; /* # of records */
126 nd_uint32_t msys_uptime
;
128 nd_uint32_t utc_nsec
;
129 nd_uint32_t sequence
; /* v5 flow sequence number */
130 nd_uint32_t reserved
; /* v5 only */
134 struct in_addr src_ina
;
135 struct in_addr dst_ina
;
136 struct in_addr nhop_ina
;
137 nd_uint16_t input
; /* SNMP index of input interface */
138 nd_uint16_t output
; /* SNMP index of output interface */
139 nd_uint32_t packets
; /* packets in the flow */
140 nd_uint32_t octets
; /* layer 3 octets in the packets of the flow */
141 nd_uint32_t start_time
; /* sys_uptime value at start of flow */
142 nd_uint32_t last_time
; /* sys_uptime value when last packet of flow was received */
143 nd_uint16_t srcport
; /* TCP/UDP source port or equivalent */
144 nd_uint16_t dstport
; /* TCP/UDP source port or equivalent */
145 nd_byte pad1
; /* pad */
146 nd_uint8_t tcp_flags
; /* cumulative OR of TCP flags */
147 nd_uint8_t proto
; /* IP protocol type */
148 nd_uint8_t tos
; /* IP type of service */
149 nd_uint16_t src_as
; /* AS number of the source */
150 nd_uint16_t dst_as
; /* AS number of the destination */
151 nd_uint8_t src_mask
; /* source address mask bits */
152 nd_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 const struct nfhdr_v1
*nh
;
161 const struct nfrec_v1
*nr
;
169 nh
= (const struct nfhdr_v1
*)cp
;
172 ver
= EXTRACT_BE_U_2(nh
->version
);
173 nrecs
= EXTRACT_BE_U_4(nh
->count
);
176 * This is seconds since the UN*X epoch, and is followed by
177 * nanoseconds. XXX - format it, rather than just dumping the
178 * raw seconds-since-the-Epoch.
180 t
= EXTRACT_BE_U_4(nh
->utc_sec
);
183 ND_PRINT("NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver
,
184 EXTRACT_BE_U_4(nh
->msys_uptime
)/1000,
185 EXTRACT_BE_U_4(nh
->msys_uptime
)%1000,
186 EXTRACT_BE_U_4(nh
->utc_sec
), EXTRACT_BE_U_4(nh
->utc_nsec
));
188 nr
= (const struct nfrec_v1
*)&nh
[1];
190 ND_PRINT("%2u recs", nrecs
);
192 for (; nrecs
!= 0; nr
++, nrecs
--) {
197 * Make sure we have the entire record.
200 ND_PRINT("\n started %u.%03u, last %u.%03u",
201 EXTRACT_BE_U_4(nr
->start_time
)/1000,
202 EXTRACT_BE_U_4(nr
->start_time
)%1000,
203 EXTRACT_BE_U_4(nr
->last_time
)/1000,
204 EXTRACT_BE_U_4(nr
->last_time
)%1000);
206 asbuf
[0] = buf
[0] = '\0';
207 ND_PRINT("\n %s%s%s:%u ", intoa(nr
->src_ina
.s_addr
), buf
, asbuf
,
208 EXTRACT_BE_U_2(nr
->srcport
));
210 ND_PRINT("> %s%s%s:%u ", intoa(nr
->dst_ina
.s_addr
), buf
, asbuf
,
211 EXTRACT_BE_U_2(nr
->dstport
));
213 ND_PRINT(">> %s\n ", intoa(nr
->nhop_ina
.s_addr
));
215 proto
= EXTRACT_U_1(nr
->proto
);
216 if (!ndo
->ndo_nflag
&& (p_name
= netdb_protoname(proto
)) != NULL
)
217 ND_PRINT("%s ", p_name
);
219 ND_PRINT("%u ", proto
);
221 /* tcp flags for tcp only */
222 if (proto
== IPPROTO_TCP
) {
224 flags
= EXTRACT_U_1(nr
->tcp_flags
);
225 ND_PRINT("%s%s%s%s%s%s%s",
226 flags
& TH_FIN
? "F" : "",
227 flags
& TH_SYN
? "S" : "",
228 flags
& TH_RST
? "R" : "",
229 flags
& TH_PUSH
? "P" : "",
230 flags
& TH_ACK
? "A" : "",
231 flags
& TH_URG
? "U" : "",
236 ND_PRINT("tos %u, %u (%u octets) %s",
237 EXTRACT_U_1(nr
->tos
),
238 EXTRACT_BE_U_4(nr
->packets
),
239 EXTRACT_BE_U_4(nr
->octets
), buf
);
249 cnfp_v5_print(netdissect_options
*ndo
, const u_char
*cp
)
251 const struct nfhdr_v5
*nh
;
252 const struct nfrec_v5
*nr
;
260 nh
= (const struct nfhdr_v5
*)cp
;
263 ver
= EXTRACT_BE_U_2(nh
->version
);
264 nrecs
= EXTRACT_BE_U_4(nh
->count
);
267 * This is seconds since the UN*X epoch, and is followed by
268 * nanoseconds. XXX - format it, rather than just dumping the
269 * raw seconds-since-the-Epoch.
271 t
= EXTRACT_BE_U_4(nh
->utc_sec
);
274 ND_PRINT("NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver
,
275 EXTRACT_BE_U_4(nh
->msys_uptime
)/1000,
276 EXTRACT_BE_U_4(nh
->msys_uptime
)%1000,
277 EXTRACT_BE_U_4(nh
->utc_sec
), EXTRACT_BE_U_4(nh
->utc_nsec
));
279 ND_PRINT("#%u, ", EXTRACT_BE_U_4(nh
->sequence
));
280 nr
= (const struct nfrec_v5
*)&nh
[1];
282 ND_PRINT("%2u recs", nrecs
);
284 for (; nrecs
!= 0; nr
++, nrecs
--) {
289 * Make sure we have the entire record.
292 ND_PRINT("\n started %u.%03u, last %u.%03u",
293 EXTRACT_BE_U_4(nr
->start_time
)/1000,
294 EXTRACT_BE_U_4(nr
->start_time
)%1000,
295 EXTRACT_BE_U_4(nr
->last_time
)/1000,
296 EXTRACT_BE_U_4(nr
->last_time
)%1000);
298 asbuf
[0] = buf
[0] = '\0';
299 snprintf(buf
, sizeof(buf
), "/%u", EXTRACT_U_1(nr
->src_mask
));
300 snprintf(asbuf
, sizeof(asbuf
), ":%u",
301 EXTRACT_BE_U_2(nr
->src_as
));
302 ND_PRINT("\n %s%s%s:%u ", intoa(nr
->src_ina
.s_addr
), buf
, asbuf
,
303 EXTRACT_BE_U_2(nr
->srcport
));
305 snprintf(buf
, sizeof(buf
), "/%u", EXTRACT_U_1(nr
->dst_mask
));
306 snprintf(asbuf
, sizeof(asbuf
), ":%u",
307 EXTRACT_BE_U_2(nr
->dst_as
));
308 ND_PRINT("> %s%s%s:%u ", intoa(nr
->dst_ina
.s_addr
), buf
, asbuf
,
309 EXTRACT_BE_U_2(nr
->dstport
));
311 ND_PRINT(">> %s\n ", intoa(nr
->nhop_ina
.s_addr
));
313 proto
= EXTRACT_U_1(nr
->proto
);
314 if (!ndo
->ndo_nflag
&& (p_name
= netdb_protoname(proto
)) != NULL
)
315 ND_PRINT("%s ", p_name
);
317 ND_PRINT("%u ", proto
);
319 /* tcp flags for tcp only */
320 if (proto
== IPPROTO_TCP
) {
322 flags
= EXTRACT_U_1(nr
->tcp_flags
);
323 ND_PRINT("%s%s%s%s%s%s%s",
324 flags
& TH_FIN
? "F" : "",
325 flags
& TH_SYN
? "S" : "",
326 flags
& TH_RST
? "R" : "",
327 flags
& TH_PUSH
? "P" : "",
328 flags
& TH_ACK
? "A" : "",
329 flags
& TH_URG
? "U" : "",
334 ND_PRINT("tos %u, %u (%u octets) %s",
335 EXTRACT_U_1(nr
->tos
),
336 EXTRACT_BE_U_4(nr
->packets
),
337 EXTRACT_BE_U_4(nr
->octets
), buf
);
347 cnfp_v6_print(netdissect_options
*ndo
, const u_char
*cp
)
349 const struct nfhdr_v6
*nh
;
350 const struct nfrec_v6
*nr
;
358 nh
= (const struct nfhdr_v6
*)cp
;
361 ver
= EXTRACT_BE_U_2(nh
->version
);
362 nrecs
= EXTRACT_BE_U_4(nh
->count
);
365 * This is seconds since the UN*X epoch, and is followed by
366 * nanoseconds. XXX - format it, rather than just dumping the
367 * raw seconds-since-the-Epoch.
369 t
= EXTRACT_BE_U_4(nh
->utc_sec
);
372 ND_PRINT("NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver
,
373 EXTRACT_BE_U_4(nh
->msys_uptime
)/1000,
374 EXTRACT_BE_U_4(nh
->msys_uptime
)%1000,
375 EXTRACT_BE_U_4(nh
->utc_sec
), EXTRACT_BE_U_4(nh
->utc_nsec
));
377 ND_PRINT("#%u, ", EXTRACT_BE_U_4(nh
->sequence
));
378 nr
= (const struct nfrec_v6
*)&nh
[1];
380 ND_PRINT("%2u recs", nrecs
);
382 for (; nrecs
!= 0; nr
++, nrecs
--) {
387 * Make sure we have the entire record.
390 ND_PRINT("\n started %u.%03u, last %u.%03u",
391 EXTRACT_BE_U_4(nr
->start_time
)/1000,
392 EXTRACT_BE_U_4(nr
->start_time
)%1000,
393 EXTRACT_BE_U_4(nr
->last_time
)/1000,
394 EXTRACT_BE_U_4(nr
->last_time
)%1000);
396 asbuf
[0] = buf
[0] = '\0';
397 snprintf(buf
, sizeof(buf
), "/%u", EXTRACT_U_1(nr
->src_mask
));
398 snprintf(asbuf
, sizeof(asbuf
), ":%u",
399 EXTRACT_BE_U_2(nr
->src_as
));
400 ND_PRINT("\n %s%s%s:%u ", intoa(nr
->src_ina
.s_addr
), buf
, asbuf
,
401 EXTRACT_BE_U_2(nr
->srcport
));
403 snprintf(buf
, sizeof(buf
), "/%u", EXTRACT_U_1(nr
->dst_mask
));
404 snprintf(asbuf
, sizeof(asbuf
), ":%u",
405 EXTRACT_BE_U_2(nr
->dst_as
));
406 ND_PRINT("> %s%s%s:%u ", intoa(nr
->dst_ina
.s_addr
), buf
, asbuf
,
407 EXTRACT_BE_U_2(nr
->dstport
));
409 ND_PRINT(">> %s\n ", intoa(nr
->nhop_ina
.s_addr
));
411 proto
= EXTRACT_U_1(nr
->proto
);
412 if (!ndo
->ndo_nflag
&& (p_name
= netdb_protoname(proto
)) != NULL
)
413 ND_PRINT("%s ", p_name
);
415 ND_PRINT("%u ", proto
);
417 /* tcp flags for tcp only */
418 if (proto
== IPPROTO_TCP
) {
420 flags
= EXTRACT_U_1(nr
->tcp_flags
);
421 ND_PRINT("%s%s%s%s%s%s%s",
422 flags
& TH_FIN
? "F" : "",
423 flags
& TH_SYN
? "S" : "",
424 flags
& TH_RST
? "R" : "",
425 flags
& TH_PUSH
? "P" : "",
426 flags
& TH_ACK
? "A" : "",
427 flags
& TH_URG
? "U" : "",
432 snprintf(buf
, sizeof(buf
), "(%u<>%u encaps)",
433 (EXTRACT_BE_U_2(nr
->flags
) >> 8) & 0xff,
434 (EXTRACT_BE_U_2(nr
->flags
)) & 0xff);
435 ND_PRINT("tos %u, %u (%u octets) %s",
436 EXTRACT_U_1(nr
->tos
),
437 EXTRACT_BE_U_4(nr
->packets
),
438 EXTRACT_BE_U_4(nr
->octets
), buf
);
448 cnfp_print(netdissect_options
*ndo
, const u_char
*cp
)
453 * First 2 bytes are the version number.
456 ver
= EXTRACT_BE_U_2(cp
);
460 cnfp_v1_print(ndo
, cp
);
464 cnfp_v5_print(ndo
, cp
);
468 cnfp_v6_print(ndo
, cp
);
472 ND_PRINT("NetFlow v%x", ver
);