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