]> The Tcpdump Group git mirrors - tcpdump/blob - print-cnfp.c
add cisco NetFlow support from OpenBSD.
[tcpdump] / print-cnfp.c
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 #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
43 #include <stdio.h>
44 #include <string.h>
45
46 #include "interface.h"
47
48 struct nfhdr {
49 u_int32_t ver_cnt; /* version [15], and # of records */
50 u_int32_t msys_uptime;
51 u_int32_t utc_sec;
52 u_int32_t utc_nsec;
53 u_int32_t sequence; /* v5 flow sequence number */
54 u_int32_t reserved; /* v5 only */
55 };
56
57 struct nfrec {
58 in_addr_t src_ina;
59 in_addr_t dst_ina;
60 in_addr_t nhop_ina;
61 u_int32_t ifaces; /* src,dst ifaces */
62 u_int32_t packets;
63 u_int32_t octets;
64 u_int32_t start_time; /* sys_uptime value */
65 u_int32_t last_time; /* sys_uptime value */
66 u_int32_t ports; /* src,dst ports */
67 u_int32_t proto_tos; /* proto, tos, pad, flags(v5) */
68 u_int32_t asses; /* v1: flags; v5: src,dst AS */
69 u_int32_t masks; /* src,dst addr prefix */
70
71 };
72
73 void
74 cnfp_print(const u_char *cp, u_int len, const u_char *bp)
75 {
76 register const struct nfhdr *nh;
77 register const struct nfrec *nr;
78 register const struct ip *ip;
79 struct protoent *pent;
80 int nrecs, ver;
81 time_t t;
82 char *p;
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 sprintf(buf, "/%d", (ntohl(nr->masks) >> 24) & 0xff);
121 sprintf(asbuf, "%d:", (ntohl(nr->asses) >> 16) & 0xffff);
122 }
123 printf("\n %s%s%s:%u ", inet_ntoa(nr->src_ina), buf, asbuf,
124 ntohl(nr->ports) >> 16);
125
126 if (ver == 5) {
127 sprintf(buf, "/%d", (ntohl(nr->masks) >> 16) & 0xff);
128 sprintf(asbuf, "%d:", ntohl(nr->asses) & 0xffff);
129 }
130 printf("> %s%s%s:%u ", inet_ntoa(nr->dst_ina), buf, asbuf,
131 ntohl(nr->ports) & 0xffff);
132
133 printf(">> %s\n ", inet_ntoa(nr->nhop_ina));
134
135 pent = getprotobynumber((ntohl(nr->proto_tos) >> 8) & 0xff);
136 if (!pent || nflag)
137 printf("%u ", (ntohl(nr->proto_tos) >> 8) & 0xff);
138 else
139 printf("%s ", pent->p_name);
140
141 /* tcp flags for tcp only */
142 if (pent && pent->p_proto == IPPROTO_TCP) {
143 int flags;
144 if (ver == 1)
145 flags = (ntohl(nr->asses) >> 24) & 0xff;
146 else
147 flags = (ntohl(nr->proto_tos) >> 16) & 0xff;
148 if (flags & TH_FIN) putchar('F');
149 if (flags & TH_SYN) putchar('S');
150 if (flags & TH_RST) putchar('R');
151 if (flags & TH_PUSH) putchar('P');
152 if (flags & TH_ACK) putchar('A');
153 if (flags & TH_URG) putchar('U');
154 if (flags)
155 putchar(' ');
156 }
157 printf("tos %u, %u (%u octets)", ntohl(nr->proto_tos) & 0xff,
158 ntohl(nr->packets), ntohl(nr->octets));
159
160
161 }
162
163 }
164