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 * https://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
;
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 */
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 nd_ipv4 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 */
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 nd_ipv4 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
= GET_BE_U_2(nh
->version
);
173 nrecs
= GET_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
= GET_BE_U_4(nh
->utc_sec
);
183 ND_PRINT("NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver
,
184 GET_BE_U_4(nh
->msys_uptime
)/1000,
185 GET_BE_U_4(nh
->msys_uptime
)%1000,
186 GET_BE_U_4(nh
->utc_sec
), GET_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 GET_BE_U_4(nr
->start_time
)/1000,
202 GET_BE_U_4(nr
->start_time
)%1000,
203 GET_BE_U_4(nr
->last_time
)/1000,
204 GET_BE_U_4(nr
->last_time
)%1000);
206 asbuf
[0] = buf
[0] = '\0';
207 ND_PRINT("\n %s%s%s:%u ",
208 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->src_ina
)),
210 GET_BE_U_2(nr
->srcport
));
212 ND_PRINT("> %s%s%s:%u ",
213 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->dst_ina
)),
215 GET_BE_U_2(nr
->dstport
));
218 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->nhop_ina
)));
220 proto
= GET_U_1(nr
->proto
);
221 if (!ndo
->ndo_nflag
&& (p_name
= netdb_protoname(proto
)) != NULL
)
222 ND_PRINT("%s ", p_name
);
224 ND_PRINT("%u ", proto
);
226 /* tcp flags for tcp only */
227 if (proto
== IPPROTO_TCP
) {
229 flags
= GET_U_1(nr
->tcp_flags
);
230 ND_PRINT("%s%s%s%s%s%s%s",
231 flags
& TH_FIN
? "F" : "",
232 flags
& TH_SYN
? "S" : "",
233 flags
& TH_RST
? "R" : "",
234 flags
& TH_PUSH
? "P" : "",
235 flags
& TH_ACK
? "A" : "",
236 flags
& TH_URG
? "U" : "",
241 ND_PRINT("tos %u, %u (%u octets) %s",
243 GET_BE_U_4(nr
->packets
),
244 GET_BE_U_4(nr
->octets
), buf
);
254 cnfp_v5_print(netdissect_options
*ndo
, const u_char
*cp
)
256 const struct nfhdr_v5
*nh
;
257 const struct nfrec_v5
*nr
;
265 nh
= (const struct nfhdr_v5
*)cp
;
268 ver
= GET_BE_U_2(nh
->version
);
269 nrecs
= GET_BE_U_4(nh
->count
);
272 * This is seconds since the UN*X epoch, and is followed by
273 * nanoseconds. XXX - format it, rather than just dumping the
274 * raw seconds-since-the-Epoch.
276 t
= GET_BE_U_4(nh
->utc_sec
);
279 ND_PRINT("NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver
,
280 GET_BE_U_4(nh
->msys_uptime
)/1000,
281 GET_BE_U_4(nh
->msys_uptime
)%1000,
282 GET_BE_U_4(nh
->utc_sec
), GET_BE_U_4(nh
->utc_nsec
));
284 ND_PRINT("#%u, ", GET_BE_U_4(nh
->sequence
));
285 nr
= (const struct nfrec_v5
*)&nh
[1];
287 ND_PRINT("%2u recs", nrecs
);
289 for (; nrecs
!= 0; nr
++, nrecs
--) {
294 * Make sure we have the entire record.
297 ND_PRINT("\n started %u.%03u, last %u.%03u",
298 GET_BE_U_4(nr
->start_time
)/1000,
299 GET_BE_U_4(nr
->start_time
)%1000,
300 GET_BE_U_4(nr
->last_time
)/1000,
301 GET_BE_U_4(nr
->last_time
)%1000);
303 asbuf
[0] = buf
[0] = '\0';
304 snprintf(buf
, sizeof(buf
), "/%u", GET_U_1(nr
->src_mask
));
305 snprintf(asbuf
, sizeof(asbuf
), ":%u",
306 GET_BE_U_2(nr
->src_as
));
307 ND_PRINT("\n %s%s%s:%u ",
308 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->src_ina
)),
310 GET_BE_U_2(nr
->srcport
));
312 snprintf(buf
, sizeof(buf
), "/%u", GET_U_1(nr
->dst_mask
));
313 snprintf(asbuf
, sizeof(asbuf
), ":%u",
314 GET_BE_U_2(nr
->dst_as
));
315 ND_PRINT("> %s%s%s:%u ",
316 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->dst_ina
)),
318 GET_BE_U_2(nr
->dstport
));
321 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->nhop_ina
)));
323 proto
= GET_U_1(nr
->proto
);
324 if (!ndo
->ndo_nflag
&& (p_name
= netdb_protoname(proto
)) != NULL
)
325 ND_PRINT("%s ", p_name
);
327 ND_PRINT("%u ", proto
);
329 /* tcp flags for tcp only */
330 if (proto
== IPPROTO_TCP
) {
332 flags
= GET_U_1(nr
->tcp_flags
);
333 ND_PRINT("%s%s%s%s%s%s%s",
334 flags
& TH_FIN
? "F" : "",
335 flags
& TH_SYN
? "S" : "",
336 flags
& TH_RST
? "R" : "",
337 flags
& TH_PUSH
? "P" : "",
338 flags
& TH_ACK
? "A" : "",
339 flags
& TH_URG
? "U" : "",
344 ND_PRINT("tos %u, %u (%u octets) %s",
346 GET_BE_U_4(nr
->packets
),
347 GET_BE_U_4(nr
->octets
), buf
);
357 cnfp_v6_print(netdissect_options
*ndo
, const u_char
*cp
)
359 const struct nfhdr_v6
*nh
;
360 const struct nfrec_v6
*nr
;
368 nh
= (const struct nfhdr_v6
*)cp
;
371 ver
= GET_BE_U_2(nh
->version
);
372 nrecs
= GET_BE_U_4(nh
->count
);
375 * This is seconds since the UN*X epoch, and is followed by
376 * nanoseconds. XXX - format it, rather than just dumping the
377 * raw seconds-since-the-Epoch.
379 t
= GET_BE_U_4(nh
->utc_sec
);
382 ND_PRINT("NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver
,
383 GET_BE_U_4(nh
->msys_uptime
)/1000,
384 GET_BE_U_4(nh
->msys_uptime
)%1000,
385 GET_BE_U_4(nh
->utc_sec
), GET_BE_U_4(nh
->utc_nsec
));
387 ND_PRINT("#%u, ", GET_BE_U_4(nh
->sequence
));
388 nr
= (const struct nfrec_v6
*)&nh
[1];
390 ND_PRINT("%2u recs", nrecs
);
392 for (; nrecs
!= 0; nr
++, nrecs
--) {
397 * Make sure we have the entire record.
400 ND_PRINT("\n started %u.%03u, last %u.%03u",
401 GET_BE_U_4(nr
->start_time
)/1000,
402 GET_BE_U_4(nr
->start_time
)%1000,
403 GET_BE_U_4(nr
->last_time
)/1000,
404 GET_BE_U_4(nr
->last_time
)%1000);
406 asbuf
[0] = buf
[0] = '\0';
407 snprintf(buf
, sizeof(buf
), "/%u", GET_U_1(nr
->src_mask
));
408 snprintf(asbuf
, sizeof(asbuf
), ":%u",
409 GET_BE_U_2(nr
->src_as
));
410 ND_PRINT("\n %s%s%s:%u ",
411 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->src_ina
)),
413 GET_BE_U_2(nr
->srcport
));
415 snprintf(buf
, sizeof(buf
), "/%u", GET_U_1(nr
->dst_mask
));
416 snprintf(asbuf
, sizeof(asbuf
), ":%u",
417 GET_BE_U_2(nr
->dst_as
));
418 ND_PRINT("> %s%s%s:%u ",
419 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->dst_ina
)),
421 GET_BE_U_2(nr
->dstport
));
424 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->nhop_ina
)));
426 proto
= GET_U_1(nr
->proto
);
427 if (!ndo
->ndo_nflag
&& (p_name
= netdb_protoname(proto
)) != NULL
)
428 ND_PRINT("%s ", p_name
);
430 ND_PRINT("%u ", proto
);
432 /* tcp flags for tcp only */
433 if (proto
== IPPROTO_TCP
) {
435 flags
= GET_U_1(nr
->tcp_flags
);
436 ND_PRINT("%s%s%s%s%s%s%s",
437 flags
& TH_FIN
? "F" : "",
438 flags
& TH_SYN
? "S" : "",
439 flags
& TH_RST
? "R" : "",
440 flags
& TH_PUSH
? "P" : "",
441 flags
& TH_ACK
? "A" : "",
442 flags
& TH_URG
? "U" : "",
447 snprintf(buf
, sizeof(buf
), "(%u<>%u encaps)",
448 (GET_BE_U_2(nr
->flags
) >> 8) & 0xff,
449 (GET_BE_U_2(nr
->flags
)) & 0xff);
450 ND_PRINT("tos %u, %u (%u octets) %s",
452 GET_BE_U_4(nr
->packets
),
453 GET_BE_U_4(nr
->octets
), buf
);
463 cnfp_print(netdissect_options
*ndo
, const u_char
*cp
)
468 * First 2 bytes are the version number.
470 ndo
->ndo_protocol
= "cnfp";
471 ver
= GET_BE_U_2(cp
);
475 cnfp_v1_print(ndo
, cp
);
479 cnfp_v5_print(ndo
, cp
);
483 cnfp_v6_print(ndo
, cp
);
487 ND_PRINT("NetFlow v%x", ver
);