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
45 #include "netdissect-stdinc.h"
47 #define ND_LONGJMP_FROM_TCHECK
48 #include "netdissect.h"
49 #include "addrtoname.h"
56 nd_uint16_t version
; /* version number */
57 nd_uint16_t count
; /* # of records */
58 nd_uint32_t msys_uptime
;
67 nd_uint16_t input
; /* SNMP index of input interface */
68 nd_uint16_t output
; /* SNMP index of output interface */
69 nd_uint32_t packets
; /* packets in the flow */
70 nd_uint32_t octets
; /* layer 3 octets in the packets of the flow */
71 nd_uint32_t start_time
; /* sys_uptime value at start of flow */
72 nd_uint32_t last_time
; /* sys_uptime value when last packet of flow was received */
73 nd_uint16_t srcport
; /* TCP/UDP source port or equivalent */
74 nd_uint16_t dstport
; /* TCP/UDP source port or equivalent */
75 nd_byte pad1
[2]; /* pad */
76 nd_uint8_t proto
; /* IP protocol type */
77 nd_uint8_t tos
; /* IP type of service */
78 nd_uint8_t tcp_flags
; /* cumulative OR of TCP flags */
79 nd_byte pad
[3]; /* padding */
80 nd_uint32_t reserved
; /* unused */
84 nd_uint16_t version
; /* version number */
85 nd_uint16_t count
; /* # of records */
86 nd_uint32_t msys_uptime
;
89 nd_uint32_t sequence
; /* flow sequence number */
90 nd_uint8_t engine_type
; /* type of flow-switching engine */
91 nd_uint8_t engine_id
; /* slot number of the flow-switching engine */
92 nd_uint16_t sampling_interval
; /* sampling mode and interval */
99 nd_uint16_t input
; /* SNMP index of input interface */
100 nd_uint16_t output
; /* SNMP index of output interface */
101 nd_uint32_t packets
; /* packets in the flow */
102 nd_uint32_t octets
; /* layer 3 octets in the packets of the flow */
103 nd_uint32_t start_time
; /* sys_uptime value at start of flow */
104 nd_uint32_t last_time
; /* sys_uptime value when last packet of flow was received */
105 nd_uint16_t srcport
; /* TCP/UDP source port or equivalent */
106 nd_uint16_t dstport
; /* TCP/UDP source port or equivalent */
107 nd_byte pad1
; /* pad */
108 nd_uint8_t tcp_flags
; /* cumulative OR of TCP flags */
109 nd_uint8_t proto
; /* IP protocol type */
110 nd_uint8_t tos
; /* IP type of service */
111 nd_uint16_t src_as
; /* AS number of the source */
112 nd_uint16_t dst_as
; /* AS number of the destination */
113 nd_uint8_t src_mask
; /* source address mask bits */
114 nd_uint8_t dst_mask
; /* destination address prefix mask bits */
116 nd_ipv4 peer_nexthop
; /* v6: IP address of the nexthop within the peer (FIB)*/
120 nd_uint16_t version
; /* version number */
121 nd_uint16_t count
; /* # of records */
122 nd_uint32_t msys_uptime
;
124 nd_uint32_t utc_nsec
;
125 nd_uint32_t sequence
; /* v5 flow sequence number */
126 nd_uint32_t reserved
; /* v5 only */
133 nd_uint16_t input
; /* SNMP index of input interface */
134 nd_uint16_t output
; /* SNMP index of output interface */
135 nd_uint32_t packets
; /* packets in the flow */
136 nd_uint32_t octets
; /* layer 3 octets in the packets of the flow */
137 nd_uint32_t start_time
; /* sys_uptime value at start of flow */
138 nd_uint32_t last_time
; /* sys_uptime value when last packet of flow was received */
139 nd_uint16_t srcport
; /* TCP/UDP source port or equivalent */
140 nd_uint16_t dstport
; /* TCP/UDP source port or equivalent */
141 nd_byte pad1
; /* pad */
142 nd_uint8_t tcp_flags
; /* cumulative OR of TCP flags */
143 nd_uint8_t proto
; /* IP protocol type */
144 nd_uint8_t tos
; /* IP type of service */
145 nd_uint16_t src_as
; /* AS number of the source */
146 nd_uint16_t dst_as
; /* AS number of the destination */
147 nd_uint8_t src_mask
; /* source address mask bits */
148 nd_uint8_t dst_mask
; /* destination address prefix mask bits */
150 nd_ipv4 peer_nexthop
; /* v6: IP address of the nexthop within the peer (FIB)*/
154 cnfp_v1_print(netdissect_options
*ndo
, const u_char
*cp
)
156 const struct nfhdr_v1
*nh
;
157 const struct nfrec_v1
*nr
;
165 nh
= (const struct nfhdr_v1
*)cp
;
167 ver
= GET_BE_U_2(nh
->version
);
168 nrecs
= GET_BE_U_4(nh
->count
);
171 * This is seconds since the UN*X epoch, and is followed by
172 * nanoseconds. XXX - format it, rather than just dumping the
173 * raw seconds-since-the-Epoch.
175 t
= GET_BE_U_4(nh
->utc_sec
);
178 ND_PRINT("NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver
,
179 GET_BE_U_4(nh
->msys_uptime
)/1000,
180 GET_BE_U_4(nh
->msys_uptime
)%1000,
181 GET_BE_U_4(nh
->utc_sec
), GET_BE_U_4(nh
->utc_nsec
));
183 nr
= (const struct nfrec_v1
*)&nh
[1];
185 ND_PRINT("%2u recs", nrecs
);
187 for (; nrecs
!= 0; nr
++, nrecs
--) {
188 ND_PRINT("\n started %u.%03u, last %u.%03u",
189 GET_BE_U_4(nr
->start_time
)/1000,
190 GET_BE_U_4(nr
->start_time
)%1000,
191 GET_BE_U_4(nr
->last_time
)/1000,
192 GET_BE_U_4(nr
->last_time
)%1000);
194 ND_PRINT("\n %s:%u ",
195 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->src_ina
)),
196 GET_BE_U_2(nr
->srcport
));
199 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->dst_ina
)),
200 GET_BE_U_2(nr
->dstport
));
203 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->nhop_ina
)));
205 proto
= GET_U_1(nr
->proto
);
206 if (!ndo
->ndo_nflag
&& (p_name
= netdb_protoname(proto
)) != NULL
)
207 ND_PRINT("%s ", p_name
);
209 ND_PRINT("%u ", proto
);
211 /* tcp flags for tcp only */
212 if (proto
== IPPROTO_TCP
) {
214 flags
= GET_U_1(nr
->tcp_flags
);
216 ND_PRINT("%s ", bittok2str_nosep(tcp_flag_values
, "", flags
));
219 ND_PRINT("tos %u, %u (%u octets)",
221 GET_BE_U_4(nr
->packets
),
222 GET_BE_U_4(nr
->octets
));
223 /* This was not all of struct nfrec_v1. */
229 cnfp_v5_print(netdissect_options
*ndo
, const u_char
*cp
)
231 const struct nfhdr_v5
*nh
;
232 const struct nfrec_v5
*nr
;
240 nh
= (const struct nfhdr_v5
*)cp
;
242 ver
= GET_BE_U_2(nh
->version
);
243 nrecs
= GET_BE_U_4(nh
->count
);
246 * This is seconds since the UN*X epoch, and is followed by
247 * nanoseconds. XXX - format it, rather than just dumping the
248 * raw seconds-since-the-Epoch.
250 t
= GET_BE_U_4(nh
->utc_sec
);
253 ND_PRINT("NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver
,
254 GET_BE_U_4(nh
->msys_uptime
)/1000,
255 GET_BE_U_4(nh
->msys_uptime
)%1000,
256 GET_BE_U_4(nh
->utc_sec
), GET_BE_U_4(nh
->utc_nsec
));
258 ND_PRINT("#%u, ", GET_BE_U_4(nh
->sequence
));
259 /* This was not all of struct nfhdr_v5. */
261 nr
= (const struct nfrec_v5
*)&nh
[1];
263 ND_PRINT("%2u recs", nrecs
);
265 for (; nrecs
!= 0; nr
++, nrecs
--) {
266 ND_PRINT("\n started %u.%03u, last %u.%03u",
267 GET_BE_U_4(nr
->start_time
)/1000,
268 GET_BE_U_4(nr
->start_time
)%1000,
269 GET_BE_U_4(nr
->last_time
)/1000,
270 GET_BE_U_4(nr
->last_time
)%1000);
272 ND_PRINT("\n %s/%u:%u:%u ",
273 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->src_ina
)),
274 GET_U_1(nr
->src_mask
), GET_BE_U_2(nr
->src_as
),
275 GET_BE_U_2(nr
->srcport
));
277 ND_PRINT("> %s/%u:%u:%u ",
278 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->dst_ina
)),
279 GET_U_1(nr
->dst_mask
), GET_BE_U_2(nr
->dst_as
),
280 GET_BE_U_2(nr
->dstport
));
283 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->nhop_ina
)));
285 proto
= GET_U_1(nr
->proto
);
286 if (!ndo
->ndo_nflag
&& (p_name
= netdb_protoname(proto
)) != NULL
)
287 ND_PRINT("%s ", p_name
);
289 ND_PRINT("%u ", proto
);
291 /* tcp flags for tcp only */
292 if (proto
== IPPROTO_TCP
) {
294 flags
= GET_U_1(nr
->tcp_flags
);
296 ND_PRINT("%s ", bittok2str_nosep(tcp_flag_values
, "", flags
));
299 ND_PRINT("tos %u, %u (%u octets)",
301 GET_BE_U_4(nr
->packets
),
302 GET_BE_U_4(nr
->octets
));
303 /* This was not all of struct nfrec_v5. */
309 cnfp_v6_print(netdissect_options
*ndo
, const u_char
*cp
)
311 const struct nfhdr_v6
*nh
;
312 const struct nfrec_v6
*nr
;
320 nh
= (const struct nfhdr_v6
*)cp
;
322 ver
= GET_BE_U_2(nh
->version
);
323 nrecs
= GET_BE_U_4(nh
->count
);
326 * This is seconds since the UN*X epoch, and is followed by
327 * nanoseconds. XXX - format it, rather than just dumping the
328 * raw seconds-since-the-Epoch.
330 t
= GET_BE_U_4(nh
->utc_sec
);
333 ND_PRINT("NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver
,
334 GET_BE_U_4(nh
->msys_uptime
)/1000,
335 GET_BE_U_4(nh
->msys_uptime
)%1000,
336 GET_BE_U_4(nh
->utc_sec
), GET_BE_U_4(nh
->utc_nsec
));
338 ND_PRINT("#%u, ", GET_BE_U_4(nh
->sequence
));
339 /* This was not all of struct nfhdr_v6. */
341 nr
= (const struct nfrec_v6
*)&nh
[1];
343 ND_PRINT("%2u recs", nrecs
);
345 for (; nrecs
!= 0; nr
++, nrecs
--) {
346 ND_PRINT("\n started %u.%03u, last %u.%03u",
347 GET_BE_U_4(nr
->start_time
)/1000,
348 GET_BE_U_4(nr
->start_time
)%1000,
349 GET_BE_U_4(nr
->last_time
)/1000,
350 GET_BE_U_4(nr
->last_time
)%1000);
352 ND_PRINT("\n %s/%u:%u:%u ",
353 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->src_ina
)),
354 GET_U_1(nr
->src_mask
), GET_BE_U_2(nr
->src_as
),
355 GET_BE_U_2(nr
->srcport
));
357 ND_PRINT("> %s/%u:%u:%u ",
358 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->dst_ina
)),
359 GET_U_1(nr
->dst_mask
), GET_BE_U_2(nr
->dst_as
),
360 GET_BE_U_2(nr
->dstport
));
363 intoa(GET_IPV4_TO_NETWORK_ORDER(nr
->nhop_ina
)));
365 proto
= GET_U_1(nr
->proto
);
366 if (!ndo
->ndo_nflag
&& (p_name
= netdb_protoname(proto
)) != NULL
)
367 ND_PRINT("%s ", p_name
);
369 ND_PRINT("%u ", proto
);
371 /* tcp flags for tcp only */
372 if (proto
== IPPROTO_TCP
) {
374 flags
= GET_U_1(nr
->tcp_flags
);
376 ND_PRINT("%s ", bittok2str_nosep(tcp_flag_values
, "", flags
));
379 ND_PRINT("tos %u, %u (%u octets) (%u<>%u encaps)",
381 GET_BE_U_4(nr
->packets
),
382 GET_BE_U_4(nr
->octets
),
383 (GET_BE_U_2(nr
->flags
) >> 8) & 0xff,
384 (GET_BE_U_2(nr
->flags
)) & 0xff);
385 /* This was not all of struct nfrec_v6. */
391 cnfp_print(netdissect_options
*ndo
, const u_char
*cp
)
396 * First 2 bytes are the version number.
398 ndo
->ndo_protocol
= "cnfp";
399 ver
= GET_BE_U_2(cp
);
403 cnfp_v1_print(ndo
, cp
);
407 cnfp_v5_print(ndo
, cp
);
411 cnfp_v6_print(ndo
, cp
);
415 ND_PRINT("NetFlow v%x", ver
);