xref: /openbsd/usr.sbin/tcpdump/print-cnfp.c (revision 274d7c50)
1 /*	$OpenBSD: print-cnfp.c,v 1.10 2018/07/06 05:47:22 dlg 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(const u_char *cp, u_int len)
72 {
73 	const struct nfhdr *nh;
74 	const struct nfrec *nr;
75 	int nrecs, ver, proto;
76 	time_t t;
77 
78 	nh = (struct nfhdr *)cp;
79 
80 	if ((u_char *)(nh + 1) > snapend)
81 		return;
82 
83 	nrecs = ntohl(nh->ver_cnt) & 0xffff;
84 	ver = (ntohl(nh->ver_cnt) & 0xffff0000) >> 16;
85 	t = ntohl(nh->utc_sec);
86 /*	(p = ctime(&t))[24] = '\0'; */
87 
88 	printf("NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver,
89 	       ntohl(nh->msys_uptime)/1000, ntohl(nh->msys_uptime)%1000,
90 	       ntohl(nh->utc_sec), ntohl(nh->utc_nsec));
91 
92 	if (ver == 5) {
93 		printf("#%u, ", htonl(nh->sequence));
94 		nr = (struct nfrec *)&nh[1];
95 	} else
96 		nr = (struct nfrec *)&nh->sequence;
97 
98 	printf("%2u recs", nrecs);
99 
100 	for (; nrecs-- && (u_char *)(nr + 1) <= snapend; nr++) {
101 		char buf[5];
102 		char asbuf[7];
103 
104 		printf("\n  started %u.%03u, last %u.%03u",
105 			ntohl(nr->start_time)/1000, ntohl(nr->start_time)%1000,
106 			ntohl(nr->last_time)/1000, ntohl(nr->last_time)%1000);
107 
108 		asbuf[0] = buf[0] = '\0';
109 		if (ver == 5) {
110 			snprintf(buf, sizeof buf, "/%d",
111 			    (ntohl(nr->masks) >> 24) & 0xff);
112 			snprintf(asbuf, sizeof asbuf, ":%d",
113 			    (ntohl(nr->asses) >> 16) & 0xffff);
114 		}
115 		printf("\n    %s%s%s:%u ", inet_ntoa(nr->src_ina), buf, asbuf,
116 			ntohl(nr->ports) >> 16);
117 
118 		if (ver == 5) {
119 			snprintf(buf, sizeof buf, "/%d",
120 			    (ntohl(nr->masks) >> 16) & 0xff);
121 			snprintf(asbuf, sizeof asbuf, ":%d",
122 			    ntohl(nr->asses) & 0xffff);
123 		}
124 		printf("> %s%s%s:%u ", inet_ntoa(nr->dst_ina), buf, asbuf,
125 			ntohl(nr->ports) & 0xffff);
126 
127 		printf(">> %s\n    ", inet_ntoa(nr->nhop_ina));
128 
129 		proto = (ntohl(nr->proto_tos) >> 8) & 0xff;
130 		printf("%s ", ipproto_string(proto));
131 
132 		/* tcp flags for tcp only */
133 		if (proto == IPPROTO_TCP) {
134 			int flags;
135 			if (ver == 1)
136 				flags = (ntohl(nr->asses) >> 24) & 0xff;
137 			else
138 				flags = (ntohl(nr->proto_tos) >> 16) & 0xff;
139 			if (flags & TH_FIN)	putchar('F');
140 			if (flags & TH_SYN)	putchar('S');
141 			if (flags & TH_RST)	putchar('R');
142 			if (flags & TH_PUSH)	putchar('P');
143 			if (flags & TH_ACK)	putchar('A');
144 			if (flags & TH_URG)	putchar('U');
145 			if (flags)
146 				putchar(' ');
147 		}
148 		printf("tos %u, %u (%u octets)", ntohl(nr->proto_tos) & 0xff,
149 			ntohl(nr->packets), ntohl(nr->octets));
150 
151 
152 	}
153 
154 }
155 
156