]> The Tcpdump Group git mirrors - tcpdump/blob - print-cnfp.c
Rename 'tcpdump-stdinc.h' to 'netdissect-stdinc.h'
[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 /*
34 * Cisco NetFlow protocol
35 *
36 * See
37 *
38 * http://www.cisco.com/c/en/us/td/docs/net_mgmt/netflow_collection_engine/3-6/user/guide/format.html#wp1005892
39 */
40
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44
45 #include <netdissect-stdinc.h>
46
47 #include <stdio.h>
48 #include <string.h>
49
50 #include "netdissect.h"
51 #include "addrtoname.h"
52 #include "extract.h"
53
54 #include "tcp.h"
55 #include "ipproto.h"
56
57 struct nfhdr_v1 {
58 uint16_t version; /* version number */
59 uint16_t count; /* # of records */
60 uint32_t msys_uptime;
61 uint32_t utc_sec;
62 uint32_t utc_nsec;
63 };
64
65 struct nfrec_v1 {
66 struct in_addr src_ina;
67 struct in_addr dst_ina;
68 struct in_addr nhop_ina;
69 uint16_t input; /* SNMP index of input interface */
70 uint16_t output; /* SNMP index of output interface */
71 uint32_t packets; /* packets in the flow */
72 uint32_t octets; /* layer 3 octets in the packets of the flow */
73 uint32_t start_time; /* sys_uptime value at start of flow */
74 uint32_t last_time; /* sys_uptime value when last packet of flow was received */
75 uint16_t srcport; /* TCP/UDP source port or equivalent */
76 uint16_t dstport; /* TCP/UDP source port or equivalent */
77 uint16_t pad1; /* pad */
78 uint8_t proto; /* IP protocol type */
79 uint8_t tos; /* IP type of service */
80 uint8_t tcp_flags; /* cumulative OR of TCP flags */
81 uint8_t pad[3]; /* padding */
82 uint32_t reserved; /* unused */
83 };
84
85 struct nfhdr_v5 {
86 uint16_t version; /* version number */
87 uint16_t count; /* # of records */
88 uint32_t msys_uptime;
89 uint32_t utc_sec;
90 uint32_t utc_nsec;
91 uint32_t sequence; /* flow sequence number */
92 uint8_t engine_type; /* type of flow-switching engine */
93 uint8_t engine_id; /* slot number of the flow-switching engine */
94 uint16_t sampling_interval; /* sampling mode and interval */
95 };
96
97 struct nfrec_v5 {
98 struct in_addr src_ina;
99 struct in_addr dst_ina;
100 struct in_addr nhop_ina;
101 uint16_t input; /* SNMP index of input interface */
102 uint16_t output; /* SNMP index of output interface */
103 uint32_t packets; /* packets in the flow */
104 uint32_t octets; /* layer 3 octets in the packets of the flow */
105 uint32_t start_time; /* sys_uptime value at start of flow */
106 uint32_t last_time; /* sys_uptime value when last packet of flow was received */
107 uint16_t srcport; /* TCP/UDP source port or equivalent */
108 uint16_t dstport; /* TCP/UDP source port or equivalent */
109 uint8_t pad1; /* pad */
110 uint8_t tcp_flags; /* cumulative OR of TCP flags */
111 uint8_t proto; /* IP protocol type */
112 uint8_t tos; /* IP type of service */
113 uint16_t src_as; /* AS number of the source */
114 uint16_t dst_as; /* AS number of the destination */
115 uint8_t src_mask; /* source address mask bits */
116 uint8_t dst_mask; /* destination address prefix mask bits */
117 uint16_t pad2;
118 struct in_addr peer_nexthop; /* v6: IP address of the nexthop within the peer (FIB)*/
119 };
120
121 struct nfhdr_v6 {
122 uint16_t version; /* version number */
123 uint16_t count; /* # of records */
124 uint32_t msys_uptime;
125 uint32_t utc_sec;
126 uint32_t utc_nsec;
127 uint32_t sequence; /* v5 flow sequence number */
128 uint32_t reserved; /* v5 only */
129 };
130
131 struct nfrec_v6 {
132 struct in_addr src_ina;
133 struct in_addr dst_ina;
134 struct in_addr nhop_ina;
135 uint16_t input; /* SNMP index of input interface */
136 uint16_t output; /* SNMP index of output interface */
137 uint32_t packets; /* packets in the flow */
138 uint32_t octets; /* layer 3 octets in the packets of the flow */
139 uint32_t start_time; /* sys_uptime value at start of flow */
140 uint32_t last_time; /* sys_uptime value when last packet of flow was received */
141 uint16_t srcport; /* TCP/UDP source port or equivalent */
142 uint16_t dstport; /* TCP/UDP source port or equivalent */
143 uint8_t pad1; /* pad */
144 uint8_t tcp_flags; /* cumulative OR of TCP flags */
145 uint8_t proto; /* IP protocol type */
146 uint8_t tos; /* IP type of service */
147 uint16_t src_as; /* AS number of the source */
148 uint16_t dst_as; /* AS number of the destination */
149 uint8_t src_mask; /* source address mask bits */
150 uint8_t dst_mask; /* destination address prefix mask bits */
151 uint16_t flags;
152 struct in_addr peer_nexthop; /* v6: IP address of the nexthop within the peer (FIB)*/
153 };
154
155 static void
156 cnfp_v1_print(netdissect_options *ndo, const u_char *cp)
157 {
158 register const struct nfhdr_v1 *nh;
159 register const struct nfrec_v1 *nr;
160 struct protoent *pent;
161 int nrecs, ver;
162 #if 0
163 time_t t;
164 #endif
165
166 nh = (const struct nfhdr_v1 *)cp;
167 ND_TCHECK(*nh);
168
169 ver = EXTRACT_16BITS(&nh->version);
170 nrecs = EXTRACT_32BITS(&nh->count);
171 #if 0
172 /*
173 * This is seconds since the UN*X epoch, and is followed by
174 * nanoseconds. XXX - format it, rather than just dumping the
175 * raw seconds-since-the-Epoch.
176 */
177 t = EXTRACT_32BITS(&nh->utc_sec);
178 #endif
179
180 ND_PRINT((ndo, "NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver,
181 EXTRACT_32BITS(&nh->msys_uptime)/1000,
182 EXTRACT_32BITS(&nh->msys_uptime)%1000,
183 EXTRACT_32BITS(&nh->utc_sec), EXTRACT_32BITS(&nh->utc_nsec)));
184
185 nr = (const struct nfrec_v1 *)&nh[1];
186
187 ND_PRINT((ndo, "%2u recs", nrecs));
188
189 for (; nrecs != 0; nr++, nrecs--) {
190 char buf[20];
191 char asbuf[20];
192
193 /*
194 * Make sure we have the entire record.
195 */
196 ND_TCHECK(*nr);
197 ND_PRINT((ndo, "\n started %u.%03u, last %u.%03u",
198 EXTRACT_32BITS(&nr->start_time)/1000,
199 EXTRACT_32BITS(&nr->start_time)%1000,
200 EXTRACT_32BITS(&nr->last_time)/1000,
201 EXTRACT_32BITS(&nr->last_time)%1000));
202
203 asbuf[0] = buf[0] = '\0';
204 ND_PRINT((ndo, "\n %s%s%s:%u ", intoa(nr->src_ina.s_addr), buf, asbuf,
205 EXTRACT_16BITS(&nr->srcport)));
206
207 ND_PRINT((ndo, "> %s%s%s:%u ", intoa(nr->dst_ina.s_addr), buf, asbuf,
208 EXTRACT_16BITS(&nr->dstport)));
209
210 ND_PRINT((ndo, ">> %s\n ", intoa(nr->nhop_ina.s_addr)));
211
212 pent = getprotobynumber(nr->proto);
213 if (!pent || ndo->ndo_nflag)
214 ND_PRINT((ndo, "%u ", nr->proto));
215 else
216 ND_PRINT((ndo, "%s ", pent->p_name));
217
218 /* tcp flags for tcp only */
219 if (pent && pent->p_proto == IPPROTO_TCP) {
220 int flags;
221 flags = nr->tcp_flags;
222 ND_PRINT((ndo, "%s%s%s%s%s%s%s",
223 flags & TH_FIN ? "F" : "",
224 flags & TH_SYN ? "S" : "",
225 flags & TH_RST ? "R" : "",
226 flags & TH_PUSH ? "P" : "",
227 flags & TH_ACK ? "A" : "",
228 flags & TH_URG ? "U" : "",
229 flags ? " " : ""));
230 }
231
232 buf[0]='\0';
233 ND_PRINT((ndo, "tos %u, %u (%u octets) %s",
234 nr->tos,
235 EXTRACT_32BITS(&nr->packets),
236 EXTRACT_32BITS(&nr->octets), buf));
237 }
238 return;
239
240 trunc:
241 ND_PRINT((ndo, "[|cnfp]"));
242 return;
243 }
244
245 static void
246 cnfp_v5_print(netdissect_options *ndo, const u_char *cp)
247 {
248 register const struct nfhdr_v5 *nh;
249 register const struct nfrec_v5 *nr;
250 struct protoent *pent;
251 int nrecs, ver;
252 #if 0
253 time_t t;
254 #endif
255
256 nh = (const struct nfhdr_v5 *)cp;
257 ND_TCHECK(*nh);
258
259 ver = EXTRACT_16BITS(&nh->version);
260 nrecs = EXTRACT_32BITS(&nh->count);
261 #if 0
262 /*
263 * This is seconds since the UN*X epoch, and is followed by
264 * nanoseconds. XXX - format it, rather than just dumping the
265 * raw seconds-since-the-Epoch.
266 */
267 t = EXTRACT_32BITS(&nh->utc_sec);
268 #endif
269
270 ND_PRINT((ndo, "NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver,
271 EXTRACT_32BITS(&nh->msys_uptime)/1000,
272 EXTRACT_32BITS(&nh->msys_uptime)%1000,
273 EXTRACT_32BITS(&nh->utc_sec), EXTRACT_32BITS(&nh->utc_nsec)));
274
275 ND_PRINT((ndo, "#%u, ", EXTRACT_32BITS(&nh->sequence)));
276 nr = (const struct nfrec_v5 *)&nh[1];
277
278 ND_PRINT((ndo, "%2u recs", nrecs));
279
280 for (; nrecs != 0; nr++, nrecs--) {
281 char buf[20];
282 char asbuf[20];
283
284 /*
285 * Make sure we have the entire record.
286 */
287 ND_TCHECK(*nr);
288 ND_PRINT((ndo, "\n started %u.%03u, last %u.%03u",
289 EXTRACT_32BITS(&nr->start_time)/1000,
290 EXTRACT_32BITS(&nr->start_time)%1000,
291 EXTRACT_32BITS(&nr->last_time)/1000,
292 EXTRACT_32BITS(&nr->last_time)%1000));
293
294 asbuf[0] = buf[0] = '\0';
295 snprintf(buf, sizeof(buf), "/%u", nr->src_mask);
296 snprintf(asbuf, sizeof(asbuf), ":%u",
297 EXTRACT_16BITS(&nr->src_as));
298 ND_PRINT((ndo, "\n %s%s%s:%u ", intoa(nr->src_ina.s_addr), buf, asbuf,
299 EXTRACT_16BITS(&nr->srcport)));
300
301 snprintf(buf, sizeof(buf), "/%d", nr->dst_mask);
302 snprintf(asbuf, sizeof(asbuf), ":%u",
303 EXTRACT_16BITS(&nr->dst_as));
304 ND_PRINT((ndo, "> %s%s%s:%u ", intoa(nr->dst_ina.s_addr), buf, asbuf,
305 EXTRACT_16BITS(&nr->dstport)));
306
307 ND_PRINT((ndo, ">> %s\n ", intoa(nr->nhop_ina.s_addr)));
308
309 pent = getprotobynumber(nr->proto);
310 if (!pent || ndo->ndo_nflag)
311 ND_PRINT((ndo, "%u ", nr->proto));
312 else
313 ND_PRINT((ndo, "%s ", pent->p_name));
314
315 /* tcp flags for tcp only */
316 if (pent && pent->p_proto == IPPROTO_TCP) {
317 int flags;
318 flags = nr->tcp_flags;
319 ND_PRINT((ndo, "%s%s%s%s%s%s%s",
320 flags & TH_FIN ? "F" : "",
321 flags & TH_SYN ? "S" : "",
322 flags & TH_RST ? "R" : "",
323 flags & TH_PUSH ? "P" : "",
324 flags & TH_ACK ? "A" : "",
325 flags & TH_URG ? "U" : "",
326 flags ? " " : ""));
327 }
328
329 buf[0]='\0';
330 ND_PRINT((ndo, "tos %u, %u (%u octets) %s",
331 nr->tos,
332 EXTRACT_32BITS(&nr->packets),
333 EXTRACT_32BITS(&nr->octets), buf));
334 }
335 return;
336
337 trunc:
338 ND_PRINT((ndo, "[|cnfp]"));
339 return;
340 }
341
342 static void
343 cnfp_v6_print(netdissect_options *ndo, const u_char *cp)
344 {
345 register const struct nfhdr_v6 *nh;
346 register const struct nfrec_v6 *nr;
347 struct protoent *pent;
348 int nrecs, ver;
349 #if 0
350 time_t t;
351 #endif
352
353 nh = (const struct nfhdr_v6 *)cp;
354 ND_TCHECK(*nh);
355
356 ver = EXTRACT_16BITS(&nh->version);
357 nrecs = EXTRACT_32BITS(&nh->count);
358 #if 0
359 /*
360 * This is seconds since the UN*X epoch, and is followed by
361 * nanoseconds. XXX - format it, rather than just dumping the
362 * raw seconds-since-the-Epoch.
363 */
364 t = EXTRACT_32BITS(&nh->utc_sec);
365 #endif
366
367 ND_PRINT((ndo, "NetFlow v%x, %u.%03u uptime, %u.%09u, ", ver,
368 EXTRACT_32BITS(&nh->msys_uptime)/1000,
369 EXTRACT_32BITS(&nh->msys_uptime)%1000,
370 EXTRACT_32BITS(&nh->utc_sec), EXTRACT_32BITS(&nh->utc_nsec)));
371
372 ND_PRINT((ndo, "#%u, ", EXTRACT_32BITS(&nh->sequence)));
373 nr = (const struct nfrec_v6 *)&nh[1];
374
375 ND_PRINT((ndo, "%2u recs", nrecs));
376
377 for (; nrecs != 0; nr++, nrecs--) {
378 char buf[20];
379 char asbuf[20];
380
381 /*
382 * Make sure we have the entire record.
383 */
384 ND_TCHECK(*nr);
385 ND_PRINT((ndo, "\n started %u.%03u, last %u.%03u",
386 EXTRACT_32BITS(&nr->start_time)/1000,
387 EXTRACT_32BITS(&nr->start_time)%1000,
388 EXTRACT_32BITS(&nr->last_time)/1000,
389 EXTRACT_32BITS(&nr->last_time)%1000));
390
391 asbuf[0] = buf[0] = '\0';
392 snprintf(buf, sizeof(buf), "/%u", nr->src_mask);
393 snprintf(asbuf, sizeof(asbuf), ":%u",
394 EXTRACT_16BITS(&nr->src_as));
395 ND_PRINT((ndo, "\n %s%s%s:%u ", intoa(nr->src_ina.s_addr), buf, asbuf,
396 EXTRACT_16BITS(&nr->srcport)));
397
398 snprintf(buf, sizeof(buf), "/%d", nr->dst_mask);
399 snprintf(asbuf, sizeof(asbuf), ":%u",
400 EXTRACT_16BITS(&nr->dst_as));
401 ND_PRINT((ndo, "> %s%s%s:%u ", intoa(nr->dst_ina.s_addr), buf, asbuf,
402 EXTRACT_16BITS(&nr->dstport)));
403
404 ND_PRINT((ndo, ">> %s\n ", intoa(nr->nhop_ina.s_addr)));
405
406 pent = getprotobynumber(nr->proto);
407 if (!pent || ndo->ndo_nflag)
408 ND_PRINT((ndo, "%u ", nr->proto));
409 else
410 ND_PRINT((ndo, "%s ", pent->p_name));
411
412 /* tcp flags for tcp only */
413 if (pent && pent->p_proto == IPPROTO_TCP) {
414 int flags;
415 flags = nr->tcp_flags;
416 ND_PRINT((ndo, "%s%s%s%s%s%s%s",
417 flags & TH_FIN ? "F" : "",
418 flags & TH_SYN ? "S" : "",
419 flags & TH_RST ? "R" : "",
420 flags & TH_PUSH ? "P" : "",
421 flags & TH_ACK ? "A" : "",
422 flags & TH_URG ? "U" : "",
423 flags ? " " : ""));
424 }
425
426 buf[0]='\0';
427 snprintf(buf, sizeof(buf), "(%u<>%u encaps)",
428 (EXTRACT_16BITS(&nr->flags) >> 8) & 0xff,
429 (EXTRACT_16BITS(&nr->flags)) & 0xff);
430 ND_PRINT((ndo, "tos %u, %u (%u octets) %s",
431 nr->tos,
432 EXTRACT_32BITS(&nr->packets),
433 EXTRACT_32BITS(&nr->octets), buf));
434 }
435 return;
436
437 trunc:
438 ND_PRINT((ndo, "[|cnfp]"));
439 return;
440 }
441
442 void
443 cnfp_print(netdissect_options *ndo, const u_char *cp)
444 {
445 int ver;
446
447 /*
448 * First 2 bytes are the version number.
449 */
450 ND_TCHECK2(*cp, 2);
451 ver = EXTRACT_16BITS(cp);
452 switch (ver) {
453
454 case 1:
455 cnfp_v1_print(ndo, cp);
456 break;
457
458 case 5:
459 cnfp_v5_print(ndo, cp);
460 break;
461
462 case 6:
463 cnfp_v6_print(ndo, cp);
464 break;
465
466 default:
467 ND_PRINT((ndo, "NetFlow v%x", ver));
468 break;
469 }
470 return;
471
472 trunc:
473 ND_PRINT((ndo, "[|cnfp]"));
474 return;
475 }