xref: /openbsd-src/usr.sbin/tcpdump/print-cnfp.c (revision 3a3fbb3f2e2521ab7c4a56b7ff7462ebd9095ec5)
1 /*	$OpenBSD: print-cnfp.c,v 1.4 2001/11/06 03:11:40 deraadt 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 #include <sys/types.h>
36 #include <sys/time.h>
37 #include <sys/socket.h>
38 #include <netdb.h>
39 
40 #include <netinet/in.h>
41 #include <netinet/tcp.h>
42 #include <arpa/inet.h>
43 
44 #include <stdio.h>
45 #include <string.h>
46 
47 #include "interface.h"
48 
49 struct nfhdr {
50 	u_int32_t	ver_cnt;	/* version [15], and # of records */
51 	u_int32_t	msys_uptime;
52 	u_int32_t	utc_sec;
53 	u_int32_t	utc_nsec;
54 	u_int32_t	sequence;	/* v5 flow sequence number */
55 	u_int32_t	reserved;	/* v5 only */
56 };
57 
58 struct nfrec {
59 	struct in_addr  src_ina;
60 	struct in_addr  dst_ina;
61 	struct in_addr  nhop_ina;
62 	u_int32_t	ifaces;		/* src,dst ifaces */
63 	u_int32_t	packets;
64 	u_int32_t	octets;
65 	u_int32_t	start_time;	/* sys_uptime value */
66 	u_int32_t	last_time;	/* sys_uptime value */
67 	u_int32_t	ports;		/* src,dst ports */
68 	u_int32_t	proto_tos;	/* proto, tos, pad, flags(v5) */
69 	u_int32_t	asses;		/* v1: flags; v5: src,dst AS */
70 	u_int32_t	masks;		/* src,dst addr prefix */
71 
72 };
73 
74 void
75 cnfp_print(register const u_char *cp, u_int len, register const u_char *bp)
76 {
77 	register const struct nfhdr *nh;
78 	register const struct nfrec *nr;
79 	register const struct ip *ip;
80 	struct protoent *pent;
81 	int nrecs, ver;
82 	time_t t;
83 
84 	ip = (struct ip *)bp;
85 	nh = (struct nfhdr *)cp;
86 
87 	if ((u_char *)(nh + 1) > snapend)
88 		return;
89 
90 	nrecs = ntohl(nh->ver_cnt) & 0xffff;
91 	ver = (ntohl(nh->ver_cnt) & 0xffff0000) >> 16;
92 	t = ntohl(nh->utc_sec);
93 /*	(p = ctime(&t))[24] = '\0'; */
94 
95 	printf("NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver,
96 	       ntohl(nh->msys_uptime)/1000, ntohl(nh->msys_uptime)%1000,
97 	       ntohl(nh->utc_sec), ntohl(nh->utc_nsec));
98 
99 	if (ver == 5) {
100 		printf("#%u, ", htonl(nh->sequence));
101 		nr = (struct nfrec *)&nh[1];
102 		snaplen -= 24;
103 	} else {
104 		nr = (struct nfrec *)&nh->sequence;
105 		snaplen -= 16;
106 	}
107 
108 	printf("%2u recs", nrecs);
109 
110 	for (; nrecs-- && (u_char *)(nr + 1) <= snapend; nr++) {
111 		char buf[5];
112 		char asbuf[7];
113 
114 		printf("\n  started %u.%03u, last %u.%03u",
115 			ntohl(nr->start_time)/1000, ntohl(nr->start_time)%1000,
116 			ntohl(nr->last_time)/1000, ntohl(nr->last_time)%1000);
117 
118 		asbuf[0] = buf[0] = '\0';
119 		if (ver == 5) {
120 			snprintf(buf, sizeof buf, "/%d",
121 			    (ntohl(nr->masks) >> 24) & 0xff);
122 			snprintf(asbuf, sizeof asbuf, "%d:",
123 			    (ntohl(nr->asses) >> 16) & 0xffff);
124 		}
125 		printf("\n    %s%s%s:%u ", inet_ntoa(nr->src_ina), buf, asbuf,
126 			ntohl(nr->ports) >> 16);
127 
128 		if (ver == 5) {
129 			snprintf(buf, sizeof buf, "/%d",
130 			    (ntohl(nr->masks) >> 16) & 0xff);
131 			snprintf(asbuf, sizeof asbuf, "%d:",
132 			    ntohl(nr->asses) & 0xffff);
133 		}
134 		printf("> %s%s%s:%u ", inet_ntoa(nr->dst_ina), buf, asbuf,
135 			ntohl(nr->ports) & 0xffff);
136 
137 		printf(">> %s\n    ", inet_ntoa(nr->nhop_ina));
138 
139 		pent = getprotobynumber((ntohl(nr->proto_tos) >> 8) & 0xff);
140 		if (!pent || nflag)
141 			printf("%u ", (ntohl(nr->proto_tos) >> 8) & 0xff);
142 		else
143 			printf("%s ", pent->p_name);
144 
145 		/* tcp flags for tcp only */
146 		if (pent && pent->p_proto == IPPROTO_TCP) {
147 			int flags;
148 			if (ver == 1)
149 				flags = (ntohl(nr->asses) >> 24) & 0xff;
150 			else
151 				flags = (ntohl(nr->proto_tos) >> 16) & 0xff;
152 			if (flags & TH_FIN)	putchar('F');
153 			if (flags & TH_SYN)	putchar('S');
154 			if (flags & TH_RST)	putchar('R');
155 			if (flags & TH_PUSH)	putchar('P');
156 			if (flags & TH_ACK)	putchar('A');
157 			if (flags & TH_URG)	putchar('U');
158 			if (flags)
159 				putchar(' ');
160 		}
161 		printf("tos %u, %u (%u octets)", ntohl(nr->proto_tos) & 0xff,
162 			ntohl(nr->packets), ntohl(nr->octets));
163 
164 
165 	}
166 
167 }
168 
169