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"
49 #define ND_LONGJMP_FROM_TCHECK
50 #include "netdissect.h"
51 #include "addrtoname.h"
58 nd_uint16_t version
; /* version number */
59 nd_uint16_t count
; /* # of records */
60 nd_uint32_t msys_uptime
;
69 nd_uint16_t input
; /* SNMP index of input interface */
70 nd_uint16_t output
; /* SNMP index of output interface */
71 nd_uint32_t packets
; /* packets in the flow */
72 nd_uint32_t octets
; /* layer 3 octets in the packets of the flow */
73 nd_uint32_t start_time
; /* sys_uptime value at start of flow */
74 nd_uint32_t last_time
; /* sys_uptime value when last packet of flow was received */
75 nd_uint16_t srcport
; /* TCP/UDP source port or equivalent */
76 nd_uint16_t dstport
; /* TCP/UDP source port or equivalent */
77 nd_byte pad1
[2]; /* pad */
78 nd_uint8_t proto
; /* IP protocol type */
79 nd_uint8_t tos
; /* IP type of service */
80 nd_uint8_t tcp_flags
; /* cumulative OR of TCP flags */
81 nd_byte pad
[3]; /* padding */
82 nd_uint32_t reserved
; /* unused */
86 nd_uint16_t version
; /* version number */
87 nd_uint16_t count
; /* # of records */
88 nd_uint32_t msys_uptime
;
91 nd_uint32_t sequence
; /* flow sequence number */
92 nd_uint8_t engine_type
; /* type of flow-switching engine */
93 nd_uint8_t engine_id
; /* slot number of the flow-switching engine */
94 nd_uint16_t sampling_interval
; /* sampling mode and interval */
101 nd_uint16_t input
; /* SNMP index of input interface */
102 nd_uint16_t output
; /* SNMP index of output interface */
103 nd_uint32_t packets
; /* packets in the flow */
104 nd_uint32_t octets
; /* layer 3 octets in the packets of the flow */
105 nd_uint32_t start_time
; /* sys_uptime value at start of flow */
106 nd_uint32_t last_time
; /* sys_uptime value when last packet of flow was received */
107 nd_uint16_t srcport
; /* TCP/UDP source port or equivalent */
108 nd_uint16_t dstport
; /* TCP/UDP source port or equivalent */
109 nd_byte pad1
; /* pad */
110 nd_uint8_t tcp_flags
; /* cumulative OR of TCP flags */
111 nd_uint8_t proto
; /* IP protocol type */
112 nd_uint8_t tos
; /* IP type of service */
113 nd_uint16_t src_as
; /* AS number of the source */
114 nd_uint16_t dst_as
; /* AS number of the destination */
115 nd_uint8_t src_mask
; /* source address mask bits */
116 nd_uint8_t dst_mask
; /* destination address prefix mask bits */
118 nd_ipv4 peer_nexthop
; /* v6: IP address of the nexthop within the peer (FIB)*/
122 nd_uint16_t version
; /* version number */
123 nd_uint16_t count
; /* # of records */
124 nd_uint32_t msys_uptime
;
126 nd_uint32_t utc_nsec
;
127 nd_uint32_t sequence
; /* v5 flow sequence number */
128 nd_uint32_t reserved
; /* v5 only */
135 nd_uint16_t input
; /* SNMP index of input interface */
136 nd_uint16_t output
; /* SNMP index of output interface */
137 nd_uint32_t packets
; /* packets in the flow */
138 nd_uint32_t octets
; /* layer 3 octets in the packets of the flow */
139 nd_uint32_t start_time
; /* sys_uptime value at start of flow */
140 nd_uint32_t last_time
; /* sys_uptime value when last packet of flow was received */
141 nd_uint16_t srcport
; /* TCP/UDP source port or equivalent */
142 nd_uint16_t dstport
; /* TCP/UDP source port or equivalent */
143 nd_byte pad1
; /* pad */
144 nd_uint8_t tcp_flags
; /* cumulative OR of TCP flags */
145 nd_uint8_t proto
; /* IP protocol type */
146 nd_uint8_t tos
; /* IP type of service */
147 nd_uint16_t src_as
; /* AS number of the source */
148 nd_uint16_t dst_as
; /* AS number of the destination */
149 nd_uint8_t src_mask
; /* source address mask bits */
150 nd_uint8_t dst_mask
; /* destination address prefix mask bits */
152 nd_ipv4 peer_nexthop
; /* v6: IP address of the nexthop within the peer (FIB)*/
156 cnfp_v1_print(netdissect_options
*ndo
, const u_char
*cp
)
158 const struct nfhdr_v1
*nh
;
159 const struct nfrec_v1
*nr
;
167 nh
= (const struct nfhdr_v1
*)cp
;
169 ver
= GET_BE_U_2(nh
->version
);
170 nrecs
= GET_BE_U_4(nh
->count
);
173 * This is seconds since the UN*X epoch, and is followed by
174 * nanoseconds. XXX - format it, rather than just dumping the
175 * raw seconds-since-the-Epoch.
177 t
= GET_BE_U_4(nh
->utc_sec
);
180 ND_PRINT("NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver
,
181 GET_BE_U_4(nh
->msys_uptime
)/1000,
182 GET_BE_U_4(nh
->msys_uptime
)%1000,
183 GET_BE_U_4(nh
->utc_sec
), GET_BE_U_4(nh
->utc_nsec
));
185 nr
= (const struct nfrec_v1
*)&nh
[1];
187 ND_PRINT("%2u recs", nrecs
);
189 for (; nrecs
!= 0; nr
++, nrecs
--) {
190 ND_PRINT("\n started %u.%03u, last %u.%03u",
191 GET_BE_U_4(nr
->start_time
)/1000,
192 GET_BE_U_4(nr
->start_time
)%1000,
193 GET_BE_U_4(nr
->last_time
)/1000,
194 GET_BE_U_4(nr
->last_time
)%1000);
196 ND_PRINT("\n %s:%u ",
197 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->src_ina
)),
198 GET_BE_U_2(nr
->srcport
));
201 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->dst_ina
)),
202 GET_BE_U_2(nr
->dstport
));
205 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->nhop_ina
)));
207 proto
= GET_U_1(nr
->proto
);
208 if (!ndo
->ndo_nflag
&& (p_name
= netdb_protoname(proto
)) != NULL
)
209 ND_PRINT("%s ", p_name
);
211 ND_PRINT("%u ", proto
);
213 /* tcp flags for tcp only */
214 if (proto
== IPPROTO_TCP
) {
216 flags
= GET_U_1(nr
->tcp_flags
);
218 ND_PRINT("%s ", bittok2str_nosep(tcp_flag_values
, "", flags
));
221 ND_PRINT("tos %u, %u (%u octets)",
223 GET_BE_U_4(nr
->packets
),
224 GET_BE_U_4(nr
->octets
));
225 /* This was not all of struct nfrec_v1. */
231 cnfp_v5_print(netdissect_options
*ndo
, const u_char
*cp
)
233 const struct nfhdr_v5
*nh
;
234 const struct nfrec_v5
*nr
;
242 nh
= (const struct nfhdr_v5
*)cp
;
244 ver
= GET_BE_U_2(nh
->version
);
245 nrecs
= GET_BE_U_4(nh
->count
);
248 * This is seconds since the UN*X epoch, and is followed by
249 * nanoseconds. XXX - format it, rather than just dumping the
250 * raw seconds-since-the-Epoch.
252 t
= GET_BE_U_4(nh
->utc_sec
);
255 ND_PRINT("NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver
,
256 GET_BE_U_4(nh
->msys_uptime
)/1000,
257 GET_BE_U_4(nh
->msys_uptime
)%1000,
258 GET_BE_U_4(nh
->utc_sec
), GET_BE_U_4(nh
->utc_nsec
));
260 ND_PRINT("#%u, ", GET_BE_U_4(nh
->sequence
));
261 /* This was not all of struct nfhdr_v5. */
263 nr
= (const struct nfrec_v5
*)&nh
[1];
265 ND_PRINT("%2u recs", nrecs
);
267 for (; nrecs
!= 0; nr
++, nrecs
--) {
268 ND_PRINT("\n started %u.%03u, last %u.%03u",
269 GET_BE_U_4(nr
->start_time
)/1000,
270 GET_BE_U_4(nr
->start_time
)%1000,
271 GET_BE_U_4(nr
->last_time
)/1000,
272 GET_BE_U_4(nr
->last_time
)%1000);
274 ND_PRINT("\n %s/%u:%u:%u ",
275 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->src_ina
)),
276 GET_U_1(nr
->src_mask
), GET_BE_U_2(nr
->src_as
),
277 GET_BE_U_2(nr
->srcport
));
279 ND_PRINT("> %s/%u:%u:%u ",
280 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->dst_ina
)),
281 GET_U_1(nr
->dst_mask
), GET_BE_U_2(nr
->dst_as
),
282 GET_BE_U_2(nr
->dstport
));
285 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->nhop_ina
)));
287 proto
= GET_U_1(nr
->proto
);
288 if (!ndo
->ndo_nflag
&& (p_name
= netdb_protoname(proto
)) != NULL
)
289 ND_PRINT("%s ", p_name
);
291 ND_PRINT("%u ", proto
);
293 /* tcp flags for tcp only */
294 if (proto
== IPPROTO_TCP
) {
296 flags
= GET_U_1(nr
->tcp_flags
);
298 ND_PRINT("%s ", bittok2str_nosep(tcp_flag_values
, "", flags
));
301 ND_PRINT("tos %u, %u (%u octets)",
303 GET_BE_U_4(nr
->packets
),
304 GET_BE_U_4(nr
->octets
));
305 /* This was not all of struct nfrec_v5. */
311 cnfp_v6_print(netdissect_options
*ndo
, const u_char
*cp
)
313 const struct nfhdr_v6
*nh
;
314 const struct nfrec_v6
*nr
;
322 nh
= (const struct nfhdr_v6
*)cp
;
324 ver
= GET_BE_U_2(nh
->version
);
325 nrecs
= GET_BE_U_4(nh
->count
);
328 * This is seconds since the UN*X epoch, and is followed by
329 * nanoseconds. XXX - format it, rather than just dumping the
330 * raw seconds-since-the-Epoch.
332 t
= GET_BE_U_4(nh
->utc_sec
);
335 ND_PRINT("NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver
,
336 GET_BE_U_4(nh
->msys_uptime
)/1000,
337 GET_BE_U_4(nh
->msys_uptime
)%1000,
338 GET_BE_U_4(nh
->utc_sec
), GET_BE_U_4(nh
->utc_nsec
));
340 ND_PRINT("#%u, ", GET_BE_U_4(nh
->sequence
));
341 /* This was not all of struct nfhdr_v6. */
343 nr
= (const struct nfrec_v6
*)&nh
[1];
345 ND_PRINT("%2u recs", nrecs
);
347 for (; nrecs
!= 0; nr
++, nrecs
--) {
348 ND_PRINT("\n started %u.%03u, last %u.%03u",
349 GET_BE_U_4(nr
->start_time
)/1000,
350 GET_BE_U_4(nr
->start_time
)%1000,
351 GET_BE_U_4(nr
->last_time
)/1000,
352 GET_BE_U_4(nr
->last_time
)%1000);
354 ND_PRINT("\n %s/%u:%u:%u ",
355 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->src_ina
)),
356 GET_U_1(nr
->src_mask
), GET_BE_U_2(nr
->src_as
),
357 GET_BE_U_2(nr
->srcport
));
359 ND_PRINT("> %s/%u:%u:%u ",
360 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->dst_ina
)),
361 GET_U_1(nr
->dst_mask
), GET_BE_U_2(nr
->dst_as
),
362 GET_BE_U_2(nr
->dstport
));
365 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->nhop_ina
)));
367 proto
= GET_U_1(nr
->proto
);
368 if (!ndo
->ndo_nflag
&& (p_name
= netdb_protoname(proto
)) != NULL
)
369 ND_PRINT("%s ", p_name
);
371 ND_PRINT("%u ", proto
);
373 /* tcp flags for tcp only */
374 if (proto
== IPPROTO_TCP
) {
376 flags
= GET_U_1(nr
->tcp_flags
);
378 ND_PRINT("%s ", bittok2str_nosep(tcp_flag_values
, "", flags
));
381 ND_PRINT("tos %u, %u (%u octets) (%u<>%u encaps)",
383 GET_BE_U_4(nr
->packets
),
384 GET_BE_U_4(nr
->octets
),
385 (GET_BE_U_2(nr
->flags
) >> 8) & 0xff,
386 (GET_BE_U_2(nr
->flags
)) & 0xff);
387 /* This was not all of struct nfrec_v6. */
393 cnfp_print(netdissect_options
*ndo
, const u_char
*cp
)
398 * First 2 bytes are the version number.
400 ndo
->ndo_protocol
= "cnfp";
401 ver
= GET_BE_U_2(cp
);
405 cnfp_v1_print(ndo
, cp
);
409 cnfp_v5_print(ndo
, cp
);
413 cnfp_v6_print(ndo
, cp
);
417 ND_PRINT("NetFlow v%x", ver
);