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