xref: /freebsd/contrib/tcpdump/print-cnfp.c (revision 64de80195bba295c961a4cdf96dbe0e4979bdf2a)
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 /* Cisco NetFlow protocol */
34 
35 #define NETDISSECT_REWORKED
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39 
40 #include <tcpdump-stdinc.h>
41 
42 #include <stdio.h>
43 #include <string.h>
44 
45 #include "interface.h"
46 #include "addrtoname.h"
47 #include "extract.h"
48 
49 #include "tcp.h"
50 #include "ipproto.h"
51 
52 struct nfhdr {
53 	uint32_t	ver_cnt;	/* version [15], and # of records */
54 	uint32_t	msys_uptime;
55 	uint32_t	utc_sec;
56 	uint32_t	utc_nsec;
57 	uint32_t	sequence;	/* v5 flow sequence number */
58 	uint32_t	reserved;	/* v5 only */
59 };
60 
61 struct nfrec {
62 	struct in_addr	src_ina;
63 	struct in_addr	dst_ina;
64 	struct in_addr	nhop_ina;
65 	uint32_t	ifaces;		/* src,dst ifaces */
66 	uint32_t	packets;
67 	uint32_t	octets;
68 	uint32_t	start_time;	/* sys_uptime value */
69 	uint32_t	last_time;	/* sys_uptime value */
70 	uint32_t	ports;		/* src,dst ports */
71 	uint32_t	proto_tos;	/* proto, tos, pad, flags(v5) */
72 	uint32_t	asses;		/* v1: flags; v5: src,dst AS */
73 	uint32_t	masks;		/* src,dst addr prefix; v6: encaps */
74 	struct in_addr	peer_nexthop;	/* v6: IP address of the nexthop within the peer (FIB)*/
75 };
76 
77 void
78 cnfp_print(netdissect_options *ndo,
79            const u_char *cp, const u_char *bp _U_)
80 {
81 	register const struct nfhdr *nh;
82 	register const struct nfrec *nr;
83 	struct protoent *pent;
84 	int nrecs, ver;
85 #if 0
86 	time_t t;
87 #endif
88 
89 	nh = (const struct nfhdr *)cp;
90 
91 	if ((const u_char *)(nh + 1) > ndo->ndo_snapend)
92 		return;
93 
94 	nrecs = EXTRACT_32BITS(&nh->ver_cnt) & 0xffff;
95 	ver = (EXTRACT_32BITS(&nh->ver_cnt) & 0xffff0000) >> 16;
96 #if 0
97 	/*
98 	 * This is seconds since the UN*X epoch, and is followed by
99 	 * nanoseconds.  XXX - format it, rather than just dumping the
100 	 * raw seconds-since-the-Epoch.
101 	 */
102 	t = EXTRACT_32BITS(&nh->utc_sec);
103 #endif
104 
105 	ND_PRINT((ndo, "NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver,
106 	       EXTRACT_32BITS(&nh->msys_uptime)/1000,
107 	       EXTRACT_32BITS(&nh->msys_uptime)%1000,
108 	       EXTRACT_32BITS(&nh->utc_sec), EXTRACT_32BITS(&nh->utc_nsec)));
109 
110 	if (ver == 5 || ver == 6) {
111 		ND_PRINT((ndo, "#%u, ", EXTRACT_32BITS(&nh->sequence)));
112 		nr = (const struct nfrec *)&nh[1];
113 		ndo->ndo_snaplen -= 24;
114 	} else {
115 		nr = (const struct nfrec *)&nh->sequence;
116 		ndo->ndo_snaplen -= 16;
117 	}
118 
119 	ND_PRINT((ndo, "%2u recs", nrecs));
120 
121 	for (; nrecs-- && (const u_char *)(nr + 1) <= ndo->ndo_snapend; nr++) {
122 		char buf[20];
123 		char asbuf[20];
124 
125 		ND_PRINT((ndo, "\n  started %u.%03u, last %u.%03u",
126 		       EXTRACT_32BITS(&nr->start_time)/1000,
127 		       EXTRACT_32BITS(&nr->start_time)%1000,
128 		       EXTRACT_32BITS(&nr->last_time)/1000,
129 		       EXTRACT_32BITS(&nr->last_time)%1000));
130 
131 		asbuf[0] = buf[0] = '\0';
132 		if (ver == 5 || ver == 6) {
133 			snprintf(buf, sizeof(buf), "/%u",
134 				 (EXTRACT_32BITS(&nr->masks) >> 24) & 0xff);
135 			snprintf(asbuf, sizeof(asbuf), ":%u",
136 				 (EXTRACT_32BITS(&nr->asses) >> 16) & 0xffff);
137 		}
138 		ND_PRINT((ndo, "\n    %s%s%s:%u ", intoa(nr->src_ina.s_addr), buf, asbuf,
139 			EXTRACT_32BITS(&nr->ports) >> 16));
140 
141 		if (ver == 5 || ver ==6) {
142 			snprintf(buf, sizeof(buf), "/%d",
143 				 (EXTRACT_32BITS(&nr->masks) >> 16) & 0xff);
144 			snprintf(asbuf, sizeof(asbuf), ":%u",
145 				 EXTRACT_32BITS(&nr->asses) & 0xffff);
146 		}
147 		ND_PRINT((ndo, "> %s%s%s:%u ", intoa(nr->dst_ina.s_addr), buf, asbuf,
148 			EXTRACT_32BITS(&nr->ports) & 0xffff));
149 
150 		ND_PRINT((ndo, ">> %s\n    ", intoa(nr->nhop_ina.s_addr)));
151 
152 		pent = getprotobynumber((EXTRACT_32BITS(&nr->proto_tos) >> 8) & 0xff);
153 		if (!pent || ndo->ndo_nflag)
154 			ND_PRINT((ndo, "%u ",
155 			       (EXTRACT_32BITS(&nr->proto_tos) >> 8) & 0xff));
156 		else
157 			ND_PRINT((ndo, "%s ", pent->p_name));
158 
159 		/* tcp flags for tcp only */
160 		if (pent && pent->p_proto == IPPROTO_TCP) {
161 			int flags;
162 			if (ver == 1)
163 				flags = (EXTRACT_32BITS(&nr->asses) >> 24) & 0xff;
164 			else
165 				flags = (EXTRACT_32BITS(&nr->proto_tos) >> 16) & 0xff;
166 			ND_PRINT((ndo, "%s%s%s%s%s%s%s",
167 				flags & TH_FIN  ? "F" : "",
168 				flags & TH_SYN  ? "S" : "",
169 				flags & TH_RST  ? "R" : "",
170 				flags & TH_PUSH ? "P" : "",
171 				flags & TH_ACK  ? "A" : "",
172 				flags & TH_URG  ? "U" : "",
173 				flags           ? " " : ""));
174 		}
175 
176 		buf[0]='\0';
177 		if (ver == 6) {
178 			snprintf(buf, sizeof(buf), "(%u<>%u encaps)",
179 				 (EXTRACT_32BITS(&nr->masks) >> 8) & 0xff,
180 				 (EXTRACT_32BITS(&nr->masks)) & 0xff);
181 		}
182 		ND_PRINT((ndo, "tos %u, %u (%u octets) %s",
183 		       EXTRACT_32BITS(&nr->proto_tos) & 0xff,
184 		       EXTRACT_32BITS(&nr->packets),
185 		       EXTRACT_32BITS(&nr->octets), buf));
186 	}
187 }
188