]> The Tcpdump Group git mirrors - tcpdump/blob - print-cnfp.c
Revert partially the commit 21b1273
[tcpdump] / print-cnfp.c
1 /* $OpenBSD: print-cnfp.c,v 1.2 1998/06/25 20:26:59 mickey Exp $ */
2
3 /*
4 * Copyright (c) 1998 Michael Shalayeff
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
20 *
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.
31 */
32
33 /* \summary: Cisco NetFlow protocol printer */
34
35 /*
36 * Cisco NetFlow protocol
37 *
38 * See
39 *
40 * https://www.cisco.com/c/en/us/td/docs/net_mgmt/netflow_collection_engine/3-6/user/guide/format.html#wp1005892
41 */
42
43 #ifdef HAVE_CONFIG_H
44 #include <config.h>
45 #endif
46
47 #include "netdissect-stdinc.h"
48
49 #include <stdio.h>
50 #include <string.h>
51
52 #include "netdissect.h"
53 #include "addrtoname.h"
54 #include "extract.h"
55
56 #include "tcp.h"
57 #include "ipproto.h"
58
59 struct nfhdr_v1 {
60 nd_uint16_t version; /* version number */
61 nd_uint16_t count; /* # of records */
62 nd_uint32_t msys_uptime;
63 nd_uint32_t utc_sec;
64 nd_uint32_t utc_nsec;
65 };
66
67 struct nfrec_v1 {
68 nd_ipv4 src_ina;
69 nd_ipv4 dst_ina;
70 nd_ipv4 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 */
85 };
86
87 struct nfhdr_v5 {
88 nd_uint16_t version; /* version number */
89 nd_uint16_t count; /* # of records */
90 nd_uint32_t msys_uptime;
91 nd_uint32_t utc_sec;
92 nd_uint32_t utc_nsec;
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 */
97 };
98
99 struct nfrec_v5 {
100 nd_ipv4 src_ina;
101 nd_ipv4 dst_ina;
102 nd_ipv4 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 */
119 nd_byte pad2[2];
120 nd_ipv4 peer_nexthop; /* v6: IP address of the nexthop within the peer (FIB)*/
121 };
122
123 struct nfhdr_v6 {
124 nd_uint16_t version; /* version number */
125 nd_uint16_t count; /* # of records */
126 nd_uint32_t msys_uptime;
127 nd_uint32_t utc_sec;
128 nd_uint32_t utc_nsec;
129 nd_uint32_t sequence; /* v5 flow sequence number */
130 nd_uint32_t reserved; /* v5 only */
131 };
132
133 struct nfrec_v6 {
134 nd_ipv4 src_ina;
135 nd_ipv4 dst_ina;
136 nd_ipv4 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 */
153 nd_uint16_t flags;
154 nd_ipv4 peer_nexthop; /* v6: IP address of the nexthop within the peer (FIB)*/
155 };
156
157 static void
158 cnfp_v1_print(netdissect_options *ndo, const u_char *cp)
159 {
160 const struct nfhdr_v1 *nh;
161 const struct nfrec_v1 *nr;
162 const char *p_name;
163 uint8_t proto;
164 u_int nrecs, ver;
165 #if 0
166 time_t t;
167 #endif
168
169 nh = (const struct nfhdr_v1 *)cp;
170 ND_TCHECK_SIZE(nh);
171
172 ver = GET_BE_U_2(nh->version);
173 nrecs = GET_BE_U_4(nh->count);
174 #if 0
175 /*
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.
179 */
180 t = GET_BE_U_4(nh->utc_sec);
181 #endif
182
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));
187
188 nr = (const struct nfrec_v1 *)&nh[1];
189
190 ND_PRINT("%2u recs", nrecs);
191
192 for (; nrecs != 0; nr++, nrecs--) {
193 char buf[20];
194 char asbuf[20];
195
196 /*
197 * Make sure we have the entire record.
198 */
199 ND_TCHECK_SIZE(nr);
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);
205
206 asbuf[0] = buf[0] = '\0';
207 ND_PRINT("\n %s%s%s:%u ",
208 intoa(GET_IPV4_TO_NETWORK_ORDER(nr->src_ina)),
209 buf, asbuf,
210 GET_BE_U_2(nr->srcport));
211
212 ND_PRINT("> %s%s%s:%u ",
213 intoa(GET_IPV4_TO_NETWORK_ORDER(nr->dst_ina)),
214 buf, asbuf,
215 GET_BE_U_2(nr->dstport));
216
217 ND_PRINT(">> %s\n ",
218 intoa(GET_IPV4_TO_NETWORK_ORDER(nr->nhop_ina)));
219
220 proto = GET_U_1(nr->proto);
221 if (!ndo->ndo_nflag && (p_name = netdb_protoname(proto)) != NULL)
222 ND_PRINT("%s ", p_name);
223 else
224 ND_PRINT("%u ", proto);
225
226 /* tcp flags for tcp only */
227 if (proto == IPPROTO_TCP) {
228 u_int flags;
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" : "",
237 flags ? " " : "");
238 }
239
240 buf[0]='\0';
241 ND_PRINT("tos %u, %u (%u octets) %s",
242 GET_U_1(nr->tos),
243 GET_BE_U_4(nr->packets),
244 GET_BE_U_4(nr->octets), buf);
245 }
246 return;
247
248 trunc:
249 nd_print_trunc(ndo);
250 return;
251 }
252
253 static void
254 cnfp_v5_print(netdissect_options *ndo, const u_char *cp)
255 {
256 const struct nfhdr_v5 *nh;
257 const struct nfrec_v5 *nr;
258 const char *p_name;
259 uint8_t proto;
260 u_int nrecs, ver;
261 #if 0
262 time_t t;
263 #endif
264
265 nh = (const struct nfhdr_v5 *)cp;
266 ND_TCHECK_SIZE(nh);
267
268 ver = GET_BE_U_2(nh->version);
269 nrecs = GET_BE_U_4(nh->count);
270 #if 0
271 /*
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.
275 */
276 t = GET_BE_U_4(nh->utc_sec);
277 #endif
278
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));
283
284 ND_PRINT("#%u, ", GET_BE_U_4(nh->sequence));
285 nr = (const struct nfrec_v5 *)&nh[1];
286
287 ND_PRINT("%2u recs", nrecs);
288
289 for (; nrecs != 0; nr++, nrecs--) {
290 char buf[20];
291 char asbuf[20];
292
293 /*
294 * Make sure we have the entire record.
295 */
296 ND_TCHECK_SIZE(nr);
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);
302
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)),
309 buf, asbuf,
310 GET_BE_U_2(nr->srcport));
311
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)),
317 buf, asbuf,
318 GET_BE_U_2(nr->dstport));
319
320 ND_PRINT(">> %s\n ",
321 intoa(GET_IPV4_TO_NETWORK_ORDER(nr->nhop_ina)));
322
323 proto = GET_U_1(nr->proto);
324 if (!ndo->ndo_nflag && (p_name = netdb_protoname(proto)) != NULL)
325 ND_PRINT("%s ", p_name);
326 else
327 ND_PRINT("%u ", proto);
328
329 /* tcp flags for tcp only */
330 if (proto == IPPROTO_TCP) {
331 u_int flags;
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" : "",
340 flags ? " " : "");
341 }
342
343 buf[0]='\0';
344 ND_PRINT("tos %u, %u (%u octets) %s",
345 GET_U_1(nr->tos),
346 GET_BE_U_4(nr->packets),
347 GET_BE_U_4(nr->octets), buf);
348 }
349 return;
350
351 trunc:
352 nd_print_trunc(ndo);
353 return;
354 }
355
356 static void
357 cnfp_v6_print(netdissect_options *ndo, const u_char *cp)
358 {
359 const struct nfhdr_v6 *nh;
360 const struct nfrec_v6 *nr;
361 const char *p_name;
362 uint8_t proto;
363 u_int nrecs, ver;
364 #if 0
365 time_t t;
366 #endif
367
368 nh = (const struct nfhdr_v6 *)cp;
369 ND_TCHECK_SIZE(nh);
370
371 ver = GET_BE_U_2(nh->version);
372 nrecs = GET_BE_U_4(nh->count);
373 #if 0
374 /*
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.
378 */
379 t = GET_BE_U_4(nh->utc_sec);
380 #endif
381
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));
386
387 ND_PRINT("#%u, ", GET_BE_U_4(nh->sequence));
388 nr = (const struct nfrec_v6 *)&nh[1];
389
390 ND_PRINT("%2u recs", nrecs);
391
392 for (; nrecs != 0; nr++, nrecs--) {
393 char buf[20];
394 char asbuf[20];
395
396 /*
397 * Make sure we have the entire record.
398 */
399 ND_TCHECK_SIZE(nr);
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);
405
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)),
412 buf, asbuf,
413 GET_BE_U_2(nr->srcport));
414
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)),
420 buf, asbuf,
421 GET_BE_U_2(nr->dstport));
422
423 ND_PRINT(">> %s\n ",
424 intoa(GET_IPV4_TO_NETWORK_ORDER(nr->nhop_ina)));
425
426 proto = GET_U_1(nr->proto);
427 if (!ndo->ndo_nflag && (p_name = netdb_protoname(proto)) != NULL)
428 ND_PRINT("%s ", p_name);
429 else
430 ND_PRINT("%u ", proto);
431
432 /* tcp flags for tcp only */
433 if (proto == IPPROTO_TCP) {
434 u_int flags;
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" : "",
443 flags ? " " : "");
444 }
445
446 buf[0]='\0';
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",
451 GET_U_1(nr->tos),
452 GET_BE_U_4(nr->packets),
453 GET_BE_U_4(nr->octets), buf);
454 }
455 return;
456
457 trunc:
458 nd_print_trunc(ndo);
459 return;
460 }
461
462 void
463 cnfp_print(netdissect_options *ndo, const u_char *cp)
464 {
465 int ver;
466
467 /*
468 * First 2 bytes are the version number.
469 */
470 ndo->ndo_protocol = "cnfp";
471 ver = GET_BE_U_2(cp);
472 switch (ver) {
473
474 case 1:
475 cnfp_v1_print(ndo, cp);
476 break;
477
478 case 5:
479 cnfp_v5_print(ndo, cp);
480 break;
481
482 case 6:
483 cnfp_v6_print(ndo, cp);
484 break;
485
486 default:
487 ND_PRINT("NetFlow v%x", ver);
488 break;
489 }
490 }