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