Remove building with NOCRYPTO option
[minix.git] / external / bsd / tcpdump / dist / print-cnfp.c
blobf44886d5c657df23511ef190fb6dada83aaa15fa
1 /* $OpenBSD: print-cnfp.c,v 1.2 1998/06/25 20:26:59 mickey Exp $ */
3 /*
4 * Copyright (c) 1998 Michael Shalayeff
5 * All rights reserved.
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * Cisco NetFlow protocol
31 * See
33 * http://www.cisco.com/c/en/us/td/docs/net_mgmt/netflow_collection_engine/3-6/user/guide/format.html#wp1005892
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __RCSID("$NetBSD: print-cnfp.c,v 1.5 2015/03/31 21:59:35 christos Exp $");
39 #endif
41 #define NETDISSECT_REWORKED
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
46 #include <tcpdump-stdinc.h>
48 #include <stdio.h>
49 #include <string.h>
51 #include "interface.h"
52 #include "addrtoname.h"
53 #include "extract.h"
55 #include "tcp.h"
56 #include "ipproto.h"
58 struct nfhdr_v1 {
59 uint16_t version; /* version number */
60 uint16_t count; /* # of records */
61 uint32_t msys_uptime;
62 uint32_t utc_sec;
63 uint32_t utc_nsec;
66 struct nfrec_v1 {
67 struct in_addr src_ina;
68 struct in_addr dst_ina;
69 struct in_addr nhop_ina;
70 uint16_t input; /* SNMP index of input interface */
71 uint16_t output; /* SNMP index of output interface */
72 uint32_t packets; /* packets in the flow */
73 uint32_t octets; /* layer 3 octets in the packets of the flow */
74 uint32_t start_time; /* sys_uptime value at start of flow */
75 uint32_t last_time; /* sys_uptime value when last packet of flow was received */
76 uint16_t srcport; /* TCP/UDP source port or equivalent */
77 uint16_t dstport; /* TCP/UDP source port or equivalent */
78 uint16_t pad1; /* pad */
79 uint8_t proto; /* IP protocol type */
80 uint8_t tos; /* IP type of service */
81 uint8_t tcp_flags; /* cumulative OR of TCP flags */
82 uint8_t pad[3]; /* padding */
83 uint32_t reserved; /* unused */
86 struct nfhdr_v5 {
87 uint16_t version; /* version number */
88 uint16_t count; /* # of records */
89 uint32_t msys_uptime;
90 uint32_t utc_sec;
91 uint32_t utc_nsec;
92 uint32_t sequence; /* flow sequence number */
93 uint8_t engine_type; /* type of flow-switching engine */
94 uint8_t engine_id; /* slot number of the flow-switching engine */
95 uint16_t sampling_interval; /* sampling mode and interval */
98 struct nfrec_v5 {
99 struct in_addr src_ina;
100 struct in_addr dst_ina;
101 struct in_addr nhop_ina;
102 uint16_t input; /* SNMP index of input interface */
103 uint16_t output; /* SNMP index of output interface */
104 uint32_t packets; /* packets in the flow */
105 uint32_t octets; /* layer 3 octets in the packets of the flow */
106 uint32_t start_time; /* sys_uptime value at start of flow */
107 uint32_t last_time; /* sys_uptime value when last packet of flow was received */
108 uint16_t srcport; /* TCP/UDP source port or equivalent */
109 uint16_t dstport; /* TCP/UDP source port or equivalent */
110 uint8_t pad1; /* pad */
111 uint8_t tcp_flags; /* cumulative OR of TCP flags */
112 uint8_t proto; /* IP protocol type */
113 uint8_t tos; /* IP type of service */
114 uint16_t src_as; /* AS number of the source */
115 uint16_t dst_as; /* AS number of the destination */
116 uint8_t src_mask; /* source address mask bits */
117 uint8_t dst_mask; /* destination address prefix mask bits */
118 uint16_t pad2;
119 struct in_addr peer_nexthop; /* v6: IP address of the nexthop within the peer (FIB)*/
122 struct nfhdr_v6 {
123 uint16_t version; /* version number */
124 uint16_t count; /* # of records */
125 uint32_t msys_uptime;
126 uint32_t utc_sec;
127 uint32_t utc_nsec;
128 uint32_t sequence; /* v5 flow sequence number */
129 uint32_t reserved; /* v5 only */
132 struct nfrec_v6 {
133 struct in_addr src_ina;
134 struct in_addr dst_ina;
135 struct in_addr nhop_ina;
136 uint16_t input; /* SNMP index of input interface */
137 uint16_t output; /* SNMP index of output interface */
138 uint32_t packets; /* packets in the flow */
139 uint32_t octets; /* layer 3 octets in the packets of the flow */
140 uint32_t start_time; /* sys_uptime value at start of flow */
141 uint32_t last_time; /* sys_uptime value when last packet of flow was received */
142 uint16_t srcport; /* TCP/UDP source port or equivalent */
143 uint16_t dstport; /* TCP/UDP source port or equivalent */
144 uint8_t pad1; /* pad */
145 uint8_t tcp_flags; /* cumulative OR of TCP flags */
146 uint8_t proto; /* IP protocol type */
147 uint8_t tos; /* IP type of service */
148 uint16_t src_as; /* AS number of the source */
149 uint16_t dst_as; /* AS number of the destination */
150 uint8_t src_mask; /* source address mask bits */
151 uint8_t dst_mask; /* destination address prefix mask bits */
152 uint16_t flags;
153 struct in_addr peer_nexthop; /* v6: IP address of the nexthop within the peer (FIB)*/
156 static void
157 cnfp_v1_print(netdissect_options *ndo, const u_char *cp)
159 register const struct nfhdr_v1 *nh;
160 register const struct nfrec_v1 *nr;
161 struct protoent *pent;
162 int nrecs, ver;
163 #if 0
164 time_t t;
165 #endif
167 nh = (const struct nfhdr_v1 *)cp;
168 ND_TCHECK(*nh);
170 ver = EXTRACT_16BITS(&nh->version);
171 nrecs = EXTRACT_32BITS(&nh->count);
172 #if 0
174 * This is seconds since the UN*X epoch, and is followed by
175 * nanoseconds. XXX - format it, rather than just dumping the
176 * raw seconds-since-the-Epoch.
178 t = EXTRACT_32BITS(&nh->utc_sec);
179 #endif
181 ND_PRINT((ndo, "NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver,
182 EXTRACT_32BITS(&nh->msys_uptime)/1000,
183 EXTRACT_32BITS(&nh->msys_uptime)%1000,
184 EXTRACT_32BITS(&nh->utc_sec), EXTRACT_32BITS(&nh->utc_nsec)));
186 nr = (const struct nfrec_v1 *)&nh[1];
188 ND_PRINT((ndo, "%2u recs", nrecs));
190 for (; nrecs != 0; nr++, nrecs--) {
191 char buf[20];
192 char asbuf[20];
195 * Make sure we have the entire record.
197 ND_TCHECK(*nr);
198 ND_PRINT((ndo, "\n started %u.%03u, last %u.%03u",
199 EXTRACT_32BITS(&nr->start_time)/1000,
200 EXTRACT_32BITS(&nr->start_time)%1000,
201 EXTRACT_32BITS(&nr->last_time)/1000,
202 EXTRACT_32BITS(&nr->last_time)%1000));
204 asbuf[0] = buf[0] = '\0';
205 ND_PRINT((ndo, "\n %s%s%s:%u ", intoa(nr->src_ina.s_addr), buf, asbuf,
206 EXTRACT_16BITS(&nr->srcport)));
208 ND_PRINT((ndo, "> %s%s%s:%u ", intoa(nr->dst_ina.s_addr), buf, asbuf,
209 EXTRACT_16BITS(&nr->dstport)));
211 ND_PRINT((ndo, ">> %s\n ", intoa(nr->nhop_ina.s_addr)));
213 pent = getprotobynumber(nr->proto);
214 if (!pent || ndo->ndo_nflag)
215 ND_PRINT((ndo, "%u ", nr->proto));
216 else
217 ND_PRINT((ndo, "%s ", pent->p_name));
219 /* tcp flags for tcp only */
220 if (pent && pent->p_proto == IPPROTO_TCP) {
221 int flags;
222 flags = nr->tcp_flags;
223 ND_PRINT((ndo, "%s%s%s%s%s%s%s",
224 flags & TH_FIN ? "F" : "",
225 flags & TH_SYN ? "S" : "",
226 flags & TH_RST ? "R" : "",
227 flags & TH_PUSH ? "P" : "",
228 flags & TH_ACK ? "A" : "",
229 flags & TH_URG ? "U" : "",
230 flags ? " " : ""));
233 buf[0]='\0';
234 ND_PRINT((ndo, "tos %u, %u (%u octets) %s",
235 nr->tos,
236 EXTRACT_32BITS(&nr->packets),
237 EXTRACT_32BITS(&nr->octets), buf));
239 return;
241 trunc:
242 ND_PRINT((ndo, "[|cnfp]"));
243 return;
246 static void
247 cnfp_v5_print(netdissect_options *ndo, const u_char *cp)
249 register const struct nfhdr_v5 *nh;
250 register const struct nfrec_v5 *nr;
251 struct protoent *pent;
252 int nrecs, ver;
253 #if 0
254 time_t t;
255 #endif
257 nh = (const struct nfhdr_v5 *)cp;
258 ND_TCHECK(*nh);
260 ver = EXTRACT_16BITS(&nh->version);
261 nrecs = EXTRACT_32BITS(&nh->count);
262 #if 0
264 * This is seconds since the UN*X epoch, and is followed by
265 * nanoseconds. XXX - format it, rather than just dumping the
266 * raw seconds-since-the-Epoch.
268 t = EXTRACT_32BITS(&nh->utc_sec);
269 #endif
271 ND_PRINT((ndo, "NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver,
272 EXTRACT_32BITS(&nh->msys_uptime)/1000,
273 EXTRACT_32BITS(&nh->msys_uptime)%1000,
274 EXTRACT_32BITS(&nh->utc_sec), EXTRACT_32BITS(&nh->utc_nsec)));
276 ND_PRINT((ndo, "#%u, ", EXTRACT_32BITS(&nh->sequence)));
277 nr = (const struct nfrec_v5 *)&nh[1];
279 ND_PRINT((ndo, "%2u recs", nrecs));
281 for (; nrecs != 0; nr++, nrecs--) {
282 char buf[20];
283 char asbuf[20];
286 * Make sure we have the entire record.
288 ND_TCHECK(*nr);
289 ND_PRINT((ndo, "\n started %u.%03u, last %u.%03u",
290 EXTRACT_32BITS(&nr->start_time)/1000,
291 EXTRACT_32BITS(&nr->start_time)%1000,
292 EXTRACT_32BITS(&nr->last_time)/1000,
293 EXTRACT_32BITS(&nr->last_time)%1000));
295 asbuf[0] = buf[0] = '\0';
296 snprintf(buf, sizeof(buf), "/%u", nr->src_mask);
297 snprintf(asbuf, sizeof(asbuf), ":%u",
298 EXTRACT_16BITS(&nr->src_as));
299 ND_PRINT((ndo, "\n %s%s%s:%u ", intoa(nr->src_ina.s_addr), buf, asbuf,
300 EXTRACT_16BITS(&nr->srcport)));
302 snprintf(buf, sizeof(buf), "/%d", nr->dst_mask);
303 snprintf(asbuf, sizeof(asbuf), ":%u",
304 EXTRACT_16BITS(&nr->dst_as));
305 ND_PRINT((ndo, "> %s%s%s:%u ", intoa(nr->dst_ina.s_addr), buf, asbuf,
306 EXTRACT_16BITS(&nr->dstport)));
308 ND_PRINT((ndo, ">> %s\n ", intoa(nr->nhop_ina.s_addr)));
310 pent = getprotobynumber(nr->proto);
311 if (!pent || ndo->ndo_nflag)
312 ND_PRINT((ndo, "%u ", nr->proto));
313 else
314 ND_PRINT((ndo, "%s ", pent->p_name));
316 /* tcp flags for tcp only */
317 if (pent && pent->p_proto == IPPROTO_TCP) {
318 int flags;
319 flags = nr->tcp_flags;
320 ND_PRINT((ndo, "%s%s%s%s%s%s%s",
321 flags & TH_FIN ? "F" : "",
322 flags & TH_SYN ? "S" : "",
323 flags & TH_RST ? "R" : "",
324 flags & TH_PUSH ? "P" : "",
325 flags & TH_ACK ? "A" : "",
326 flags & TH_URG ? "U" : "",
327 flags ? " " : ""));
330 buf[0]='\0';
331 ND_PRINT((ndo, "tos %u, %u (%u octets) %s",
332 nr->tos,
333 EXTRACT_32BITS(&nr->packets),
334 EXTRACT_32BITS(&nr->octets), buf));
336 return;
338 trunc:
339 ND_PRINT((ndo, "[|cnfp]"));
340 return;
343 static void
344 cnfp_v6_print(netdissect_options *ndo, const u_char *cp)
346 register const struct nfhdr_v6 *nh;
347 register const struct nfrec_v6 *nr;
348 struct protoent *pent;
349 int nrecs, ver;
350 #if 0
351 time_t t;
352 #endif
354 nh = (const struct nfhdr_v6 *)cp;
355 ND_TCHECK(*nh);
357 ver = EXTRACT_16BITS(&nh->version);
358 nrecs = EXTRACT_32BITS(&nh->count);
359 #if 0
361 * This is seconds since the UN*X epoch, and is followed by
362 * nanoseconds. XXX - format it, rather than just dumping the
363 * raw seconds-since-the-Epoch.
365 t = EXTRACT_32BITS(&nh->utc_sec);
366 #endif
368 ND_PRINT((ndo, "NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver,
369 EXTRACT_32BITS(&nh->msys_uptime)/1000,
370 EXTRACT_32BITS(&nh->msys_uptime)%1000,
371 EXTRACT_32BITS(&nh->utc_sec), EXTRACT_32BITS(&nh->utc_nsec)));
373 ND_PRINT((ndo, "#%u, ", EXTRACT_32BITS(&nh->sequence)));
374 nr = (const struct nfrec_v6 *)&nh[1];
376 ND_PRINT((ndo, "%2u recs", nrecs));
378 for (; nrecs != 0; nr++, nrecs--) {
379 char buf[20];
380 char asbuf[20];
383 * Make sure we have the entire record.
385 ND_TCHECK(*nr);
386 ND_PRINT((ndo, "\n started %u.%03u, last %u.%03u",
387 EXTRACT_32BITS(&nr->start_time)/1000,
388 EXTRACT_32BITS(&nr->start_time)%1000,
389 EXTRACT_32BITS(&nr->last_time)/1000,
390 EXTRACT_32BITS(&nr->last_time)%1000));
392 asbuf[0] = buf[0] = '\0';
393 snprintf(buf, sizeof(buf), "/%u", nr->src_mask);
394 snprintf(asbuf, sizeof(asbuf), ":%u",
395 EXTRACT_16BITS(&nr->src_as));
396 ND_PRINT((ndo, "\n %s%s%s:%u ", intoa(nr->src_ina.s_addr), buf, asbuf,
397 EXTRACT_16BITS(&nr->srcport)));
399 snprintf(buf, sizeof(buf), "/%d", nr->dst_mask);
400 snprintf(asbuf, sizeof(asbuf), ":%u",
401 EXTRACT_16BITS(&nr->dst_as));
402 ND_PRINT((ndo, "> %s%s%s:%u ", intoa(nr->dst_ina.s_addr), buf, asbuf,
403 EXTRACT_16BITS(&nr->dstport)));
405 ND_PRINT((ndo, ">> %s\n ", intoa(nr->nhop_ina.s_addr)));
407 pent = getprotobynumber(nr->proto);
408 if (!pent || ndo->ndo_nflag)
409 ND_PRINT((ndo, "%u ", nr->proto));
410 else
411 ND_PRINT((ndo, "%s ", pent->p_name));
413 /* tcp flags for tcp only */
414 if (pent && pent->p_proto == IPPROTO_TCP) {
415 int flags;
416 flags = nr->tcp_flags;
417 ND_PRINT((ndo, "%s%s%s%s%s%s%s",
418 flags & TH_FIN ? "F" : "",
419 flags & TH_SYN ? "S" : "",
420 flags & TH_RST ? "R" : "",
421 flags & TH_PUSH ? "P" : "",
422 flags & TH_ACK ? "A" : "",
423 flags & TH_URG ? "U" : "",
424 flags ? " " : ""));
427 buf[0]='\0';
428 snprintf(buf, sizeof(buf), "(%u<>%u encaps)",
429 (EXTRACT_16BITS(&nr->flags) >> 8) & 0xff,
430 (EXTRACT_16BITS(&nr->flags)) & 0xff);
431 ND_PRINT((ndo, "tos %u, %u (%u octets) %s",
432 nr->tos,
433 EXTRACT_32BITS(&nr->packets),
434 EXTRACT_32BITS(&nr->octets), buf));
436 return;
438 trunc:
439 ND_PRINT((ndo, "[|cnfp]"));
440 return;
443 void
444 cnfp_print(netdissect_options *ndo, const u_char *cp)
446 int ver;
449 * First 2 bytes are the version number.
451 ND_TCHECK2(*cp, 2);
452 ver = EXTRACT_16BITS(cp);
453 switch (ver) {
455 case 1:
456 cnfp_v1_print(ndo, cp);
457 break;
459 case 5:
460 cnfp_v5_print(ndo, cp);
461 break;
463 case 6:
464 cnfp_v6_print(ndo, cp);
465 break;
467 default:
468 ND_PRINT((ndo, "NetFlow v%x", ver));
469 break;
471 return;
473 trunc:
474 ND_PRINT((ndo, "[|cnfp]"));
475 return;